From 2f80b7085c4eabce06d3491306b75eecc275275f Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期四, 30 四月 2026 17:31:57 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New_pro' into dev_New_pro

---
 src/main/java/com/ruoyi/production/service/impl/ProductionOrderPickServiceImpl.java |  348 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 305 insertions(+), 43 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderPickServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderPickServiceImpl.java
index 8f2f728..72a237e 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductionOrderPickServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductionOrderPickServiceImpl.java
@@ -3,6 +3,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.production.bean.dto.ProductionOrderPickDto;
@@ -18,6 +19,7 @@
 import com.ruoyi.stock.dto.StockInventoryDto;
 import com.ruoyi.stock.mapper.StockInventoryMapper;
 import com.ruoyi.stock.pojo.StockInventory;
+import com.ruoyi.stock.service.StockInventoryService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -39,10 +41,14 @@
 @RequiredArgsConstructor
 public class ProductionOrderPickServiceImpl extends ServiceImpl<ProductionOrderPickMapper, ProductionOrderPick> implements ProductionOrderPickService {
 
+    private static final byte PICK_TYPE_NORMAL = 1;
+    private static final byte PICK_TYPE_FEEDING = 2;
+
     private final ProductionOrderMapper productionOrderMapper;
     private final ProductionOperationTaskMapper productionOperationTaskMapper;
     private final ProductionOrderPickRecordMapper productionOrderPickRecordMapper;
     private final StockInventoryMapper stockInventoryMapper;
+    private final StockInventoryService stockInventoryService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -56,7 +62,7 @@
             List<String> batchNoList = resolveBatchNoList(resolvedDto);
             String inventoryBatchNo = pickInventoryBatchNo(batchNoList);
             String storedBatchNo = formatBatchNoStorage(batchNoList);
-            subtractInventory(resolvedDto.getProductModelId(), inventoryBatchNo, resolvedDto.getPickQuantity(), rowNo);
+            subtractInventory(resolvedDto.getProductModelId(), storedBatchNo, resolvedDto.getPickQuantity(), rowNo);
 
             ProductionOrderPick orderPick = new ProductionOrderPick();
             orderPick.setProductionOrderId(resolvedDto.getProductionOrderId());
@@ -68,6 +74,7 @@
             orderPick.setTechnologyOperationId(resolvedDto.getTechnologyOperationId());
             orderPick.setDemandedQuantity(resolvedDto.getDemandedQuantity());
             orderPick.setBom(resolvedDto.getBom());
+            orderPick.setReturned(false);
             baseMapper.insert(orderPick);
 
             insertPickRecord(orderPick.getId(),
@@ -79,7 +86,8 @@
                     BigDecimal.ZERO,
                     resolvedDto.getPickQuantity(),
                     resolvedDto.getPickType(),
-                    resolvedDto.getRemark());
+                    resolvedDto.getRemark(),
+                    resolvedDto.getFeedingReason());
         }
         return true;
     }
@@ -105,6 +113,15 @@
         Map<Long, ProductionOrderPick> existingPickMap = existingPickList.stream()
                 .filter(item -> item.getId() != null)
                 .collect(Collectors.toMap(ProductionOrderPick::getId, Function.identity(), (a, b) -> a));
+
+        if (isFeedingRequest(dto)) {
+            processFeedingPickItems(dto, existingPickMap, productionOrderId);
+            return true;
+        }
+        if (isReturnRequest(dto)) {
+            processReturnPickItems(dto, existingPickMap, productionOrderId);
+            return true;
+        }
 
         processDeletePickIds(dto, existingPickMap, productionOrderId);
 
@@ -174,7 +191,8 @@
                     oldQuantity,
                     BigDecimal.ZERO,
                     rootDto.getPickType(),
-                    rootDto.getRemark());
+                    rootDto.getRemark(),
+                    rootDto.getFeedingReason());
             existingPickMap.remove(deleteId);
         }
     }
