From c67e3d0295858a61122354a15dec8835044bac0d Mon Sep 17 00:00:00 2001
From: chen.lin <1442464845@qq.com>
Date: 星期二, 24 二月 2026 15:54:53 +0800
Subject: [PATCH] 库位拣料出库数量调整

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java |  173 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 127 insertions(+), 46 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
index 09ff644..476bf94 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -18,6 +18,7 @@
 import com.vincent.rsf.server.api.entity.params.WcsTaskParams;
 import com.vincent.rsf.server.api.service.WcsService;
 import com.vincent.rsf.server.common.constant.Constants;
+import com.vincent.rsf.server.common.utils.QuantityUtils;
 import com.vincent.rsf.server.manager.controller.params.LocToTaskParams;
 import com.vincent.rsf.server.manager.controller.params.PakinItem;
 import com.vincent.rsf.server.manager.enums.*;
@@ -860,33 +861,37 @@
             throw new CoolException("浠诲姟鏄庣粏涓嶅瓨鍦紒锛�");
         }
 
-        List<LocItem> items = new ArrayList<>();
-        for (TaskItem taskItem : taskItems) {
-            LocItem locItem = new LocItem();
-            LocItemWorking locWorking = locItemWorkingService.getOne(new LambdaQueryWrapper<LocItemWorking>()
-                    .eq(LocItemWorking::getTaskId, taskItem.getTaskId())
-                    .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex())
-                    .eq(StringUtils.isNotEmpty(taskItem.getBatch()), LocItemWorking::getBatch, taskItem.getBatch())
-                    .eq(LocItemWorking::getMatnrId, taskItem.getMatnrId()));
-            if (Objects.isNull(locWorking)) {
-               continue;
+        if (TaskType.TASK_TYPE_PICK_IN.type.equals(task.getTaskType())) {
+            // 鎷f枡鍐嶅叆搴擄細鍑哄簱鏃跺凡鍦� pickOrCheckTask 涓墸鍑忓師搴撲綅锛�1100 -> 1089.899锛夛紝
+            // 鍏ュ簱瀹屾垚鏃朵笉鍐嶅洖鍐�/绱姞搴撲綅鏄庣粏锛屼繚鎸� 1 鏉� 1089.899锛岄伩鍏嶅嚭鐜颁袱鏉� 1100
+            // 浠呮洿鏂板簱浣嶇姸鎬併�佹竻鐞� LocItemWorking銆佷换鍔$姸鎬佸強娴佹按
+        } else {
+            // 鐩樼偣鍏ュ簱绛夛細娌跨敤鍘熼�昏緫锛屼粠 LocItemWorking 鍥炲啓骞� saveBatch
+            List<LocItem> items = new ArrayList<>();
+            for (TaskItem taskItem : taskItems) {
+                LocItem locItem = new LocItem();
+                LocItemWorking locWorking = locItemWorkingService.getOne(new LambdaQueryWrapper<LocItemWorking>()
+                        .eq(LocItemWorking::getTaskId, taskItem.getTaskId())
+                        .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItemWorking::getFieldsIndex, taskItem.getFieldsIndex())
+                        .eq(StringUtils.isNotEmpty(taskItem.getBatch()), LocItemWorking::getBatch, taskItem.getBatch())
+                        .eq(LocItemWorking::getMatnrId, taskItem.getMatnrId()));
+                if (Objects.isNull(locWorking)) {
+                   continue;
+                }
+                if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
+                    locWorking.setAnfme(taskItem.getAnfme());
+                }
+                BeanUtils.copyProperties(locWorking, locItem);
+                locItem.setWorkQty(0.0).setQty(0.0).setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null).setUpdateBy(loginUserId).setUpdateTime(new Date());
+                //鏁伴噺涓洪浂鐨勪笉鍏ュ簱
+                if (locItem.getAnfme().compareTo(0.0) > 0) {
+                    items.add(locItem);
+                }
             }
