package com.ruoyi.postCheck.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.PostPlanEnum;
import com.ruoyi.common.constant.PostProgrammeEnum;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.postCheck.domain.PostChecked;
import com.ruoyi.postCheck.domain.PostProgramme;
import com.ruoyi.postCheck.mapper.PostProgrammeMapper;
import com.ruoyi.postCheck.service.IPostProgrammeService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 岗检方案Service业务层处理
 *
 * @author csg
 * @date 2023-12-19
 */
@Service
public class PostProgrammeServiceImpl extends ServiceImpl<PostProgrammeMapper, PostProgramme> implements IPostProgrammeService {

    @Autowired
    private PostProgrammeMapper postProgrammeMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询岗检方案
     *
     * @param id 岗检方案主键
     * @return 岗检方案
     */
    @Override
    public PostProgramme selectPostProgrammeById(Long id) {
        return postProgrammeMapper.selectPostProgrammeById(id);
    }

    /**
     * 查询岗检方案列表
     *
     * @param postProgramme 岗检方案
     * @return 岗检方案
     */
    @Override
    public List<PostProgramme> selectPostProgrammeList(PostProgramme postProgramme) {
        return postProgrammeMapper.selectPostProgrammeList(postProgramme);
    }

    /**
     * 新增岗检方案
     *
     * @param postProgramme 岗检方案
     * @return 结果
     */
    @Override
    public int insertPostProgramme(PostProgramme postProgramme) {
        postProgramme.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
        postProgramme.setCreateDefault();

        //从登录信息中查询用户部门
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        postProgramme.setDeptId(deptId);
        postProgramme.setDeptName(sysDept.getDeptName());
        postProgramme.setStep(PostProgrammeEnum.STEP_INIT.getValue());

        postProgramme.setSubmitDate(new Date());//提交日期
        postProgramme.setStatus(PostProgrammeEnum.STATUS_WAIT.getValue());//状态：1-待审核；2-审核中；3-通过；4-驳回；
        postProgramme.setStatusIcfa(PostProgrammeEnum.STATUS_ICFA_WAIT.getValue());//状态：1-内控法务审核审批-状态：1-待审核；3-通过；4-驳回；
        return postProgrammeMapper.insertPostProgramme(postProgramme);
    }

    /**
     * 修改岗检方案
     *
     * @param postProgramme 岗检方案
     * @return 结果
     */
    @Override
    public int updatePostProgramme(PostProgramme postProgramme) {
        postProgramme.setUpdateDefault();

        //从登录信息中查询用户部门
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        postProgramme.setDeptId(deptId);
        postProgramme.setDeptName(sysDept.getDeptName());

        return postProgrammeMapper.updatePostProgramme(postProgramme);
    }

    /**
     * 批量删除岗检方案
     *
     * @param ids 需要删除的岗检方案主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePostProgrammeByIds(Long[] ids) {

        List<PostProgramme> postProgrammeList = postProgrammeMapper.selectBatchIds(Arrays.asList(ids));
        if (ids.length <= 0 || postProgrammeList.size() != ids.length) {
            throw new BaseException("前后端记录数量匹配不一致，请刷新页面再试！");
        }

        for (PostProgramme programme : postProgrammeList) {
            Integer status = programme.getStatus();
            if (status == PostProgrammeEnum.STATUS_PASS.getValue()) {
                throw new BaseException("已审核通过的记录不允许删除，请刷新页面再试！");
            }
        }

        return postProgrammeMapper.deletePostProgrammeByIds(ids);
    }

    /**
     * 删除岗检方案信息
     *
     * @param id 岗检方案主键
     * @return 结果
    @Override
    public int deletePostProgrammeById(Long id) {
        return postProgrammeMapper.deletePostProgrammeById(id);
    }
     */


