From 82065a03737fa1370eb9f4f01ab5332933baf08a Mon Sep 17 00:00:00 2001
From: chen.lin <1442464845@qq.com>
Date: 星期四, 05 三月 2026 09:23:37 +0800
Subject: [PATCH] 云仓WMS+RCS+自动入库临时方法配置

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java |  367 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 248 insertions(+), 119 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java
index 2fb0faf..b912340 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/WaitPakinServiceImpl.java
@@ -8,12 +8,16 @@
 import com.vincent.rsf.server.manager.entity.*;
 import com.vincent.rsf.server.manager.enums.PakinIOStatus;
 import com.vincent.rsf.server.manager.mapper.WaitPakinMapper;
+import com.vincent.rsf.server.manager.mapper.MatnrMapper;
 import com.vincent.rsf.server.manager.service.*;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.vincent.rsf.server.common.utils.QuantityUtils;
+import com.vincent.rsf.server.system.constant.GlobalConfigCode;
 import com.vincent.rsf.server.system.constant.SerialRuleCode;
+import com.vincent.rsf.server.system.entity.Config;
+import com.vincent.rsf.server.system.service.ConfigService;
 import com.vincent.rsf.server.system.utils.SerialRuleUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -41,6 +45,10 @@
     private TaskService taskService;
     @Autowired
     private TaskItemService taskItemService;
