ShipmentAdviceService.java 22.6 KB
package com.huaheng.pc.shipment.shipmentContainerAdvice.service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.shipment.shipmentContainerAdvice.domain.ShipmentContainerAdvice;
import com.huaheng.pc.shipment.shipmentContainerAdvice.service.ShipmentContainerAdviceService;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
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;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ShipmentAdviceService{

    @Resource
    private ShipmentHeaderService shipmentHeaderService;

    @Autowired
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Autowired
    private ShipmentContainerAdviceService shipmentContainerAdviceService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private TaskHeaderService taskHeaderService;
    @Autowired
    private StationService stationService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;
    @Resource
    private TaskDetailService taskDetailService;



    /**
     * 自动预配盘出库
     * @param shipmentId
     * @param warehouseCode
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult autoShipmentAdvice(Integer shipmentId, String warehouseCode) {
        if (StringUtils.isEmpty(shipmentId)) {
            return AjaxResult.error("自动预配盘出库, 出库单编码为空");
        }
        ShipmentHeader shipmentHeader = shipmentHeaderService.getById(shipmentId);
        if (shipmentHeader == null) {
            throw new ServiceException("自动预配盘出库, 没有找到出库单");
        }
        warehouseCode=shipmentHeader.getWarehouseCode();
        if(StringUtils.isEmpty(shipmentHeader.getCompanyCode())){
            throw new ServiceException("自动预配盘出库, 没有找到出库单货主");
        }
        if(StringUtils.isEmpty(shipmentHeader.getUWarehouseCode())){
            throw new ServiceException("自动预配盘出库, 没有找到出库单上游仓库");
        }
        String shipmentCode=shipmentHeader.getCode();
        AjaxResult result = shipmentContainerHeaderService.autoCombination(shipmentCode,true,warehouseCode);
        if (result.hasErr()) {
            throw new ServiceException("自动预配盘出库," + result.getMsg());
        }
        List<ShipmentContainerAdvice> shipmentContainerAdviceList =
                shipmentContainerAdviceService.getShipmentContainerAdviceListByShipmentCode(shipmentCode, warehouseCode);
        if (CollectionUtils.isEmpty(shipmentContainerAdviceList)) {
            throw new ServiceException("自动预配盘出库, 没有找到出库配盘详情或找不到含SN库存");
        }
        List<Integer> shipmentContainerIdList =
                shipmentContainerAdviceList.stream().map(ShipmentContainerAdvice::getShipmentContainerId).distinct().collect(Collectors.toList());
        long shipmentOrder = System.currentTimeMillis();
        int sequenceNumber = shipmentContainerIdList.size();
        int sequence = 0;
        for (int shipmentContainerId : shipmentContainerIdList) {
            sequence++;
            ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getById(shipmentContainerId);
            if (shipmentContainerHeader == null) {
                throw new ServiceException("自动预配盘出库, 没有找到出库表头:" + shipmentContainerId);
            }
            int taskType = shipmentContainerHeader.getTaskType();
            int type = QuantityConstant.STATION_PICK;
            if (taskType == QuantityConstant.TASK_TYPE_WHOLESHIPMENT) {
                type = QuantityConstant.STATION_OUT;
            }
            String fromLocationCode = shipmentContainerHeader.getLocationCode();
            if (StringUtils.isEmpty(fromLocationCode)) {
                throw new ServiceException("自动预配盘出库, 没有起始库位编码");
            }
            Location fromLocation = locationService.getLocationByCode(fromLocationCode, warehouseCode);
            if (fromLocation == null) {
                throw new ServiceException("自动预配盘出库, 没有起始库位" + fromLocationCode);
            }
            String roadWay = fromLocation.getRoadway();
            List<Station> portList =stationService.getListByType(type, roadWay, shipmentContainerHeader.getZoneCode(), warehouseCode);
            String portcode=null;
            if (portList == null||portList.size()==0) {
                //地面库没有站台
//                throw new ServiceException("自动预配盘出库, 没有合适的出库口");
            }else{
                Station port = portList.get(0);
                portcode=port.getCode();
            }

            ShipmentContainerHeader shipmentContainerHeader1 = new ShipmentContainerHeader();
            shipmentContainerHeader1.setId(shipmentContainerHeader.getId());
            shipmentContainerHeader1.setPort(portcode);
            shipmentContainerHeader1.setAdvice(QuantityConstant.STATUS_OPEN);
            boolean success = shipmentContainerHeaderService.updateById(shipmentContainerHeader1);
            if (!success) {
                throw new ServiceException("自动预配盘出库, 更新出库组盘头失败");
            }
            result = shipmentContainerAdviceService.createCallShipmentTask(shipmentContainerHeader, warehouseCode, shipmentOrder, sequence, sequenceNumber);
            if (result.hasErr()) {
                throw new ServiceException("自动预配盘出库, " + result.getMsg());
            }
            String containerCode = shipmentContainerHeader.getContainerCode();
            if (StringUtils.isEmpty(containerCode)) {
                throw new ServiceException("自动预配盘出库, 容器号为空");
            }
            TaskHeader taskHeader = taskHeaderService.getUnCompleteTaskByContainerCode(containerCode, warehouseCode);
            if (taskHeader == null) {
                throw new ServiceException("自动预配盘出库, 没有找到容器号对应任务" + containerCode);
            }
        }
        return AjaxResult.success("自动预配盘出库成功");
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult reduceInventoryDetailBySn(String snList, String warehouseCode) {
        List<String> snList1 = Arrays.asList(snList.split(","));
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.in(InventoryDetail::getSn, snList1).eq(InventoryDetail::getWarehouseCode, warehouseCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(inventoryDetailList)) {
            return AjaxResult.error("预配盘扣减库存失败,没有找到库存详情");
        }
        List<String> containerCodeList =
                inventoryDetailList.stream().map(InventoryDetail::getContainerCode).distinct().collect(java.util.stream.Collectors.toList());
        if (containerCodeList.size() > 1) {
            return AjaxResult.error("预配盘扣减库存失败,库存详情不在同一个托盘上");
        }
        String containerCode = containerCodeList.get(0);
        TaskHeader taskHeader = taskHeaderService.getUnCompleteTaskByContainerCode(containerCode, warehouseCode);
        if (taskHeader == null) {
            return AjaxResult.error("预配盘扣减库存失败,托盘号:" + containerCode + "没有未完成的任务");
        }
        if (taskHeader.getTaskType() != QuantityConstant.TASK_TYPE_SORTINGSHIPMENT) {
            return AjaxResult.error("预配盘扣减库存失败,托盘号:" + containerCode + "不是出库任务");
        }
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            String materialCode = inventoryDetail.getMaterialCode();
            LambdaQueryWrapper<ShipmentContainerAdvice> shipmentContainerAdviceLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentContainerAdviceLambdaQueryWrapper.eq(ShipmentContainerAdvice::getMaterialCode, materialCode)
                    .eq(ShipmentContainerAdvice::getContainerCode, containerCode)
                    .lt(ShipmentContainerAdvice::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
            List<ShipmentContainerAdvice> shipmentContainerAdvices = shipmentContainerAdviceService.list(shipmentContainerAdviceLambdaQueryWrapper);
            if (shipmentContainerAdvices == null||shipmentContainerAdvices.size()==0) {
                throw new ServiceException("预配盘扣减库存失败,托盘号:" + containerCode + "没有找到物料编码:" + materialCode + "的预配盘");
            }

            List<ShipmentContainerAdvice> shipmentContainerAdvice2=shipmentContainerAdvices.stream().filter(l ->
                    l.getInventoryDetailId().equals(inventoryDetail.getId())
            ).collect(Collectors.toList());
            ShipmentContainerAdvice shipmentContainerAdvice=null;
            if(shipmentContainerAdvice2.size()>0){
                shipmentContainerAdvice=shipmentContainerAdvice2.get(0);
            }else{
                shipmentContainerAdvice=shipmentContainerAdvices.get(0);
            }
            BigDecimal inventoryDetailQty = inventoryDetail.getQty();
            BigDecimal shipmentContainerAdviceQty = shipmentContainerAdvice.getQty();
            BigDecimal shipmentContainerAdviceTaskQty = shipmentContainerAdvice.getTaskQty();
            BigDecimal qty = shipmentContainerAdviceQty.subtract(shipmentContainerAdviceTaskQty);
            if (inventoryDetailQty.compareTo(qty) > 0) {
                throw new ServiceException("预配盘扣减库存失败, sn:"+inventoryDetail.getSn()+",库存详情ID:" + inventoryDetail.getId() + "的数量大于预配盘ID:" + shipmentContainerAdvice.getId() + "的数量");
            }
            shipmentContainerAdviceTaskQty = shipmentContainerAdviceTaskQty.add(inventoryDetailQty);
            if (!shipmentContainerAdviceService.updateTaskQtyById(shipmentContainerAdviceTaskQty, shipmentContainerAdvice.getId())) {
                throw new ServiceException("预配盘扣减库存失败, 更新预配盘ID:" + shipmentContainerAdvice.getId() + "的任务数量失败");
            }
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(shipmentContainerAdvice.getShipmentDetailId());
            if (shipmentDetail == null) {
                throw new ServiceException("预配盘扣减库存失败, 没有找到出库单,出库详情ID:" + shipmentContainerAdvice.getShipmentDetailId());
            }
            BigDecimal shipmentDetailShipmentQty = shipmentDetail.getTaskQty();
//            shipmentDetailShipmentQty = shipmentDetailShipmentQty.add(inventoryDetailQty);
//            if (shipmentDetailShipmentQty.compareTo(shipmentDetail.getQty()) > 0) {
//                throw new ServiceException("预配盘扣减库存失败, 出库单ID:" + shipmentDetail.getShipmentId() + "的出库数量大于出库单数量");
//            }
            if (!shipmentDetailService.updateTashQtyById(shipmentDetailShipmentQty, shipmentDetail.getId())) {
                throw new ServiceException("预配盘扣减库存失败, 更新出库单ID:" + shipmentDetail.getShipmentId() + "的出库数量失败");
            }
            if (shipmentDetailShipmentQty.compareTo(shipmentDetail.getQty()) == 0) {
                if (!shipmentDetailService.updateStatusById(QuantityConstant.SHIPMENT_HEADER_COMPLETED, shipmentDetail.getId())) {
                    throw new ServiceException("预配盘扣减库存失败, 更新出库单ID:" + shipmentDetail.getShipmentId() + "的状态失败");
                }
            }
            ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getById(shipmentContainerAdvice.getShipmentContainerId());
            if (shipmentContainerHeader == null) {
                throw new ServiceException("预配盘扣减库存失败, 没有找到" + shipmentContainerAdvice.getShipmentContainerId() + "的出库组盘头");
            }
            ShipmentContainerDetail shipmentContainerDetail = new ShipmentContainerDetail();
            shipmentContainerDetail.setShipmentCode(shipmentContainerAdvice.getShipmentCode());
            shipmentContainerDetail.setShippingContainerId(shipmentContainerHeader.getId());
            shipmentContainerDetail.setMaterialCode(shipmentContainerAdvice.getMaterialCode());
            shipmentContainerDetail.setMaterialName(shipmentContainerAdvice.getMaterialName());
            shipmentContainerDetail.setMaterialSpec(shipmentContainerAdvice.getMaterialSpec());
            shipmentContainerDetail.setMaterialUnit(shipmentContainerAdvice.getMaterialUnit());
            shipmentContainerDetail.setQty(inventoryDetailQty);
            shipmentContainerDetail.setSn(inventoryDetail.getSn());
            shipmentContainerDetail.setInventoryId(inventoryDetail.getId());
            shipmentContainerDetail.setShipmentId(shipmentContainerAdvice.getShipmentId());
            shipmentContainerDetail.setBatch(shipmentContainerAdvice.getBatch());
            shipmentContainerDetail.setContainerCode(shipmentContainerAdvice.getContainerCode());
            shipmentContainerDetail.setLocationCode(inventoryDetail.getLocationCode());
            shipmentContainerDetail.setWarehouseCode(warehouseCode);
            shipmentContainerDetail.setShipmentDetailId(shipmentContainerAdvice.getShipmentDetailId());
            shipmentContainerDetail.setShipmentId(shipmentContainerAdvice.getShipmentId());
            shipmentContainerDetail.setCompanyCode(shipmentContainerAdvice.getCompanyCode());
            shipmentContainerDetail.setInventorySts(inventoryDetail.getInventorySts());
            if (!shipmentContainerDetailService.save(shipmentContainerDetail)) {
                throw new ServiceException("预配盘扣减库存失败, 保存出库组盘明细表失败");
            }
            //生成任务
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(taskHeader.getId());
            taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
            taskDetail.setCompanyCode(taskHeader.getCompanyCode());
            taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());
            taskDetail.setUWarehouseCode(shipmentContainerDetail.getUWarehouseCode());
            taskDetail.setErpWarehouseCode(shipmentContainerDetail.getErpWarehouseCode());
            taskDetail.setCompanyCode(taskHeader.getCompanyCode());
            taskDetail.setTaskType(taskHeader.getTaskType());
            taskDetail.setToInventoryId(shipmentContainerDetail.getInventoryId());
            taskDetail.setAllocationId(shipmentContainerDetail.getId());
            taskDetail.setBillCode(shipmentContainerDetail.getShipmentCode());
            taskDetail.setBillDetailId(shipmentContainerDetail.getShipmentDetailId());
            taskDetail.setReferenceCode(shipmentDetail.getReferCode());
            try {
                taskDetail.setReferenceLineId(Integer.valueOf(shipmentDetail.getReferLineNum()));
            } catch (NumberFormatException e) {
            }
            taskDetail.setMaterialCode(shipmentContainerDetail.getMaterialCode());
            taskDetail.setMaterialName(shipmentContainerDetail.getMaterialName());
            taskDetail.setMaterialSpec(shipmentContainerDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(shipmentContainerDetail.getMaterialUnit());
            taskDetail.setFromInventoryId(shipmentContainerDetail.getInventoryId());
            taskDetail.setQty(shipmentContainerDetail.getQty());
            taskDetail.setContainerCode(taskHeader.getContainerCode());
            taskDetail.setGoodsShelfNo(taskHeader.getGoodsShelfNo());
            taskDetail.setFromLocation(taskHeader.getFromLocation());
            taskDetail.setReferenceCode(shipmentDetail.getReferCode());
            taskDetail.setMoCode(shipmentDetail.getMoCode());
            taskDetail.setLot(shipmentContainerDetail.getLot());
            taskDetail.setBatch(shipmentContainerDetail.getBatch());
            taskDetail.setProjectNo(shipmentContainerDetail.getProjectNo());
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
            taskDetail.setWaveId(shipmentContainerDetail.getWaveId());
            taskDetail.setInventorySts(shipmentContainerDetail.getInventorySts());
            taskDetail.setSn(shipmentContainerDetail.getSn());

            taskDetail.setCreatedBy(shipmentContainerDetail.getCreatedBy());
            if (!taskDetailService.save(taskDetail)) {
                throw new ServiceException("新建任务明细失败,sql报错");
            }

            InventoryTransaction inventoryTransaction = new InventoryTransaction();
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_SHIPMENT);
            inventoryTransaction.setWarehouseCode(warehouseCode);
            inventoryTransaction.setCompanyCode(inventoryDetail.getCompanyCode());
            inventoryTransaction.setContainerCode(containerCode);
            inventoryTransaction.setZoneCode(inventoryDetail.getZoneCode());
            inventoryTransaction.setLocationCode(inventoryDetail.getLocationCode());
            inventoryTransaction.setMaterialCode(inventoryDetail.getMaterialCode());
            inventoryTransaction.setMaterialName(inventoryDetail.getMaterialName());
            inventoryTransaction.setMaterialSpec(inventoryDetail.getMaterialSpec());
            inventoryTransaction.setMaterialUnit(inventoryDetail.getMaterialUnit());
            inventoryTransaction.setInventorySts(inventoryDetail.getInventorySts());
            inventoryTransaction.setBillCode(shipmentContainerAdvice.getShipmentCode());
            inventoryTransaction.setBillDetailId(shipmentContainerAdvice.getShipmentDetailId());

            inventoryTransaction.setBatch(inventoryDetail.getBatch());
            inventoryTransaction.setLot(inventoryDetail.getLot());
            inventoryTransaction.setProjectNo(inventoryDetail.getProjectNo());
            inventoryTransaction.setTaskQty(inventoryDetailQty);

            if (!inventoryTransactionService.save(inventoryTransaction)) {
                throw new ServiceException("预配盘扣减库存失败, 保存库存交易记录失败");
            }
            if (!inventoryDetailService.removeById(inventoryDetail.getId())) {
                throw new ServiceException("预配盘扣减库存失败, 删除库存明细" + inventoryDetail.getId() + "失败");
            }
            Integer adviceInventory=shipmentContainerAdvice.getInventoryDetailId();
            if(adviceInventory!=null&&!adviceInventory.equals(inventoryDetail.getId())){
                InventoryDetail inventoryDetail1=inventoryDetailService.getById(adviceInventory);
                InventoryDetail inventoryDetail2=new InventoryDetail();
                inventoryDetail2.setId(inventoryDetail1.getId());
                BigDecimal qtyAdvice=inventoryDetail1.getTaskQty().subtract(inventoryDetail.getQty());
                inventoryDetail2.setTaskQty(qtyAdvice);
                inventoryDetailService.updateById(inventoryDetail2);
            }

        }
        InventoryHeader inventoryHeader = inventoryHeaderService.getInventoryHeaderByContainerCode(containerCode, warehouseCode);
        if (inventoryHeader == null) {
            throw new ServiceException("预配盘扣减库存失败, 没有找到" + containerCode + "库存表头");
        }
        List<InventoryDetail> inventoryDetailList1 = inventoryDetailService.getInventoryDetailListByInventoryHeaderId(inventoryHeader.getId());
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(inventoryDetailList1)) {
            if (!inventoryHeaderService.removeById(inventoryHeader.getId())) {
                throw new ServiceException("预配盘扣减库存失败,删除库存头失败" + inventoryHeader.getId());
            }
        } else {
            if (!taskHeaderService.combineInventoryDetail(containerCode, warehouseCode)) {
                throw new ServiceException("预配盘扣减库存失败,托盘:" + containerCode + "调整库存失败");
            }
        }
        return AjaxResult.success("预配盘扣减库存成功");
    }

}