@@ -191,7 +209,7 @@
                 .filter(item -> item.getId() != null)
                 .filter(item -> Objects.equals(item.getProductionOrderId(), productionOrderId))
                 .filter(item -> !keepPickIdSet.contains(item.getId()))
-                .collect(Collectors.toList());
+                .toList();
         for (ProductionOrderPick missingPick : missingPickList) {
             String oldBatchNo = resolveInventoryBatchNoFromStored(missingPick.getBatchNo());
             BigDecimal oldQuantity = defaultDecimal(missingPick.getQuantity());
@@ -209,7 +227,8 @@
                     oldQuantity,
                     BigDecimal.ZERO,
                     rootDto.getPickType(),
-                    rootDto.getRemark());
+                    rootDto.getRemark(),
+                    rootDto.getFeedingReason());
             existingPickMap.remove(missingPick.getId());
         }
     }
@@ -218,7 +237,7 @@
         List<String> batchNoList = resolveBatchNoList(dto);
         String inventoryBatchNo = pickInventoryBatchNo(batchNoList);
         String storedBatchNo = formatBatchNoStorage(batchNoList);
-        subtractInventory(dto.getProductModelId(), inventoryBatchNo, dto.getPickQuantity(), rowNo);
+        subtractInventory(dto.getProductModelId(), storedBatchNo, dto.getPickQuantity(), rowNo);
 
         ProductionOrderPick orderPick = new ProductionOrderPick();
         orderPick.setProductionOrderId(dto.getProductionOrderId());
@@ -230,6 +249,7 @@
         orderPick.setTechnologyOperationId(dto.getTechnologyOperationId());
         orderPick.setDemandedQuantity(dto.getDemandedQuantity());
         orderPick.setBom(dto.getBom());
