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

@WorkflowTaskListener(name = "销售管理", group = "销售订单")
public class SaleOrderListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private ISaleOrderService saleOrderService;

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

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

    }

}
