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.PurchasePrice;
import com.yh.saas.plugin.yiliangyiyun.entity.SaleOrder;
import com.yh.saas.plugin.yiliangyiyun.service.IPurchasePriceService;
import com.yh.saas.plugin.yiliangyiyun.service.ISaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;

@WorkflowTaskListener(name = "收购管理", group = "仓库设置")
public class AcquisitionListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IPurchasePriceService purchasePriceService;

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

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
        PurchasePrice purchasePrice = purchasePriceService.selectById(businessKey);
        if (purchasePrice == null) {
            return;
        }
        purchasePrice.setStatusFlag(StatusEnum.PURCHASE_FAILED.getFlag());
        purchasePrice.setStatus(StatusEnum.PURCHASE_FAILED.getName());
        purchasePrice.setApproveStatus(null);
        purchasePriceService.updateAllColumnById(purchasePrice);

    }

}
