TaskBackQueue.java 9.64 KB
package com.huaheng.pc.task.taskHeader.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskBackDomain;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TaskBackQueue implements BackQueue{

    @Resource
    private  ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private  TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;

    @Override
    public void receiveBack(int taskId, CallBack callBack) {
        Map<String,String> backMsg = new HashMap<>();
        Integer type = null;
        TaskBackDomain domain = new TaskBackDomain().setIsSuc(false)
                .setBackNum(QuantityConstant.ISRETURN_FAIL);

        AjaxResult ajaxResult = AjaxResult.success();
        if(StringUtils.isNull(taskId)){

            callBack.back(domain.setMsg("任务id不能为空"));
            return;
        }
        TaskHeader header = taskHeaderService.getById(taskId);
        if(header == null){
            callBack.back(domain.setMsg("找不到任务id为:"+taskId+"的任务"));
            return;
        }
        switch (header.getTaskType()){
            case 500 :
            case 600 :
            case 800 :
            case 900 :
            case 1000 :
            case 1100 :
            case 1200 :
                domain.setBackNum(QuantityConstant.NOT_RETURN).setMsg("无需回传").setIsSuc(true);
                callBack.back(domain);
                return;
        }
        List<TaskDetail> taskDetails = taskDetailService.findByTaskId(taskId);
        if(taskDetails==null || taskDetails.size()==0){
            callBack.back(domain.setMsg("找不到任务id为:"+taskId+"的任务,没有任务明细"));
            return;
        }
        List<String> billCodes = taskDetails.stream().map(TaskDetail::getBillCode).collect(Collectors.toList());
        Map<Integer, BigDecimal> detailsQtyMap =new HashMap<>();
        for (TaskDetail taskDetail : taskDetails) {
            BigDecimal bigDecimal = detailsQtyMap.get(taskDetail.getBillDetailId());
            if(bigDecimal!=null){
                detailsQtyMap.put(taskDetail.getBillDetailId(),bigDecimal.add(taskDetail.getQty()));
            }else{
                detailsQtyMap.put(taskDetail.getBillDetailId(),taskDetail.getQty());
            }
        }
        switch (header.getInternalTaskType()){
            case 100 :
                String receiptHeaderId = "";

                LambdaQueryWrapper<ReceiptHeader> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.in(ReceiptHeader::getCode,billCodes)
                        .eq(ReceiptHeader::getWarehouseCode,header.getWarehouseCode());
                //获取任务中的入库单
                List<ReceiptHeader> list = receiptHeaderService.list(queryWrapper);
                for (ReceiptHeader receiptHeader : list) {
                    if(StringUtils.isEmpty(receiptHeader.getReferCode())){
                        domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
                        callBack.back(domain);
                        continue;
                    }
                    if(taskAllComplete(receiptHeader.getCode())){
                        continue;
                    }
                    receiptHeaderId = receiptHeader.getId().toString();
                    //获取未回传任务集
                    List<TaskDetail> unBackTask = getUnBackTask(receiptHeader.getCode());
                    if(unBackTask.size()==0){
                        continue;
                    }
                    Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
                    ajaxResult = receiptHeaderService.postBack(header,receiptHeaderId, taskFinishMaps);
                    switch (receiptHeader.getReceiptType()){
                        case "SI" :
                            receiptHeaderService.postBack2Mes(header,receiptHeaderId, taskFinishMaps);
                            break;
                    }
                    String msg = ajaxResult.getMsg();
                    List<Map> list1 = JSON.parseArray(msg, Map.class);
                    if (!ajaxResult.hasErr()) {
                        switch (receiptHeader.getReceiptType()){
                            case "PI" :
                                receiptHeaderService.postBack2SRM(receiptHeader,list1,taskFinishMaps);
                                break;
                        }
                    }
                    if(!ajaxResult.hasErr()){
                        for (TaskDetail taskDetail : unBackTask) {
                            taskDetail.setBack(1);
                        }
                        taskDetailService.updateBatchById(unBackTask);
                    }
                    backMsg.put(receiptHeader.getCode(),ajaxResult.getMsg());
                    type = 1;
                }
                break;
            case 200 :
                String shipmentHeaderId = "";
                //需回传的任务
                LambdaQueryWrapper<ShipmentHeader> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.in(ShipmentHeader::getCode,billCodes)
                                .eq(ShipmentHeader::getWarehouseCode,header.getWarehouseCode());
                //获取任务中的出库单
                List<ShipmentHeader> list1 = shipmentHeaderService.list(queryWrapper1);
                //遍历回传每个出库单
                for (ShipmentHeader shipmentHeader : list1) {
                    if(StringUtils.isEmpty(shipmentHeader.getReferCode())){
                        domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
                        continue;
                    }
                    if(taskAllComplete(shipmentHeader.getCode())){
                        continue;
                    }
                    shipmentHeaderId = shipmentHeader.getId().toString();
                    //获取未回传任务集
                    List<TaskDetail> unBackTask = getUnBackTask(shipmentHeader.getCode());
                    if(unBackTask.size()==0){
                        continue;
                    }
                    Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
                    ajaxResult = shipmentHeaderService.postBack(header,shipmentHeaderId,taskFinishMaps);
                    if(!ajaxResult.hasErr()){
                        for (TaskDetail taskDetail : unBackTask) {
                            taskDetail.setBack(1);
                        }
                        taskDetailService.updateBatchById(unBackTask);
                    }
                    backMsg.put(shipmentHeader.getCode(),ajaxResult.getMsg());
                    type = 2;
                }
                break;
        }
        boolean flag = !ajaxResult.hasErr();
        String  message = ajaxResult.getMsg();
        Integer result = flag ?  QuantityConstant.ISRETURN_SUCC : QuantityConstant.ISRETURN_FAIL;
        domain.setIsSuc(flag).setMsg(message).setBackNum(result).setType(type).setMsgMaps(backMsg);
        callBack.back(domain);
    }

    private boolean taskAllComplete(String billCode){
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getBillCode,billCode)
                .lt(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
        return !taskDetails.isEmpty();
    }

    private Map<Integer, BigDecimal> getTaskFinishMaps(List<TaskDetail> taskDetails){
        Map<Integer, BigDecimal> map = new HashMap<>();
        for (TaskDetail taskDetail : taskDetails) {
            if(StringUtils.isNull(map.get(taskDetail.getBillDetailId()))){
                map.put(taskDetail.getBillDetailId(),taskDetail.getQty());
            }else{
                map.put(taskDetail.getBillDetailId(),map.get(taskDetail.getBillDetailId()).add(taskDetail.getQty()));
            }
        }
        return map;
    }

    private List<TaskDetail> getUnBackTask(String billCode){
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getBillCode,billCode)
                .eq(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED)
                .eq(TaskDetail::getBack,0);
        List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
        return taskDetails;
    }

}