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

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java |  677 ++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 554 insertions(+), 123 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 610b525..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.*;
@@ -508,25 +509,157 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Task operateComplete(Long id, Long loginUserId) {
-        List<Integer> longs = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id);
+        // 鍏堟煡璇换鍔★紝涓嶉檺鍒剁姸鎬�
         Task task = taskService.getOne(new LambdaQueryWrapper<Task>()
-                .eq(Task::getId, id)
-                .in(Task::getTaskStatus, longs));
+                .eq(Task::getId, id));
 
         if (Objects.isNull(task)) {
-            throw new CoolException("鏁版嵁閿欒锛氬綋鍓嶄换鍔′笉鍙墽琛屽畬缁撴搷浣滐紒锛�");
+            throw new CoolException("浠诲姟涓嶅瓨鍦紒锛�");
+        }
+
+        // 鏍规嵁鍓嶇閫昏緫鍒ゆ柇鏄惁鍏佽瀹屾垚锛�
+        // 鍓嶇鏉′欢锛�((taskStatus < 98) || (taskType >= 101 && taskStatus < 198)) || (taskType == 11 && taskStatus == 101)
+        // 1. 浠讳綍浠诲姟鐘舵�� < 98 閮藉彲浠ュ畬鎴�
+        // 2. 鍑哄簱浠诲姟锛坱askType >= 101锛変笖鐘舵�� < 198 鍙互瀹屾垚
+        // 3. 搴撴牸绉昏浇锛坱askType == 11锛変笖鐘舵�� == 101 鍙互瀹屾垚
+        boolean canComplete = false;
+        if (task.getTaskStatus() < TaskStsType.COMPLETE_IN.id) {
+            // 浠讳綍浠诲姟鐘舵�� < 98 閮藉彲浠ュ畬鎴�
+            canComplete = true;
+        } else if (task.getTaskType() >= 101 && task.getTaskStatus() < TaskStsType.COMPLETE_OUT.id) {
+            // 鍑哄簱浠诲姟涓旂姸鎬� < 198
+            canComplete = true;
+        } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type) 
+                && task.getTaskStatus().equals(TaskStsType.GENERATE_OUT.id)) {
+            // 搴撴牸绉昏浇涓旂姸鎬� == 101
+            canComplete = true;
+        }
+
+        if (!canComplete) {
+            throw new CoolException("鏁版嵁閿欒锛氬綋鍓嶄换鍔′笉鍙墽琛屽畬缁撴搷浣滐紒锛佷换鍔$被鍨嬶細" + task.getTaskType() + "锛屼换鍔$姸鎬侊細" + task.getTaskStatus());
         }
 
         modiftyTaskSort(task, loginUserId);
-        //
-//        if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
-//            task.setTaskStatus(TaskStsType.COMPLETE_OUT.id);
-//        } else {
-        task.setTaskStatus(task.getTaskType() < 100 ? TaskStsType.COMPLETE_IN.id : TaskStsType.AWAIT.id);
-//        }
+        
+        // 濡傛灉浠诲姟鐘舵�佸凡缁忔槸AWAIT (196)锛屽啀娆$偣鍑诲畬缁撴椂锛岀洿鎺ュ畬鎴�
+        if (task.getTaskStatus().equals(TaskStsType.AWAIT.id)) {
+            // AWAIT鐘舵�佺殑浠诲姟鍐嶆瀹岀粨锛岀洿鎺ヨ缃负鍑哄簱瀹屾垚
+            task.setTaskStatus(TaskStsType.COMPLETE_OUT.id);
+            
+            // 鏇存柊鍑哄簱绔欑偣鐘舵�侊紙涓嶳CS閫氱煡瀹岀粨淇濇寔涓�鑷达級
+            if (task.getTaskType() >= TaskType.TASK_TYPE_OUT.type && StringUtils.isNotBlank(task.getTargSite())) {
+                BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
+                        .eq(BasStation::getStationName, task.getTargSite()));
+                if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
+                    station.setUseStatus(LocStsType.LOC_STS_TYPE_F.type);
+                    if (!basStationService.updateById(station)) {
+                        throw new CoolException("鍑哄簱绔欑偣鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+                    }
+                }
+            }
+        } else {
+            // 鍏朵粬鎯呭喌鎸夊師鏈夐�昏緫澶勭悊
+            // 鍏ュ簱浠诲姟锛坱askType < 100锛夛細璁剧疆涓哄叆搴撳畬鎴�
+            // 鍑哄簱浠诲姟锛坱askType >= 100锛夛細璁剧疆涓虹瓑寰呯‘璁�
+            Integer newStatus = task.getTaskType() < 100 ? TaskStsType.COMPLETE_IN.id : TaskStsType.AWAIT.id;
+            task.setTaskStatus(newStatus);
+            
+            // 濡傛灉鏄叆搴撲换鍔″畬鎴愶紝鏇存柊鍏ュ簱绔欑偣鐘舵�侊紙涓嶳CS閫氱煡瀹岀粨淇濇寔涓�鑷达級
+            if (newStatus.equals(TaskStsType.COMPLETE_IN.id) && StringUtils.isNotBlank(task.getOrgSite())) {
+                BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
+                        .eq(BasStation::getStationName, task.getOrgSite()));
+                if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
+                    station.setUseStatus(LocStsType.LOC_STS_TYPE_O.type);
+                    if (!basStationService.updateById(station)) {
+                        throw new CoolException("鍏ュ簱绔欑偣鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+                    }
+                }
+            }
+        }
+        
         if (!this.updateById(task)) {
             throw new CoolException("瀹屾垚浠诲姟澶辫触");
         }
