ShipmentContainerAdviceService.java 18.6 KB
package com.huaheng.pc.shipment.shipmentContainerAdvice.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.RetCode;
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.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
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.shipment.shipmentContainerAdvice.domain.ShipmentContainerAdvice;
import com.huaheng.pc.shipment.shipmentContainerAdvice.mapper.ShipmentContainerAdviceMapper;
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
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.service.ShipmentHeaderService;
import com.huaheng.pc.system.config.domain.Config;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ShipmentContainerAdviceService extends ServiceImpl<ShipmentContainerAdviceMapper, ShipmentContainerAdvice> {

    private static final Logger logger = LoggerFactory.getLogger(ShipmentContainerAdviceService.class);
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private LocationService locationService;
    @Resource
    private ContainerService containerService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private IDictDataService dictDataService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;

    public boolean updateStatusById(int status, int id) {
        ShipmentContainerAdvice shipmentContainerAdvice = new ShipmentContainerAdvice();
        shipmentContainerAdvice.setStatus(status);
        shipmentContainerAdvice.setId(id);
        boolean success = updateById(shipmentContainerAdvice);
        return success;
    }

    public boolean updateTaskQtyById(BigDecimal qty, int id) {
        ShipmentContainerAdvice shipmentContainerAdvice = new ShipmentContainerAdvice();
        shipmentContainerAdvice.setTaskQty(qty);
        shipmentContainerAdvice.setId(id);
        shipmentContainerAdvice.setStatus(QuantityConstant.SHIPMENT_CONTAINER_TASK);
        boolean success = updateById(shipmentContainerAdvice);
        return success;
    }

    public List<ShipmentContainerAdvice> getShipmentContainerAdviceListHeaderId(Integer id) {
        LambdaQueryWrapper<ShipmentContainerAdvice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShipmentContainerAdvice::getShipmentContainerId, id);
        List<ShipmentContainerAdvice> list = this.list(wrapper);
        return list;
    }
    public Boolean deleteShipmentContainerAdviceListHeaderId(Integer id) {
        LambdaQueryWrapper<ShipmentContainerAdvice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShipmentContainerAdvice::getShipmentContainerId, id);
        Boolean tag = this.remove(wrapper);
        return tag;
    }


    public List<ShipmentContainerAdvice> getShipmentContainerAdviceListByShipmentCode(String shipmentCode, String warehouseCode) {
        LambdaQueryWrapper<ShipmentContainerAdvice> shipmentContainerAdviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerAdviceLambdaQueryWrapper.eq(ShipmentContainerAdvice::getShipmentCode, shipmentCode)
                .eq(ShipmentContainerAdvice::getWarehouseCode, warehouseCode).lt(ShipmentContainerAdvice::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
        List<ShipmentContainerAdvice> shipmentContainerAdviceList = list(shipmentContainerAdviceLambdaQueryWrapper);
        return shipmentContainerAdviceList;
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createCallShipmentTask(ShipmentContainerHeader shipmentContainerHeader, String warehouseCode, long shipmentOrder, int sequence,
                                             int sequenceNumber) {
        Integer preTaskNo = 0;
        if (shipmentContainerHeader == null) {
            return AjaxResult.error("生成出库任务时, 出库组盘头" + "未找到,操作中止");
        }
        shipmentContainerHeader = shipmentContainerHeaderService.getById(shipmentContainerHeader.getId());
        if (shipmentContainerHeader.getStatus() >= QuantityConstant.SHIPMENT_CONTAINER_TASK) {
            return AjaxResult.error("生成出库任务时, 出库组盘头" + shipmentContainerHeader.getId() + "已经生成任务,请不要重复生成,操作中止");
        }
        if (!shipmentContainerHeader.getWarehouseCode().equals(warehouseCode)) {
            return AjaxResult.error("生成出库任务时, 出库组盘头" + shipmentContainerHeader.getId() + "仓库编码和登录的仓库编码不一致");
        }
        String fromLocationCode = shipmentContainerHeader.getLocationCode();
        String containerCode = shipmentContainerHeader.getContainerCode();
        if (StringUtils.isEmpty(fromLocationCode)) {
            return AjaxResult.error("生成出库任务时, 出库组盘头没有起始库位号");
        }
        Location location = locationService.getLocationByCode(fromLocationCode, warehouseCode);
        if (location == null) {
            return AjaxResult.error("生成出库任务时, 库位号" + fromLocationCode + "没有找到库位");
        }
        if (location.getRowFlag() == QuantityConstant.ROW_OUT) {
            Location location1 = locationService.getNear(location);
            if (location1 != null) {
                String locationCode = location1.getCode();
                TaskHeader taskHeader = taskHeaderService.getUnCompleteTaskByToLocationCode(locationCode, warehouseCode);
                if (taskHeader != null) {
                    preTaskNo = taskHeader.getPreTaskNo();
                }
            }
        }
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return AjaxResult.error("生成出库任务时, 托盘不存在" + containerCode);
        }
        if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
            return AjaxResult.error("生成出库任务时, 托盘已经锁定,containerCode:" + containerCode);
        }
        container.setStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
        boolean success = containerService.updateById(container);
        if (!success) {
            throw new ServiceException("生成出库任务时, 更新容器失败,containerCode:" + containerCode);
        }
        locationService.updateStatus(fromLocationCode, QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        if (!success) {
            throw new ServiceException("生成出库任务时, 更新库位失败,fromLocationCode:" + fromLocationCode);
        }
        int taskType = QuantityConstant.TASK_TYPE_SORTINGSHIPMENT;
        String zoneCode = location.getZoneCode();
        TaskHeader taskHeader = new TaskHeader();
        taskHeader.setCompanyCode(shipmentContainerHeader.getCompanyCode());
        taskHeader.setPreTaskNo(preTaskNo);
        taskHeader.setTaskType(taskType);
        taskHeader.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskHeader.setZoneCode(zoneCode);
        taskHeader.setAdvice(1);
        taskHeader.setWarehouseCode(warehouseCode);
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        taskHeader.setContainerCode(containerCode);
        taskHeader.setFromLocation(fromLocationCode);

        String value = dictDataService.getDictValueByLabel(QuantityConstant.RULE_SHIPMENT_TASK, taskHeader.getZoneCode(), taskHeader.getWarehouseCode());
        int shipmentTaskRule = Integer.parseInt(value);
        //整盘出库
        if(shipmentTaskRule == QuantityConstant.RULE_TASK_WHOLE_SHIPMENT) {
        }else{
            taskHeader.setToLocation(fromLocationCode);
        }

        taskHeader.setAllocationHeadId(shipmentContainerHeader.getId());
        success = taskHeaderService.save(taskHeader);
        if (!success) {
            throw new ServiceException("生成出库任务时, 创建任务失败");
        }
        ShipmentContainerHeader shipmentContainerHeader1 = new ShipmentContainerHeader();
        shipmentContainerHeader1.setId(shipmentContainerHeader.getId());
        shipmentContainerHeader1.setTaskType(taskType);
        shipmentContainerHeader1.setStatus(QuantityConstant.SHIPMENT_CONTAINER_TASK);
        success = shipmentContainerHeaderService.updateById(shipmentContainerHeader1);
        if (!success) {
            throw new ServiceException("生成出库任务时, 更新出库组盘头失败");
        }
        success = this.updateInventoryContainerStatusByContainerCode(containerCode, warehouseCode);
        if (!success) {
            throw new ServiceException("生成出库任务时, 更新库存头失败");
        }
        return AjaxResult.success("生成出库任务成功", taskHeader);
    }
    @Transactional
    public boolean updateInventoryContainerStatusByContainerCode(String containerCode, String warehouseCode) {
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            return false;
        }
        InventoryHeader inventoryHeader = inventoryHeaderService.getInventoryHeaderByContainerCode(containerCode, warehouseCode);
        if (inventoryHeader == null) {
            return true;
        }
        if (!inventoryHeaderService.updateContainerStatusById(container.getStatus(), inventoryHeader.getId())) {
            throw new ServiceException("更新库存头表状态失败");
        }
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.getInventoryDetailListByContainerCode(containerCode, warehouseCode);
        List<InventoryDetail> updateInventoryDetailList = new ArrayList<>();
        if (inventoryDetailList.size() > 0) {
            for (InventoryDetail inventoryDetail : inventoryDetailList) {
                InventoryDetail updateInventoryDetail = new InventoryDetail();
                updateInventoryDetail.setId(inventoryDetail.getId());
                updateInventoryDetailList.add(updateInventoryDetail);
            }
            if (!inventoryDetailService.updateBatchById(updateInventoryDetailList)) {
                throw new ServiceException("更新库存明细表状态失败");
            }
        }
        return true;
    }
    public boolean updateQtyById(BigDecimal qty, int id) {
        ShipmentContainerAdvice shipmentContainerAdvice = new ShipmentContainerAdvice();
        shipmentContainerAdvice.setQty(qty);
        shipmentContainerAdvice.setId(id);
        boolean success = updateById(shipmentContainerAdvice);
        return success;
    }

    @Transactional(rollbackFor = Exception.class)
    public ShipmentContainerAdvice addShipmentContainerAdvice(ShipmentContainerHeader shipmentContainerHeader, ShipmentCombinationModel combinationModel) {
        boolean success = false;
        BigDecimal shipmentQty = combinationModel.getShipQty();
        Integer shipmentDetailId = combinationModel.getShipmentDetailId();
        Integer inventoryDetailId = combinationModel.getInventoryDetailId();
        ShipmentDetail shipmentDetail = shipmentDetailService.getById(shipmentDetailId);
        InventoryDetail inventoryDetail = inventoryDetailService.getById(inventoryDetailId);
        ShipmentContainerAdvice shipmentContainerAdvice=new ShipmentContainerAdvice();
        String warehouseCode = inventoryDetail.getWarehouseCode();
        String materialCode = inventoryDetail.getMaterialCode();
        Material material = materialService.getMaterialByCode(materialCode);
        if (material == null) {
            throw new ServiceException("出库单(" + shipmentDetail.getShipmentCode() + ")的物料(" + materialCode + ")不存在!");
        }
        shipmentContainerAdvice = new ShipmentContainerAdvice();
        shipmentContainerAdvice.setWarehouseCode(shipmentContainerHeader.getWarehouseCode());
        shipmentContainerAdvice.setCompanyCode(shipmentDetail.getCompanyCode());
        shipmentContainerAdvice.setContainerCode(shipmentContainerHeader.getContainerCode());
//            shipmentContainerAdvice.setInventoryDetailId(inventoryDetail.getId());
        shipmentContainerAdvice.setShipmentCode(shipmentDetail.getShipmentCode());
        shipmentContainerAdvice.setShipmentId(shipmentDetail.getShipmentId());
        shipmentContainerAdvice.setShipmentDetailId(shipmentDetail.getId());
        shipmentContainerAdvice.setShipmentContainerId(shipmentContainerHeader.getId());
        shipmentContainerAdvice.setMaterialCode(shipmentDetail.getMaterialCode());
        shipmentContainerAdvice.setMaterialName(shipmentDetail.getMaterialName());
        shipmentContainerAdvice.setMaterialSpec(shipmentDetail.getMaterialSpec());
        shipmentContainerAdvice.setMaterialUnit(shipmentDetail.getMaterialUnit());
        shipmentContainerAdvice.setQty(shipmentQty);
        shipmentContainerAdvice.setInventoryDetailId(inventoryDetailId);
        shipmentContainerAdvice.setBatch(shipmentDetail.getBatch());
        shipmentContainerAdvice.setStatus(QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        shipmentContainerAdvice.setCreatedBy(shipmentDetail.getCreatedBy());
        shipmentContainerAdvice.setCreated(new Date());
        success = this.save(shipmentContainerAdvice);
        if (!success) {
            throw new ServiceException("新建预配盘明细失败,sql错误");
        }
        return shipmentContainerAdvice;
    }

    /**
     * 取消预配盘
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelCombinationAdvice(Integer shipmentContainerHeaderId,ShipmentContainerHeader shipmentContainerHeader) {

        List<ShipmentContainerAdvice> list=this.getShipmentContainerAdviceListHeaderId(shipmentContainerHeaderId);
        for (ShipmentContainerAdvice shipmentContainerAdvice : list) {
            //获取头
            if(shipmentContainerAdvice.getStatus()>0){
                throw new ServiceException("容器" + shipmentContainerHeader.getContainerCode() + "已出库扣减库存,不允许取消明细");
            }
            if (shipmentContainerHeader.getStatus() >= QuantityConstant.SHIPMENT_CONTAINER_TASK) {
//                throw new ServiceException("容器" + shipmentContainerHeader.getContainerCode() + "非新建状态,不允许取消明细");
            }
            //恢复占用库存
            InventoryDetail inventoryDetail = inventoryDetailService.getById(shipmentContainerAdvice.getInventoryDetailId());
            if (inventoryDetail != null) {
                InventoryDetail inventoryDetail1 = new InventoryDetail();
                inventoryDetail1.setId(inventoryDetail.getId());
                inventoryDetail1.setTaskQty(inventoryDetail.getTaskQty().subtract(shipmentContainerAdvice.getQty()));
                inventoryDetailService.updateById(inventoryDetail1);
                logger.info("扣减库存::" + inventoryDetail.getId() + "容器:" + inventoryDetail.getContainerCode() + "物料code:" + inventoryDetail.getMaterialCode() + "数量:" + inventoryDetail.getTaskQty() + "组盘数量:" + shipmentContainerAdvice.getQty());
            }
            //恢复单据发货数量
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(shipmentContainerAdvice.getShipmentDetailId());
            shipmentDetail.setTaskQty(shipmentDetail.getTaskQty().subtract(shipmentContainerAdvice.getQty()));
            if (shipmentDetail.getTaskQty().compareTo(BigDecimal.ZERO) != 0) {
                shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_WAVE);//明细状态恢复,如果删除后还有以出数量就是波次
            } else {
                shipmentDetail.setStatus(QuantityConstant.SHIPMENT_HEADER_POOL);//明细状态
            }
            shipmentDetailService.saveOrUpdate(shipmentDetail);
            //删除这个配盘明细
            this.removeById(shipmentContainerAdvice.getId());

            //查询头表下还有没有明细,如果没有,则删了这个头表
            ShipmentContainerDetail condition = new ShipmentContainerDetail();
            condition.setShippingContainerId(shipmentContainerAdvice.getShipmentContainerId());
            List<ShipmentContainerAdvice> list1= this.getShipmentContainerAdviceListHeaderId(shipmentContainerHeaderId);
            if (list1 == null || list1.size() == 0) {
                shipmentContainerHeaderService.removeById(shipmentContainerHeader.getId());
                locationService.updateStatus(shipmentContainerHeader.getLocationCode(),
                        QuantityConstant.STATUS_LOCATION_EMPTY);
            }
            //更新单据状态
            shipmentHeaderService.updateShipmentStatus(shipmentDetail.getShipmentId());
        }

        return AjaxResult.success("");
    }

}