package com.ruoyi.activiti.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.activiti.service.IProcessService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.activiti.domain.BizEmployeReport;
import com.ruoyi.activiti.service.IBizEmployeReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工花名册审批业务Controller
 * 
 * @author ruoyi
 * @date 2021-12-06
 */
@RestController
@RequestMapping("/bizapprove/employee")
@AllArgsConstructor
public class BizEmployeReportController extends BaseController
{
    private IProcessService processService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IBizEmployeReportService bizEmployeReportService;

    /**
     * 查询员工花名册审批业务列表
     */
    @PreAuthorize("@ss.hasPermi('bizapprove:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizEmployeReport bizEmployeReport)
    {
        startPage();
        List<BizEmployeReport> list = bizEmployeReportService.selectBizEmployeReportList(bizEmployeReport);
        return getDataTable(list);
    }

    /**
     * 导出员工花名册审批业务列表
     */
    @PreAuthorize("@ss.hasPermi('bizapprove:employee:export')")
    @Log(title = "员工花名册审批业务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizEmployeReport bizEmployeReport)
    {
        List<BizEmployeReport> list = bizEmployeReportService.selectBizEmployeReportList(bizEmployeReport);
        ExcelUtil<BizEmployeReport> util = new ExcelUtil<BizEmployeReport>(BizEmployeReport.class);
        return util.exportExcel(list, "员工花名册审批业务数据");
    }

    /**
     * 获取员工花名册审批业务详细信息
     */
    @PreAuthorize("@ss.hasPermi('bizapprove:employee:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizEmployeReportService.selectBizEmployeReportById(id));
    }

    /**
     * 新增员工花名册审批业务
     */
    @PreAuthorize("@ss.hasPermi('bizapprove:employee:add')")
    @Log(title = "员工花名册审批业务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizEmployeReport bizEmployeReport)
    {
        return toAjax(bizEmployeReportService.insertBizEmployeReport(bizEmployeReport));
    }

    /**
     * 修改员工花名册审批业务
     */
    @PreAuthorize("@ss.hasPermi('bizapprove:employee:edit')")
    @Log(title = "员工花名册审批业务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizEmployeReport bizEmployeReport)
    {
        return toAjax(bizEmployeReportService.updateBizEmployeReport(bizEmployeReport));
    }

    /**
     * 删除员工花名册审批业务
     */
    @PreAuthorize("@ss.hasPermi('bizapprove:employee:remove')")
    @Log(title = "员工花名册审批业务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizEmployeReportService.deleteBizEmployeReportByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "员工审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply/{id}")
    @ResponseBody
    public AjaxResult submitApply(@PathVariable Long id) {
        try {
            BizEmployeReport bizEmployeReport = bizEmployeReportService.selectBizEmployeReportById(id);
            // bkcarLeader  bkwlLeader bkxxLeader
            SysDept dept = deptService.selectDeptById(bizEmployeReport.getApplyDeptId());
//            List<SysUser> userList = userService.selectDeptLeader(dept.getDeptId(),"jthr");
//            if(userList.size() == 0){
//                return error("未设置集团人力审核人员！");
//            }
            //上级领导审批
            //https://ask.csdn.net/questions/757022
            //${deptLeaderId}

            Map<String, Object> variables = new HashMap<>();
            //variables.put("approveUserId",userList.get(0).getUserName());
            processService.submitApply(bizEmployeReport, "employeapprove", variables);
            bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
        } catch (Exception e) {
            e.printStackTrace();
            return error("提交申请出错：" + e.getMessage());
        }
        return success();
    }
}
