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

@WorkflowTaskListener(name = "采购管理", group = "采购订单")
public class ProcurementOrderListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IPurchaseOrderService iPurchaseOrderService;

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

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

    }

}