+        orderPick.setReturned(false);
         baseMapper.insert(orderPick);
 
         insertPickRecord(orderPick.getId(),
@@ -241,7 +261,111 @@
                 BigDecimal.ZERO,
                 dto.getPickQuantity(),
                 dto.getPickType(),
-                dto.getRemark());
+                dto.getRemark(),
+                dto.getFeedingReason());
+    }
+
+    private void processFeedingPickItems(ProductionOrderPickDto rootDto,
+                                         Map<Long, ProductionOrderPick> existingPickMap,
+                                         Long productionOrderId) {
+        List<ProductionOrderPickDto> pickItems = resolveUpdateItems(rootDto);
+        for (int i = 0; i < pickItems.size(); i++) {
+            int rowNo = i + 1;
+            ProductionOrderPickDto resolvedDto = mergeDto(rootDto, pickItems.get(i));
+            if (isEmptyUpdateItem(resolvedDto)) {
+                continue;
+            }
+            if (!isFeedingPick(resolvedDto)) {
+                throw new ServiceException("琛ユ枡璇锋眰涓殑棰嗘枡绫诲瀷蹇呴』鍏ㄩ儴涓�2");
+            }
+            if (resolvedDto.getProductionOrderId() == null) {
+                resolvedDto.setProductionOrderId(productionOrderId);
+            }
+            validateFeedingParam(resolvedDto, rowNo);
+
+            ProductionOrderPick oldPick = existingPickMap.get(resolvedDto.getId());
+            if (oldPick == null || !Objects.equals(oldPick.getProductionOrderId(), productionOrderId)) {
+                throw new ServiceException("绗�" + rowNo + "鏉¢鏂欒褰曚笉瀛樺湪鎴栦笉灞炰簬褰撳墠璁㈠崟");
+            }
+            addFeedingPick(resolvedDto, oldPick, rowNo);
+        }
+    }
+
+    private void addFeedingPick(ProductionOrderPickDto dto, ProductionOrderPick oldPick, int rowNo) {
+        if (dto.getProductModelId() != null && !Objects.equals(dto.getProductModelId(), oldPick.getProductModelId())) {
+            throw new ServiceException("绗�" + rowNo + "鏉¤ˉ鏂欎骇鍝佽鏍间笌棰嗘枡璁板綍涓嶄竴鑷�");
+        }
+        Long productModelId = oldPick.getProductModelId();
+        List<String> batchNoList = resolveBatchNoList(dto);
+        String inventoryBatchNo = batchNoList.isEmpty()
+                ? resolveInventoryBatchNoFromStored(oldPick.getBatchNo())
+                : formatBatchNoStorage(batchNoList);
+        BigDecimal feedingQuantity = dto.getFeedingQuantity();
+
+        subtractInventory(productModelId, inventoryBatchNo, feedingQuantity, rowNo);
+
+        BigDecimal beforeFeedingQty = sumFeedingQuantity(dto.getProductionOrderId(), oldPick.getId());
+        BigDecimal afterFeedingQty = beforeFeedingQty.add(feedingQuantity);
+        insertPickRecord(oldPick.getId(),
+                dto.getProductionOrderId(),
+                dto.getProductionOperationTaskId(),
+                productModelId,
+                inventoryBatchNo,
+                feedingQuantity,
+                beforeFeedingQty,
+                afterFeedingQty,
+                PICK_TYPE_FEEDING,
+                dto.getRemark(),
+                dto.getFeedingReason());
+
+        ProductionOrderPick updatePick = new ProductionOrderPick();
+        updatePick.setId(oldPick.getId());
+        updatePick.setFeedingQty(afterFeedingQty);
+        updatePick.setActualQty(calculateActualQty(oldPick, afterFeedingQty));
+        int affected = baseMapper.updateById(updatePick);
+        if (affected <= 0) {
+            throw new ServiceException("绗�" + rowNo + "鏉¤ˉ鏂欐�婚噺鏇存柊澶辫触");
+        }
+        oldPick.setFeedingQty(afterFeedingQty);
+        oldPick.setActualQty(updatePick.getActualQty());
+    }
+
+    private void processReturnPickItems(ProductionOrderPickDto rootDto,
+                                        Map<Long, ProductionOrderPick> existingPickMap,
+                                        Long productionOrderId) {
+        List<ProductionOrderPickDto> pickItems = resolveUpdateItems(rootDto);
+        for (int i = 0; i < pickItems.size(); i++) {
+            int rowNo = i + 1;
+            ProductionOrderPickDto resolvedDto = mergeDto(rootDto, pickItems.get(i));
+            if (isEmptyUpdateItem(resolvedDto)) {
+                continue;
+            }
+            if (resolvedDto.getProductionOrderId() == null) {
+                resolvedDto.setProductionOrderId(productionOrderId);
+            }
+            validateReturnParam(resolvedDto, rowNo);
+
+            ProductionOrderPick oldPick = existingPickMap.get(resolvedDto.getId());
+            if (oldPick == null || !Objects.equals(oldPick.getProductionOrderId(), productionOrderId)) {
+                throw new ServiceException("绗�" + rowNo + "鏉¢鏂欒褰曚笉瀛樺湪鎴栦笉灞炰簬褰撳墠璁㈠崟");
+            }
+            updateReturnPick(resolvedDto, oldPick, rowNo);
+        }
+    }
+
+    private void updateReturnPick(ProductionOrderPickDto dto, ProductionOrderPick oldPick, int rowNo) {
+        ProductionOrderPick updatePick = new ProductionOrderPick();
+        updatePick.setId(oldPick.getId());
+        updatePick.setReturnQty(dto.getReturnQty());
+        updatePick.setActualQty(dto.getActualQty());
+        updatePick.setReturned(true);
+        int affected = baseMapper.updateById(updatePick);
+        if (affected <= 0) {
+            throw new ServiceException("绗�" + rowNo + "鏉¢��鏂欎俊鎭洿鏂板け璐�");
+        }
+        oldPick.setReturnQty(updatePick.getReturnQty());
+        oldPick.setActualQty(updatePick.getActualQty());
+        oldPick.setReturned(true);
     }
 
     private void updateExistingPick(ProductionOrderPickDto dto,
@@ -267,13 +391,13 @@
         if (sameStockKey) {
             BigDecimal delta = newQuantity.subtract(oldQuantity);
             if (delta.compareTo(BigDecimal.ZERO) > 0) {
-                subtractInventory(newProductModelId, newBatchNo, delta, rowNo);
+                subtractInventory(newProductModelId, newStoredBatchNo, delta, rowNo);
             } else if (delta.compareTo(BigDecimal.ZERO) < 0) {
                 addInventory(oldProductModelId, oldBatchNo, delta.abs());
             }
         } else {
             addInventory(oldProductModelId, oldBatchNo, oldQuantity);
-            subtractInventory(newProductModelId, newBatchNo, newQuantity, rowNo);
+            subtractInventory(newProductModelId, newStoredBatchNo, newQuantity, rowNo);
         }
 
         oldPick.setProductModelId(newProductModelId);
@@ -304,7 +428,8 @@
                     oldQuantity,
                     newQuantity,
                     dto.getPickType(),
-                    dto.getRemark());
+                    dto.getRemark(),
+                    dto.getFeedingReason());
         }
     }
 