-            if (task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
-                locWorking.setAnfme(taskItem.getAnfme());
-            } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) && taskItem.getQty() != null && taskItem.getQty().compareTo(0.0) > 0) {
-                // 鎷f枡鍐嶅叆搴擄細鍏ュ簱鏁伴噺涓烘湰娆℃嫞鏂欐暟閲�(taskItem.qty)锛屼繚璇佷笌鍑哄簱鎵e噺涓�鑷�
-                locWorking.setAnfme(taskItem.getQty());
-            }
-            BeanUtils.copyProperties(locWorking, locItem);
-            locItem.setWorkQty(0.0).setQty(0.0).setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null).setUpdateBy(loginUserId).setUpdateTime(new Date());
-            //鏁伴噺涓洪浂鐨勪笉鍏ュ簱
-            if (locItem.getAnfme().compareTo(0.0) > 0) {
-                items.add(locItem);
-            }
-        }
 
-        if (!locItemService.saveBatch(items)) {
+            if (!items.isEmpty() && !locItemService.saveBatch(items)) {
 //            throw new CoolException("浣滀笟搴撳瓨鍥炲啓澶辫触锛侊紒");
+            }
         }
 
         TaskItem taskItem = taskItems.stream().findFirst().get();
@@ -1290,7 +1295,7 @@
 //        if (Objects.isNull(locInfo)) {
 //            throw new CoolException("鑾峰彇搴撲綅澶辫触锛侊紒");
 //        }
-        //甯屾棩涓婃姤鐗╂湁鎯呭喌锛屼笉闇�瑕佽幏鍙栨柊搴撲綅
+        //涓婃姤鐗╂湁鎯呭喌锛屼笉闇�瑕佽幏鍙栨柊搴撲綅
         task.setTargLoc(task.getOrgLoc())
                 .setOrgSite(task.getTargSite());
 
@@ -1299,16 +1304,70 @@
         }
         //鑾峰彇鍥犲綋鍓嶄换鍔″嚭搴撶殑鎵�鏈夌墿鏂欎俊鎭�
         List<LocItemWorking> tempLocs = locItemWorkingService.list(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()));
-        if (tempLocs.isEmpty()) {
-            throw new CoolException("鏁版嵁閿欒锛屼綔涓氫腑搴撳瓨鏁版嵁涓㈠け锛侊紒");
-        }
         List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
         if (taskItems.isEmpty()) {
             throw new CoolException("鏁版嵁閿欒锛氫换鍔℃槑缁嗕负绌猴紒锛�");
         }
+        // 涓庢煡璇竴鑷达細鑻ユ棤浣滀笟涓簱瀛樹絾瀛樺湪浠诲姟鏄庣粏锛岀敤浠诲姟鏄庣粏鍦ㄥ唴瀛樹腑鍏滃簳锛岄伩鍏嶁�滆兘鏌ュ埌鍗存棤娉曠‘璁も��
+        if (tempLocs.isEmpty()) {
+            tempLocs = taskItems.stream().map(ti -> {
+                LocItemWorking w = new LocItemWorking();
+                w.setTaskId(task.getId());
+                w.setFieldsIndex(ti.getFieldsIndex());
+                w.setAnfme(ti.getAnfme());
+                w.setMatnrId(ti.getMatnrId());
+                w.setMaktx(ti.getMaktx());
+                w.setMatnrCode(ti.getMatnrCode());
+                w.setSpec(ti.getSpec());
+                w.setBatch(ti.getBatch());
+                w.setUnit(ti.getUnit());
+                w.setModel(ti.getModel());
+                return w;
+            }).collect(Collectors.toList());
+        }
 