+        return task;
+    }
+
+    /**
+     * 鍏ㄧ増鍑哄簱瀹岀粨锛氭墸闄ゅ簱浣嶆暟閲忥紝灏嗗簱浣嶇姸鎬佽涓虹┖
+     *
+     * @param id 浠诲姟ID
+     * @param loginUserId 鐧诲綍鐢ㄦ埛ID
+     * @return 浠诲姟瀵硅薄
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Task completeFullOutStock(Long id, Long loginUserId) {
+        // 鏌ヨ浠诲姟
+        Task task = taskService.getOne(new LambdaQueryWrapper<Task>()
+                .eq(Task::getId, id));
+
+        if (Objects.isNull(task)) {
+            throw new CoolException("浠诲姟涓嶅瓨鍦紒锛�");
+        }
+
+        // 妫�鏌ヤ换鍔$被鍨嬫槸鍚︿负鍏ㄧ増鍑哄簱
+        if (!task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
+            throw new CoolException("褰撳墠浠诲姟涓嶆槸鍏ㄧ増鍑哄簱浠诲姟锛屾棤娉曟墽琛屾鎿嶄綔锛侊紒");
+        }
+
+        // 妫�鏌ヤ换鍔$姸鎬侊細蹇呴』鏄�199锛圵AVE_SEED锛夌姸鎬佹墠鑳芥墜鍔ㄥ畬缁�
+        if (!task.getTaskStatus().equals(TaskStsType.WAVE_SEED.id)) {
+            throw new CoolException("浠诲姟鐘舵�佷笉鏄瓑寰呯‘璁ょ姸鎬侊紙199锛夛紝鏃犳硶鎵ц姝ゆ搷浣滐紒锛佸綋鍓嶇姸鎬侊細" + task.getTaskStatus());
+        }
+
+        // 鏌ヨ搴撲綅
+        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getOrgLoc()));
+        if (Objects.isNull(loc)) {
+            throw new CoolException("搴撲綅涓嶅瓨鍦紒锛�");
+        }
+
+        // 鍒犻櫎搴撲綅鏄庣粏锛堟墸闄ゆ暟閲忥級
+        try {
+            subtractLocItem(loc);
+        } catch (Exception e) {
+            logger.error("鍒犻櫎搴撲綅鏄庣粏澶辫触", e);
+            throw new CoolException("鍒犻櫎搴撲綅鏄庣粏澶辫触锛�" + e.getMessage());
+        }
+
+        // 鍒犻櫎浣滀笟涓簱瀛樿褰曪紙LocItemWorking锛�
+        locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>()
+                .eq(LocItemWorking::getTaskId, task.getId()));
+
+        // 灏嗗簱浣嶇姸鎬佽涓虹┖锛圤鐘舵�侊級
+        if (!locService.update(new LambdaUpdateWrapper<Loc>()
+                .set(Loc::getBarcode, null)
+                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
+                .set(Loc::getUpdateBy, loginUserId)
+                .set(Loc::getUpdateTime, new Date())
+                .eq(Loc::getCode, task.getOrgLoc()))) {
+            throw new CoolException("搴撲綅鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+        }
+
+        // 鏇存柊鍑哄簱绔欑偣鐘舵�侊紙濡傛灉鏈夌洰鏍囩珯鐐癸級
+        if (StringUtils.isNotBlank(task.getTargSite())) {
+            BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
+                    .eq(BasStation::getStationName, task.getTargSite()));
+            if (Objects.nonNull(station) && station.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
+                station.setUseStatus(LocStsType.LOC_STS_TYPE_F.type);
+                if (!basStationService.updateById(station)) {
+                    throw new CoolException("鍑哄簱绔欑偣鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+                }
+            }
+        }
+
+        // 鏇存柊浠诲姟鐘舵�佷负搴撳瓨鏇存柊瀹屾垚锛�200锛�
+        task.setTaskStatus(TaskStsType.UPDATED_OUT.id)
+                .setUpdateBy(loginUserId)
+                .setUpdateTime(new Date());
+
+        if (!this.updateById(task)) {
+            throw new CoolException("浠诲姟鐘舵�佹洿鏂板け璐ワ紒锛�");
+        }
+
         return task;
     }
 
@@ -728,30 +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(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());
-            }
-            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();
@@ -828,11 +968,19 @@
         // 濡傛灉鏈変换鍔″凡涓嬪彂鍒癛CS锛屽厛璋冪敤RCS鍙栨秷鎺ュ彛
         boolean rcsCancelSuccess = false;
         if (!rcsTaskCodes.isEmpty()) {
-            try {
-                log.info("========== 寮�濮嬪彇娑圧CS浠诲姟 ==========");
-                log.info("闇�瑕佸彇娑堢殑RCS浠诲姟缂栧彿锛歿}", rcsTaskCodes);
-                String rcsUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.cancelTask;
-                log.info("RCS鍙栨秷浠诲姟璇锋眰鍦板潃锛歿}", rcsUrl);
+            // 妫�鏌� RCS API 閰嶇疆鏄惁鏈夋晥
+            if (rcsApi == null || StringUtils.isBlank(rcsApi.getHost()) || StringUtils.isBlank(rcsApi.getPort())) {
+                log.error("========== RCS浠诲姟鍙栨秷澶辫触 ==========");
+                log.error("RCS API 閰嶇疆鏃犳晥锛乭ost: {}, port: {}", 
+                        rcsApi != null ? rcsApi.getHost() : "null", 
+                        rcsApi != null ? rcsApi.getPort() : "null");
+                // 鍗充娇閰嶇疆鏃犳晥锛屼篃缁х画鎵ц浠诲姟鍒犻櫎鎿嶄綔
+            } else {
+                try {
+                    log.info("========== 寮�濮嬪彇娑圧CS浠诲姟 ==========");
+                    log.info("闇�瑕佸彇娑堢殑RCS浠诲姟缂栧彿锛歿}", rcsTaskCodes);
+                    String rcsUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.cancelTask;
+                    log.info("RCS鍙栨秷浠诲姟璇锋眰鍦板潃锛歿}", rcsUrl);
                 
                 // 濡傛灉娌℃湁鎵规缂栧彿锛屼娇鐢ㄧ涓�涓换鍔$紪鍙蜂綔涓烘壒娆$紪鍙�
                 if (StringUtils.isBlank(batchNo) && !rcsTaskCodes.isEmpty()) {
@@ -878,12 +1026,13 @@
                     log.error("RCS鍙栨秷浠诲姟澶辫触锛歿}", result.getMsg());
                     throw new CoolException("RCS鍙栨秷浠诲姟澶辫触锛�" + result.getMsg());
                 }
-            } catch (JsonProcessingException e) {
-                log.error("RCS鍙栨秷浠诲姟鍝嶅簲瑙f瀽澶辫触锛歿}", e.getMessage(), e);
-                throw new CoolException("RCS鍙栨秷浠诲姟鍝嶅簲瑙f瀽澶辫触锛�" + e.getMessage());
-            } catch (Exception e) {
-                log.error("RCS鍙栨秷浠诲姟寮傚父锛歿}", e.getMessage(), e);
-                throw new CoolException("RCS鍙栨秷浠诲姟寮傚父锛�" + e.getMessage());
+                } catch (JsonProcessingException e) {
+                    log.error("RCS鍙栨秷浠诲姟鍝嶅簲瑙f瀽澶辫触锛歿}", e.getMessage(), e);
+                    throw new CoolException("RCS鍙栨秷浠诲姟鍝嶅簲瑙f瀽澶辫触锛�" + e.getMessage());
+                } catch (Exception e) {
+                    log.error("RCS鍙栨秷浠诲姟寮傚父锛歿}", e.getMessage(), e);
+                    throw new CoolException("RCS鍙栨秷浠诲姟寮傚父锛�" + e.getMessage());
+                }
             }
         }
         
@@ -1146,7 +1295,7 @@
 //        if (Objects.isNull(locInfo)) {
 //            throw new CoolException("鑾峰彇搴撲綅澶辫触锛侊紒");
 //        }
-        //甯屾棩涓婃姤鐗╂湁鎯呭喌锛屼笉闇�瑕佽幏鍙栨柊搴撲綅
+        //涓婃姤鐗╂湁鎯呭喌锛屼笉闇�瑕佽幏鍙栨柊搴撲綅
         task.setTargLoc(task.getOrgLoc())
                 .setOrgSite(task.getTargSite());
 
@@ -1155,27 +1304,96 @@
         }
         //鑾峰彇鍥犲綋鍓嶄换鍔″嚭搴撶殑鎵�鏈夌墿鏂欎俊鎭�
         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枡鍏ュ簱锛氬厛绠楀墿浣欐暟閲忓苟鏇存柊 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());
+        }
 
         tempLocs.forEach(working -> {
             taskItems.forEach(taskItem -> {
-                if (taskItem.getFieldsIndex().equals(working.getFieldsIndex())) {
+                if (Objects.equals(taskItem.getFieldsIndex(), working.getFieldsIndex())) {
                     Double minQty = taskItem.getAnfme();
                     if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
-                        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鐨勫墿浣欐暟閲忥紙闈炵洏鐐瑰叆搴撴椂闇�瑕佹洿鏂帮級锛涗粎鎸佷箙鍖栬褰曟墠鍐欏簱
+                        if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
+                            working.setAnfme(minQty);
+                            if (working.getId() != null && !locItemWorkingService.updateById(working)) {
+                                throw new CoolException("浣滀笟搴撳瓨鏁伴噺鏇存柊澶辫触锛侊紒");
+                            }
+                        }
                     } else {
+                        // 鍓╀綑鏁伴噺灏忎簬0锛屽垹闄や换鍔℃槑缁�
                         if (!taskItemService.removeById(taskItem)) {
                             log.error("浠诲姟鏄庣粏淇敼澶辫触锛侊紒");
                         }
@@ -1183,10 +1401,13 @@
                 }
             });
         });
-        List<String> matnrIds = taskItems.stream().map(TaskItem::getFieldsIndex).collect(Collectors.toList());
+        List<String> matnrIds = taskItems.stream()
+                .map(TaskItem::getFieldsIndex)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
         //鍒犻櫎涓庝换鍔℃槑缁嗛噸澶嶇殑搴撳瓨淇℃伅锛屼互浠诲姟鏄庣粏涓哄噯
         List<LocItemWorking> itemWorkings = tempLocs.stream()
-                .filter(working -> !matnrIds.contains(working.getFieldsIndex()))
+                .filter(working -> working.getFieldsIndex() != null && !matnrIds.contains(working.getFieldsIndex()))
                 .collect(Collectors.toList());
         itemWorkings.forEach(working -> {
             TaskItem taskItem = taskItems.stream().findFirst().get();
@@ -1222,6 +1443,10 @@
                     .setQty(0.0)
                     .setLocId(loc1.getId())
                     .setLocCode(loc1.getCode());
+            // 鎷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);
         });
 
@@ -1254,42 +1479,67 @@
         if (Objects.isNull(loc)) {
             throw new CoolException("搴撲綅涓嶅瓨鍦紒锛�");
         }
-        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
-            throw new CoolException("搴撲綅鐘舵�佷笉澶勭悊浜嶳.鍑哄簱棰勭害锛侊紒");
-        }
-
+        
         List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
         if (taskItems.isEmpty()) {
             throw new CoolException("浠诲姟鏄庣粏涓嶅瓨鍦紒锛�");
         }
 
         List<LocItem> locItems = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
-        if (locItems.isEmpty()) {
-            throw new CoolException("搴撲綅鏄庣粏涓嶅瓨鍦紒锛�");
+        
+        // 濡傛灉搴撲綅鐘舵�佷笉鏄疪锛屾鏌ユ槸鍚﹀凡缁忓鐞嗚繃
+        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_R.type)) {
+            // 濡傛灉搴撲綅鏄庣粏涓虹┖锛岃鏄庡凡缁忓鐞嗚繃浜嗭紝鐩存帴鏇存柊浠诲姟鐘舵�佷负199
+            if (locItems.isEmpty()) {
+                logger.warn("浠诲姟{}鐨勫簱浣峽}鐘舵�佷负{}锛屼絾搴撲綅鏄庣粏涓虹┖锛屽彲鑳藉凡缁忓鐞嗚繃锛岀洿鎺ユ洿鏂颁换鍔$姸鎬佷负199", 
+                        task.getId(), loc.getCode(), loc.getUseStatus());
+                if (!this.update(new LambdaUpdateWrapper<Task>()
+                        .eq(Task::getId, task.getId())
+                        .set(Task::getUpdateBy, loginUserId)
+                        .set(Task::getUpdateTime, new Date())
+                        .set(Task::getTaskStatus, TaskStsType.WAVE_SEED.id))) {
+                    throw new CoolException("浠诲姟鐘舵�佹洿鏂板け璐ワ紒锛�");
+                }
+                return; // 璺宠繃鍚庣画澶勭悊
+            } else {
+                // 搴撲綅鏄庣粏涓嶄负绌轰絾鐘舵�佷笉鏄疪锛岃烦杩囧鐞�
+                logger.error("浠诲姟{}鐨勫簱浣峽}鐘舵�佷负{}锛屼笉鏄疪.鍑哄簱棰勭害鐘舵�侊紝浣嗗簱浣嶆槑缁嗕笉涓虹┖锛岃烦杩囧鐞嗐�備换鍔$紪鐮侊細{}锛屽簱浣嶇紪鐮侊細{}", 
+                        task.getId(), loc.getCode(), loc.getUseStatus(), task.getTaskCode(), loc.getCode());
+                return;
+            }
         }
+        
+        // 濡傛灉搴撲綅鏄庣粏涓虹┖锛屽彲鑳芥槸宸茬粡琚鐞嗚繃浜嗭紝鍏佽缁х画鎵ц
+        if (!locItems.isEmpty()) {
+            List<LocItemWorking> workings = new ArrayList<>();
+            for (LocItem item : locItems) {
+                LocItemWorking working = new LocItemWorking();
+                BeanUtils.copyProperties(item, working);
+                working.setId(null)
+                        .setTaskId(task.getId())
+                        .setLocItemId(item.getId())
+                        .setUpdateBy(loginUserId)
+                        .setUpdateTime(new Date());
+                workings.add(working);
+            }
 
-        List<LocItemWorking> workings = new ArrayList<>();
-        for (LocItem item : locItems) {
-            LocItemWorking working = new LocItemWorking();
-            BeanUtils.copyProperties(item, working);
-            working.setId(null)
-                    .setTaskId(task.getId())
-                    .setLocItemId(item.getId())
-                    .setUpdateBy(loginUserId)
-                    .setUpdateTime(new Date());
-            workings.add(working);
-        }
+            if (!workings.isEmpty() && !locItemWorkingService.saveBatch(workings)) {
+                throw new CoolException("涓存椂搴撳瓨淇濆瓨澶辫触锛侊紒");
+            }
 
-        if (!locItemWorkingService.saveBatch(workings)) {
-            throw new CoolException("涓存椂搴撳瓨淇濆瓨澶辫触锛侊紒");
-        }
-
-        try {
-            //鏇存柊搴撲綅鏄庣粏
-            subtractLocItem(loc);
-        } catch (Exception e) {
-            logger.error("<UNK>", e);
-            throw new CoolException(e.getMessage());
+            try {
+                // 鏍规嵁浠诲姟绫诲瀷鏇存柊搴撲綅鏄庣粏
+                if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
+                    // 鍏ㄧ増鍑哄簱锛氫笉鍒犻櫎搴撲綅鏄庣粏锛岀瓑寰匬DA蹇�熸嫞璐х‘璁ゆ椂鍐嶅垹闄�
+                    // subtractLocItem(loc); // 宸茬Щ闄わ紝鏀逛负鍦╟ompleteFullOutStock涓垹闄�
+                } else if (!TaskType.TASK_TYPE_PICK_AGAIN_OUT.type.equals(task.getTaskType())) {
+                    // 閮ㄥ垎鍑哄簱锛堝鐩樼偣鍑哄簱锛夛細鏍规嵁TaskItem鏁伴噺鎵e噺搴撲綅鏄庣粏锛涙嫞鏂欏嚭搴撳湪鐢熸垚鎷f枡鍏ュ簱鍗曟椂鎵e噺
+                    subtractLocItemByTaskItems(loc, taskItems, loginUserId);
+                }
+            } catch (Exception e) {
+                logger.error("<UNK>", e);
+                throw new CoolException(e.getMessage());
+            }
         }
 
         //娣诲姞鍑哄叆搴撹褰曚俊鎭�
@@ -1343,6 +1593,8 @@
                 throw new CoolException(e.getMessage());
             }
         }
+        
+        // 鏍规嵁浠诲姟绫诲瀷鏇存柊搴撲綅鐘舵��
         if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) {
             /**淇敼涓哄簱浣嶇姸鎬佷负S.棰勭害鍏ュ簱锛屼繚鐣欏師鏈夊簱浣�*/
             if (!locService.update(new LambdaUpdateWrapper<Loc>()
@@ -1353,6 +1605,9 @@
                     .eq(Loc::getId, loc.getId()))) {
                 throw new CoolException("搴撲綅鐘舵�佷慨鏀瑰け璐ワ紒锛�");
             }
