ShipmentQuickService.java 13.9 KB
package com.huaheng.pc.task.taskHeader.service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.huaheng.api.general.service.ShipmentApiService;
import com.huaheng.common.support.Convert;
import com.huaheng.pc.shipment.shippingCombination.service.ShippingCombinationService;
import com.huaheng.pc.shipment.wave.domain.Wave;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.ConfigService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.materialWarnning.service.IMaterialWarningService;
import com.huaheng.pc.config.shipmentPreference.service.ShipmentPreferenceService;
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.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentCombinationModel;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
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.system.dict.service.IDictDataService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.ShipmentTaskCreateModel;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;

/**
 * 入库任务创建和完成
 *
 * @author mahua
 * @date 2020/7/14
 */
@Service
public class ShipmentQuickService {

    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;

    @Resource
    private InventoryHeaderService inventoryHeaderService;

    @Resource
    ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    ShipmentApiService shipmentApiService;



    /**
     * 整盘出库
     * @param inventoryHeader
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentInventoryHeader(InventoryHeader inventoryHeader) {
        if (StringUtils.isNull(inventoryHeader)) {
            return AjaxResult.error("批量快速出库,所选库存头为空");
        }
        if(StringUtils.isNull(inventoryHeader.getId())){
            return AjaxResult.error("批量快速出库,库存ID为空");
        }
        InventoryHeader inventoryHeader1=inventoryHeaderService.getById(inventoryHeader.getId());
        String toPortCode = inventoryHeader.getPort();
        if (StringUtils.isEmpty(toPortCode)) {
//            return AjaxResult.error("批量快速出库,出库站台编码为空");
        }
        String warehouseCode=inventoryHeader1.getWarehouseCode();
        LambdaQueryWrapper<InventoryDetail> lambdaQueryWrapper2 = Wrappers.lambdaQuery();
        lambdaQueryWrapper2.eq(InventoryDetail::getWarehouseCode, warehouseCode)
                .eq(InventoryDetail::getInventoryHeaderId, inventoryHeader1.getId());
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(lambdaQueryWrapper2);
        if (StringUtils.isEmpty(inventoryDetails)) {
            return AjaxResult.error("批量快速出库, 排除锁定库存后没有可出库存详情");
        }
        //调用出库单接口创建出库单
        ShipmentDomain shipmentDomain=new ShipmentDomain();
        List<ShipmentDetail> shipmentDetails=new ArrayList<>();
        String companyCode = inventoryDetails.get(0).getCompanyCode();
        for (InventoryDetail inventoryDetail:inventoryDetails){
            ShipmentDetail shipmentDetail=new ShipmentDetail();
            shipmentDetail.setCompanyCode(companyCode);
            shipmentDetail.setWarehouseCode(warehouseCode);
            shipmentDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            shipmentDetail.setMaterialName(inventoryDetail.getMaterialName());
            shipmentDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            shipmentDetail.setQty(inventoryDetail.getQty());
            shipmentDetail.setMoCode(inventoryDetail.getMoCode());
            shipmentDetail.setInventorySts(inventoryDetail.getInventorySts());
            shipmentDetail.setUserDef1(inventoryDetail.getId().toString());
            shipmentDetails.add(shipmentDetail);
        }
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setWarehouseCode(warehouseCode);
        shipmentHeader.setCompanyCode(companyCode);
        shipmentHeader.setShipmentType(QuantityConstant.SHIPMENT_TYPE_PO);
//        shipmentHeader.setReferCode(inventoryHeader.get);
        shipmentHeader.setRemark("快速出库");
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetails);
        AjaxResult result = shipmentApiService.shipment(shipmentDomain);
        if (result.hasErr()) {
            throw new ServiceException("批量快速出库,创建出库单失败:".concat(result.getMsg()));
        }
//        String shipmentCode=(String)result.getData();
        // 进行手动出库组盘
        for (ShipmentDetail shipmentDetail:shipmentDetails){
            ShipmentCombinationModel shipmentCombinationModel = new ShipmentCombinationModel();
            shipmentCombinationModel.setShipmentDetailId(shipmentDetail.getId());
            shipmentCombinationModel.setShipQty(shipmentDetail.getQty());
            shipmentCombinationModel.setInventoryDetailId(Integer.valueOf(shipmentDetail.getUserDef1()));
            ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.combination(shipmentCombinationModel);
        }
        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeader = Wrappers.lambdaQuery();
        shipmentContainerHeader.eq(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        shipmentContainerHeader.eq(ShipmentContainerHeader::getContainerCode, inventoryHeader1.getContainerCode());
        List<ShipmentContainerHeader> shipmentContainerHeaders = shipmentContainerHeaderService.list(shipmentContainerHeader);
        for (ShipmentContainerHeader item : shipmentContainerHeaders) {
            List<Integer> idList = Arrays.asList(Convert.toIntArray(item.getId().toString()));
            AjaxResult ajaxResults = shipmentContainerHeaderService.createTask(idList);
            if (ajaxResults.hasErr()) {
                throw new ServiceException("任务生成失败:" + ajaxResults.getMsg());
            }
            // 查询出库任务
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getAllocationHeadId, item.getId());
            TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
            if (taskHeader == null){
                throw new RuntimeException("创建出库任务失败!");
            }
            if(StringUtils.isNotEmpty(toPortCode)){
                TaskHeader taskHeaderTemp = new TaskHeader();
                taskHeaderTemp.setId(taskHeader.getId());
                taskHeaderTemp.setPickingCartPos(toPortCode);
                // 修改任务出库口
                if(taskHeader.getZoneCode().equals("JLK")){
                    taskHeaderTemp.setPort(QuantityConstant.JIJIA_PORT);
                }
                taskHeaderService.updateById(taskHeaderTemp);
            }
        }
        return AjaxResult.success("批量快速出库成功");
    }

    /**
     * 分拣出库
     * @param inventoryDetaillist
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipmentInventoryDetail(List<InventoryDetail> inventoryDetaillist) {
        if (StringUtils.isNull(inventoryDetaillist)) {
            return AjaxResult.error("批量快速出库,所选数据为空");
        }
        List<Integer> ids= inventoryDetaillist.stream().map(InventoryDetail::getId).collect(Collectors.toList());
        LambdaQueryWrapper<InventoryDetail> lambdaQueryWrapper2 = Wrappers.lambdaQuery();
        lambdaQueryWrapper2.in(InventoryDetail::getId, ids);
        List<InventoryDetail> inventoryDetails = inventoryDetailService.list(lambdaQueryWrapper2);
        if (StringUtils.isEmpty(inventoryDetails)) {
            return AjaxResult.error("批量快速出库, 排除锁定库存后没有可出库存详情");
        }
        InventoryHeader inventoryHeader=inventoryHeaderService.getById(inventoryDetails.get(0).getInventoryHeaderId());
        String warehouseCode=inventoryDetails.get(0).getWarehouseCode();
        //调用出库单接口创建出库单
        ShipmentDomain shipmentDomain=new ShipmentDomain();
        List<ShipmentDetail> shipmentDetails=new ArrayList<>();
        String companyCode = inventoryDetails.get(0).getCompanyCode();
        for (InventoryDetail inventoryDetail:inventoryDetails){
            ShipmentDetail shipmentDetail=new ShipmentDetail();
            shipmentDetail.setCompanyCode(companyCode);
            shipmentDetail.setWarehouseCode(warehouseCode);
            shipmentDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            shipmentDetail.setMaterialName(inventoryDetail.getMaterialName());
            shipmentDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            shipmentDetail.setQty(inventoryDetail.getQty());
            shipmentDetail.setMoCode(inventoryDetail.getMoCode());
            shipmentDetail.setInventorySts(inventoryDetail.getInventorySts());
            shipmentDetail.setUserDef1(inventoryDetail.getId().toString());
            shipmentDetails.add(shipmentDetail);
        }
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setWarehouseCode(warehouseCode);
        shipmentHeader.setCompanyCode(companyCode);
        shipmentHeader.setShipmentType(QuantityConstant.SHIPMENT_TYPE_PO);
//        shipmentHeader.setReferCode(inventoryHeader.get);
        shipmentHeader.setRemark("快速出库");
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetails);
        AjaxResult result = shipmentApiService.shipment(shipmentDomain);
        if (result.hasErr()) {
            throw new ServiceException("批量快速出库,创建出库单失败:".concat(result.getMsg()));
        }
//        String shipmentCode=(String)result.getData();
        // 进行手动出库组盘
        for (ShipmentDetail shipmentDetail:shipmentDetails){
            ShipmentCombinationModel shipmentCombinationModel = new ShipmentCombinationModel();
            shipmentCombinationModel.setShipmentDetailId(shipmentDetail.getId());
            shipmentCombinationModel.setShipQty(shipmentDetail.getQty());
            shipmentCombinationModel.setInventoryDetailId(Integer.valueOf(shipmentDetail.getUserDef1()));
            ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.combination(shipmentCombinationModel);
        }


        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeader = Wrappers.lambdaQuery();
        shipmentContainerHeader.eq(ShipmentContainerHeader::getStatus, QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        shipmentContainerHeader.eq(ShipmentContainerHeader::getContainerCode, inventoryHeader.getContainerCode());
        List<ShipmentContainerHeader> shipmentContainerHeaders = shipmentContainerHeaderService.list(shipmentContainerHeader);
        for (ShipmentContainerHeader item : shipmentContainerHeaders) {
            List<Integer> idList = Arrays.asList(Convert.toIntArray(item.getId().toString()));
            AjaxResult ajaxResults = shipmentContainerHeaderService.createTask(idList);
            if (ajaxResults.hasErr()) {
                throw new ServiceException("任务生成失败:" + ajaxResults.getMsg());
            }
            // 查询出库任务
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getAllocationHeadId, item.getId());
            TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
            if (taskHeader == null){
                throw new RuntimeException("创建出库任务失败!");
            }

        }
        return AjaxResult.success("批量快速出库成功");
    }


}