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.BatchReviewInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.PurchaseReceiptReport;
import com.yh.saas.plugin.yiliangyiyun.entity.TranSettlementReport;
import com.yh.saas.plugin.yiliangyiyun.service.IBatchReviewInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IPurchaseReceiptReportService;
import com.yh.saas.plugin.yiliangyiyun.service.ITranSettlementReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

@WorkflowTaskListener(name = "统计报表", group = "运输结算统计")
public class TranSettlementReportListener extends AbstractWorkflowTaskListener {

    private static final long serialVersionUID = 4103656734835351132L;

    @Autowired
    private ITranSettlementReportService tranSettlementReportService;
    @Autowired
    private IBatchReviewInfoService batchReviewInfoService;

    @Override
    public void handleStatusChanged(String businessKey, String status, String statusEn, boolean processIsEnd) {
//        TranSettlementReport tranSettlementReport = tranSettlementReportService.selectById(businessKey);
        BatchReviewInfo batchReviewInfo = batchReviewInfoService.selectById(businessKey);
        if (batchReviewInfo == null) {
            return;
        }
        int flag = 0;
        if (processIsEnd) {
            batchReviewInfo.setStatusFlag(StatusEnum.ORDER_PASSED.getFlag());
            batchReviewInfo.setStatus(StatusEnum.ORDER_PASSED.getName());
            batchReviewInfo.setWorkflowId(null);
            batchReviewInfo.setApproveStatus(null);
            flag =1;
        } else {
            batchReviewInfo.setApproveStatus(status);
        }
        batchReviewInfoService.updateAllColumnById(batchReviewInfo);

        //  更新详细状态
//        batchReviewInfo = batchReviewInfoService.selectById(batchReviewInfo.getId());
        List<TranSettlementReport> tranSettlementReports = tranSettlementReportService.selectList(new EntityWrapper<TranSettlementReport>()
                .eq("batch_id",businessKey));
        if(!CollectionUtils.isEmpty(tranSettlementReports)){
            for(int i=0;i<tranSettlementReports.size();i++){
                TranSettlementReport settlementReport = tranSettlementReports.get(i);
                if(flag==1){
                    settlementReport.setWorkflowId(null);
                    settlementReport.setStatus(batchReviewInfo.getStatus());
                    settlementReport.setApproveStatus(null);
                }
                else{
                    settlementReport.setWorkflowId(batchReviewInfo.getWorkflowId());
                    settlementReport.setStatus(batchReviewInfo.getStatus());
                    settlementReport.setApproveStatus(batchReviewInfo.getApproveStatus());
                }

            }
            tranSettlementReportService.updateBatchById(tranSettlementReports);

        }
    }

    @Override
    public void handleRejectApplicationRepulsed(String workflowId, String businessKey, boolean needReapply) {
//        TranSettlementReport tranSettlementReport = tranSettlementReportService.selectById(businessKey);
        BatchReviewInfo batchReviewInfo = batchReviewInfoService.selectById(businessKey);
        if (batchReviewInfo == null) {
            return;
        }
        batchReviewInfo.setStatusFlag(StatusEnum.TASK_RETURN.getFlag());
        batchReviewInfo.setStatus(StatusEnum.TASK_RETURN.getName());
        batchReviewInfo.setApproveStatus(null);
        batchReviewInfoService.updateAllColumnById(batchReviewInfo);
        //  更新详细状态
//        batchReviewInfo = batchReviewInfoService.selectById(batchReviewInfo.getId());
        List<TranSettlementReport> tranSettlementReports = tranSettlementReportService.selectList(new EntityWrapper<TranSettlementReport>()
                .eq("batch_id",businessKey));
        if(!CollectionUtils.isEmpty(tranSettlementReports)){
            for(int i=0;i<tranSettlementReports.size();i++){
                TranSettlementReport settlementReport = tranSettlementReports.get(i);
                settlementReport.setWorkflowId(batchReviewInfo.getWorkflowId());
                settlementReport.setStatus(batchReviewInfo.getStatus());
                settlementReport.setApproveStatus(batchReviewInfo.getApproveStatus());

            }
            tranSettlementReportService.updateBatchById(tranSettlementReports);

        }

    }

}