    /**
     * 审核1：岗检方案审核-Icfa
     *
     * @param id         主键ID
     * @param statusIcfa 2-内控法务部审批-状态：1-待审核；3-通过；4-驳回；
     * @param reasonIcfa 2-内控法务部审批-未通过理由
     * @return
     */
    @Override
    public int approveIcfa(Long id, Integer statusIcfa, String reasonIcfa) {

        if (id == null || statusIcfa == null) {
            return 0;
        }
        //状态错误
        if (statusIcfa != PostProgrammeEnum.STATUS_ICFA_PASS.getValue() && statusIcfa != PostProgrammeEnum.STATUS_ICFA_BACK.getValue()) {
            return 0;
        }
        //未写理由
        if (statusIcfa == PostProgrammeEnum.STATUS_ICFA_BACK.getValue() && StringUtils.isBlank(reasonIcfa)) {
            return 0;
        }

        PostProgramme postProgramme = selectPostProgrammeById(id);
        if (postProgramme == null) {
            throw new BaseException("记录不存在，请刷新页面重试！");
        }

        Integer statusIcfaDB = postProgramme.getStatusIcfa();

        // 审核过得不在审核
        if (statusIcfaDB == statusIcfa) {
            throw new BaseException("重复提交，请刷新页面再试！");
        }

        // PostProgrammeEnum.STATUS_ICFA_WAIT
        //审核
        if (statusIcfa == PostProgrammeEnum.STATUS_ICFA_PASS.getValue()) {
            throw new BaseException("已审核通过的记录不允许重复操作，请刷新页面再试！");
        }
        //驳回
        else if (statusIcfa == PostProgrammeEnum.STATUS_ICFA_BACK.getValue()) {

            if (statusIcfaDB == PostProgrammeEnum.STATUS_ICFA_PASS.getValue()) {
                throw new BaseException("已审核通过的记录不允许驳回操作，请刷新页面再试！");
            }

        }

        PostProgramme data = new PostProgramme();
        data.setId(id);
        data.setStatusIcfa(statusIcfa);
        data.setReasonIcfa(reasonIcfa);
        data.setStatus(statusIcfa);//最终审核结果
        if (statusIcfa == PostPlanEnum.STATUS_HR_PASS.getValue()) {
            data.setStatus(PostProgrammeEnum.STATUS_ING.getValue());//审核中
            data.setStep(PostProgrammeEnum.STEP.getValue());
            data.setStatusHr(PostProgrammeEnum.STATUS_HR_WAIT.getValue());//HR待审核
            data.setReasonIcfa("--");
        } else {
            data.setStatus(PostPlanEnum.STATUS_BACK.getValue());//驳回
        }
        data.setStatusIcfaUserId(SecurityUtils.getUserId());
        data.setDateIcfa(DateUtils.getNowDate());
        return postProgrammeMapper.updatePostProgramme(data);
    }

    /**
     * 审核2：岗检方案审核-HR
     *
     * @param id       主键ID
     * @param statusHr 1-人力资源审批-状态：1-待审核；3-通过；4-驳回；
     * @param reasonHr 1-人力资源审批-未通过理由
     * @return
     */
    @Override
    public int approveHr(Long id, Integer statusHr, String reasonHr) {

        if (id == null || statusHr == null) {
            return 0;
        }
        //状态错误
        if (statusHr != PostProgrammeEnum.STATUS_HR_PASS.getValue() && statusHr != PostProgrammeEnum.STATUS_HR_BACK.getValue()) {
            return 0;
        }

        //未写理由
        if (statusHr == PostProgrammeEnum.STATUS_HR_BACK.getValue() && StringUtils.isBlank(reasonHr)) {
            return 0;
        }

        // 判断当前状态
        PostProgramme postProgramme = selectPostProgrammeById(id);
        Integer statusHrDB = postProgramme.getStatusHr();


        // 判断顺序
        if (postProgramme.getStatusIcfa() != PostProgrammeEnum.STATUS_ICFA_PASS.getValue()
                && postProgramme.getStatus() != PostProgrammeEnum.STATUS_ING.getValue()) {
            throw new BaseException("内控法务部还未审核，请刷新页面再试！");
        }


        //审核
        if (statusHr == PostProgrammeEnum.STATUS_HR_PASS.getValue()) {

            if (statusHrDB == PostProgrammeEnum.STATUS_HR_PASS.getValue()) {
                throw new BaseException("已审核通过的记录不允许重复操作，请刷新页面再试！");
            }

        }
        //驳回
        else if (statusHr == PostProgrammeEnum.STATUS_HR_BACK.getValue()) {

            if (statusHrDB == PostProgrammeEnum.STATUS_HR_PASS.getValue()) {
                throw new BaseException("已审核通过的记录不允许驳回操作，请刷新页面再试！");
            }

        }


        PostProgramme data = new PostProgramme();
        data.setId(id);
        data.setStatusHr(statusHr);
        data.setReasonHr(reasonHr);
        data.setDateHr(DateUtils.getNowDate());
        data.setStatusHrUserId(SecurityUtils.getUserId());
        if (statusHr == PostPlanEnum.STATUS_HR_PASS.getValue()) {
            data.setStatus(PostProgrammeEnum.STATUS_PASS.getValue());//审核通过
            data.setStep(PostProgrammeEnum.STEP_UP.getValue());
            data.setReasonHr("--");
        } else {
            data.setStatus(PostProgrammeEnum.STATUS_BACK.getValue());//驳回
        }
        return postProgrammeMapper.updatePostProgramme(data);
    }
}
