package com.ruoyi.activiti.listener;

import com.ruoyi.activiti.domain.BizEmployeReport;
import com.ruoyi.activiti.service.IBizEmployeReportService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.constant.EmployeApproveStatus;
import com.ruoyi.system.constant.EmployeApproveType;
import com.ruoyi.system.domain.SysGzReportEmploye;
import com.ruoyi.system.service.ISysGzReportEmployeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 流程开始前执行监听器 demo
 * @author 一只闲鹿
 */
@Component
public class BizEmployeReportApprovedEndListener implements ExecutionListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    IBizEmployeReportService bizEmployeReportService;

    @Autowired
    ISysGzReportEmployeService sysGzReportEmployeService;

    @Override
    public void notify(DelegateExecution execution) {
        String eventName = execution.getEventName();
        if(this.EVENTNAME_START.equals(eventName)){
            if(execution.hasVariable("pass")){
                Boolean pass = (Boolean) execution.getVariable("pass");
                BizEmployeReport bizEmployeReport = bizEmployeReportService.selectBizEmployeReportById(new Long(execution.getProcessInstanceBusinessKey()));
                bizEmployeReport.setFinishTime(DateUtils.getNowDate());
                bizEmployeReport.setApproveBy(SecurityUtils.getUsername());
                bizEmployeReport.setApproveTime(new Date());
                bizEmployeReport.setApproveContent((String)execution.getVariable("comment"));
                bizEmployeReport.setUpdateTime(DateUtils.getNowDate());

                if("addapply".equals(bizEmployeReport.getApproveType())) { //新增申请
                    if (pass) {//如果是审批通过，则更新业务表状态 已批准

                        SysGzReportEmploye checkIdcard = sysGzReportEmployeService.selectSysGzReportEmployeByIdCard(bizEmployeReport.getIdcardNo());
                        if(checkIdcard != null){
                            throw new RuntimeException("身份证号已存在，不能重复添加！");
                        }

                        //插入花名册 ； 审批业务信息（新增的员工信息）
                        SysGzReportEmploye employe = new SysGzReportEmploye();
                        employe.setUniqueCode(bizEmployeReport.getUniqueCode());
                        employe.setDeptId(bizEmployeReport.getDeptId());
                        employe.setName(bizEmployeReport.getName());
                        employe.setPhone(bizEmployeReport.getPhone());
                        employe.setIdcardNo(bizEmployeReport.getIdcardNo());
                        employe.setSex(bizEmployeReport.getSex());
                        employe.setBirthDay(bizEmployeReport.getBirthDay());
                        employe.setOnlyChild(bizEmployeReport.getOnlyChild());
                        employe.setFolk(bizEmployeReport.getFolk());
                        employe.setPoliticalStatus(bizEmployeReport.getPoliticalStatus());
                        employe.setNativePlace(bizEmployeReport.getNativePlace());
                        employe.setFirstEducation(bizEmployeReport.getFirstEducation());
                        employe.setFirstEducationSchool(bizEmployeReport.getFirstEducationSchool());
                        employe.setFirstEducationProfession(bizEmployeReport.getFirstEducationProfession());
                        employe.setTopEducation(bizEmployeReport.getTopEducation());
                        employe.setTopEducationSchool(bizEmployeReport.getTopEducationSchool());
                        employe.setTopEducationProfession(bizEmployeReport.getTopEducationProfession());
                        employe.setProfessionalTitle(bizEmployeReport.getProfessionalTitle());
                        employe.setPostJob(bizEmployeReport.getPostJob());
                        employe.setPostCode(bizEmployeReport.getPostCode());
                        employe.setPostTitle(bizEmployeReport.getPostTitle());
                        employe.setPostLevel(bizEmployeReport.getPostLevel());
                        employe.setPostTime(bizEmployeReport.getPostTime());
                        employe.setWorkDate(bizEmployeReport.getWorkDate());
                        employe.setHiredate(bizEmployeReport.getHiredate());
                        employe.setFiredate(bizEmployeReport.getFiredate());
                        employe.setEmployeType(bizEmployeReport.getEmployeType());
                        employe.setPersonnelDossier(bizEmployeReport.getPersonnelDossier());
                        employe.setPersonnelDossierLocation(bizEmployeReport.getPersonnelDossierLocation());
                        employe.setStatus(bizEmployeReport.getStatus());
                        employe.setApproveStatus(EmployeApproveStatus.Enable);
                        employe.setApproveType(EmployeApproveType.addapply);
                        employe.setApproveBy(SecurityUtils.getUsername());
                        employe.setApproveTime(new Date());
                        employe.setApproveContent((String)execution.getVariable("comment"));
                        employe.setCreateBy(bizEmployeReport.getCreateBy());
                        employe.setCreateDate(new Date());
                        employe.setCreateTime(bizEmployeReport.getCreateTime());
                        employe.setRemark(bizEmployeReport.getRemark());
                        int row = sysGzReportEmployeService.insertSysGzReportEmploye(employe);
                        if(row > 0){
                            //更新业务表 , 并将新增员工ID，更新到业务审批表
                            bizEmployeReport.setEmployeId(employe.getEmployeId());
                            bizEmployeReport.setApproveStatus(EmployeApproveStatus.Enable);
                            bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                        }
                    }else{//否则 未批准
                        bizEmployeReport.setApproveStatus(EmployeApproveStatus.Reject);  //审核被拒
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }
                }else if("leaveapply".equals(bizEmployeReport.getApproveType())){ //离职申请
                    //更新花名册信息
                    SysGzReportEmploye sysEmploye = sysGzReportEmployeService.selectSysGzReportEmployeById(bizEmployeReport.getEmployeId());
                    if (pass) {//如果是审批通过，则更新业务表状态 已批准
                        sysEmploye.setApproveType(EmployeApproveType.leaveapply);
                        sysEmploye.setApproveStatus(EmployeApproveStatus.Enable); //0：审批同已生效，1：审批中; 2: 审批被拒
                        sysEmploye.setStatus(bizEmployeReport.getStatus());  //离职
                        sysEmploye.setFiredate(bizEmployeReport.getFiredate());
                        sysEmploye.setApproveBy(SecurityUtils.getUsername());
                        sysEmploye.setApproveTime(new Date());
                        sysEmploye.setApproveContent((String)execution.getVariable("comment"));
                        sysEmploye.setUpdateBy(SecurityUtils.getUsername());
                        sysEmploye.setUpdateDate(new Date());
                        int row = sysGzReportEmployeService.updateSysGzReportEmploye(sysEmploye, false, "");
                        if(row > 0 ){
                            //更新业务表 审批状态
                            bizEmployeReport.setApproveStatus(EmployeApproveStatus.Enable);
                            bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                        }
                    }else{// 如果是审批不通过，则 花名册 不要更新； 只需要更新 审批表
                        bizEmployeReport.setApproveStatus(EmployeApproveStatus.Reject);
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }
                }else if("delapply".equals(bizEmployeReport.getApproveType())){ //删除申请
                    //更新花名册信息
                    SysGzReportEmploye sysEmploye = sysGzReportEmployeService.selectSysGzReportEmployeById(bizEmployeReport.getEmployeId());
                    if (pass) {//如果是审批通过，则更新业务表状态 已批准
                        sysEmploye.setApproveType(EmployeApproveType.delapply);
                        sysEmploye.setApproveStatus(EmployeApproveStatus.Enable); //0：审批通过已生效，1：审批中; 2: 审批被拒
                        sysEmploye.setDeleteStatus(bizEmployeReport.getDeleteStatus());
                        sysEmploye.setDeleteBy(SecurityUtils.getUsername());
                        sysEmploye.setDeleteTime(new Date());
                        sysEmploye.setApproveBy(SecurityUtils.getUsername());
                        sysEmploye.setApproveTime(new Date());
                        sysEmploye.setApproveContent((String)execution.getVariable("comment"));
                        sysEmploye.setUpdateBy(SecurityUtils.getUsername());
                        sysEmploye.setUpdateDate(new Date());
                        int row = sysGzReportEmployeService.updateSysGzReportEmploye(sysEmploye, false, "");
                        if(row > 0 ){
                            //更新业务表 审批状态
                            bizEmployeReport.setApproveStatus(EmployeApproveStatus.Enable);
                            bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                        }
                    }else{
                        bizEmployeReport.setApproveStatus(EmployeApproveStatus.Reject);
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }
                }else if("transferapply".equals(bizEmployeReport.getApproveType())){ //调动申请
                    //更新花名册信息
                    SysGzReportEmploye sysEmploye = sysGzReportEmployeService.selectSysGzReportEmployeById(bizEmployeReport.getEmployeId());
                    if (pass) {//如果是审批通过，则更新业务表状态 已批准
                        sysEmploye.setApproveType(EmployeApproveType.transferapply);
                        sysEmploye.setApproveStatus(EmployeApproveStatus.Enable); //0：审批通过已生效，1：审批中; 2: 审批被拒
                        sysEmploye.setDeptId(bizEmployeReport.getDeptId());
                        sysEmploye.setApproveBy(SecurityUtils.getUsername());
                        sysEmploye.setApproveTime(new Date());
                        sysEmploye.setApproveContent((String)execution.getVariable("comment"));
                        sysEmploye.setUpdateBy(SecurityUtils.getUsername());
                        sysEmploye.setUpdateDate(new Date());
                        int row = sysGzReportEmployeService.updateSysGzReportEmploye(sysEmploye, false, "");
                        if(row > 0 ){
                            //更新业务表 审批状态
                            bizEmployeReport.setApproveStatus(EmployeApproveStatus.Enable);
                            bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                        }
                    }else{
                        bizEmployeReport.setApproveStatus(EmployeApproveStatus.Reject);
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }
                }
            }
        }
    }
}