@@ -317,7 +442,8 @@
                                   BigDecimal beforeQuantity,
                                   BigDecimal afterQuantity,
                                   Byte pickType,
-                                  String remark) {
+                                  String remark,
+                                  String feedingReason) {
         ProductionOrderPickRecord pickRecord = new ProductionOrderPickRecord();
         pickRecord.setPickId(pickId);
         pickRecord.setProductionOrderId(productionOrderId);
@@ -327,28 +453,68 @@
         pickRecord.setPickQuantity(defaultDecimal(pickQuantity));
         pickRecord.setBeforeQuantity(defaultDecimal(beforeQuantity));
         pickRecord.setAfterQuantity(defaultDecimal(afterQuantity));
-        pickRecord.setPickType(pickType == null ? (byte) 1 : pickType);
+        pickRecord.setPickType(pickType == null ? PICK_TYPE_NORMAL : pickType);
         pickRecord.setRemark(remark);
+        pickRecord.setFeedingReason(feedingReason);
         productionOrderPickRecordMapper.insert(pickRecord);
     }
 
     private void subtractInventory(Long productModelId, String batchNo, BigDecimal quantity, int rowNo) {
-        StockInventory stockInventory = stockInventoryMapper.selectOne(buildStockWrapper(productModelId, batchNo));
-        if (stockInventory == null) {
-            throw new ServiceException("绗�" + rowNo + "鏉¢鏂欏搴斿簱瀛樹笉瀛樺湪");
+        BigDecimal deductQuantity = defaultDecimal(quantity);
+        if (deductQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+            return;
         }
-        BigDecimal availableQuantity = defaultDecimal(stockInventory.getQualitity())
-                .subtract(defaultDecimal(stockInventory.getLockedQuantity()));
-        if (quantity.compareTo(availableQuantity) > 0) {
-            throw new ServiceException("绗�" + rowNo + "鏉¢鏂欏彲鐢ㄥ簱瀛樹笉瓒�");
+
+        List<String> batchNoList = parseBatchNoValue(batchNo);
+        if (batchNoList.isEmpty()) {
+            batchNoList = Collections.singletonList(null);
         }
-        StockInventoryDto stockInventoryDto = new StockInventoryDto();
-        stockInventoryDto.setProductModelId(productModelId);
-        stockInventoryDto.setBatchNo(batchNo);
-        stockInventoryDto.setQualitity(quantity);
-        int affected = stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
-        if (affected <= 0) {
-            throw new ServiceException("绗�" + rowNo + "鏉¢鏂欐墸鍑忓簱瀛樺け璐�");
+
+        Map<String, BigDecimal> availableQuantityMap = new LinkedHashMap<>();
+        BigDecimal totalAvailableQuantity = BigDecimal.ZERO;
+        for (String currentBatchNo : batchNoList) {
+            StockInventory stockInventory = stockInventoryMapper.selectOne(buildStockWrapper(productModelId, currentBatchNo));
+            BigDecimal availableQuantity = BigDecimal.ZERO;
+            if (stockInventory != null) {
+                availableQuantity = defaultDecimal(stockInventory.getQualitity())
+                        .subtract(defaultDecimal(stockInventory.getLockedQuantity()));
+                if (availableQuantity.compareTo(BigDecimal.ZERO) < 0) {
+                    availableQuantity = BigDecimal.ZERO;
+                }
+            }
+            availableQuantityMap.put(currentBatchNo, availableQuantity);
+            totalAvailableQuantity = totalAvailableQuantity.add(availableQuantity);
+        }
+
+        if (deductQuantity.compareTo(totalAvailableQuantity) > 0) {
+            BigDecimal shortQuantity = deductQuantity.subtract(totalAvailableQuantity);
+            throw new ServiceException("棰嗘枡鍙敤搴撳瓨涓嶈冻锛屽彲鐢ㄥ簱瀛樹负" + formatQuantity(totalAvailableQuantity)
+                    + "锛岃繕宸�" + formatQuantity(shortQuantity));
+        }
+
+        BigDecimal remainingQuantity = deductQuantity;
+        for (Map.Entry<String, BigDecimal> entry : availableQuantityMap.entrySet()) {
+            if (remainingQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+                break;
+            }
+            BigDecimal availableQuantity = defaultDecimal(entry.getValue());
+            if (availableQuantity.compareTo(BigDecimal.ZERO) <= 0) {
+                continue;
+            }
+            BigDecimal currentDeductQuantity = remainingQuantity.min(availableQuantity);
+            StockInventoryDto stockInventoryDto = new StockInventoryDto();
+            stockInventoryDto.setProductModelId(productModelId);
+            stockInventoryDto.setBatchNo(entry.getKey());
+            stockInventoryDto.setQualitity(currentDeductQuantity);
+            int affected = stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
+            if (affected <= 0) {
+                throw new ServiceException("绗�" + rowNo + "鏉¢鏂欐墸鍑忓簱瀛樺け璐�");
+            }
+            remainingQuantity = remainingQuantity.subtract(currentDeductQuantity);
+        }
+
+        if (remainingQuantity.compareTo(BigDecimal.ZERO) > 0) {
+            throw new ServiceException("绗�" + rowNo + "鏉¢鏂欐墸鍑忓簱瀛樺け璐ワ紝鍓╀綑寰呮墸鍑忔暟閲忎负" + formatQuantity(remainingQuantity));
         }
     }
 
@@ -357,25 +523,13 @@
         if (addQuantity.compareTo(BigDecimal.ZERO) <= 0) {
             return;
         }
-        StockInventory stockInventory = stockInventoryMapper.selectOne(buildStockWrapper(productModelId, batchNo));
-        if (stockInventory == null) {
-            StockInventory newStockInventory = new StockInventory();
-            newStockInventory.setProductModelId(productModelId);
-            newStockInventory.setBatchNo(batchNo);
-            newStockInventory.setQualitity(addQuantity);
-            newStockInventory.setLockedQuantity(BigDecimal.ZERO);
-            newStockInventory.setVersion(1);
-            stockInventoryMapper.insert(newStockInventory);
-            return;
-        }
         StockInventoryDto stockInventoryDto = new StockInventoryDto();
         stockInventoryDto.setProductModelId(productModelId);
         stockInventoryDto.setBatchNo(batchNo);
         stockInventoryDto.setQualitity(addQuantity);
-        int affected = stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
-        if (affected <= 0) {
-            throw new ServiceException("搴撳瓨鍥為��澶辫触锛屼骇鍝佽鏍糏D=" + productModelId);
-        }
+        stockInventoryDto.setRecordType(String.valueOf(StockInQualifiedRecordTypeEnum.PICK_RETURN_IN.getCode()));
+        stockInventoryDto.setRecordId(0L);
+        stockInventoryService.addStockInRecordOnly(stockInventoryDto);
     }
 
     private List<ProductionOrderPickDto> resolvePickItems(ProductionOrderPickDto dto) {
@@ -405,6 +559,11 @@
                 && StringUtils.isEmpty(dto.getBatchNo())
                 && (dto.getBatchNoList() == null || dto.getBatchNoList().isEmpty())
                 && dto.getPickType() == null
+                && dto.getFeedingQuantity() == null
+                && StringUtils.isEmpty(dto.getFeedingReason())
+                && dto.getReturnQty() == null
+                && dto.getActualQty() == null
+                && dto.getReturned() == null
                 && dto.getProductionOperationTaskId() == null
                 && dto.getTechnologyOperationId() == null
                 && StringUtils.isEmpty(dto.getOperationName())
@@ -440,10 +599,15 @@
             merged.setPickQuantity(itemDto.getPickQuantity());
             merged.setPickType(itemDto.getPickType());
             merged.setRemark(itemDto.getRemark());
+            merged.setFeedingReason(itemDto.getFeedingReason());
+            merged.setFeedingQuantity(itemDto.getFeedingQuantity());
             merged.setTechnologyOperationId(itemDto.getTechnologyOperationId());
             merged.setOperationName(itemDto.getOperationName());
             merged.setDemandedQuantity(itemDto.getDemandedQuantity());
             merged.setBom(itemDto.getBom());
+            merged.setReturnQty(itemDto.getReturnQty());
+            merged.setActualQty(itemDto.getActualQty());
+            merged.setReturned(itemDto.getReturned());
         }
         if (merged.getId() == null) {
             merged.setId(rootDto.getId());
@@ -472,6 +636,12 @@
         if (merged.getRemark() == null) {
             merged.setRemark(rootDto.getRemark());
         }
+        if (merged.getFeedingReason() == null) {
+            merged.setFeedingReason(rootDto.getFeedingReason());
+        }
+        if (merged.getFeedingQuantity() == null) {
+            merged.setFeedingQuantity(rootDto.getFeedingQuantity());
+        }
         if (merged.getTechnologyOperationId() == null) {
             merged.setTechnologyOperationId(rootDto.getTechnologyOperationId());
         }
@@ -483,6 +653,15 @@
         }
         if (merged.getBom() == null) {
             merged.setBom(rootDto.getBom());
+        }
+        if (merged.getReturnQty() == null) {
+            merged.setReturnQty(rootDto.getReturnQty());
+        }
+        if (merged.getActualQty() == null) {
+            merged.setActualQty(rootDto.getActualQty());
+        }
+        if (merged.getReturned() == null) {
+            merged.setReturned(rootDto.getReturned());
         }
         return merged;
     }
@@ -497,9 +676,89 @@
         if (dto.getPickQuantity() == null || dto.getPickQuantity().compareTo(BigDecimal.ZERO) <= 0) {
             throw new ServiceException("绗�" + rowNo + "鏉¢鏂欐暟閲忓繀椤诲ぇ浜�0");
         }
-        if (dto.getPickType() != null && dto.getPickType() != 1 && dto.getPickType() != 2) {
+        if (dto.getPickType() != null && dto.getPickType() != PICK_TYPE_NORMAL && dto.getPickType() != PICK_TYPE_FEEDING) {
             throw new ServiceException("绗�" + rowNo + "鏉¢鏂欑被鍨嬪彧鑳芥槸1鎴�2");
         }
+    }
+
+    private void validateFeedingParam(ProductionOrderPickDto dto, int rowNo) {
+        if (dto.getProductionOrderId() == null) {
+            throw new ServiceException("绗�" + rowNo + "鏉$敓浜ц鍗旾D涓嶈兘涓虹┖");
+        }
+        if (dto.getId() == null) {
+            throw new ServiceException("绗�" + rowNo + "鏉¢鏂橧D涓嶈兘涓虹┖");
+        }
+        if (dto.getFeedingQuantity() == null || dto.getFeedingQuantity().compareTo(BigDecimal.ZERO) <= 0) {
+            throw new ServiceException("绗�" + rowNo + "鏉℃湰娆¤ˉ鏂欐暟閲忓繀椤诲ぇ浜�0");
+        }
+        if (!isFeedingPick(dto)) {
+            throw new ServiceException("绗�" + rowNo + "鏉¤ˉ鏂欑被鍨嬪繀椤讳负2");
+        }
+    }
+
+    private void validateReturnParam(ProductionOrderPickDto dto, int rowNo) {
+        if (dto.getProductionOrderId() == null) {
+            throw new ServiceException("绗�" + rowNo + "鏉$敓浜ц鍗旾D涓嶈兘涓虹┖");
+        }
+        if (dto.getId() == null) {
+            throw new ServiceException("绗�" + rowNo + "鏉¢鏂橧D涓嶈兘涓虹┖");
+        }
+        if (dto.getReturnQty() == null || dto.getReturnQty().compareTo(BigDecimal.ZERO) < 0) {
+            throw new ServiceException("绗�" + rowNo + "鏉¢��鏂欐暟閲忎笉鑳戒负绌轰笖涓嶈兘灏忎簬0");
+        }
+        if (dto.getActualQty() == null || dto.getActualQty().compareTo(BigDecimal.ZERO) < 0) {
+            throw new ServiceException("绗�" + rowNo + "鏉″疄闄呮暟閲忎笉鑳戒负绌轰笖涓嶈兘灏忎簬0");
+        }
+    }
+
+    private boolean isFeedingRequest(ProductionOrderPickDto dto) {
+        if (isFeedingPick(dto)) {
+            return true;
+        }
+        if (dto.getPickList() == null || dto.getPickList().isEmpty()) {
+            return false;
+        }
+        return dto.getPickList().stream()
+                .filter(Objects::nonNull)
+                .anyMatch(this::isFeedingPick);
+    }
+
+    private boolean isFeedingPick(ProductionOrderPickDto dto) {
+        return dto != null && Objects.equals(dto.getPickType(), PICK_TYPE_FEEDING);
+    }
+
+    private boolean isReturnRequest(ProductionOrderPickDto dto) {
+        if (isReturnPick(dto)) {
+            return true;
+        }
+        if (dto.getPickList() == null || dto.getPickList().isEmpty()) {
+            return false;
+        }
+        return dto.getPickList().stream()
+                .filter(Objects::nonNull)
+                .anyMatch(this::isReturnPick);
+    }
+
+    private boolean isReturnPick(ProductionOrderPickDto dto) {
+        return dto != null && Boolean.TRUE.equals(dto.getReturned());
+    }
+
+    private BigDecimal sumFeedingQuantity(Long productionOrderId, Long pickId) {
+        List<ProductionOrderPickRecord> feedingRecords = productionOrderPickRecordMapper.selectList(
+                Wrappers.<ProductionOrderPickRecord>lambdaQuery()
+                        .eq(ProductionOrderPickRecord::getProductionOrderId, productionOrderId)
+                        .eq(ProductionOrderPickRecord::getPickId, pickId)
+                        .eq(ProductionOrderPickRecord::getPickType, PICK_TYPE_FEEDING));
+        return feedingRecords.stream()
+                .map(ProductionOrderPickRecord::getPickQuantity)
+                .map(this::defaultDecimal)
+                .reduce(BigDecimal.ZERO, BigDecimal::add);
+    }
+
+    private BigDecimal calculateActualQty(ProductionOrderPick pick, BigDecimal feedingQty) {
+        return defaultDecimal(pick.getQuantity())
+                .add(defaultDecimal(feedingQty))
+                .subtract(defaultDecimal(pick.getReturnQty()));
     }
 
     private String normalizeBatchNo(String batchNo) {
@@ -653,5 +912,8 @@
     private BigDecimal defaultDecimal(BigDecimal value) {
         return value == null ? BigDecimal.ZERO : value;
     }
-}
 
+    private String formatQuantity(BigDecimal value) {
+        return defaultDecimal(value).stripTrailingZeros().toPlainString();
+    }
+}

--
Gitblit v1.9.3