ErpBackUpdateStatusServiceImpl.java 11.9 KB
package com.huaheng.api.erp.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.huaheng.common.utils.Wrappers;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import org.apache.xpath.operations.Bool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.erp.domainBack.*;
import com.huaheng.api.erp.domainBack.finishedR.ParameterFinished;
import com.huaheng.api.erp.domainBack.inventoryScrapC.ParameterInventoryScrap;
import com.huaheng.api.erp.domainBack.issuanceC.ParameterIssuance;
import com.huaheng.api.erp.domainBack.miscellaneousR.ParameterMiscellaneous;
import com.huaheng.api.erp.domainBack.noticeWmsC.ParameterNotice;
import com.huaheng.api.erp.domainBack.purchaseWmsR.ParameterPurchase;
import com.huaheng.api.erp.domainBack.saleR.Parameter;
import com.huaheng.api.erp.domainBack.scrapC.ParameterScrap;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
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.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;

/**
 * @author 游杰
 */
@Service
public class ErpBackUpdateStatusServiceImpl implements IErpBackUpdateStatusService {

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


    @Transactional(rollbackFor = Exception.class)
    public AjaxResult backTaskUpdateStatus(TaskHeader task,List<BackTaskStatus> backTaskStatusList){
        String msg=task.getBackInfo();
        msg="";
        if(StringUtils.isNotEmpty(msg)){
            msg=msg+",";
        }else{
            msg="";
        }
        Integer status=0;
        for(BackTaskStatus backTaskStatus:backTaskStatusList){
            //更新入库或出库单表状态
            switch (task.getInternalTaskType()){
                case 100:
                    //成功了去检查详情有没有都完成
                    ReceiptHeader receiptHeader=new ReceiptHeader();
                    receiptHeader.setId(backTaskStatus.getBillId());
                    String backMsg="";
                    ReceiptHeader receiptHeader1=receiptHeaderService.getById(receiptHeader.getId());
                    if(receiptHeader1!=null&&StringUtils.isNotEmpty(receiptHeader1.getBackMsg())){
                        backMsg=receiptHeader1.getBackMsg()+",";
                    }
                    if(backTaskStatus.getIsSuccess()){
                        boolean tag=checkReceiptDetailComplete(backTaskStatus.getBillId(),task.getWarehouseCode());
                        if(backMsg.contains("失败")){
                            backMsg="";
                        }
                        receiptHeader.setBackMsg(backMsg+backTaskStatus.getMsg());
                        if(tag){
                            //更改入库单状态
                            receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                            receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                        }
                        compaleteAllTask(backTaskStatus,task,msg,status,receiptHeader,null);
                    }else{
                        //返回错误
//                        receiptHeader.setBackMsg("失败");
                    }
                    receiptHeaderService.updateById(receiptHeader);

                    break;
                case 200:
                    //成功了去检查详情有没有都完成
                    ShipmentHeader shipmentHeader=new ShipmentHeader();
                    shipmentHeader.setId(backTaskStatus.getBillId());
                    String backMsg1="";
                    ShipmentHeader shipmentHeader1=shipmentHeaderService.getById(shipmentHeader.getId());
                    if(shipmentHeader1!=null&&StringUtils.isNotEmpty(shipmentHeader1.getBackMsg())){
                        backMsg1=shipmentHeader1.getBackMsg()+",";
                    }
                    if(backTaskStatus.getIsSuccess()){
                        boolean tag=checkShipmentDetailComplete(backTaskStatus.getBillId(),task.getWarehouseCode());
                        if(backMsg1.contains("失败")){
                            backMsg1="";
                        }
                        shipmentHeader.setBackMsg(backMsg1+backTaskStatus.getMsg());
                        if(tag){
                            //更改入库单状态
                            shipmentHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                            shipmentHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RETURN);
                        }
                        compaleteAllTask(backTaskStatus,task,msg,status,null,shipmentHeader);
                    }else{
//                        shipmentHeader.setBackMsg("失败");
                    }
                    shipmentHeaderService.updateById(shipmentHeader);
                    break;
                default:
                    break;
            }
            msg+=backTaskStatus.getMsg()+",";
            status=backTaskStatus.getStatus();
        }
        TaskHeader taskHeader1 = new TaskHeader();
        taskHeader1.setId(task.getId());
        taskHeader1.setBackQty(task.getBackQty() + 1);
        taskHeader1.setBackInfo(msg);
        if(msg.length()>600){
            taskHeader1.setBackInfo("请求失败,具体信息请查看日志");
        }else{
            taskHeader1.setBackInfo(msg);
        }
        taskHeader1.setIsReturn(status);
        //查找详情有上游号未回传的,有则为false
        if(status!=QuantityConstant.SN_RETURN&&taskDetailAllBack(task.getWarehouseCode(),task.getId())){
            //整单回传的,等待中,没有回传信息,则不更改状态
            if(StringUtils.isNotEmpty(taskHeader1.getBackInfo())){
                taskHeader1.setIsReturn(1);
            }
        }
        taskHeaderService.updateById(taskHeader1);
        if(status!=2){
            return AjaxResult.error("请求失败,具体信息请查看日志:"+msg);
        }
        return AjaxResult.success(msg);
    }

    /**
     * 更新该单据其他任务状态
     */
    public void compaleteAllTask(BackTaskStatus backTaskStatus,TaskHeader task,String msg,Integer status,ReceiptHeader receiptHeader,ShipmentHeader shipmentHeader){
        Set<Integer> taskIds=backTaskStatus.getTaskIds();
        if(taskIds!=null && taskIds.size()>0){
            //整单回传的该单据其他任务如果完成了,则查找该任务底下详情是否都已完成,已完成则更改任务状态
            for(Integer taskId:taskIds){
                if(taskId!=null&&taskId!=task.getId()){
                    //获取未回传任务集
                    Boolean tag=null;
                    if(receiptHeader!=null){
                        tag=erpBackService.taskAllComplete(receiptHeader.getCode(),receiptHeader.getWarehouseCode(),task.getId());
                    }else{
                        tag=erpBackService.taskAllComplete(shipmentHeader.getCode(),shipmentHeader.getWarehouseCode(),task.getId());
                    }
                    if(tag){
                        //同一个单据该任务还有未完成的
//                        TaskHeader taskHeader1 = new TaskHeader();
//                        taskHeader1.setId(taskId);
//                        taskHeader1.setBackQty(task.getBackQty() + 1);
//                        taskHeader1.setBackInfo(backmsg+msg);
//                        taskHeader1.setIsReturn(status);
//                        taskHeaderService.updateById(taskHeader1);
                    }
                }
            }
        }
    }

    public Boolean checkReceiptDetailComplete(Integer billId,String warehouseCode){
        LambdaQueryWrapper<ReceiptDetail> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ReceiptDetail::getWarehouseCode,warehouseCode);
        wrapper.eq(ReceiptDetail::getReceiptId,billId);
        List<ReceiptDetail> list=receiptDetailService.findByReceiptId(billId);
        Boolean flag=true;
        for(ReceiptDetail receiptDetail:list){
            flag=isAllComplete(receiptDetail.getQty(),receiptDetail.getTaskQty(),receiptDetail.getBackQty());
            if(!flag){
                break;
            }
        }
        return flag;
    }
    public Boolean checkShipmentDetailComplete(Integer billId,String warehouseCode){
        List<ShipmentDetail> list=shipmentDetailService.getListByShipmentHeaderId(billId,warehouseCode);
        Boolean flag=true;
        for(ShipmentDetail shipmentDetail:list){
            flag=isAllComplete(shipmentDetail.getQty(),shipmentDetail.getTaskQty(),shipmentDetail.getBackQty());
            if(!flag){
                break;
            }
        }
        return flag;
    }
    private Boolean isAllComplete(BigDecimal qty,BigDecimal taskQty,BigDecimal backQty){
        Boolean flag = false;
        // 判断总数量 和 收货数量 是否一致
        if(qty.compareTo(taskQty) != 0){
            return false;
        }
        if(backQty.compareTo(qty)==0){
            flag = true;
        }else{
            //判断 回传数量和总数量
            if(qty.compareTo(backQty) == 0){
                flag = true;
            }else{
                return false;
            }
        }
        return flag;
    }

    /**
     * 返回true则是还有未完成的,false则是空的
     * @param warehouseCode
     * @param taskid
     * @return  返回空位false  有数据有true
     */
    public boolean taskDetailAllBack(String warehouseCode,Integer taskid){
        LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskDetail::getWarehouseCode,warehouseCode)
                .eq(TaskDetail::getTaskId,taskid)
                .apply(" (billCode not  like 'transfer_c%')")
                .apply(" (referenceCode is not null and referenceCode!='')")
                .eq(TaskDetail::getBack,0);
        List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
        return !taskDetails.isEmpty();
    }

    /**
     * 判断该出通单任务是否含有sn,有sn则仓库手动回传
     * @param unbackTask
     * @return
     */
    public boolean shipmentHaveSn(List<TaskDetail> unbackTask){
        for(TaskDetail taskDetail:unbackTask){
            if(StringUtils.isNotEmpty(taskDetail.getSn())){
                return true;
            }
        }
        return false;
    }
}