+        } else if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
+            // 鍏ㄧ増鍑哄簱锛氫笉鏇存柊搴撲綅鐘舵�佷负O锛岀瓑寰匬DA蹇�熸嫞璐х‘璁ゆ椂鍐嶆洿鏂�
+            // 搴撲綅鐘舵�佷繚鎸佸師鏍凤紙R.鍑哄簱棰勭害鐘舵�侊級
         } else {
             /**淇敼涓哄簱浣嶇姸鎬佷负O.绌哄簱*/
             if (!locService.update(new LambdaUpdateWrapper<Loc>()
@@ -1517,6 +1772,15 @@
     @Transactional(rollbackFor = Exception.class)
     public void pubTaskToWcs(List<Task> tasks) {
         /**浠诲姟涓嬪彂鎺ュ彛*/
+        // 妫�鏌� RCS API 閰嶇疆鏄惁鏈夋晥
+        if (rcsApi == null || StringUtils.isBlank(rcsApi.getHost()) || StringUtils.isBlank(rcsApi.getPort())) {
+            log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+            log.error("RCS API 閰嶇疆鏃犳晥锛乭ost: {}, port: {}", 
+                    rcsApi != null ? rcsApi.getHost() : "null", 
+                    rcsApi != null ? rcsApi.getPort() : "null");
+            return;
+        }
+        
         String pubTakUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.pubTask;
         
         for (Task task : tasks) {
@@ -1541,7 +1805,9 @@
             if (!task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                 station = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite()));
                 if (Objects.isNull(station)) {
-                    throw new CoolException("绔欑偣涓嶅瓨鍦紒锛�");
+                    log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                    log.error("绔欑偣涓嶅瓨鍦紒锛佷换鍔$紪鐮侊細{}锛岀洰鏍囩珯鐐癸細{}", task.getTaskCode(), task.getTargSite());
+                    continue;
                 }
             }
 
@@ -1550,12 +1816,16 @@
                 if (task.getTaskType() <= TaskType.TASK_TYPE_CHECK_IN.type && !task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                     station.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
                     if (!basStationService.updateById(station)) {
-                        throw new CoolException("绔欑偣鐘舵�佹洿鏂板け璐ワ紒锛�");
+                        log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                        log.error("绔欑偣鐘舵�佹洿鏂板け璐ワ紒锛佷换鍔$紪鐮侊細{}锛岀珯鐐癸細{}", task.getTaskCode(), station.getStationName());
+                        continue;
                     }
                 } else if (task.getTaskType() >= TaskType.TASK_TYPE_OUT.type) {
                     station.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
                     if (!basStationService.updateById(station)) {
-                        throw new CoolException("绔欑偣鐘舵�佹洿鏂板け璐ワ紒锛�");
+                        log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                        log.error("绔欑偣鐘舵�佹洿鏂板け璐ワ紒锛佷换鍔$紪鐮侊細{}锛岀珯鐐癸細{}", task.getTaskCode(), station.getStationName());
+                        continue;
                     }
                 }
             }
@@ -1578,34 +1848,95 @@
                     || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)
                     || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)
                     || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_OUT.type)) {
-                /**鍑哄簱鍙傛暟*/
-                itemParam.setOriLoc(task.getOrgLoc())
-                        .setDestSta(task.getTargSite());
+                /**鍑哄簱鍙傛暟 - 闇�瑕佹牴鎹换鍔℃槑缁嗕腑鐨勫涓簱浣嶈繘琛岀粍瑁�*/
+                // 鏌ヨ浠诲姟鏄庣粏
+                List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
+                if (!taskItems.isEmpty()) {
+                    // 閫氳繃TaskItem鐨剆ource瀛楁鏌ヨLocItem锛岃幏鍙栧簱浣嶄俊鎭�
+                    Set<String> locCodes = new HashSet<>();
+                    Map<String, LocItem> locItemMap = new HashMap<>();
+                    
+                    for (TaskItem taskItem : taskItems) {
+                        if (taskItem.getSource() != null) {
+                            // source瀛楁瀵瑰簲LocItem鐨刬d
+                            LocItem locItem = locItemService.getById(taskItem.getSource());
+                            if (locItem != null && StringUtils.isNotBlank(locItem.getLocCode())) {
+                                locCodes.add(locItem.getLocCode());
+                                locItemMap.put(locItem.getLocCode(), locItem);
+                            }
+                        }
+                    }
+                    
+                    // 濡傛灉閫氳繃source娌℃湁鎵惧埌搴撲綅锛屼娇鐢═ask鐨刼rgLoc浣滀负榛樿鍊�
+                    if (locCodes.isEmpty() && StringUtils.isNotBlank(task.getOrgLoc())) {
+                        locCodes.add(task.getOrgLoc());
+                    }
+                    
+                    // 涓烘瘡涓笉鍚岀殑搴撲綅鍒涘缓涓�涓猅askItemParam
+                    for (String locCode : locCodes) {
+                        TaskItemParam outItemParam = new TaskItemParam();
+                        String taskNo = locCodes.size() > 1
+                                ? task.getTaskCode() + "_" + locCode 
+                                : task.getTaskCode();
+                        outItemParam.setTaskNo(taskNo);
+                        outItemParam.setPriority(1);
+                        outItemParam.setOriLoc(locCode);
+                        outItemParam.setDestSta(task.getTargSite());
+                        if (task.getBarcode() != null) {
+                            outItemParam.setZpallet(task.getBarcode());
+                        }
+                        items.add(outItemParam);
+                    }
+                    
+                    log.info("鍑哄簱浠诲姟鍖呭惈{}涓簱浣嶏細{}", locCodes.size(), locCodes);
+                } else {
+                    // 濡傛灉娌℃湁浠诲姟鏄庣粏锛屼娇鐢═ask鐨刼rgLoc
+                    itemParam.setOriLoc(task.getOrgLoc())
+                            .setDestSta(task.getTargSite());
+                    items.add(itemParam);
+                }
             } else {
                 /**绔欑偣闂寸Щ搴撳弬鏁�*/
                 itemParam.setOriSta(task.getOrgSite()).setDestSta(task.getTargSite());
 
                 BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getOrgSite()));
                 if (Objects.isNull(curSta)) {
-                    throw new CoolException("绔欑偣涓嶅瓨鍦紒锛�");
+                    log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                    log.error("绔欑偣涓嶅瓨鍦紒锛佷换鍔$紪鐮侊細{}锛屾簮绔欑偣锛歿}", task.getTaskCode(), task.getOrgSite());
+                    continue;
                 }
                 if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                     if (!curSta.getUseStatus().equals(LocStsType.LOC_STS_TYPE_F.type)) {
-                        throw new CoolException("褰撳墠绔欑偣涓嶆槸F.鍦ㄥ簱鐘舵�侊紒锛�");
+                        log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                        log.error("褰撳墠绔欑偣涓嶆槸F.鍦ㄥ簱鐘舵�侊紒锛佷换鍔$紪鐮侊細{}锛岀珯鐐癸細{}锛屽綋鍓嶇姸鎬侊細{}", task.getTaskCode(), curSta.getStationName(), curSta.getUseStatus());
+                        continue;
                     }
                 }
                 // 绔欑偣闂寸Щ搴撻渶瑕佽幏鍙栫洰鏍囩珯鐐�
                 BasStation targetStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite()));
                 if (Objects.isNull(targetStation)) {
-                    throw new CoolException("鐩爣绔欑偣涓嶅瓨鍦紒锛�");
+                    log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                    log.error("鐩爣绔欑偣涓嶅瓨鍦紒锛佷换鍔$紪鐮侊細{}锛岀洰鏍囩珯鐐癸細{}", task.getTaskCode(), task.getTargSite());
+                    continue;
                 }
                 if (targetStation.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                     if (!targetStation.getUseStatus().equals(LocStsType.LOC_STS_TYPE_O.type)) {
-                        throw new CoolException("鐩爣绔欑偣涓嶆槸O.绌洪棽鐘舵�侊紒锛�");
+                        log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                        log.error("鐩爣绔欑偣涓嶆槸O.绌洪棽鐘舵�侊紒锛佷换鍔$紪鐮侊細{}锛岀珯鐐癸細{}锛屽綋鍓嶇姸鎬侊細{}", task.getTaskCode(), targetStation.getStationName(), targetStation.getUseStatus());
+                        continue;
                     }
                 }
             }
