BackInvokeService.java 7.28 KB
package com.huaheng.pc.task.taskHeader.service;

import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
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.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
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 BackInvokeService {

    @Resource
    private BackQueue backQueue;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    public void invokeBack(int taskId){
        new Thread(()->{
            backQueue.receiveBack(taskId, new CallBack() {
                @Override
                    public void back(TaskBackDomain domain) {
                    TaskHeader taskHeader = taskHeaderService.getById(taskId);
                    taskHeader.setBackQty(taskHeader.getBackQty()+1);
                    taskHeader.setBackInfo(domain.getMsg());
                    taskHeader.setIsReturn(domain.getBackNum());
                    taskHeaderService.updateById(taskHeader);
                    List<TaskDetail> taskDetails = taskDetailService.findByTaskId(taskId);
                    List<String> billCodes = taskDetails.stream().map(TaskDetail::getBillCode).collect(Collectors.toList());

                    if(StringUtils.isNotNull(domain.getType())){
                        if(domain.getType()==1){
                            //获取所有入库单
                            List<ReceiptHeader> receiptHeaders = receiptHeaderService.listByCodes(billCodes, taskHeader.getWarehouseCode());
                            for (ReceiptHeader receiptHeader : receiptHeaders) {
                                List<ReceiptDetail> details = receiptDetailService.findByReceiptId(receiptHeader.getId());
                                int isFlag = 0;
                                for (ReceiptDetail detail : details) {
                                    isFlag = isComplete(detail.getQty(),detail.getTaskQty(),detail.getBackQty());
                                    if(isFlag == 0){
                                        break;
                                    }
                                }
                                String s = domain.getMsgMaps().get(receiptHeader.getCode());
                                if(StringUtils.isNull(receiptHeader.getBackMsg())){
                                    receiptHeader.setBackMsg(s);
                                }else{
                                    if(!receiptHeader.getBackMsg().contains(s)){
                                        receiptHeader.setBackMsg(receiptHeader.getBackMsg()+","+s);
                                    }
                                }
                                if(domain.getIsSuc()){
                                    receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                                    if(isFlag==1){
                                        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                                    }
                                }
                                receiptHeaderService.updateById(receiptHeader);
                            }
                        }else if(domain.getType()==2){
                            //获取所有入库单
                            List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.listByCodes(billCodes, taskHeader.getWarehouseCode());
                            for (ShipmentHeader shipmentHeader : shipmentHeaders) {
                                List<ShipmentDetail> details = shipmentDetailService.getListByShipmentHeaderId(shipmentHeader.getId(),shipmentHeader.getWarehouseCode());
                                int isFlag = 0;
                                for (ShipmentDetail detail : details) {
                                    // 判断总数量 和 收货数量 是否一致
                                    isFlag = isComplete(detail.getQty(),detail.getTaskQty(),detail.getBackQty());
                                    if(isFlag == 0){
                                        break;
                                    }
                                }
                                String msg = domain.getMsgMaps().get(shipmentHeader.getCode());
                                if(StringUtils.isNull(shipmentHeader.getBackMsg())){
                                    shipmentHeader.setBackMsg(msg);
                                }else{
                                    if(!shipmentHeader.getBackMsg().contains(msg)){
                                        shipmentHeader.setBackMsg(shipmentHeader.getBackMsg()+","+msg);
                                    }
                                }
                                if(domain.getIsSuc()){
                                    shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_RETURN);
                                    if(isFlag==1){
                                        shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_RETURN);
                                    }
                                }
                                shipmentHeaderService.updateById(shipmentHeader);
                            }
                        }
                    }
                }
            });
        },"async-thread").start();
    }

    private int isComplete(BigDecimal qty,BigDecimal taskQty,BigDecimal backQty){
        int num = 0;
        // 判断总数量 和 收货数量 是否一致
        if(qty.compareTo(taskQty) != 0){
            return 0;
        }
        if(backQty.compareTo(qty)==0){
            num = 1;
        }else{
            //判断 回传数量和总数量
            if(qty.compareTo(backQty) == 0){
                num = 1;
            }else{
                return 0;
            }
        }
        return num;
    }
}