-        // 鎷f枡鍏ュ簱锛氬湪鐢熸垚鎷f枡鍏ュ簱鍗曟椂鎵e噺鍘熷簱浣嶆暟閲忥紝涓嶄緷璧栧嚭搴撳畬鎴愭椂搴撲綅鐘舵�佷负R
+        // 鎷f枡鍏ュ簱锛氬厛绠楀墿浣欐暟閲忓苟鏇存柊 taskItem.anfme锛屽凡鎷f暟閲� taskItem.qty = 鍘熷簱浣� - 鍓╀綑锛堜繚璇� 鏁伴噺=100銆佸凡鎷f暟閲�=1銆佸簱瀛樻槑缁�=100锛�
         if (TaskType.TASK_TYPE_PICK_IN.type.equals(type)) {
+            log.debug("[鎷f枡鍏ュ簱] 寮�濮嬪鐞� taskId={}, taskCode={}, orgLoc={}, tempLocs.size={}, taskItems.size={}",
+                    task.getId(), task.getTaskCode(), task.getOrgLoc(), tempLocs.size(), taskItems.size());
+            tempLocs.forEach(working -> {
+                taskItems.forEach(taskItem -> {
+                    if (Objects.equals(taskItem.getFieldsIndex(), working.getFieldsIndex())) {
+                        // 宸叉嫞鏁伴噺锛氫紭鍏堢敤 taskItem.qty锛涗负 0 鏃朵粠鍑哄簱鍗曟槑缁嗗彇 鎵ц鏁�(workQty) 鎴� 璁㈠崟鏁伴噺(anfme)锛岄伩鍏嶆墜鍔ㄥ畬缁撴湭濉� qty 瀵艰嚧涓嶆墸鍑�
+                        Double pickedQty = taskItem.getQty() != null && QuantityUtils.isPositive(taskItem.getQty())
+                                ? taskItem.getQty()
+                                : 0.0;
+                        log.debug("[鎷f枡鍏ュ簱] taskItemId={}, fieldsIndex={}, working.anfme={}, taskItem.qty={}, taskItem.orderItemId={}, pickedQty(鍒�)={}",
+                                taskItem.getId(), taskItem.getFieldsIndex(), working.getAnfme(), taskItem.getQty(), taskItem.getOrderItemId(), pickedQty);
+                        if (pickedQty <= 0 && taskItem.getOrderItemId() != null) {
+                            WkOrderItem orderItem = asnOrderItemService.getById(taskItem.getOrderItemId());
+                            log.debug("[鎷f枡鍏ュ簱] 鏌ュ嚭搴撳崟鏄庣粏 orderItemId={}, orderItem={}, workQty={}, anfme={}",
+                                    taskItem.getOrderItemId(), orderItem != null ? "瀛樺湪" : "null",
+                                    orderItem != null ? orderItem.getWorkQty() : null, orderItem != null ? orderItem.getAnfme() : null);
+                            if (orderItem != null) {
+                                if (orderItem.getWorkQty() != null && QuantityUtils.isPositive(orderItem.getWorkQty())) {
+                                    pickedQty = orderItem.getWorkQty();
+                                } else if (orderItem.getAnfme() != null && QuantityUtils.isPositive(orderItem.getAnfme())) {
+                                    pickedQty = orderItem.getAnfme();
+                                }
+                            }
+                        }
+                        Double minQty = QuantityUtils.subtract(working.getAnfme(), pickedQty);
+                        log.debug("[鎷f枡鍏ュ簱] 璁$畻鍚� pickedQty={}, minQty(鍓╀綑)={}, 灏嗘洿鏂� taskItem.anfme={}, taskItem.qty={}",
+                                pickedQty, minQty, minQty, pickedQty);
+                        if (QuantityUtils.isNonNegative(minQty)) {
+                            taskItem.setAnfme(minQty);
+                            taskItem.setQty(pickedQty);
+                            if (!taskItemService.updateById(taskItem)) {
+                                throw new CoolException("浠诲姟鏄庣粏淇敼澶辫触锛侊紒");
+                            }
+                        } else {
+                            log.warn("[鎷f枡鍏ュ簱] minQty<0 鏈洿鏂� taskItem, taskItemId={}", taskItem.getId());
+                        }
+                    }
+                });
+            });
+            log.debug("[鎷f枡鍏ュ簱] 鍗冲皢鎵e噺搴撲綅 locId={}, locCode={}", loc.getId(), loc.getCode());
             subtractLocItemByTaskItems(loc, taskItems, SystemAuthUtils.getLoginUserId());
         }
 
