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 |  460 +++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 332 insertions(+), 128 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 c325590..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
@@ -1,20 +1,23 @@
 package com.vincent.rsf.server.manager.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.vincent.rsf.framework.common.R;
 import com.vincent.rsf.framework.exception.CoolException;
 import com.vincent.rsf.server.manager.controller.params.PakinItem;
 import com.vincent.rsf.server.manager.controller.params.WaitPakinParam;
 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;
@@ -38,6 +41,14 @@
     private WarehouseAreasItemService warehouseAreasItemService;
     @Autowired
     private LocService locService;
+    @Autowired
+    private TaskService taskService;
+    @Autowired
+    private TaskItemService taskItemService;
+    @Autowired
+    private MatnrMapper matnrMapper;
+    @Autowired
+    private ConfigService configService;
 
 
     /**
@@ -55,35 +66,41 @@
             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 list = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
-                .notIn(WaitPakin::getIoStatus, asList)
+        WaitPakin pakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
                 .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
-        if (!Objects.isNull(list)) {
-            throw new CoolException("鎵樼洏鐮侊細" + waitPakin.getBarcode() + "宸茶缁勬墭鍗曪細" + list.getCode() + "浣跨敤锛侊紒");
+
+        // 鍗婄鏃跺厑璁稿悓涓�鏂欑鐮佺户缁粍鎵樿拷鍔狅紱闈炲崐绠辨椂鏂欑鐮佸凡缁勬墭鍒欎笉鍙啀鐢�
+        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湪鎵ц锛屼笉鑳藉啀娆$粍鎵橈紒锛�");
+        }
+        
+        // 妫�鏌ユ枡绠辩爜鏄惁鍦ㄥ簱瀛樹腑瀛樺湪锛堥�氳繃搴撲綅琛ㄦ煡璇級
         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();
 
-        WaitPakin waitPakin1 = new WaitPakin();
-        WaitPakin pakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>()
-                .in(WaitPakin::getIoStatus, asList)
-                .eq(WaitPakin::getBarcode, waitPakin.getBarcode()));
+        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(Short.parseShort(PakinIOStatus.PAKIN_IO_STATUS_DONE.val))
+                    .setIoStatus(PakinIOStatus.PAKIN_IO_STATUS_DONE.val)
                     .setAnfme(sum)
                     .setUpdateBy(userId)
                     .setCreateBy(userId)
@@ -92,138 +109,218 @@
                 throw new CoolException("涓诲崟淇濆瓨澶辫触锛侊紒");
             }
         } else {
-            BeanUtils.copyProperties(pakin, waitPakin1);
-            waitPakin1.setAnfme(sum);
-            if (!this.saveOrUpdate(waitPakin1)) {
-                throw new CoolException("涓诲崟淇敼澶辫触锛侊紒");
-            }
+            // 鍗婄杩藉姞锛氭部鐢ㄥ凡鏈夌粍鎵樺崟锛屽彧杩藉姞鏄庣粏
+            waitPakin1 = pakin;
         }
-        /**鐗╂枡璺熻釜鐮�*/
 
-        List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>()
-                .eq(WaitPakinItem::getPakinId, waitPakin1.getId()))
-                ;
-        if (!pakinItems.isEmpty()) {
-            if (!waitPakinItemService.remove(new LambdaQueryWrapper<WaitPakinItem>()
-                    .eq(WaitPakinItem::getPakinId, waitPakin1.getId())
-                    )) {
-                throw new CoolException("鍘熷崟鎹竻闄ゅけ璐ワ紒锛�");
-            }
-        }
         List<WaitPakinItem> items = new ArrayList<>();
