package com.ruoyi.activiti.listener;

import com.ruoyi.activiti.domain.BizEmployeReport;
import com.ruoyi.activiti.service.IBizEmployeReportService;
import com.ruoyi.common.utils.DateUtils;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

    private static final long serialVersionUID = 1L;

    @Autowired
    IBizEmployeReportService bizEmployeReportService;


    @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()));

                //addapply    leaveapply   updateapply  delapply  transferapply

                if (!pass) {//如果是审批退回，则更新业务表状态 已退回
                    if("addapply".equals(bizEmployeReport.getApproveType())){ //新增申请
                        bizEmployeReport.setApproveStatus("3"); //审核退回
                        bizEmployeReport.setFinishTime(DateUtils.getNowDate());
                        bizEmployeReport.setUpdateTime(DateUtils.getNowDate());
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }else if("leaveapply".equals(bizEmployeReport.getApproveType())){ //离职申请
                        bizEmployeReport.setApproveStatus("3"); //审核退回
                        bizEmployeReport.setFinishTime(DateUtils.getNowDate());
                        bizEmployeReport.setUpdateTime(DateUtils.getNowDate());
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }else if("delapply".equals(bizEmployeReport.getApproveType())){ //删除申请
                        bizEmployeReport.setApproveStatus("3"); //审核退回
                        bizEmployeReport.setFinishTime(DateUtils.getNowDate());
                        bizEmployeReport.setUpdateTime(DateUtils.getNowDate());
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }else if("transferapply".equals(bizEmployeReport.getApproveType())){ //调动申请
                        bizEmployeReport.setApproveStatus("3"); //审核退回
                        bizEmployeReport.setFinishTime(DateUtils.getNowDate());
                        bizEmployeReport.setUpdateTime(DateUtils.getNowDate());
                        bizEmployeReportService.updateBizEmployeReport(bizEmployeReport);
                    }
                }
            }
        }
    }
}