-            items.add(itemParam);
+            
+            // 瀵逛簬闈炲嚭搴撲换鍔★紝娣诲姞鍗曚釜itemParam
+            if (!(task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)
+                    || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)
+                    || task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)
+                    || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)
+                    || task.getTaskType().equals(TaskType.TASK_TYPE_MERGE_OUT.type))) {
+                items.add(itemParam);
+            }
+            
             taskParams.setTasks(items);
             
             // 璁板綍褰撳墠浠诲姟淇℃伅
@@ -1636,7 +1967,7 @@
             } catch (org.springframework.web.client.ResourceAccessException e) {
                 long endTime = System.currentTimeMillis();
                 log.error("========== RCS浠诲姟涓嬪彂璧勬簮璁块棶寮傚父 ==========");
-                log.error("璇锋眰RCS-璧勬簮璁块棶寮傚父锛堝彲鑳藉寘鍚繛鎺ヨ秴鏃讹級锛岃�楁椂锛歿}ms锛屼换鍔$紪鐮侊細{}", (endTime - startTime), task.getTaskCode(), e);
+                log.error("璇锋眰RCS-璧勬簮璁块棶寮傚父锛岃�楁椂锛歿}ms锛屼换鍔$紪鐮侊細{}", (endTime - startTime), task.getTaskCode(), e);
                 log.error("璇锋眰RCS-璇锋眰鍦板潃锛歿}", pubTakUrl);
                 log.error("璇锋眰RCS-璇锋眰鍙傛暟锛歿}", JSONObject.toJSONString(taskParams));
                 // 妫�鏌ユ槸鍚︽槸杩炴帴瓒呮椂寮傚父
@@ -1645,21 +1976,24 @@
                 if (cause instanceof java.net.SocketTimeoutException || 
                     (cause instanceof java.net.ConnectException && cause.getMessage() != null && cause.getMessage().contains("timed out")) ||
                     (errorMsg != null && (errorMsg.contains("Connection timed out") || errorMsg.contains("timed out") || errorMsg.contains("timeout")))) {
-                    throw new CoolException("RCS杩炴帴瓒呮椂锛屼换鍔′笅鍙戝け璐ワ紒浠诲姟缂栫爜锛�" + task.getTaskCode() + "锛岄敊璇俊鎭細" + errorMsg);
+                    log.error("RCS杩炴帴瓒呮椂锛屼换鍔′笅鍙戝け璐ワ紒浠诲姟缂栫爜锛歿}锛岄敊璇俊鎭細{}", task.getTaskCode(), errorMsg);
+                } else {
+                    log.error("RCS璧勬簮璁块棶寮傚父锛屼换鍔′笅鍙戝け璐ワ紒浠诲姟缂栫爜锛歿}锛岄敊璇俊鎭細{}", task.getTaskCode(), errorMsg);
                 }
-                throw new CoolException("RCS璧勬簮璁块棶寮傚父锛屼换鍔′笅鍙戝け璐ワ紒浠诲姟缂栫爜锛�" + task.getTaskCode() + "锛岄敊璇俊鎭細" + errorMsg);
+                continue;
             } catch (Exception e) {
                 long endTime = System.currentTimeMillis();
                 log.error("========== RCS浠诲姟涓嬪彂寮傚父 ==========");
-                log.error("璇锋眰RCS-寮傚父锛岃�楁椂锛歿}ms锛屼换鍔$紪鐮侊細{}", (endTime - startTime), task.getTaskCode(), e);
-                log.error("璇锋眰RCS-鍦板潃锛歿}", pubTakUrl);
-                log.error("璇锋眰RCS-鍙傛暟锛歿}", JSONObject.toJSONString(taskParams));
                 String errorMsg = e.getMessage();
                 // 妫�鏌ユ槸鍚︽槸杩炴帴瓒呮椂鐩稿叧鐨勫紓甯�
                 if (errorMsg != null && (errorMsg.contains("Connection timed out") || errorMsg.contains("timed out") || errorMsg.contains("timeout"))) {
-                    throw new CoolException("RCS杩炴帴瓒呮椂锛屼换鍔′笅鍙戝け璐ワ紒浠诲姟缂栫爜锛�" + task.getTaskCode() + "锛岄敊璇俊鎭細" + errorMsg);
+                log.error("璇锋眰RCS-杩炴帴瓒呮椂寮傚父锛岃�楁椂锛歿}ms锛屼换鍔$紪鐮侊細{}锛岄敊璇俊鎭細{}-{}", (endTime - startTime), task.getTaskCode(), e, errorMsg);
+                } else {
+                log.error("璇锋眰RCS-寮傚父锛岃�楁椂锛歿}ms锛屼换鍔$紪鐮侊細{}锛岄敊璇俊鎭細{}-{}", (endTime - startTime), task.getTaskCode(), e, errorMsg);
                 }
-                throw new CoolException("RCS浠诲姟涓嬪彂寮傚父锛佷换鍔$紪鐮侊細" + task.getTaskCode() + "锛岄敊璇俊鎭細" + errorMsg);
+                log.error("璇锋眰RCS-鍦板潃锛歿}", pubTakUrl);
+                log.error("璇锋眰RCS-鍙傛暟锛歿}", JSONObject.toJSONString(taskParams));
+                continue;
             }
             
             if (Objects.isNull(exchange) || Objects.isNull(exchange.getBody())) {
@@ -1668,7 +2002,8 @@
                 log.error("璇锋眰RCS-鍦板潃锛歿}", pubTakUrl);
                 log.error("璇锋眰RCS-鍙傛暟锛歿}", JSONObject.toJSONString(taskParams));
                 log.error("璇锋眰RCS-澶辫触鐨勪换鍔$紪鐮侊細{}", task.getTaskCode());
-                throw new CoolException("浠诲姟涓嬪彂澶辫触锛孯CS鍝嶅簲浣撲负绌猴紒锛佷换鍔$紪鐮侊細" + task.getTaskCode());
+                log.error("浠诲姟涓嬪彂澶辫触锛孯CS鍝嶅簲浣撲负绌猴紒锛佷换鍔$紪鐮侊細{}", task.getTaskCode());
+                continue;
             } else {
                 try {
                     ObjectMapper objectMapper = new ObjectMapper();
@@ -1688,13 +2023,17 @@
 
                             BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getOrgSite()));
                             if (Objects.isNull(curSta)) {
-                                throw new CoolException("绔欑偣涓嶅瓨鍦紒锛�");
+                                log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                                log.error("绔欑偣涓嶅瓨鍦紒锛佷换鍔$紪鐮侊細{}锛屾簮绔欑偣锛歿}", task.getTaskCode(), task.getOrgSite());
+                                continue;
                             }
 
                             log.info("鏇存柊鍏ュ簱浠诲姟鐘舵�� - 浠诲姟缂栫爜锛歿}锛屾柊鐘舵�侊細{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_IN.id);
                             if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode())
                                     .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_IN.id))) {
-                                throw new CoolException("浠诲姟鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+                                log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                                log.error("浠诲姟鐘舵�佷慨鏀瑰け璐ワ紒锛佷换鍔$紪鐮侊細{}", task.getTaskCode());
+                                continue;
                             }
                             log.info("鍏ュ簱浠诲姟鐘舵�佹洿鏂版垚鍔� - 浠诲姟缂栫爜锛歿}", task.getTaskCode());
                             /**鎺掗櫎绉诲簱鍔熻兘*/
@@ -1703,7 +2042,9 @@
                                 if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                                     curSta.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
                                     if (!basStationService.updateById(curSta)) {
-                                        throw new CoolException("绔欑偣棰勭害澶辫触锛侊紒");
+                                        log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                                        log.error("绔欑偣棰勭害澶辫触锛侊紒浠诲姟缂栫爜锛歿}锛岀珯鐐癸細{}", task.getTaskCode(), curSta.getStationName());
+                                        continue;
                                     }
                                 }
                             }
