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.PurchaseClosingReport;
import com.yh.saas.plugin.yiliangyiyun.entity.PurchaseReceiptReport;
import com.yh.saas.plugin.yiliangyiyun.service.IPurchaseClosingReportService;
import com.yh.saas.plugin.yiliangyiyun.service.IPurchaseReceiptReportService;
import org.springframework.beans.factory.annotation.Autowired;

@WorkflowTaskListener(name = "统计报表", group = "采购平仓统计")
public class PurchaseClosingReportListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IPurchaseClosingReportService purchaseClosingReportService;

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

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

    }

}