-        for (PakinItem pakinItem1 : waitPakin.getItems()) {
-            //涓嶈壇鏍囩缁勬墭
-            if (!Objects.isNull(waitPakin.getType()) && waitPakin.getType().equals("defective")) {
-                List<AsnOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().in(AsnOrderItem::getAsnCode, pakinItem1.getAsnCode()));
-                if (Objects.isNull(orderItems) || orderItems.isEmpty()) {
-                    throw new CoolException("鍗曟嵁涓嶅瓨鍦紒锛�");
-                }
-                for (AsnOrderItem item : orderItems) {
-                    WaitPakinItem pakinItem = new WaitPakinItem();
-                    pakinItem.setAnfme(item.getAnfme())
-                            .setPakinId(waitPakin1.getId())
-                            .setAsnId(item.getAsnId())
-                            .setAsnCode(item.getAsnCode())
-                            .setAsnItemId(item.getId())
-                            .setBatch(item.getSplrBatch())
-                            .setUnit(item.getStockUnit())
-                            .setFieldsIndex(item.getFieldsIndex())
-                            .setMatnrId(item.getMatnrId())
-                            .setMaktx(item.getMaktx())
-                            .setUpdateBy(userId)
-                            .setCreateBy(userId)
-                            .setMatnrCode(item.getMatnrCode());
-                    AsnOrder order = asnOrderService.getById(item.getAsnId());
-                    if (!Objects.isNull(order)) {
-                        pakinItem.setType(null == order.getType() ?  null :order.getType())
-                                .setWkType(null == order.getWkType() ? null : Short.parseShort(order.getWkType()) );
-                    }
-                    for (PakinItem waitPakinItem : waitPakin.getItems()) {
-                        if (waitPakinItem.getTrackCode().equals(item.getTrackCode())) {
-                            if (waitPakinItem.getReceiptQty() > item.getAnfme() || waitPakinItem.getReceiptQty().compareTo(0.0) >= 0) {
-                                throw new CoolException("缁勬墭鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺锛侊紒");
-                            }
-                            pakinItem.setAnfme(waitPakinItem.getReceiptQty()).setTrackCode(waitPakinItem.getTrackCode());
-                        }
-                    }
-                    items.add(pakinItem);
-                }
-            } else {
-                WarehouseAreasItem warehouseAreasItems = warehouseAreasItemService.getById(pakinItem1.getId());
-                if (null == warehouseAreasItems) {
+        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.setAnfme(warehouseAreasItems.getAnfme())
-                        .setPakinId(waitPakin1.getId())
-                        .setAsnId(warehouseAreasItems.getAsnId())
-                        .setAsnCode(warehouseAreasItems.getAsnCode())
-                        .setAsnItemId(warehouseAreasItems.getId())
-                        .setBatch(warehouseAreasItems.getSplrBatch())
-                        .setUnit(warehouseAreasItems.getStockUnit())
-                        .setFieldsIndex(warehouseAreasItems.getFieldsIndex())
-                        .setMatnrId(warehouseAreasItems.getMatnrId())
-                        .setMaktx(warehouseAreasItems.getMaktx())
+                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(warehouseAreasItems.getMatnrCode());
-                AsnOrder 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()) );
+                        .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);
                 }
-
-                for (PakinItem waitPakinItem : waitPakin.getItems()) {
-                    if (waitPakinItem.getTrackCode().equals(warehouseAreasItems.getTrackCode())) {
-                        if (waitPakinItem.getReceiptQty() > warehouseAreasItems.getAnfme() || waitPakinItem.getReceiptQty().compareTo(0.0) <= 0) {
-                            throw new CoolException("缁勬墭鏁伴噺涓嶈兘澶т簬鏀惰揣鏁伴噺涓斾笉鑳藉皬浜庨浂锛侊紒");
-                        }
-                        pakinItem.setAnfme(waitPakinItem.getReceiptQty()).setTrackCode(waitPakinItem.getTrackCode());
-                    }
+                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);
+            }
+            if (!waitPakinItemService.saveBatch(items)) {
+                throw new CoolException("缁勬墭鏄庣粏淇濆瓨澶辫触锛侊紒");
+            }
+            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);
             }
         }
-        double sum1 = items.stream().mapToDouble(WaitPakinItem::getAnfme).sum();
+        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("缁勬墭鏄庣粏淇濆瓨澶辫触锛侊紒");
         }
 
-        for (WaitPakinItem pakinItem : items) {
-            WarehouseAreasItem one = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>()
-                    .eq(WarehouseAreasItem::getTrackCode, pakinItem.getTrackCode())
-                    .eq(WarehouseAreasItem::getSplrBatch, pakinItem.getBatch()));
-            if (Objects.isNull(one)) {
-                throw new CoolException("鏀惰揣鍖烘暟鎹敊璇紒锛�");
-            }
-            one.setWorkQty(one.getWorkQty() + pakinItem.getAnfme() + one.getQty()) ;
-            if (one.getWorkQty() > one.getAnfme()) {
-                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;
 
+        // 鏀惰揣鍖哄凡鍋滅敤锛屼笉鍐嶆洿鏂版敹璐у尯
+        // 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(sum1);
+        waitPakin1.setAnfme(waitSum);
         if (!this.updateById(waitPakin1)) {
             throw new CoolException("缁勬墭鏁伴噺淇敼澶辫触锛侊紒");
         }
-
-        //TODO 缁勬墭瀹屾垚鍚庯紝鎵e噺鏀惰揣鍖哄簱瀛�
-
-        return pakin;
+        return waitPakin1;
     }
 
     /**
-     * @author Ryan
-     * @description 缁勬墭瑙g粦
      * @param
      * @return
+     * @author Ryan
+     * @description 缁勬墭瑙g粦
      * @time 2025/3/29 14:42
      */
     @Override
@@ -231,27 +328,134 @@
     public synchronized WaitPakin unBind(WaitPakinParam param) {
         String barcode = param.getBarcode();
         if (StringUtils.isNotBlank(barcode)) {
-            WaitPakin waitPakins = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, barcode), false);
+            WaitPakin waitPakins = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, barcode));
             if (Objects.isNull(waitPakins)) {
                 throw new CoolException("缁勬墭涓嶅瓨鍦紒锛�");
             }
             List<PakinItem> paramItems = param.getItems();