@@ -1714,20 +2055,26 @@
                                 || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)) {
                             BasStation curSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite()));
                             if (Objects.isNull(curSta)) {
-                                throw new CoolException("绔欑偣涓嶅瓨鍦紒锛�");
+                                log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                                log.error("绔欑偣涓嶅瓨鍦紒锛佷换鍔$紪鐮侊細{}锛岀洰鏍囩珯鐐癸細{}", task.getTaskCode(), task.getTargSite());
+                                continue;
                             }
 
                             log.info("鏇存柊鍑哄簱浠诲姟鐘舵�� - 浠诲姟缂栫爜锛歿}锛屾柊鐘舵�侊細{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_OUT.id);
                             if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode())
                                     .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_OUT.id))) {
-                                throw new CoolException("浠诲姟鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+                                log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                                log.error("浠诲姟鐘舵�佷慨鏀瑰け璐ワ紒锛佷换鍔$紪鐮侊細{}", task.getTaskCode());
+                                continue;
                             }
                             log.info("鍑哄簱浠诲姟鐘舵�佹洿鏂版垚鍔� - 浠诲姟缂栫爜锛歿}", task.getTaskCode());
                             /**濡傛灉鏄櫘閫氱珯鐐癸紝淇敼绔欑偣鐘舵�佷负鍏ュ簱棰勭害*/
                             if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                                 curSta.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
                                 if (!basStationService.updateById(curSta)) {
-                                    throw new CoolException("绔欑偣棰勭害澶辫触锛侊紒");
+                                    log.error("========== RCS浠诲姟涓嬪彂澶辫触 ==========");
+                                    log.error("绔欑偣棰勭害澶辫触锛侊紒浠诲姟缂栫爜锛歿}锛岀珯鐐癸細{}", task.getTaskCode(), curSta.getStationName());
+                                    continue;
                                 }
                             }
                         }
