From 98d88ac8caf7f0991d741079474c262f1e252927 Mon Sep 17 00:00:00 2001
From: chen.lin <1442464845@qq.com>
Date: 星期五, 06 三月 2026 08:14:54 +0800
Subject: [PATCH] 拣货过程中的出库库存匹配

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java |  181 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 171 insertions(+), 10 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 7c3e65e..7ebc3ce 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
@@ -1383,6 +1383,15 @@
             });
             log.debug("[鎷f枡鍏ュ簱] 鍗冲皢鎵e噺搴撲綅 locId={}, locCode={}", loc.getId(), loc.getCode());
             subtractLocItemByTaskItems(loc, taskItems, SystemAuthUtils.getLoginUserId());
+            // 鏂欑宸插叏閮ㄧ敤瀹屽垯涓嶇敓鎴愭嫞璐у叆搴撳崟锛屼粎瀹岀粨鍑哄簱
+            double totalRemaining = taskItems.stream().mapToDouble(ti -> ti.getAnfme() != null && ti.getAnfme().compareTo(0.0) > 0 ? ti.getAnfme() : 0.0).sum();
+            if (totalRemaining <= 0.0) {
+                task.setTaskType(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type);
+                task.setTaskStatus(TaskStsType.UPDATED_OUT.id);
+                this.updateById(task);
+                locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()));
+                return task;
+            }
         }
 
         tempLocs.forEach(working -> {
@@ -1478,6 +1487,137 @@
     }
 
     /**
+     * 鍚岀鐮佷笅澶氭潯 200 鎷f枡鍑哄簱涓�娆℃�у鐞嗭細鎸夌浉鍚岀墿鏂欏悎璁℃墸鍑忓簱浣嶃�佹洿鏂板嚭搴撳崟/搴撳瓨鏄庣粏銆佺敓鎴愪竴寮犳嫞鏂欏叆搴撳崟锛堟湁浣欓噺鏃讹級銆佹洿鏂板簱浣嶇姸鎬�
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void processPickOutBarcodeAll200(List<Task> all200Tasks) {
+        if (all200Tasks == null || all200Tasks.isEmpty()) {
+            return;
+        }
+        Task first = all200Tasks.get(0);
+        if (!TaskType.TASK_TYPE_PICK_AGAIN_OUT.type.equals(first.getTaskType()) || !TaskStsType.UPDATED_OUT.id.equals(first.getTaskStatus())) {
+            throw new CoolException("闈炴嫞鏂欏嚭搴�200浠诲姟锛屼笉鍙壒閲忓鐞�");
+        }
+        List<Long> taskIds = all200Tasks.stream().map(Task::getId).collect(Collectors.toList());
+        List<TaskItem> allItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().in(TaskItem::getTaskId, taskIds));
+        if (allItems.isEmpty()) {
+            throw new CoolException("浠诲姟鏄庣粏涓虹┖");
+        }
+        Long loginUserId = SystemAuthUtils.getLoginUserId();
+        if (loginUserId == null) {
+            loginUserId = 1L;
+        }
+        String orgLoc = first.getOrgLoc();
+        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, orgLoc));
+        if (loc == null) {
+            throw new CoolException("搴撲綅涓嶅瓨鍦細" + orgLoc);
+        }
+        // 鎸夌墿鏂�+鎵规+绁ㄥ彿姹囨�诲凡鎷f暟閲�
+        Map<String, List<TaskItem>> byKey = allItems.stream().collect(Collectors.groupingBy(ti ->
+                (ti.getMatnrId() != null ? ti.getMatnrId() : "") + "_" + (ti.getBatch() != null ? ti.getBatch() : "") + "_" + (ti.getFieldsIndex() != null ? ti.getFieldsIndex() : "")));
+        List<TaskItem> aggregatedForDeduct = new ArrayList<>();
+        Map<String, Double> remainderByKey = new LinkedHashMap<>();
+        for (Map.Entry<String, List<TaskItem>> e : byKey.entrySet()) {
+            List<TaskItem> group = e.getValue();
+            double totalQty = group.stream().mapToDouble(ti -> ti.getQty() != null && ti.getQty() > 0 ? ti.getQty() : (ti.getAnfme() != null ? ti.getAnfme() : 0)).sum();
+            TaskItem rep = group.get(0);
+            TaskItem forDeduct = new TaskItem();
+            forDeduct.setMatnrId(rep.getMatnrId()).setBatch(rep.getBatch()).setFieldsIndex(rep.getFieldsIndex()).setQty(totalQty);
+            aggregatedForDeduct.add(forDeduct);
+            LambdaQueryWrapper<LocItem> qw = new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()).eq(LocItem::getMatnrId, rep.getMatnrId());
+            if (StringUtils.isNotBlank(rep.getBatch())) qw.eq(LocItem::getBatch, rep.getBatch());
+            else qw.and(w -> w.isNull(LocItem::getBatch).or().eq(LocItem::getBatch, ""));
+            if (StringUtils.isNotBlank(rep.getFieldsIndex())) qw.eq(LocItem::getFieldsIndex, rep.getFieldsIndex());
+            else qw.and(w -> w.isNull(LocItem::getFieldsIndex).or().eq(LocItem::getFieldsIndex, ""));
+            LocItem li = locItemService.getOne(qw);
+            double remainder = (li != null && li.getAnfme() != null ? li.getAnfme() : 0) - totalQty;
+            if (remainder > 0) {
+                remainderByKey.put(e.getKey(), remainder);
+            }
+        }
+        subtractLocItemByTaskItems(loc, aggregatedForDeduct, loginUserId);
+        // 鎸� source 鍒嗙粍鏇存柊鍑哄簱鍗曞苟鍐欏簱瀛樻祦姘�
+        Map<Long, List<TaskItem>> bySource = allItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
+        for (Map.Entry<Long, List<TaskItem>> e : bySource.entrySet()) {
+            Long key = e.getKey();
+            List<TaskItem> items = e.getValue();
+            if (first.getResource() != null && first.getResource().equals(TaskResouceType.TASK_RESOUCE_WAVE_TYPE.val)) {
+                WaveItem waveItem = waveItemService.getById(key);
+                if (waveItem != null) {
+                    try {
+                        saveOutStockItem(items, null, waveItem, null, loginUserId);
+                    } catch (Exception ex) {
+                        throw new CoolException(ex.getMessage());
+                    }
+                }
+            } else if (first.getResource() != null && first.getResource().equals(TaskResouceType.TASK_RESOUCE_ORDER_TYPE.val)) {
+                WkOrderItem orderItem = asnOrderItemService.getById(key);
+                if (orderItem != null) {
+                    try {
+                        saveOutStockItem(items, orderItem, null, null, loginUserId);
+                    } catch (Exception ex) {
+                        throw new CoolException(ex.getMessage());
+                    }
+                }
+            }
+        }
+        // 鏈変綑閲忓垯鐢熸垚涓�寮犳嫞鏂欏叆搴撳崟
+        if (!remainderByKey.isEmpty()) {
+            Task pickInTask = new Task();
+            pickInTask.setTaskCode(SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, first));
+            pickInTask.setTaskType(TaskType.TASK_TYPE_PICK_IN.type);
+            pickInTask.setTaskStatus(TaskStsType.GENERATE_IN.id);
+            pickInTask.setBarcode(first.getBarcode());
+            pickInTask.setOrgLoc(orgLoc);
+            pickInTask.setTargLoc(orgLoc);
+            pickInTask.setOrgSite(first.getTargSite());
+            pickInTask.setTargSite(first.getTargSite());
+            pickInTask.setResource(first.getResource());
+            if (!this.save(pickInTask)) {
+                throw new CoolException("鎷f枡鍏ュ簱浠诲姟鍒涘缓澶辫触");
+            }
+            List<LocItemWorking> workings = new ArrayList<>();
+            for (Map.Entry<String, Double> re : remainderByKey.entrySet()) {
+                String k = re.getKey();
+                Double rem = re.getValue();
+                if (rem == null || rem <= 0) continue;
+                List<TaskItem> group = byKey.get(k);
+                if (group == null || group.isEmpty()) continue;
+                TaskItem rep = group.get(0);
+                TaskItem ti = new TaskItem();
+                ti.setTaskId(pickInTask.getId());
+                ti.setMatnrId(rep.getMatnrId()).setMaktx(rep.getMaktx()).setMatnrCode(rep.getMatnrCode());
+                ti.setBatch(rep.getBatch()).setFieldsIndex(rep.getFieldsIndex()).setUnit(rep.getUnit()).setSpec(rep.getSpec()).setModel(rep.getModel());
+                ti.setAnfme(rem).setQty(0.0);
+                taskItemService.save(ti);
+                LocItemWorking w = new LocItemWorking();
+                w.setTaskId(pickInTask.getId());
+                w.setLocId(loc.getId());
+                w.setLocCode(loc.getCode());
+                w.setMatnrId(rep.getMatnrId()).setMaktx(rep.getMaktx()).setMatnrCode(rep.getMatnrCode());
+                w.setBatch(rep.getBatch()).setFieldsIndex(rep.getFieldsIndex()).setUnit(rep.getUnit());
+                w.setAnfme(rem);
+                workings.add(w);
+            }
+            if (!workings.isEmpty()) {
+                locItemWorkingService.saveBatch(workings);
+            }
+            loc.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
+            locService.updateById(loc);
+        } else {
+            loc.setUseStatus(LocStsType.LOC_STS_TYPE_O.type);
+            loc.setBarcode(null);
+            loc.setUpdateBy(loginUserId);
+            loc.setUpdateTime(new Date());
+            locService.updateById(loc);
+        }
+        for (Long tid : taskIds) {
+            locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, tid));
+        }
+    }
+
+    /**
      * @author Ryan
      * @date 2025/5/20
      * @description: 瀹屾垚鍑哄簱浠诲姟锛屾洿鏂板嚭搴撳簱瀛樹俊鎭�
@@ -1493,7 +1633,35 @@
         if (Objects.isNull(loc)) {
             throw new CoolException("搴撲綅涓嶅瓨鍦紒锛�");
         }
-        
+
+        // 绌烘澘鍑哄簱锛氭棤浠诲姟鏄庣粏锛屼笉闇�瑕� PDA 鎷h揣纭锛孯CS 鍥炶皟鍚庣洿鎺ュ畬鎴愬簱浣嶆洿鏂板苟缃负 UPDATED_OUT
+        if (task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_OUT.type)) {
+            List<TaskItem> emptyItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
+            if (emptyItems.isEmpty()) {
+                if (!LocStsType.LOC_STS_TYPE_R.type.equals(loc.getUseStatus())) {
+                    log.warn("绌烘澘鍑哄簱浠诲姟{}鐨勫簱浣峽}鐘舵�佷笉鏄疪.鍑哄簱棰勭害锛岃烦杩�", task.getId(), loc.getCode());
+                    return;
+                }
+                if (!locService.update(new LambdaUpdateWrapper<Loc>()
+                        .eq(Loc::getId, loc.getId())
+                        .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
+                        .set(Loc::getBarcode, null)
+                        .set(Loc::getUpdateBy, loginUserId)
+                        .set(Loc::getUpdateTime, new Date()))) {
+                    throw new CoolException("绌烘澘鍑哄簱搴撲綅鐘舵�佹洿鏂板け璐ワ紒锛�");
+                }
+                if (!this.update(new LambdaUpdateWrapper<Task>()
+                        .eq(Task::getId, task.getId())
+                        .set(Task::getUpdateBy, loginUserId)
+                        .set(Task::getUpdateTime, new Date())
+                        .set(Task::getTaskStatus, TaskStsType.UPDATED_OUT.id))) {
+                    throw new CoolException("绌烘澘鍑哄簱浠诲姟鐘舵�佹洿鏂板け璐ワ紒锛�");
+                }
+                log.info("[绌烘澘鍑哄簱] 浠诲姟{} RCS鍥炶皟鍚庡凡鐩存帴瀹屾垚搴撲綅鏇存柊锛屾棤闇�PDA纭", task.getTaskCode());
+                return;
+            }
+        }
+
         List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
         if (taskItems.isEmpty()) {
             throw new CoolException("浠诲姟鏄庣粏涓嶅瓨鍦紒锛�");
@@ -1610,15 +1778,8 @@
         
         // 鏍规嵁浠诲姟绫诲瀷鏇存柊搴撲綅鐘舵��
         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>()
-                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type)
-                    .set(Loc::getBarcode, null)
-                    .set(Loc::getUpdateBy, loginUserId)
-                    .set(Loc::getUpdateTime, new Date())
-                    .eq(Loc::getId, loc.getId()))) {
-                throw new CoolException("搴撲綅鐘舵�佷慨鏀瑰け璐ワ紒锛�");
-            }
+            // 鎷f枡鍑哄簱/鐩樼偣鍑哄簱锛氬湪鏈敓鎴愭嫞鏂欏叆搴撳崟涔嬪墠淇濇寔 R.棰勭害鍑哄簱锛屽惁鍒欎笅鍙戜换鍔℃椂鏌ヤ笉鍒拌搴撲綅锛堝彧鏌� F+R锛夊鑷粹�滃簱瀛樹笉瓒斥��
+            // 绛� PDA 纭骞剁敓鎴愭嫞鏂欏叆搴撲换鍔℃椂锛屽啀鍦� pickOrCheckTask 涓皢鐩爣搴撲綅鏀逛负 S.棰勭害鍏ュ簱
         } else if (task.getTaskType().equals(TaskType.TASK_TYPE_OUT.type)) {
             // 鍏ㄧ増鍑哄簱锛氫笉鏇存柊搴撲綅鐘舵�佷负O锛岀瓑寰匬DA蹇�熸嫞璐х‘璁ゆ椂鍐嶆洿鏂�
             // 搴撲綅鐘舵�佷繚鎸佸師鏍凤紙R.鍑哄簱棰勭害鐘舵�侊級

--
Gitblit v1.9.1