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

@WorkflowTaskListener(name = "统计报表", group = "现货采购入库统计")
public class GoodsPurchaseReceiptReportListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IStockPurchaseReceiptReportService stockPurchaseReceiptReportService;

    @Override
    public void handleStatusChanged(String businessKey, String status, String statusEn, boolean processIsEnd  ) {
        StockPurchaseReceiptReport purchaseReceiptReport = stockPurchaseReceiptReportService.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);
        }
        stockPurchaseReceiptReportService.updateAllColumnById(purchaseReceiptReport);
    }

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

    }

}
