package jnpf.personnels.controller.web.audit;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.ActionResult;
import jnpf.enums.personnel.FtbPersonnelsCheckStatusCodeEnum;
import jnpf.model.personnels.dto.base.FtbPersonnelsBaseForOA;
import jnpf.model.personnels.po.FtbPersonnelsStaffEmploymentApply;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.model.personnels.vo.task.FtbPersonnelsAuditRunTaskVO;
import jnpf.personnels.service.FtbPersonnelsAuditRunTaskService;
import jnpf.personnels.service.FtbPersonnelsStaffEmploymentApplyService;
import jnpf.personnels.service.FtbPersonnelsStaffRosterService;
import jnpf.util.NoDataSourceBind;
import jnpf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * web审核前置校验
 *
 * @author peng.hao
 */
@RestController
@RequestMapping("/web/personnels-task")
public class FtbPersonnelsAuditRunTaskController {


    @Resource
    private FtbPersonnelsAuditRunTaskService service;

    @Autowired
    private FtbPersonnelsStaffRosterService staffRosterService;

    @Autowired
    FtbPersonnelsStaffEmploymentApplyService staffEmploymentApplyService;

    /**
     * 查看对应业务的审批流程
     *
     * @param id 运行的taskId
     * @return 当前审批流程的结果Action
     */
    @GetMapping("/view-list-by-task/{id}")
    @Deprecated(since = "人事接入oa废弃原审批代码")
    public ActionResult<FtbPersonnelsAuditRunTaskVO> viewTheCurrentApprovalProcessByTaskId(@PathVariable("id") String id){
        return ActionResult.success(service.viewTheCurrentApprovalProcessByTaskId(id));
    }

    /**
     * 查询当前用户是否处于流程中(for oa)
     *
     * @return  null 不处于 ,1转正 2,调岗, 3离职, 4晋升
     */
    @GetMapping("/query-my-approved-review-process/userId")
    public ActionResult<Void> queryMyApprovedReviewProcess(FtbPersonnelsBaseForOA dto){
        ActionResult<Void> result = new ActionResult<>();
        String string = checkWhetherTheCheckIsInTheProcessForOA(dto.getUserIds().get(0));
        if (StringUtils.isNotEmpty(string)){
            result.setMsg(string);
            result.setCode(FtbPersonnelsCheckStatusCodeEnum.CHECK_REFUSE.getCode());
        }else {
            result.setMsg(FtbPersonnelsCheckStatusCodeEnum.CHECK_PASS.getMsg());
            result.setCode(FtbPersonnelsCheckStatusCodeEnum.CHECK_PASS.getCode());
        }
        return result;
    }
    /**
     * 查询当前用户是否处于流程中
     *
     * @return  null 不处于 ,1 转正 2,调岗, 3离职
     */
    @GetMapping("/query-my-approved-review-process/code/userId")
    public ActionResult<String> queryMyApprovedReviewProcessWithCode(FtbPersonnelsBaseForOA dto){
        String isInTheReviewProcess = service.checkWhetherTheCurrentPersonnelIsInTheReviewProcess(dto.getUserIds().get(0));
        return ActionResult.success("",isInTheReviewProcess);
    }
    private String checkWhetherTheCheckIsInTheProcessForOA(String userId) {
        // null 不处于 ,1 转正 2,调岗, 3离职 4晋升 5调店 6降职
        String s = service.checkWhetherTheCurrentPersonnelIsInTheReviewProcess(userId);
        if (StringUtil.isNotEmpty(s)){
            // 1 转正 2 调岗, 3 离职
            String str="";
            switch (s){
                case "1":
                    str = "转正";
                    break;
                case "2":
                    str = "调岗";
                    break;
                case "3":
                    str = "离职";
                    break;
                case "4":
                    str = "晋升";
                    break;
                case "5":
                    str = "调店";
                    break;
                case "6":
                    str = "降职";
                    break;
                case "7":
                    str = "借调";
                    break;
            }
            return ("当前人员处于"+str+"审批流程中，无法再次发起流程！");
        }
        LambdaQueryWrapper<FtbPersonnelsStaffRoster> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(FtbPersonnelsStaffRoster::getUserId, userId);
        queryWrapper1.ne(FtbPersonnelsStaffRoster::getWorkerStatus, "305");
        queryWrapper1.eq(FtbPersonnelsStaffRoster::getEnabledMark, 0);
        long count = staffRosterService.count(queryWrapper1);
        LambdaQueryWrapper<FtbPersonnelsStaffEmploymentApply> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getUserId, userId);
        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getEnabledMark, 0);
        queryWrapper.eq(FtbPersonnelsStaffEmploymentApply::getIsNeedCheck,0);
        queryWrapper.notIn(FtbPersonnelsStaffEmploymentApply::getCheckStatus, 2);
        queryWrapper.last("limit 1");
        FtbPersonnelsStaffEmploymentApply one = staffEmploymentApplyService.getOne(queryWrapper);
        if (Objects.nonNull(one) && count == 0) {
            throw new RuntimeException("当前选择人员还未办理入职,无法提交!");
        }
        return null;
    }


    /**
     * 关闭多个租户正在审核数据 归为审核历史
     * @param teantIdList 租户ID集合
     * @return
     */
    @PostMapping("/closeAllExecutionData")
    @NoDataSourceBind
    @SaIgnore
    @Deprecated(since = "人事接入oa废弃原审批代码")
    public ActionResult<Object> closeAllExecutionData(@RequestBody List<String> teantIdList){
        service.closeAllExecutionData(teantIdList);
        return ActionResult.success();
    }
}
