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.ContractManagementInfo;
import com.yh.saas.plugin.yiliangyiyun.service.IContractManagementInfoService;
import org.springframework.beans.factory.annotation.Autowired;

@WorkflowTaskListener(name = "合同管理", group = "代收合同")
public class DsContractListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IContractManagementInfoService contractManagementInfoService;

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

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
        ContractManagementInfo contractManagementInfo = contractManagementInfoService.selectById(businessKey);
        if (contractManagementInfo == null) {
            return;
        }
        contractManagementInfo.setStatusFlag(StatusEnum.TASK_RETURN.getFlag());
        contractManagementInfo.setStatus(StatusEnum.TASK_RETURN.getName());
        contractManagementInfo.setApproveStatus(null);
        contractManagementInfoService.updateAllColumnById(contractManagementInfo);

    }

}