+
             if (Objects.isNull(paramItems) || paramItems.isEmpty()) {
                 throw new CoolException("瑙g粦鐗╂枡涓嶈兘涓虹┖锛侊紒");
             }
-            List<Long> list = paramItems.stream().map(PakinItem::getMatnrId).collect(Collectors.toList());
+            List<Long> list = paramItems.stream().map(PakinItem::getId).collect(Collectors.toList());
             List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>()
-                    .eq(WaitPakinItem::getPakinId, waitPakins.getId())
-                    .in(WaitPakinItem::getMatnrId, list));
+                    .in(WaitPakinItem::getId, list)
+                    );
             if (pakinItems.isEmpty()) {
                 throw new CoolException("鏁版嵁閿欒锛氱粍鎵樻槑缁嗕笉瀛樺湪锛侊紒");
             }
-            List<Long> ids = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toList());
-            if (!waitPakinItemService.removeByIds(ids)) {
-                throw new CoolException("缁勬墭鏄庣粏瑙g粦澶辫触锛侊紒");
+//            List<Long> ids = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toList());
+//            if (!waitPakinItemService.removeByIds(ids)) {
+//                throw new CoolException("缁勬墭鏄庣粏瑙g粦澶辫触锛侊紒");
+//            }
+            // 鏀惰揣鍖哄凡鍋滅敤锛氫粎淇濈暀闈炵┖ 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 (QuantityUtils.compare(pakinItems.get(i1).getAnfme(), item.getReceiptQty()) > 0) {
+                            if (QuantityUtils.compare(item.getReceiptQty(), 0.00) == 0) {
+                                throw new CoolException("瑙g粦鏁伴噺涓嶈兘涓洪浂锛侊紒");
+                            }
+                            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 = QuantityUtils.subtract(pakinItems.get(i1).getAnfme(), item.getReceiptQty());
+                           pakinItems.get(i1).setAnfme(anfme);
+                           if (!waitPakinItemService.updateById(pakinItems.get(i1))) {
+                               throw new CoolException("缁勬墭鏄庣粏鏁伴噺淇敼澶辫触锛侊紒");
+                           }
+                        } else {
+                            if (!waitPakinItemService.removeById(pakinItems.get(i1).getId())) {
+                                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;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+            Double anfmes = QuantityUtils.roundToScale(paramItems.stream().mapToDouble(PakinItem::getReceiptQty).sum());
+//            double anfmes = warehouseAreasItems.stream().mapToDouble(WarehouseAreasItem::getAnfme).sum();
+            if (QuantityUtils.compare(waitPakins.getAnfme(), anfmes) <= 0) {
+                if (!waitPakinService.removeById(waitPakins.getId())) {
+                    throw new CoolException("缁勬墭鍒犻櫎澶辫触锛侊紒");
+                }
+            } else {
+                waitPakins.setAnfme(QuantityUtils.subtract(waitPakins.getAnfme(), anfmes));
+                if (!waitPakinService.updateById(waitPakins)) {
+                    throw new CoolException("缁勬墭鏁版嵁淇敼澶辫触锛侊紒");
+                }
             }
             return waitPakins;
         }
         return new WaitPakin();
     }
+
+    /**
+     * @author Ryan
+     * @date 2025/5/7
+     * @description: 鍒犻櫎缁勬嫋淇℃伅
+     * @version 1.0
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R removePakin(List<Long> pakinIds) {
+        List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>()
+                .in(WaitPakinItem::getPakinId, pakinIds));
+        if (!pakinItems.isEmpty()) {
+            List<Long> list = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toList());
+            List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().in(TaskItem::getSource, list));
+            if (!taskItems.isEmpty()) {
+                return R.error("缁勬嫋妗f湁鏄庣粏浠诲姟");
+            }
+
+            Set<Long> sourceIds = pakinItems.stream().map(WaitPakinItem::getSource).filter(Objects::nonNull).collect(Collectors.toSet());
+
+            List<WarehouseAreasItem> areasItems = sourceIds.isEmpty() ? Collections.emptyList() : warehouseAreasItemService.list(new LambdaQueryWrapper<WarehouseAreasItem>().in(WarehouseAreasItem::getId, sourceIds));
+
+            // 鏀惰揣鍖哄凡鍋滅敤锛屼粎鍏煎鏃ф暟鎹椂鍥炴粴鏀惰揣鍖烘暟閲�
+            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("鏀惰揣鍖烘暟鎹洖婊氬け璐ワ紒锛�");
+                    }
+                }
+            }
+
+            Set<Long> pakinItemIds = pakinItems.stream().map(WaitPakinItem::getId).collect(Collectors.toSet());
+
+            if (!waitPakinItemService.removeByIds(pakinItemIds)) {
+                throw new CoolException("鏄庣粏鍒犻櫎澶辫触锛侊紒");
+            }
+        }
+
+        if (!waitPakinService.removeByIds(pakinIds)) {
+            return R.error("Delete Fail");
+        }
+
+        return R.ok("Delete Success").add(pakinIds);
+    }
 }

--
Gitblit v1.9.1