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

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

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IPurchaseReceiptReportService purchaseReceiptReportService;

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

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

    }

}
