package com.yh.saas.plugin.yiliangyiyun.workflow;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
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.WarehousingOrder;
import com.yh.saas.plugin.yiliangyiyun.service.IContractManagementInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IWarehousingOrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@WorkflowTaskListener(name = "仓库管理", group = "入库单")
public class WarehousingOrderListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private IWarehousingOrderService warehousingOrderService;

    @Override
    public void handleStatusChanged(String businessKey, String status, String statusEn, boolean processIsEnd) {
        List<WarehousingOrder> list = warehousingOrderService.selectList(new EntityWrapper<WarehousingOrder>().eq("batch_id",businessKey));
        for(WarehousingOrder warehousingOrder : list){
            if (warehousingOrder == null) {
                return;
            }
            if (processIsEnd) {
                warehousingOrder.setStatusFlag(StatusEnum.NOT_COLLECTION_PAY.getFlag());
                warehousingOrder.setStatus(StatusEnum.NOT_COLLECTION_PAY.getName());
                warehousingOrder.setWorkflowId(null);
                warehousingOrder.setApproveStatus(null);
            } else {
                warehousingOrder.setApproveStatus(status);
            }
            warehousingOrderService.updateAllColumnById(warehousingOrder);
        }

    }

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
        List<WarehousingOrder> list = warehousingOrderService.selectList(new EntityWrapper<WarehousingOrder>().eq("batch_id",businessKey));
        for(WarehousingOrder warehousingOrder : list){
            warehousingOrder = warehousingOrderService.selectById(warehousingOrder.getId());
            if (warehousingOrder == null) {
                return;
            }
            warehousingOrder.setStatusFlag(StatusEnum.TASK_RETURN.getFlag());
            warehousingOrder.setStatus(StatusEnum.TASK_RETURN.getName());
            warehousingOrder.setApproveStatus(null);
            warehousingOrderService.updateAllColumnById(warehousingOrder);
        }


    }

}