@@ -1736,16 +2083,19 @@
                         log.error("RCS杩斿洖閿欒 - code锛歿}锛宮sg锛歿}锛宒ata锛歿}", 
                                 result.getCode(), result.getMsg(), result.getData());
                         log.error("澶辫触鐨勪换鍔$紪鐮侊細{}锛屼换鍔$被鍨嬶細{}", task.getTaskCode(), task.getTaskType());
-                        throw new CoolException("浠诲姟涓嬪彂澶辫触锛侊紒浠诲姟缂栫爜锛�" + task.getTaskCode());
+                        log.error("浠诲姟涓嬪彂澶辫触锛侊紒浠诲姟缂栫爜锛歿}", task.getTaskCode());
+                        continue;
                     }
                 } catch (JsonProcessingException e) {
                     log.error("========== RCS浠诲姟涓嬪彂寮傚父 ==========");
                     log.error("瑙f瀽RCS鍝嶅簲澶辫触锛屽搷搴斾綋锛歿}锛屼换鍔$紪鐮侊細{}", exchange.getBody(), task.getTaskCode(), e);
-                    throw new CoolException("瑙f瀽RCS鍝嶅簲澶辫触锛�" + e.getMessage() + "锛屼换鍔$紪鐮侊細" + task.getTaskCode());
+                    log.error("瑙f瀽RCS鍝嶅簲澶辫触锛歿}锛屼换鍔$紪鐮侊細{}", e.getMessage(), task.getTaskCode());
+                    continue;
                 } catch (Exception e) {
                     log.error("========== RCS浠诲姟涓嬪彂寮傚父 ==========");
                     log.error("浠诲姟涓嬪彂杩囩▼涓彂鐢熷紓甯革紝浠诲姟缂栫爜锛歿}", task.getTaskCode(), e);
-                    throw e;
+                    log.error("浠诲姟涓嬪彂寮傚父锛歿}锛屼换鍔$紪鐮侊細{}", e.getMessage(), task.getTaskCode());
+                    continue;
                 }
             }
         }