+    @Autowired
+    private MatnrMapper matnrMapper;
+    @Autowired
+    private ConfigService configService;
 
 
     /**
@@ -58,50 +66,40 @@
             throw new CoolException("鍙傛暟閿欒锛氱墿鏂欒窡韪爜涓虹┖锛�");
         }
         if (StringUtils.isBlank(waitPakin.getBarcode())) {
-            throw new CoolException("鍙傛暟閿欒锛氭墭鐩樼爜涓虹┖锛侊紒");
+            throw new CoolException("鍙傛暟閿欒锛氭枡绠辩爜涓虹┖锛侊紒");
         }
-//        List<Short> asList = Arrays.asList(Short.valueOf(PakinIOStatus.PAKIN_IO_STATUS_DONE.val), Short.valueOf(PakinIOStatus.PAKIN_IO_STATUS_DONE.val));
         WaitPakin pakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
                 .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
 
+        // 鍗婄鏃跺厑璁稿悓涓�鏂欑鐮佺户缁粍鎵樿拷鍔狅紱闈炲崐绠辨椂鏂欑鐮佸凡缁勬墭鍒欎笉鍙啀鐢�
+        if (!Objects.isNull(pakin) && !Boolean.TRUE.equals(waitPakin.getIsHalf())) {
+            throw new CoolException("宸茬粍鎵橈紝璇锋洿鎹㈡枡绠辩爜");
+        }
+
         List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getBarcode, waitPakin.getBarcode()));
         if (!tasks.isEmpty()) {
-            throw new CoolException("褰撳墠鎵樼洏宸叉湁浠诲姟妗e湪鎵ц锛屼笉鑳藉啀娆$粍鎵橈紒锛�");
+            throw new CoolException("褰撳墠鏂欑宸叉湁浠诲姟妗e湪鎵ц锛屼笉鑳藉啀娆$粍鎵橈紒锛�");
         }
-        List<WaitPakinItem> waitPakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, pakin.getId()));
-        waitPakinItems.forEach(item -> {
-            for (PakinItem pakinItem : waitPakin.getItems()) {
-                WarehouseAreasItem warehouseAreasItems = warehouseAreasItemService.getById(pakinItem.getId());
-                if (Objects.isNull(warehouseAreasItems)) {
-                    throw new CoolException("鐗╂枡鏈�佽嚦鏀惰揣鍖烘垨宸茬粍鎵樺畬鎴愮Щ鍑烘敹璐у尯锛侊紒");
-                }
-                if (item.getFieldsIndex().equals(warehouseAreasItems.getFieldsIndex())) {
-                    throw new CoolException("绁ㄥ彿锛�" + item.getExtendFields().get("crushNo") + "宸茬粍鎵橈紝涓嶅彲閲嶅缁勬墭锛侊紒");
-                }
-            }
-        });
-
+        
+        // 妫�鏌ユ枡绠辩爜鏄惁鍦ㄥ簱瀛樹腑瀛樺湪锛堥�氳繃搴撲綅琛ㄦ煡璇級
         List<Loc> locs = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getBarcode, waitPakin.getBarcode()));
         if (!locs.isEmpty()) {
-            List<String> locCodes = locs.stream().map(Loc::getCode).collect(Collectors.toList());
-            String join = StringUtils.join(locCodes, ",");
-            throw new CoolException("鎵樼洏鐮侊細" + waitPakin.getBarcode() + "宸茶搴撲綅锛�" + join + "浣跨敤锛侊紒");
+            throw new CoolException("鍦ㄥ簱锛岃鏇存崲鏂欑鐮�");
         }
-        double sum = waitPakin.getItems().stream().mapToDouble(PakinItem::getReceiptQty).sum();
-        Double waitSum = 0.0;
-        if (!waitPakinItems.isEmpty()) {
-             waitSum = waitPakinItems.stream().mapToDouble(WaitPakinItem::getAnfme).sum();
-        }
-        Double total = Math.round((sum + waitSum) * 100) / 100.0;
 
-        WaitPakin waitPakin1 = new WaitPakin();
+        Double sum = QuantityUtils.roundToScale(waitPakin.getItems().stream().mapToDouble(PakinItem::getReceiptQty).sum());
+
+        Config directPakinConfig = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, GlobalConfigCode.DIRECT_WAIT_PAKIN));
+        boolean directWaitPakin = directPakinConfig != null && Boolean.parseBoolean(directPakinConfig.getVal());
+
+        WaitPakin waitPakin1;
         if (Objects.isNull(pakin)) {
+            waitPakin1 = new WaitPakin();
             String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_WAIT_PAKIN_CODE, null);
             if (StringUtils.isBlank(ruleCode)) {
                 throw new CoolException("缂栫爜瑙勫垯閿欒锛� 缂栫爜瑙勫垯銆孲YS_WAIT_PAKIN_CODE銆嶈鍒欐槸涓嶅瓨鍦�");
             }
             waitPakin1.setCode(ruleCode)
-                    //鐘舵�佷慨鏀逛负鍏ュ簱涓�
                     .setIoStatus(PakinIOStatus.PAKIN_IO_STATUS_DONE.val)
                     .setAnfme(sum)
                     .setUpdateBy(userId)
@@ -111,81 +109,211 @@
                 throw new CoolException("涓诲崟淇濆瓨澶辫触锛侊紒");
             }
         } else {
-            BeanUtils.copyProperties(pakin, waitPakin1);
+            // 鍗婄杩藉姞锛氭部鐢ㄥ凡鏈夌粍鎵樺崟锛屽彧杩藉姞鏄庣粏
+            waitPakin1 = pakin;
         }
 
         List<WaitPakinItem> items = new ArrayList<>();
-        for (PakinItem pakinItem1 : waitPakin.getItems()) {
-            //涓嶈壇鏍囩缁勬墭
-            WarehouseAreasItem warehouseAreasItems = warehouseAreasItemService.getById(pakinItem1.getId());
-            if (null == warehouseAreasItems) {
-                throw new CoolException("鐗╂枡鏈�佽嚦鏀惰揣鍖猴紒锛�");
+        if (directWaitPakin) {
+            // DirectWaitPakin 鍚敤锛氱粍鎵樻潵鑷敹璐у尯锛宲aram 涓� id 涓� WarehouseAreasItem.id
+            for (PakinItem pakinItem1 : waitPakin.getItems()) {
+                WarehouseAreasItem areaItem = warehouseAreasItemService.getById(pakinItem1.getId());
+                if (areaItem == null) {
+                    throw new CoolException("鐗╂枡鏈�佽嚦鏀惰揣鍖猴紒锛�");
+                }
+                WaitPakinItem pakinItem = new WaitPakinItem();
+                pakinItem.setPakinId(waitPakin1.getId())
+                        .setSource(areaItem.getId())
+                        .setAsnId(areaItem.getAsnId())
+                        .setAsnCode(areaItem.getAsnCode())
+                        .setAsnItemId(areaItem.getAsnItemId())
+                        .setIsptResult(areaItem.getIsptResult())
+                        .setPlatItemId(areaItem.getPlatItemId())
+                        .setPlatOrderCode(areaItem.getPlatOrderCode())
+                        .setPlatWorkCode(areaItem.getPlatWorkCode())
+                        .setProjectCode(areaItem.getProjectCode())
+                        .setBatch(areaItem.getSplrBatch())
+                        .setUnit(areaItem.getStockUnit())
+                        .setFieldsIndex(areaItem.getFieldsIndex())
+                        .setMatnrId(areaItem.getMatnrId())
+                        .setMaktx(areaItem.getMaktx())
+                        .setUpdateBy(userId)
+                        .setCreateBy(userId)
+                        .setMatnrCode(areaItem.getMatnrCode());
+                WkOrder order = asnOrderService.getById(areaItem.getAsnId());
+                if (order != null) {
+                    pakinItem.setType(order.getType()).setWkType(StringUtils.isNotBlank(order.getWkType()) ? Short.parseShort(order.getWkType()) : null);
+                }
+                if (pakinItem1.getReceiptQty() == null || pakinItem1.getReceiptQty().compareTo(0.0) <= 0) {
+                    throw new CoolException("缁勬墭鏁伴噺涓嶈兘灏忎簬绛変簬闆讹紒锛�");
+                }
+                if (QuantityUtils.compare(pakinItem1.getReceiptQty(), areaItem.getAnfme()) > 0) {
+                    throw new CoolException("缁勬墭鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺锛侊紒");
+                }
+                pakinItem.setAnfme(QuantityUtils.roundToScale(pakinItem1.getReceiptQty())).setTrackCode(pakinItem1.getTrackCode());
+                items.add(pakinItem);
             }
-            WaitPakinItem pakinItem = new WaitPakinItem();
-            pakinItem.setAnfme(warehouseAreasItems.getAnfme())
-                    .setPakinId(waitPakin1.getId())
-                    .setSource(warehouseAreasItems.getId())
-                    .setAsnId(warehouseAreasItems.getAsnId())
-                    .setAsnCode(warehouseAreasItems.getAsnCode())
-                    .setAsnItemId(warehouseAreasItems.getAsnItemId())
-                    .setIsptResult(warehouseAreasItems.getIsptResult())
-                    .setPlatItemId(warehouseAreasItems.getPlatItemId())
-                    .setPlatOrderCode(warehouseAreasItems.getPlatOrderCode())
-                    .setPlatWorkCode(warehouseAreasItems.getPlatWorkCode())
-                    .setProjectCode(warehouseAreasItems.getProjectCode())
-                    .setBatch(warehouseAreasItems.getSplrBatch())
-                    .setUnit(warehouseAreasItems.getStockUnit())
-                    .setFieldsIndex(warehouseAreasItems.getFieldsIndex())
-                    .setMatnrId(warehouseAreasItems.getMatnrId())
-                    .setMaktx(warehouseAreasItems.getMaktx())
-                    .setUpdateBy(userId)
-                    .setCreateBy(userId)
-                    .setMatnrCode(warehouseAreasItems.getMatnrCode());
-            WkOrder order = asnOrderService.getById(warehouseAreasItems.getAsnId());
-            if (!Objects.isNull(order)) {
-                pakinItem.setType(null == order.getType() ? null : order.getType())
-                        .setWkType(null == order.getWkType() ? null : Short.parseShort(order.getWkType()));
+            if (!waitPakinItemService.saveBatch(items)) {
+                throw new CoolException("缁勬墭鏄庣粏淇濆瓨澶辫触锛侊紒");
             }
-
-            for (PakinItem waitPakinItem : waitPakin.getItems()) {
-                if (waitPakinItem.getId().equals(warehouseAreasItems.getId())) {
-                    if (waitPakinItem.getReceiptQty() > warehouseAreasItems.getAnfme() || waitPakinItem.getReceiptQty().compareTo(0.0) <= 0) {
-                        throw new CoolException("缁勬墭鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺涓斾笉鑳藉皬浜庨浂锛侊紒");
-                    }
-                    pakinItem.setAnfme(waitPakinItem.getReceiptQty())
-                            .setTrackCode(waitPakinItem.getTrackCode());
+            for (WaitPakinItem pakinItem : items) {
+                WarehouseAreasItem one = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>().eq(WarehouseAreasItem::getId, pakinItem.getSource()));
+                if (one == null) {
+                    throw new CoolException("鏀惰揣鍖烘暟鎹敊璇紒锛�");
+                }
+                Double workQty = QuantityUtils.roundToScale(QuantityUtils.add(one.getWorkQty() != null ? one.getWorkQty() : 0.0, pakinItem.getAnfme()));
+                Double qty = QuantityUtils.roundToScale(QuantityUtils.add(workQty, one.getQty() != null ? one.getQty() : 0.0));
+                one.setWorkQty(workQty);
+                if (QuantityUtils.compare(qty, one.getAnfme() != null ? one.getAnfme() : 0.0) > 0) {
+                    throw new CoolException("缁勬墭鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺锛侊紒");
+                }
+                if (!warehouseAreasItemService.saveOrUpdate(one)) {
+                    throw new CoolException("鏀惰揣鍖烘墽琛屾暟閲忎慨鏀瑰け璐ワ紒锛�");
                 }
             }
+            // 鍗婄杩藉姞鏃堕渶鎸夎缁勬墭鍗曚笅鍏ㄩ儴鏄庣粏閲嶇畻鎬绘暟閲�
+            List<WaitPakinItem> allItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, waitPakin1.getId()));
+            waitPakin1.setAnfme(QuantityUtils.roundToScale(allItems.stream().mapToDouble(w -> w.getAnfme() != null ? w.getAnfme() : 0.0).sum()));
+            if (!this.updateById(waitPakin1)) {
+                throw new CoolException("缁勬墭鏁伴噺淇敼澶辫触锛侊紒");
+            }
+            return waitPakin1;
+        }
+
+        // 鏈堿SN鏃讹細鎸夎鍗曟槑缁嗘眹鎬诲凡缁勬墭鏁伴噺锛屾牎楠� 宸茬粍鎵�+鏈缁勭洏 鈮� 璁″垝鏁伴噺
+        Map<Long, Double> alreadyPalletizedByItemId = new java.util.HashMap<>();
+        for (PakinItem p : waitPakin.getItems()) {
+            if (StringUtils.isNotBlank(p.getAsnCode()) && p.getId() != null) {
+                alreadyPalletizedByItemId.putIfAbsent(p.getId(), null);
+            }
+        }
+        if (!alreadyPalletizedByItemId.isEmpty()) {
+            for (Long asnItemId : alreadyPalletizedByItemId.keySet()) {
+                double alreadySum = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getAsnItemId, asnItemId).eq(WaitPakinItem::getDeleted, 0))
+                        .stream().mapToDouble(w -> w.getAnfme() != null ? w.getAnfme() : 0.0).sum();
+                alreadyPalletizedByItemId.put(asnItemId, QuantityUtils.roundToScale(alreadySum));
+            }
+        }
+
+        for (PakinItem pakinItem1 : waitPakin.getItems()) {
+            WaitPakinItem pakinItem = new WaitPakinItem();
+            if (StringUtils.isBlank(pakinItem1.getAsnCode())) {
+                if (Objects.isNull(pakinItem1.getMatnrId())) {
+                    throw new CoolException("鐗╂枡ID涓嶈兘涓虹┖锛侊紒");
+                }
+                
+                // 浠庣墿鏂欎俊鎭〃鑾峰彇鐗╂枡淇℃伅
+                Matnr matnr = matnrMapper.selectById(pakinItem1.getMatnrId());
+                if (Objects.isNull(matnr)) {
+                    throw new CoolException("鐗╂枡淇℃伅涓嶅瓨鍦紝鐗╂枡ID锛�" + pakinItem1.getMatnrId());
+                }
+                
+                // 璁剧疆缁勬墭鏄庣粏淇℃伅锛堜笉璁剧疆source锛屽洜涓轰笉鍦ㄦ敹璐у尯锛�
+                pakinItem.setPakinId(waitPakin1.getId())
+                        .setSource(null) // 涓嶅湪鏀惰揣鍖猴紝source璁句负null
+                        .setAsnId(null)
+                        .setAsnCode(null)
+                        .setAsnItemId(null)
+                        .setIsptResult(null)
+                        .setPlatItemId(null)
+                        .setPlatOrderCode(null)
+                        .setPlatWorkCode(null)
+                        .setProjectCode(null)
+                        .setBatch(null)
+                        .setUnit(matnr.getStockUnit())
+                        .setFieldsIndex(matnr.getFieldsIndex())
+                        .setMatnrId(matnr.getId())
+                        .setMaktx(matnr.getName())
+                        .setUpdateBy(userId)
+                        .setCreateBy(userId)
+                        .setMatnrCode(matnr.getCode());
+                
+                // 璁剧疆缁勬墭鏁伴噺
+                if (pakinItem1.getReceiptQty() == null || pakinItem1.getReceiptQty().compareTo(0.0) <= 0) {
+                    throw new CoolException("缁勬墭鏁伴噺涓嶈兘灏忎簬绛変簬闆讹紒锛�");
+                }
+                pakinItem.setAnfme(QuantityUtils.roundToScale(pakinItem1.getReceiptQty()))
+                        .setTrackCode(pakinItem1.getTrackCode());
+            } else {
+                // 鏀惰揣鍖哄凡鍋滅敤锛氭湁ASN鍗曞彿鏃朵粠璁㈠崟鏄庣粏鑾峰彇鐗╂枡淇℃伅
+                WkOrderItem orderItem = asnOrderItemService.getById(pakinItem1.getId());
+                if (null == orderItem) {
+                    throw new CoolException("璁㈠崟鏄庣粏涓嶅瓨鍦紒锛�");
+                }
+                WkOrder order = asnOrderService.getById(orderItem.getOrderId());
+                if (null == order) {
+                    throw new CoolException("璁㈠崟涓嶅瓨鍦紒锛�");
+                }
+                pakinItem.setAnfme(QuantityUtils.roundToScale(orderItem.getAnfme()))
+                        .setPakinId(waitPakin1.getId())
+                        .setSource(null) // 鏀惰揣鍖哄凡鍋滅敤锛屼笉鍏宠仈鏀惰揣鍖�
+                        .setAsnId(order.getId())
+                        .setAsnCode(order.getCode())
+                        .setAsnItemId(orderItem.getId())
+                        .setIsptResult(orderItem.getIsptResult())
+                        .setPlatItemId(orderItem.getPlatItemId())
+                        .setPlatOrderCode(orderItem.getPlatOrderCode())
+                        .setPlatWorkCode(orderItem.getPlatWorkCode())
+                        .setProjectCode(orderItem.getProjectCode())
+                        .setBatch(orderItem.getSplrBatch())
+                        .setUnit(orderItem.getStockUnit())
+                        .setFieldsIndex(orderItem.getFieldsIndex())
+                        .setMatnrId(orderItem.getMatnrId())
+                        .setMaktx(orderItem.getMaktx())
+                        .setUpdateBy(userId)
+                        .setCreateBy(userId)
+                        .setMatnrCode(orderItem.getMatnrCode());
+                pakinItem.setType(null == order.getType() ? null : order.getType())
+                        .setWkType(StringUtils.isNotBlank(order.getWkType()) ? Short.parseShort(order.getWkType()) : null);
+
+                for (PakinItem waitPakinItem : waitPakin.getItems()) {
+                    if (waitPakinItem.getId().equals(orderItem.getId())) {
+                        if (waitPakinItem.getReceiptQty() == null || waitPakinItem.getReceiptQty().compareTo(0.0) <= 0) {
+                            throw new CoolException("缁勬墭鏁伴噺涓嶈兘灏忎簬绛変簬闆讹紒锛�");
+                        }
+                        Double anfme = orderItem.getAnfme() != null ? orderItem.getAnfme() : 0.0;
+                        Double already = alreadyPalletizedByItemId.getOrDefault(orderItem.getId(), 0.0);
+                        if (QuantityUtils.compare(QuantityUtils.add(already, waitPakinItem.getReceiptQty()), anfme) > 0) {
+                            throw new CoolException("缁勬墭鏁伴噺涓嶈兘瓒呰繃鍙粍鐩樻暟閲忥紙璁″垝" + anfme + "锛屽凡缁勬墭" + already + "锛屾湰娆℃渶澶�" + QuantityUtils.subtract(anfme, already) + "锛夛紒锛�");
+                        }
+                        pakinItem.setAnfme(QuantityUtils.roundToScale(waitPakinItem.getReceiptQty()))
+                                .setTrackCode(waitPakinItem.getTrackCode());
+                    }
+                }
+            }
+            
             items.add(pakinItem);
         }
 
         if (!waitPakinItemService.saveBatch(items)) {
             throw new CoolException("缁勬墭鏄庣粏淇濆瓨澶辫触锛侊紒");
         }
-//        Double sum1 = items.stream().mapToDouble(WaitPakinItem::getAnfme).sum();
 
-        for (WaitPakinItem pakinItem : items) {
-            WarehouseAreasItem one = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>()
-                    .eq(WarehouseAreasItem::getId, pakinItem.getSource()));
-            if (Objects.isNull(one)) {
-                throw new CoolException("鏀惰揣鍖烘暟鎹敊璇紒锛�");
-            }
-            Double workQty = Math.round((one.getWorkQty() + pakinItem.getAnfme()) * 100) / 100.0;
-            Double qty = Math.round((workQty + one.getQty()) * 100) / 100.0;
-            one.setWorkQty(workQty);
-            if (qty.compareTo(one.getAnfme()) > 0) {
-                throw new CoolException("缁勬墭鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺锛侊紒");
-            }
-            if (!warehouseAreasItemService.saveOrUpdate(one)) {
-                throw new CoolException("鏀惰揣鍖烘墽琛屾暟閲忎慨鏀瑰け璐ワ紒锛�");
-            }
+        List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().eq(WaitPakinItem::getPakinId, waitPakin1.getId()));
+        Double waitSum = 0.0;
+        if (!pakinItems.isEmpty()) {
+            waitSum = QuantityUtils.roundToScale(pakinItems.stream().mapToDouble(WaitPakinItem::getAnfme).sum());
         }
+//        Double total = Math.round((sum + waitSum) * 100) / 100.0;
 
-        waitPakin1.setAnfme(total);
+        // 鏀惰揣鍖哄凡鍋滅敤锛屼笉鍐嶆洿鏂版敹璐у尯
+        // for (WaitPakinItem pakinItem : items) {
+        //     if (Objects.isNull(pakinItem.getSource())) continue;
+        //     WarehouseAreasItem one = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>()
+        //             .eq(WarehouseAreasItem::getId, pakinItem.getSource()));
+        //     if (Objects.isNull(one)) throw new CoolException("鏀惰揣鍖烘暟鎹敊璇紒锛�");
+        //     Double workQty = Math.round((one.getWorkQty() + pakinItem.getAnfme()) * 1000000) / 1000000.0;
+        //     Double qty = Math.round((workQty + one.getQty()) * 1000000) / 1000000.0;
+        //     one.setWorkQty(workQty);
+        //     if (qty.compareTo(one.getAnfme()) > 0) throw new CoolException("缁勬墭鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺锛侊紒");
+        //     if (!warehouseAreasItemService.saveOrUpdate(one)) throw new CoolException("鏀惰揣鍖烘墽琛屾暟閲忎慨鏀瑰け璐ワ紒锛�");
+        // }
+
+        waitPakin1.setAnfme(waitSum);
         if (!this.updateById(waitPakin1)) {
             throw new CoolException("缁勬墭鏁伴噺淇敼澶辫触锛侊紒");
         }
-        return pakin;
+        return waitPakin1;
     }
 
     /**
@@ -220,21 +348,22 @@
 //            if (!waitPakinItemService.removeByIds(ids)) {
 //                throw new CoolException("缁勬墭鏄庣粏瑙g粦澶辫触锛侊紒");
 //            }
-            List<Long> list2 = pakinItems.stream().map(WaitPakinItem::getSource).collect(Collectors.toList());
-            List<WarehouseAreasItem> warehouseAreasItems = warehouseAreasItemService.listByIds(list2);
+            // 鏀惰揣鍖哄凡鍋滅敤锛氫粎淇濈暀闈炵┖ source 鐢ㄤ簬鍏煎鏃ф暟鎹紝涓嶅啀鏇存柊鏀惰揣鍖�
+            List<Long> list2 = pakinItems.stream().map(WaitPakinItem::getSource).filter(Objects::nonNull).collect(Collectors.toList());
+            List<WarehouseAreasItem> warehouseAreasItems = list2.isEmpty() ? Collections.emptyList() : warehouseAreasItemService.listByIds(list2);
             for (int i1 = 0; i1 < pakinItems.size(); i1++) {
                 for (PakinItem item : paramItems) {
                     if (item.getId().equals(pakinItems.get(i1).getId())) {
-                        if (pakinItems.get(i1).getAnfme().compareTo(item.getReceiptQty()) > 0) {
-                            if (item.getReceiptQty().compareTo(0.00) == 0) {
+                        if (QuantityUtils.compare(pakinItems.get(i1).getAnfme(), item.getReceiptQty()) > 0) {
+                            if (QuantityUtils.compare(item.getReceiptQty(), 0.00) == 0) {
                                 throw new CoolException("瑙g粦鏁伴噺涓嶈兘涓洪浂锛侊紒");
                             }
-                            Double reslt = Math.round((pakinItems.get(i1).getAnfme() - pakinItems.get(i1).getWorkQty() - pakinItems.get(i1).getQty()) * 10000) / 10000.0;
-                            if (item.getReceiptQty().compareTo(reslt) > 0) {
+                            Double reslt = QuantityUtils.subtract(QuantityUtils.subtract(pakinItems.get(i1).getAnfme(), pakinItems.get(i1).getWorkQty()), pakinItems.get(i1).getQty());
+                            if (QuantityUtils.compare(item.getReceiptQty(), reslt) > 0) {
                                 throw new CoolException("瑙g粦鏁伴噺涓嶈兘澶т簬鍓╀綑鍙墽琛屾暟锛侊紒");
                             }
 
-                            Double anfme = Math.round((pakinItems.get(i1).getAnfme() - item.getReceiptQty()) * 100) / 100.0;
+                            Double anfme = QuantityUtils.subtract(pakinItems.get(i1).getAnfme(), item.getReceiptQty());
                            pakinItems.get(i1).setAnfme(anfme);
                            if (!waitPakinItemService.updateById(pakinItems.get(i1))) {
                                throw new CoolException("缁勬墭鏄庣粏鏁伴噺淇敼澶辫触锛侊紒");
@@ -244,12 +373,16 @@
                                 throw new CoolException("缁勬墭鏄庣粏鍒犻櫎澶辫触锛侊紒");
                             }
                         }
-                        for (int i = 0; i < warehouseAreasItems.size(); i++) {
-                            if (warehouseAreasItems.get(i).getId().equals(pakinItems.get(i1).getSource())) {
-                                double v = Math.round((warehouseAreasItems.get(i).getWorkQty() - item.getReceiptQty()) * 100) / 100.0;
-                                warehouseAreasItems.get(i).setWorkQty(v);
-                                if (!warehouseAreasItemService.updateById(warehouseAreasItems.get(i))) {
-                                    throw new CoolException("鏀惰揣鍖烘暟閲忎慨鏀瑰け璐ワ紒锛�");
+                        // DirectWaitPakin 鍚敤鏃剁粍鎵樻潵鑷敹璐у尯锛岃В缁戦渶鍥炲啓鏀惰揣鍖� workQty
+                        if (Objects.nonNull(pakinItems.get(i1).getSource())) {
+                            for (WarehouseAreasItem areaItem : warehouseAreasItems) {
+                                if (areaItem.getId().equals(pakinItems.get(i1).getSource())) {
+                                    Double v = QuantityUtils.roundToScale(QuantityUtils.subtract(areaItem.getWorkQty() != null ? areaItem.getWorkQty() : 0.0, item.getReceiptQty()));
+                                    areaItem.setWorkQty(v);
+                                    if (!warehouseAreasItemService.updateById(areaItem)) {
+                                        throw new CoolException("鏀惰揣鍖烘暟閲忎慨鏀瑰け璐ワ紒锛�");
+                                    }
+                                    break;
                                 }
                             }
                         }
@@ -257,15 +390,14 @@
                 }
             }
 
-            double anfmes = paramItems.stream().mapToDouble(PakinItem::getReceiptQty).sum();
+            Double anfmes = QuantityUtils.roundToScale(paramItems.stream().mapToDouble(PakinItem::getReceiptQty).sum());
 //            double anfmes = warehouseAreasItems.stream().mapToDouble(WarehouseAreasItem::getAnfme).sum();
-            if (waitPakins.getAnfme().compareTo(anfmes) <= 0) {
+            if (QuantityUtils.compare(waitPakins.getAnfme(), anfmes) <= 0) {
                 if (!waitPakinService.removeById(waitPakins.getId())) {
                     throw new CoolException("缁勬墭鍒犻櫎澶辫触锛侊紒");
                 }
             } else {
-                Double anfme = Math.round((waitPakins.getAnfme() - anfmes) * 100) / 100.0;
-                waitPakins.setAnfme(anfme);
+                waitPakins.setAnfme(QuantityUtils.subtract(waitPakins.getAnfme(), anfmes));
                 if (!waitPakinService.updateById(waitPakins)) {
                     throw new CoolException("缁勬墭鏁版嵁淇敼澶辫触锛侊紒");
                 }
@@ -293,26 +425,23 @@
                 return R.error("缁勬嫋妗f湁鏄庣粏浠诲姟");
             }
 
-            Set<Long> sourceIds = pakinItems.stream().map(WaitPakinItem::getSource).collect(Collectors.toSet());
+            Set<Long> sourceIds = pakinItems.stream().map(WaitPakinItem::getSource).filter(Objects::nonNull).collect(Collectors.toSet());
 
-            List<WarehouseAreasItem> areasItems = warehouseAreasItemService.list(new LambdaQueryWrapper<WarehouseAreasItem>()
-                    .in(WarehouseAreasItem::getId, sourceIds));
+            List<WarehouseAreasItem> areasItems = sourceIds.isEmpty() ? Collections.emptyList() : warehouseAreasItemService.list(new LambdaQueryWrapper<WarehouseAreasItem>().in(WarehouseAreasItem::getId, sourceIds));
 
-            if (areasItems.isEmpty()) {
-                return R.error("鏀惰揣鍖烘暟鎹笉瀛樺湪锛侊紒");
-            }
-
-            Map<Long, List<WaitPakinItem>> listMap = pakinItems.stream().collect(Collectors.groupingBy(WaitPakinItem::getSource));
-            for (WarehouseAreasItem item : areasItems) {
-                List<WaitPakinItem> pakin = listMap.get(item.getId());
-                if (Objects.isNull(pakin)) {
-                    continue;
-                }
-                double sum = pakin.stream().mapToDouble(WaitPakinItem::getAnfme).sum();
-                Double workQty = Math.round((item.getWorkQty() - sum) * 100) / 100.0;
-                item.setWorkQty(workQty);
-                if (!warehouseAreasItemService.updateById(item)) {
-                    throw new CoolException("鏀惰揣鍖烘暟鎹洖婊氬け璐ワ紒锛�");
+            // 鏀惰揣鍖哄凡鍋滅敤锛屼粎鍏煎鏃ф暟鎹椂鍥炴粴鏀惰揣鍖烘暟閲�
+            if (!areasItems.isEmpty()) {
+                Map<Long, List<WaitPakinItem>> listMap = pakinItems.stream().collect(Collectors.groupingBy(WaitPakinItem::getSource));
+                for (WarehouseAreasItem item : areasItems) {
+                    List<WaitPakinItem> pakin = listMap.get(item.getId());
+                    if (Objects.isNull(pakin)) {
+                        continue;
+                    }
+                    Double sum = QuantityUtils.roundToScale(pakin.stream().mapToDouble(WaitPakinItem::getAnfme).sum());
+                    item.setWorkQty(QuantityUtils.subtract(item.getWorkQty(), sum));
+                    if (!warehouseAreasItemService.updateById(item)) {
+                        throw new CoolException("鏀惰揣鍖烘暟鎹洖婊氬け璐ワ紒锛�");
+                    }
                 }
             }
 

--
Gitblit v1.9.1