@@ -1317,19 +1376,19 @@
                 if (Objects.equals(taskItem.getFieldsIndex(), working.getFieldsIndex())) {
                     Double minQty = taskItem.getAnfme();
                     if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
-                        // 璁$畻鍓╀綑鏁伴噺锛氫粠LocItemWorking涓噺鍘籘askItem鐨勬嫞鏂欐暟閲�
-                        minQty = Math.round((working.getAnfme() - taskItem.getQty()) * 1000000) / 1000000.0;
+                        // 璁$畻鍓╀綑鏁伴噺
+                        minQty = QuantityUtils.subtract(working.getAnfme(), taskItem.getQty());
                     }
-                    if (minQty.compareTo(0.0) >= 0) {
+                    if (QuantityUtils.isNonNegative(minQty)) {
                         // 鏇存柊TaskItem鐨勫墿浣欐暟閲�
                         taskItem.setAnfme(minQty);
                         if (!taskItemService.updateById(taskItem)) {
                             throw new CoolException("浠诲姟鏄庣粏淇敼澶辫触锛侊紒");
                         }
-                        // 鏇存柊LocItemWorking鐨勫墿浣欐暟閲忥紙闈炵洏鐐瑰叆搴撴椂闇�瑕佹洿鏂帮級
+                        // 鏇存柊LocItemWorking鐨勫墿浣欐暟閲忥紙闈炵洏鐐瑰叆搴撴椂闇�瑕佹洿鏂帮級锛涗粎鎸佷箙鍖栬褰曟墠鍐欏簱
                         if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                             working.setAnfme(minQty);
-                            if (!locItemWorkingService.updateById(working)) {
+                            if (working.getId() != null && !locItemWorkingService.updateById(working)) {
                                 throw new CoolException("浣滀笟搴撳瓨鏁伴噺鏇存柊澶辫触锛侊紒");
                             }
                         }
@@ -1384,9 +1443,9 @@
                     .setQty(0.0)
                     .setLocId(loc1.getId())
                     .setLocCode(loc1.getCode());
-            // 鎷f枡鍐嶅叆搴擄細鐩爣搴撲綅鏁伴噺搴斾负鏈鎷f枡鏁伴噺(taskItem.qty)锛屼笉鏄師搴撲綅鍓╀綑鏁伴噺(taskItem.anfme)
-            if (TaskType.TASK_TYPE_PICK_IN.type.equals(task.getTaskType()) && taskItem.getQty() != null && taskItem.getQty().compareTo(0.0) > 0) {
-                itemWorking.setAnfme(taskItem.getQty());
+            // 鎷f枡鍐嶅叆搴擄細鐩爣搴撲綅鏁伴噺搴斾负鍥炲簱鐨勫墿浣欐暟閲�(taskItem.anfme)锛屽嵆鍘熷簱浣嶅噺鎺夋湰娆℃嫞鍑哄悗鐨勬暟閲忥紙濡� 1000 鍑哄簱 10 鈫� 990锛�
+            if (TaskType.TASK_TYPE_PICK_IN.type.equals(task.getTaskType()) && taskItem.getAnfme() != null && taskItem.getAnfme().compareTo(0.0) > 0) {
+                itemWorking.setAnfme(taskItem.getAnfme());
             }
             workings.add(itemWorking);
         });
@@ -2066,18 +2125,37 @@
     @Transactional(rollbackFor = Exception.class)
     public void subtractLocItemByTaskItems(Loc loc, List<TaskItem> taskItems, Long loginUserId) {
         for (TaskItem taskItem : taskItems) {
-            // 鏌ヨ瀵瑰簲鐨勫簱浣嶆槑缁�
-            LocItem locItem = locItemService.getOne(new LambdaQueryWrapper<LocItem>()
+            LambdaQueryWrapper<LocItem> locItemWrapper = new LambdaQueryWrapper<LocItem>()
                     .eq(LocItem::getLocId, loc.getId())
-                    .eq(LocItem::getMatnrId, taskItem.getMatnrId())
-                    .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch())
-                    .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex()));
+                    .eq(LocItem::getMatnrId, taskItem.getMatnrId());
+            if (StringUtils.isNotBlank(taskItem.getBatch())) {
+                locItemWrapper.eq(LocItem::getBatch, taskItem.getBatch());
+            } else {
+                locItemWrapper.and(w -> w.isNull(LocItem::getBatch).or().eq(LocItem::getBatch, ""));
+            }
+            if (StringUtils.isNotBlank(taskItem.getFieldsIndex())) {
+                locItemWrapper.eq(LocItem::getFieldsIndex, taskItem.getFieldsIndex());
+            } else {
+                locItemWrapper.and(w -> w.isNull(LocItem::getFieldsIndex).or().eq(LocItem::getFieldsIndex, ""));
+            }
+            LocItem locItem = locItemService.getOne(locItemWrapper);
+
+            log.info("[鎷f枡鍏ュ簱-鎵e噺搴撲綅] taskItemId={}, locId={}, matnrId={}, batch={}, fieldsIndex={}, locItem={}, locItem.anfme={}, taskItem.qty={}, taskItem.anfme={}",
+                    taskItem.getId(), loc.getId(), taskItem.getMatnrId(), taskItem.getBatch(), taskItem.getFieldsIndex(),
+                    locItem != null ? "瀛樺湪" : "null", locItem != null ? locItem.getAnfme() : null, taskItem.getQty(), taskItem.getAnfme());
 
             if (Objects.nonNull(locItem)) {
-                // 璁$畻鎵e噺鍚庣殑鏁伴噺
-                Double newAnfme = Math.round((locItem.getAnfme() - taskItem.getQty()) * 1000000) / 1000000.0;
-                
-                if (newAnfme.compareTo(0.0) <= 0) {
+                // 鎵e噺閲忥細浼樺厛鐢ㄦ湰娆℃嫞鏂欐暟閲� taskItem.getQty()锛涜嫢涓� 0 涓� taskItem.anfme 宸蹭负鍓╀綑鏁伴噺锛屽垯鎵e噺 = 鍘熷簱浣� - 鍓╀綑锛堢敤 BigDecimal 閬垮厤 Double 绮惧害闂锛�
+                Double deductQty = QuantityUtils.isPositive(taskItem.getQty())
+                        ? taskItem.getQty()
+                        : (taskItem.getAnfme() != null && QuantityUtils.compare(taskItem.getAnfme(), locItem.getAnfme()) < 0
+                                ? QuantityUtils.subtract(locItem.getAnfme(), taskItem.getAnfme())
+                                : 0.0);
+                Double newAnfme = QuantityUtils.subtract(locItem.getAnfme(), deductQty);
+                log.info("[鎷f枡鍏ュ簱-鎵e噺搴撲綅] locItemId={}, deductQty={}, 鍘焌nfme={}, newAnfme={}, 鎿嶄綔={}",
+                        locItem.getId(), deductQty, locItem.getAnfme(), newAnfme, QuantityUtils.isNonPositive(newAnfme) ? "鍒犻櫎" : "鏇存柊");
+
+                if (QuantityUtils.isNonPositive(newAnfme)) {
                     // 鏁伴噺灏忎簬绛変簬0锛屽垹闄ゅ簱浣嶆槑缁�
                     locItemService.removeById(locItem.getId());
                 } else {
@@ -2089,6 +2167,9 @@
                         throw new CoolException("搴撲綅鏄庣粏鏁伴噺鎵e噺澶辫触锛侊紒");
                     }
                 }
+            } else {
+                log.warn("[鎷f枡鍏ュ簱-鎵e噺搴撲綅] 鏈煡鍒板簱浣嶆槑缁� locId={}, matnrId={}, batch={}, fieldsIndex={}锛屾湭鎵e噺",
+                        loc.getId(), taskItem.getMatnrId(), taskItem.getBatch(), taskItem.getFieldsIndex());
             }
         }
     }

--
Gitblit v1.9.1