cuijian
2025-06-18 a65937bb7d85168af8256f917aad1aa12c0b8c1d
lxzn-module-tms/src/main/java/org/jeecg/modules/tms/service/impl/OutboundOrderServiceImpl.java
@@ -8,6 +8,7 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
@@ -42,6 +43,7 @@
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
@@ -83,6 +85,8 @@
   private OutboundOrderMapper outboundOrderMapper;
   @Autowired
   private OutboundDetailMapper outboundDetailMapper;
   @Autowired
   private OutboundOrderConvert outboundOrderConvert;
   
   @Override
   @Transactional(rollbackFor = Exception.class)
@@ -103,7 +107,7 @@
    @Override
   @Transactional(rollbackFor = Exception.class)
    public void addTotal(OutboundOrderAndDetailDto outboundOrder) {
      OutboundOrder order = OutboundOrderConvert.INSTANCE.convert(outboundOrder);
      OutboundOrder order = outboundOrderConvert.convert(outboundOrder);
      order.setHandler(Objects.requireNonNull(getCurrentUser()).getId());
      order.setOutNum(businessCodeRuleService.generateBusinessCodeSeq("outBoundOrder"));
      order.setOrderStatus(OutBillStatus.DRAFT.getValue());
@@ -155,7 +159,7 @@
      //删除所有明细
      outboundDetailService.remove(new LambdaQueryWrapper<OutboundDetail>()
            .eq(OutboundDetail::getOutStorehouseId, outboundOrder.getId()));
      OutboundOrder outboundOrderUpdate = OutboundOrderConvert.INSTANCE.convert(outboundOrder);
      OutboundOrder outboundOrderUpdate = outboundOrderConvert.convert(outboundOrder);
      outboundOrderMapper.updateById(outboundOrderUpdate);
      List<OutboundDetail> detailList = CollectionUtil.newArrayList();
      outboundOrder.getOutboundDetailList().forEach(item->{
@@ -383,6 +387,10 @@
      Map<String, BigDecimal> outBoundRequestMap = outBoundRequestList.stream()
            .collect(Collectors.toMap(OutBoundRequestDto::getOutboundDetailId, OutBoundRequestDto::getOutboundQuantity));
      Map<String, BigDecimal> ratedLifeMap = outBoundRequestList.stream()
            .collect(Collectors.toMap(OutBoundRequestDto::getOutboundDetailId, dto->dto.getRatedLife() != null ? dto.getRatedLife() : BigDecimal.ZERO));
      Map<String, BigDecimal> useLifeMap = outBoundRequestList.stream()
            .collect(Collectors.toMap(OutBoundRequestDto::getOutboundDetailId, dto->dto.getUseLife() != null ? dto.getUseLife() : BigDecimal.ZERO));
      //处理库存台账及台账明细
      Map<String, ToolLedger> toolLedgerMap = toolLedgerService.list(new LambdaQueryWrapper<ToolLedger>().in(ToolLedger::getToolId, toolIdList)).stream()
            .collect(Collectors.toMap(ToolLedger::getToolId, item -> item, (k1, k2) -> k1));
@@ -418,6 +426,14 @@
         //更新库存台账
         ToolLedger toolLedger = toolLedgerMap.get(detail.getToolCode());
         BigDecimal outboundQuantity = outBoundRequestMap.get(detail.getId());
         BigDecimal ratedLife = ratedLifeMap.get(detail.getId());
         BigDecimal useLife = useLifeMap.get(detail.getId());
         //唯一编码不为空时(管到把),计算剩余寿命
         BigDecimal remandLife = null;
         if(StringUtils.isNotBlank(detail.getToolId())){
            ToolLedgerDetail toolLedgerDetail = toolLedgerDetailService.lambdaQuery().eq(ToolLedgerDetail::getToolId,detail.getToolId()).one();
            remandLife = (ratedLife.multiply(toolLedgerDetail.getRemainingPercentage()).subtract(useLife).max(BigDecimal.ZERO)).divide(ratedLife, 4, RoundingMode.HALF_UP);
         }
         OutStorehouseType outStorehouseType = OutStorehouseType.getByValue(outboundOrder.getOutStorehouseType());
         switch (outStorehouseType) {
            case TOOL_BORROW:
@@ -438,20 +454,33 @@
         toolLedgerUpdateList.add(toolLedger);
         //更新台账明细
         BigDecimal finalRemandLife = remandLife;
         toolLedgerDetailList.stream().filter(item -> item.getToolCode().equals(detail.getToolCode()) && item.getToolId().equals(detail.getToolId()))
               .findFirst().ifPresent(item -> {
                  switch (outStorehouseType) {
                     case TOOL_BORROW:
                        item.setStatus(ToolCirculationStatus.BORROWED.getValue());
                        item.setRatedLife(ratedLife);
                        item.setUseLife(useLife);
                        item.setRemainingPercentage(finalRemandLife);
                        break;
                     case MAINTENANCE_OUTBOUND:
                        item.setStatus(ToolCirculationStatus.REPAIRING.getValue());
                        item.setRatedLife(ratedLife);
                        item.setUseLife(useLife);
                        item.setRemainingPercentage(finalRemandLife);
                        break;
                     case CALIBRATION_OUTBOUND:
                        item.setStatus(ToolCirculationStatus.INSPECTING.getValue());
                        item.setRatedLife(ratedLife);
                        item.setUseLife(useLife);
                        item.setRemainingPercentage(finalRemandLife);
                        break;
                     case GRINDING_OUTBOUND:
                        item.setStatus(ToolCirculationStatus.GRINDING.getValue());
                        item.setRatedLife(ratedLife);
                        item.setUseLife(useLife);
                        item.setRemainingPercentage(finalRemandLife);
                        break;
                     default:
                        throw new JeecgBootException("未知的出库类型!");
@@ -476,6 +505,9 @@
               .setGoodsShelvesCode(detail.getOutboundLocation())
               .setOutNumber(outboundQuantity)
               .setOutStorehouseId(outboundOrder.getId())
               .setRatedLife(ratedLife)
               .setUseLife(useLife)
               .setRemainingPercentage(remandLife)
               .setOperateType("1");//1:按申请单
         outStoreAddList.add(outStoreDetail);
@@ -562,6 +594,8 @@
      for (String toolLedgerDetailId : boundAddDtoMap.keySet()) {
         OutBoundAddDto outBoundAddDto = boundAddDtoMap.get(toolLedgerDetailId);
         BigDecimal outboundQuantity = outBoundAddDto.getOutboundQuantity();
         BigDecimal ratedLife = outBoundAddDto.getRatedLife();
         BigDecimal useLife = outBoundAddDto.getUseLife();
         ToolLedgerDetail toolLedgerDetail = toolLedgerDetailMap.get(toolLedgerDetailId);
         if (Objects.isNull(toolLedgerDetail) || toolLedgerDetail.getQuantity().compareTo(outboundQuantity) < 0) {
            BaseTools tools = baseToolsService.getById(outBoundAddDto.getToolCode());
@@ -573,20 +607,34 @@
               toolLedgerDetail.setQuantity(toolLedgerDetail.getQuantity().subtract(outboundQuantity));
            } else {
               //有唯一编码,管道把的
               //计算剩余寿命
               BigDecimal remandLife = (ratedLife.multiply(toolLedgerDetail.getRemainingPercentage()).subtract(useLife).max(BigDecimal.ZERO)).divide(ratedLife, 4, RoundingMode.HALF_UP);
               toolLedgerDetail.setQuantity(BigDecimal.ZERO);//数量只会是0和1
               OutStorehouseType outStorehouseType = OutStorehouseType.getByValue(boundAddList.get(0).getOutStorehouseType());
               switch (outStorehouseType) {
                  case TOOL_BORROW:
                     toolLedgerDetail.setStatus(ToolCirculationStatus.BORROWED.getValue());
                     toolLedgerDetail.setRatedLife(ratedLife);
                     toolLedgerDetail.setUseLife(useLife);
                     toolLedgerDetail.setRemainingPercentage(remandLife);
                     break;
                  case MAINTENANCE_OUTBOUND:
                     toolLedgerDetail.setStatus(ToolCirculationStatus.REPAIRING.getValue());
                     toolLedgerDetail.setRatedLife(ratedLife);
                     toolLedgerDetail.setUseLife(useLife);
                     toolLedgerDetail.setRemainingPercentage(remandLife);
                     break;
                  case CALIBRATION_OUTBOUND:
                     toolLedgerDetail.setStatus(ToolCirculationStatus.INSPECTING.getValue());
                     toolLedgerDetail.setRatedLife(ratedLife);
                     toolLedgerDetail.setUseLife(useLife);
                     toolLedgerDetail.setRemainingPercentage(remandLife);
                     break;
                  case GRINDING_OUTBOUND:
                     toolLedgerDetail.setStatus(ToolCirculationStatus.GRINDING.getValue());
                     toolLedgerDetail.setRatedLife(ratedLife);
                     toolLedgerDetail.setUseLife(useLife);
                     toolLedgerDetail.setRemainingPercentage(remandLife);
                     //同时更新刃磨单状态
                     toolSharpeningList.stream().filter(item -> item.getToolCode().equals(outBoundAddDto.getToolCode())
                           && item.getToolId().equals(outBoundAddDto.getToolId())).findFirst().ifPresent(item -> {
@@ -607,12 +655,17 @@
      //记录出库流水
      List<OutStoreDetail> outStoreAddList = CollectionUtil.newArrayList();
      for (OutBoundAddDto outBoundAddDto : boundAddList) {
         //获取剩余寿命
         ToolLedgerDetail toolLedgerDetail = toolLedgerDetailService.getById(outBoundAddDto.getToolLedgerDetailId());
         OutStoreDetail outStoreDetail = new OutStoreDetail()
               .setToolCode(outBoundAddDto.getToolCode())
               .setToolId(outBoundAddDto.getToolId())
               .setOutStorehouseType(outBoundAddDto.getOutStorehouseType())
               .setGoodsShelvesCode(outBoundAddDto.getOutboundLocation())
               .setOutNumber(outBoundAddDto.getOutboundQuantity())
               .setRatedLife(outBoundAddDto.getRatedLife())
               .setUseLife(outBoundAddDto.getUseLife())
               .setRemainingPercentage(toolLedgerDetail.getRemainingPercentage())
               .setOperateType("2");//2:手工操作
         outStoreAddList.add(outStoreDetail);
      }