@@ -1756,13 +2106,71 @@
     /**
      * @author Ryan
      * @date 2025/5/20
-     * @description: 鎵e噺搴撳瓨鏄庣粏
+     * @description: 鎵e噺搴撳瓨鏄庣粏锛堝叏鐗堝嚭搴擄細鍒犻櫎鎵�鏈夊簱浣嶆槑缁嗭級
      * @version 1.0
      */
     @Transactional(rollbackFor = Exception.class)
     public void subtractLocItem(Loc loc) throws Exception {
-        if (!locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()))) {
-            throw new CoolException("搴撳瓨鏄庣粏鍒犻櫎澶辫触锛侊紒");
+        // 鍒犻櫎搴撲綅鏄庣粏锛屽鏋滄病鏈夎褰曞垯蹇界暐锛堝彲鑳藉凡缁忚鍒犻櫎杩囦簡锛�
+        locItemService.remove(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
+    }
+
+    /**
+     * 鏍规嵁浠诲姟鏄庣粏鎵e噺搴撲綅鏄庣粏鏁伴噺锛堥儴鍒嗗嚭搴擄級
+     *
+     * @param loc 搴撲綅
+     * @param taskItems 浠诲姟鏄庣粏鍒楄〃
+     * @param loginUserId 鐧诲綍鐢ㄦ埛ID
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void subtractLocItemByTaskItems(Loc loc, List<TaskItem> taskItems, Long loginUserId) {
+        for (TaskItem taskItem : taskItems) {
+            LambdaQueryWrapper<LocItem> locItemWrapper = new LambdaQueryWrapper<LocItem>()
+                    .eq(LocItem::getLocId, loc.getId())
+                    .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噺閲忥細浼樺厛鐢ㄦ湰娆℃嫞鏂欐暟閲� 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 {
+                    // 鏇存柊搴撲綅鏄庣粏鏁伴噺
+                    locItem.setAnfme(newAnfme)
+                            .setUpdateBy(loginUserId)
+                            .setUpdateTime(new Date());
+                    if (!locItemService.updateById(locItem)) {
+                        throw new CoolException("搴撲綅鏄庣粏鏁伴噺鎵e噺澶辫触锛侊紒");
+                    }
+                }
+            } else {
+                log.warn("[鎷f枡鍏ュ簱-鎵e噺搴撲綅] 鏈煡鍒板簱浣嶆槑缁� locId={}, matnrId={}, batch={}, fieldsIndex={}锛屾湭鎵e噺",
+                        loc.getId(), taskItem.getMatnrId(), taskItem.getBatch(), taskItem.getFieldsIndex());
+            }
         }
     }
 
@@ -1805,8 +2213,10 @@
             List<TaskItem> items = orderMap.get(key);
             //淇濆瓨鍏ュ嚭搴撴槑缁�
             saveStockItems(items, task, pakinItem.getId(), pakinItem.getAsnCode(), pakinItem.getWkType(), pakinItem.getType(), loginUserId);
-            //绉诲嚭鏀惰揣鍖哄簱瀛橈紝 淇敼缁勬墭鐘舵��
-            removeReceiptStock(pakinItem, loginUserId);
+            //绉诲嚭鏀惰揣鍖哄簱瀛橈紝 淇敼缁勬墭鐘舵�侊紙鍙湁褰搒ource涓嶄负null鏃舵墠闇�瑕佺Щ闄ゆ敹璐у尯搴撳瓨锛�
+            if (Objects.nonNull(pakinItem.getSource())) {
+                removeReceiptStock(pakinItem, loginUserId);
+            }
         });
 
         Set<Long> pkinItemIds = taskItems.stream().map(TaskItem::getSource).collect(Collectors.toSet());
@@ -1842,6 +2252,10 @@
      */
     @Transactional(rollbackFor = Exception.class)
     public synchronized void removeReceiptStock(WaitPakinItem pakinItem, Long loginUserId) {
+        // 濡傛灉source涓簄ull锛岃鏄庣粍鎵樻槑缁嗕笉鍦ㄦ敹璐у尯锛屾棤闇�绉婚櫎鏀惰揣鍖哄簱瀛�
+        if (Objects.isNull(pakinItem.getSource())) {
+            return;
+        }
         WarehouseAreasItem itemServiceOne = warehouseAreasItemService.getOne(new LambdaQueryWrapper<WarehouseAreasItem>()
                 .eq(WarehouseAreasItem::getId, pakinItem.getSource()));
         if (Objects.isNull(itemServiceOne)) {
@@ -1885,12 +2299,28 @@
                 throw new CoolException("搴撲綅涓嶅瓨鍦紒锛�");
             }
             LocItem item = new LocItem();
-            LocItem locItem = locItemService.getOne(new LambdaQueryWrapper<LocItem>()
+            // 鏋勫缓鏌ヨ鏉′欢锛氶渶瑕佸悓鏃跺尮閰嶇墿鏂橧D銆佸簱浣岻D銆佹壒娆″拰绁ㄥ彿
+            LambdaQueryWrapper<LocItem> locItemWrapper = new LambdaQueryWrapper<LocItem>()
                     .eq(LocItem::getMatnrId, taskItem.getMatnrId())
-                    .eq(LocItem::getLocId, loc.getId())
-                    .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch())
-                    .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex()));
+                    .eq(LocItem::getLocId, loc.getId());
+            
+            // 鎵规鍖归厤锛氬鏋渢askItem鏈夋壒娆★紝鍒欏繀椤诲尮閰嶏紱濡傛灉taskItem娌℃湁鎵规锛屽垯鏌ヨ鎵规涓簄ull鎴栫┖瀛楃涓茬殑璁板綍
+            if (StringUtils.isNotBlank(taskItem.getBatch())) {
+                locItemWrapper.eq(LocItem::getBatch, taskItem.getBatch());
+            } else {
+                locItemWrapper.and(wrapper -> wrapper.isNull(LocItem::getBatch).or().eq(LocItem::getBatch, ""));
+            }
+            
+            // 绁ㄥ彿鍖归厤锛氬鏋渢askItem鏈夌エ鍙凤紝鍒欏繀椤诲尮閰嶏紱濡傛灉taskItem娌℃湁绁ㄥ彿锛屽垯鏌ヨ绁ㄥ彿涓簄ull鎴栫┖瀛楃涓茬殑璁板綍
+            if (StringUtils.isNotBlank(taskItem.getFieldsIndex())) {
+                locItemWrapper.eq(LocItem::getFieldsIndex, taskItem.getFieldsIndex());
+            } else {
+                locItemWrapper.and(wrapper -> wrapper.isNull(LocItem::getFieldsIndex).or().eq(LocItem::getFieldsIndex, ""));
+            }
+            
+            LocItem locItem = locItemService.getOne(locItemWrapper);
             if (Objects.isNull(locItem)) {
+                // 搴撲綅鏄庣粏涓嶅瓨鍦紝鍒涘缓鏂扮殑搴撲綅鏄庣粏
                 BeanUtils.copyProperties(taskItem, item);
                 item.setLocCode(loc.getCode())
                         .setId(null)
@@ -1902,13 +2332,14 @@
                     throw new CoolException("搴撲綅鏄庣粏鏇存柊澶辫触锛侊紒");
                 }
             } else {
-                logger.error("褰撳墠绁ㄥ彿:"  + locItem.getFieldsIndex()  + " 宸插湪搴撳唴锛岃妫�鏌ュ悗鍐嶆搷浣滐紒锛�");
-//                throw new CoolException("褰撳墠绁ㄥ彿宸插湪搴撳唴锛岃妫�鏌ュ悗鍐嶆搷浣滐紒锛�");
-//                locItem.setAnfme(Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 1000000) / 1000000.0)
-//                        .setUpdateTime(new Date());
-//                if (!locItemService.saveOrUpdate(locItem)) {
-//                    throw new CoolException("搴撲綅鏄庣粏鏇存柊澶辫触锛侊紒");
-//                }
+                // 搴撲綅鏄庣粏宸插瓨鍦紝绱姞鏁伴噺
+                Double newAnfme = Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 1000000) / 1000000.0;
+                locItem.setAnfme(newAnfme)
+                        .setUpdateBy(loginUserId)
+                        .setUpdateTime(new Date());
+                if (!locItemService.updateById(locItem)) {
+                    throw new CoolException("搴撲綅鏄庣粏鏁伴噺鏇存柊澶辫触锛侊紒");
+                }
             }
         });
 

--
Gitblit v1.9.1