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

@WorkflowTaskListener(name = "统计报表", group = "销售平仓统计")
public class SaleCloseReportListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private ISalesClosingReportService salesClosingReportService;

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

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

    }

}
