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

@WorkflowTaskListener(name = "贸易服务管理", group = "贸易服务")
public class TradeServicesListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private ITradeWarehouseReceiptApplService tradeWarehouseReceiptApplService;

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

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
        TradeWarehouseReceiptAppl tradeWarehouseReceiptAppl = tradeWarehouseReceiptApplService.selectById(businessKey);
        if (tradeWarehouseReceiptAppl == null) {
            return;
        }
        tradeWarehouseReceiptAppl.setStatusFlag(StatusEnum.RETURN_TRADE_WAREHOUSE_RECEIPT.getFlag());
        tradeWarehouseReceiptAppl.setStatus(StatusEnum.RETURN_TRADE_WAREHOUSE_RECEIPT.getName());
        tradeWarehouseReceiptAppl.setApproveStatus(null);
        tradeWarehouseReceiptApplService.updateAllColumnById(tradeWarehouseReceiptAppl);

    }

}
