package com.yh.saas.plugin.yiliangyiyun.workflow;

import com.yh.saas.common.support.workflow.AbstractWorkflowTaskListener;
import com.yh.saas.common.support.workflow.annotation.WorkflowTaskListener;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.LeaveInfo;
import com.yh.saas.plugin.yiliangyiyun.service.ILeaveInfoService;
import org.springframework.beans.factory.annotation.Autowired;

@WorkflowTaskListener(name = "考勤管理", group = "请假管理")
public class QingjiaListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private ILeaveInfoService leaveInfoService;

    @Override
    public void handleStatusChanged(String businessKey, String status, String statusEn, boolean processIsEnd) {
        LeaveInfo leaveInfo = leaveInfoService.selectById(businessKey);
        if (leaveInfo == null) {
            return;
        }
        if (processIsEnd) {
            leaveInfo.setStatusFlag(StatusEnum.ORDER_PASSED.getFlag());
            leaveInfo.setStatus(StatusEnum.ORDER_PASSED.getName());
            leaveInfo.setWorkflowId(null);
            leaveInfo.setApproveStatus(null);
        } else {
            leaveInfo.setApproveStatus(status);
        }
        leaveInfoService.updateAllColumnById(leaveInfo);
    }

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
        LeaveInfo leaveInfo = leaveInfoService.selectById(businessKey);
        if (leaveInfo == null) {
            return;
        }
        leaveInfo.setStatusFlag(StatusEnum.PURCHASE_FAILED.getFlag());
        leaveInfo.setStatus(StatusEnum.PURCHASE_FAILED.getName());
        leaveInfo.setApproveStatus(null);
        leaveInfoService.updateAllColumnById(leaveInfo);

    }

}
