From d36f413965c4ef2ea35a7e0ecefd32001a1e66f8 Mon Sep 17 00:00:00 2001
From: skyouc <creaycat@gmail.com>
Date: 星期四, 04 十二月 2025 18:59:08 +0800
Subject: [PATCH] 1. 拣料出库不换库位 2. 缓存代码优化 3. 库存按批次先进先出

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java |  197 ++++++++++++++++++++++++++-----------------------
 1 files changed, 104 insertions(+), 93 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 43a111b..dc0e983 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
@@ -19,6 +19,7 @@
 import com.vincent.rsf.server.api.service.WcsService;
 import com.vincent.rsf.server.common.constant.Constants;
 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.*;
 import com.vincent.rsf.framework.common.R;
 import com.vincent.rsf.framework.exception.CoolException;
@@ -453,25 +454,21 @@
      * @throws Exception
      */
     @Override
-    public void complateInTask(List<Task> tasks) throws Exception {
+    public void complateInTask(List<Task> tasks) {
         AtomicBoolean success = new AtomicBoolean(false);
         if (success.compareAndSet(false, true)) {
             Long loginUserId = SystemAuthUtils.getLoginUserId();
             for (Task task : tasks) {
-                try {
-                    if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) {
-                        //1.鍏ュ簱
-                        complateInstock(task, loginUserId);
-                    } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
-                        //53.鎷f枡鍐嶅叆搴�
-                        //57.鐩樼偣鍐嶅叆搴�
-                        pickComplateInStock(task, loginUserId);
-                    } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
-                        //绉诲簱
-                        moveInStock(task, loginUserId);
-                    }
-                } catch (Exception ex) {
-                    log.error(ex.getMessage(), ex);
+                if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) {
+                    //1.鍏ュ簱
+                    complateInstock(task, loginUserId);
+                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
+                    //53.鎷f枡鍐嶅叆搴�
+                    //57.鐩樼偣鍐嶅叆搴�
+                    pickComplateInStock(task, loginUserId);
+                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
+                    //绉诲簱
+                    moveInStock(task, loginUserId);
                 }
             }
         }
@@ -707,7 +704,7 @@
      * @param loginUserId
      */
     @Transactional(rollbackFor = Exception.class)
-    public void pickComplateInStock(Task task, Long loginUserId) throws Exception {
+    public void pickComplateInStock(Task task, Long loginUserId) {
         if (Objects.isNull(task)) {
             return;
         }
@@ -746,7 +743,7 @@
                 locWorking.setAnfme(taskItem.getAnfme());
             }
             BeanUtils.copyProperties(locWorking, locItem);
-            locItem.setWorkQty(0.0).setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null).setUpdateBy(loginUserId).setUpdateTime(new Date());
+            locItem.setWorkQty(0.0).setQty(0.0).setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null).setUpdateBy(loginUserId).setUpdateTime(new Date());
             items.add(locItem);
         }
 
@@ -756,7 +753,7 @@
 
         TaskItem taskItem = taskItems.stream().findFirst().get();
         //淇濆瓨鍏ュ嚭搴撴祦姘�
-        saveStockItems(taskItems, task, null, null, taskItem.getWkType(), taskItem.getOrderType(), loginUserId);
+        saveStockItems(taskItems, task, null, null, taskItem.getWkType(), OrderType.ORDER_IN.type, loginUserId);
 
         locItemWorkingService.remove(new LambdaQueryWrapper<LocItemWorking>().eq(LocItemWorking::getTaskId, task.getId()));
 
@@ -899,7 +896,7 @@
                             if (Objects.isNull(waveItem)) {
                                 throw new CoolException("娉㈡鏄庣粏涓嶅瓨鍦紒锛�");
                             }
-                            Double workQty = Math.round((waveItem.getWorkQty() - item.getAnfme()) * 100) / 100.0;
+                            Double workQty = Math.round((waveItem.getWorkQty() - item.getAnfme()) * 1000000) / 1000000.0;
                             waveItem.setWorkQty(workQty).setExceStatus(WaveItemExceStatus.WAVE_ITEM_EXCE_STATUS_UN.val);
                             if (!waveItemService.updateById(waveItem)) {
                                 throw new CoolException("娉㈡鏄庣粏鏇存柊澶辫触锛侊紒");
@@ -923,7 +920,7 @@
                             if (Objects.isNull(wkOrder)) {
                                 throw new CoolException("鏁版嵁閿欒锛氬崟鎹凡涓嶅瓨鍦紒锛�");
                             }
-                            Double workQty = Math.round((wkOrder.getWorkQty() - item.getAnfme()) * 100) / 100.0;
+                            Double workQty = Math.round((wkOrder.getWorkQty() - item.getAnfme()) * 1000000) / 1000000.0;
 
                             wkOrder.setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_INIT.val)
                                     .setWorkQty(workQty.compareTo(0.00) > 0 ? workQty : 0.00)
@@ -938,7 +935,7 @@
                                 throw new CoolException("鏁版嵁閿欒锛氬崟鎹槑缁嗗凡涓嶅瓨鍦紒锛�");
                             }
 
-                            Double workItmQty = Math.round((orderItem.getWorkQty() - item.getAnfme()) * 100) / 100.0;
+                            Double workItmQty = Math.round((orderItem.getWorkQty() - item.getAnfme()) * 1000000) / 1000000.0;
                             orderItem.setWorkQty(workItmQty);
 
                             if (!outStockItemService.updateById(orderItem)) {
@@ -1020,17 +1017,18 @@
                 .setBarcode(task.getBarcode())
                 .setTaskStatus(TaskStsType.GENERATE_IN.id);
 
-        TaskInParam param = new TaskInParam();
-        param.setSourceStaNo(task.getTargSite())
-                .setIoType(type)
-                .setLocType1(Integer.parseInt(loc.getType()));
+//        TaskInParam param = new TaskInParam();
+//        param.setSourceStaNo(task.getTargSite())
+//                .setIoType(type)
+//                .setLocType1(Integer.parseInt(loc.getType()));
         //鑾峰彇鏂板簱浣�
-        InTaskMsgDto locInfo = wcsService.getLocNo(param);
+//        InTaskMsgDto locInfo = wcsService.getLocNo(param);
 
-        if (Objects.isNull(locInfo)) {
-            throw new CoolException("鑾峰彇搴撲綅澶辫触锛侊紒");
-        }
-        task.setTargLoc(locInfo.getLocNo())
+//        if (Objects.isNull(locInfo)) {
+//            throw new CoolException("鑾峰彇搴撲綅澶辫触锛侊紒");
+//        }
+        //甯屾棩涓婃姤鐗╂湁鎯呭喌锛屼笉闇�瑕佽幏鍙栨柊搴撲綅
+        task.setTargLoc(task.getOrgLoc())
                 .setOrgSite(task.getTargSite());
 
         if (!this.updateById(task)) {
@@ -1051,7 +1049,7 @@
                 if (taskItem.getFieldsIndex().equals(working.getFieldsIndex())) {
                     Double minQty = taskItem.getAnfme();
                     if (!task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
-                        minQty = Math.round((working.getAnfme() - taskItem.getQty()) * 100) / 100.0;
+                        minQty = Math.round((working.getAnfme() - taskItem.getQty()) * 1000000) / 1000000.0;
                     }
                     if (minQty.compareTo(0.0) >= 0) {
                         taskItem.setAnfme(minQty);
@@ -1075,7 +1073,6 @@
             TaskItem taskItem = taskItems.stream().findFirst().get();
             taskItem.setMatnrId(working.getMatnrId())
                     .setMaktx(working.getMaktx())
-                    .setMatnrId(working.getMatnrId())
                     .setMatnrCode(working.getMatnrCode())
                     .setSpec(working.getSpec())
                     .setAnfme(working.getAnfme())
@@ -1100,26 +1097,25 @@
         List<LocItemWorking> workings = new ArrayList<>();
         List<TaskItem> items = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
         items.forEach(taskItem -> {
-            if (taskItem.getAnfme() > 0) {
-                LocItemWorking itemWorking = new LocItemWorking();
-                BeanUtils.copyProperties(taskItem, itemWorking);
-                itemWorking.setTaskId(task.getId())
-                        .setQty(0.0)
-                        .setLocId(loc1.getId())
-                        .setLocCode(loc1.getCode());
-                workings.add(itemWorking);
-            }
+            LocItemWorking itemWorking = new LocItemWorking();
+            BeanUtils.copyProperties(taskItem, itemWorking);
+            itemWorking.setTaskId(task.getId())
+                    .setQty(0.0)
+                    .setLocId(loc1.getId())
+                    .setLocCode(loc1.getCode());
+            workings.add(itemWorking);
         });
 
-        if (!locItemWorkingService.saveBatch(workings)) {
-            throw new CoolException("涓存椂搴撳瓨鏇存柊澶辫触锛侊紒");
+        if (!workings.isEmpty()) {
+            if (!locItemWorkingService.saveBatch(workings)) {
+                throw new CoolException("涓存椂搴撳瓨鏇存柊澶辫触锛侊紒");
+            }
         }
-
         loc1.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
-
-        if (!locService.updateById(loc1)) {
-            throw new CoolException("搴撲綅棰勭害鍏ュ簱澶辫触锛侊紒");
-        }
+        locService.updateById(loc1);
+//        if (!locService.updateById(loc1)) {
+//            throw new CoolException("搴撲綅棰勭害鍏ュ簱澶辫触锛侊紒");
+//        }
         return task;
     }
 
@@ -1131,7 +1127,7 @@
      */
     @Synchronized
     @Transactional(rollbackFor = Exception.class)
-    public void complateOutStock(Task task, Long loginUserId) throws Exception {
+    public void complateOutStock(Task task, Long loginUserId) {
         if (Objects.isNull(task)) {
             throw new CoolException("鍙傛暟涓嶈兘涓虹┖锛侊紒");
         }
@@ -1228,15 +1224,26 @@
                 throw new CoolException(e.getMessage());
             }
         }
-
-        /**淇敼涓哄簱浣嶇姸鎬佷负O.绌哄簱*/
-        if (!locService.update(new LambdaUpdateWrapper<Loc>()
-                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
-                .set(Loc::getBarcode, null)
-                .set(Loc::getUpdateBy, loginUserId)
-                .set(Loc::getUpdateTime, new Date())
-                .eq(Loc::getId, loc.getId()))) {
-            throw new CoolException("搴撲綅鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+        if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_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("搴撲綅鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+            }
+        } else {
+            /**淇敼涓哄簱浣嶇姸鎬佷负O.绌哄簱*/
+            if (!locService.update(new LambdaUpdateWrapper<Loc>()
+                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
+                    .set(Loc::getBarcode, null)
+                    .set(Loc::getUpdateBy, loginUserId)
+                    .set(Loc::getUpdateTime, new Date())
+                    .eq(Loc::getId, loc.getId()))) {
+                throw new CoolException("搴撲綅鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+            }
         }
 
         if (!this.update(new LambdaUpdateWrapper<Task>()
@@ -1310,7 +1317,7 @@
             stock.setSourceId(waveItem.getId()).setType(OrderType.ORDER_OUT.type);
         } else if (!Objects.isNull(orderItem) && StringUtils.isNotBlank(orderItem.getId() + "")) {
             WkOrder wkOrder = asnOrderService.getById(orderItem.getOrderId());
-            Double qty = Math.round((wkOrder.getQty() + sum) * 100) / 100.0;
+            Double qty = Math.round((wkOrder.getQty() + sum) * 1000000) / 1000000.0;
             wkOrder.setExceStatus(AsnExceStatus.OUT_STOCK_STATUS_TASK_WORKING.val)
                     .setQty(qty);
             if (!asnOrderService.updateById(wkOrder)) {
@@ -1585,12 +1592,9 @@
         if (taskItems.isEmpty()) {
             throw new CoolException("浠诲姟鏄庣粏涓嶅瓨鍦紒锛�");
         }
-        try {
-            //鏇存柊搴撲綅鏄庣粏
-            saveLocItem(taskItems, task.getId(), loginUserId);
-        } catch (Exception e) {
-            throw new CoolException("搴撲綅鏄庣粏鏇存柊澶辫触锛侊紒");
-        }
+
+        //鏇存柊搴撲綅鏄庣粏
+        saveLocItem(taskItems, task.getId(), loginUserId);
 
         /**瀵逛换鍔℃槑缁嗘寜缁勬嫋鏄庣粏杩涜鍒嗙粍*/
         Map<Long, List<TaskItem>> orderMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
@@ -1600,16 +1604,26 @@
                 throw new CoolException("鏁版嵁閿欒锛氱粍鎷栨暟鎹笉瀛樺湪锛岃鑱旂郴绠$悊鍛橈紒锛�");
             }
             List<TaskItem> items = orderMap.get(key);
-            try {
-                //淇濆瓨搴撳瓨鏄庣粏
-                saveStockItems(items, task, pakinItem.getId(), pakinItem.getAsnCode(), pakinItem.getWkType(), pakinItem.getType(), loginUserId);
-                //绉诲嚭鏀惰揣鍖哄簱瀛橈紝 淇敼缁勬墭鐘舵��
-                removeReceiptStock(pakinItem, loginUserId);
-            } catch (Exception e) {
-                logger.error("<UNK>", e);
-                throw new CoolException(e.getMessage());
-            }
+            //淇濆瓨鍏ュ嚭搴撴槑缁�
+            saveStockItems(items, task, pakinItem.getId(), pakinItem.getAsnCode(), pakinItem.getWkType(), pakinItem.getType(), loginUserId);
+            //绉诲嚭鏀惰揣鍖哄簱瀛橈紝 淇敼缁勬墭鐘舵��
+            removeReceiptStock(pakinItem, loginUserId);
         });
+
+        Set<Long> pkinItemIds = taskItems.stream().map(TaskItem::getSource).collect(Collectors.toSet());
+        List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().in(WaitPakinItem::getId, pkinItemIds));
+        if (pakinItems.isEmpty()) {
+            throw new CoolException("缁勬墭鏄庣粏涓嶅瓨鍦紒锛�");
+        }
+        Set<Long> pakinIds = pakinItems.stream().map(WaitPakinItem::getPakinId).collect(Collectors.toSet());
+
+        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
+                .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_DONE.val)
+                .set(WaitPakin::getUpdateBy, loginUserId)
+                .in(WaitPakin::getId, pakinIds))) {
+            throw new CoolException("缁勬嫋鐘舵�佷慨鏀瑰け璐ワ紒锛�");
+        }
+
         /**淇敼搴撲綅鐘舵�佷负F.鍦ㄥ簱*/
         if (!locService.update(new LambdaUpdateWrapper<Loc>().set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type).eq(Loc::getCode, task.getTargLoc()))) {
             throw new CoolException("搴撲綅鐘舵�佷慨鏀瑰け璐ワ紒锛�");
@@ -1634,16 +1648,9 @@
         if (Objects.isNull(itemServiceOne)) {
             throw new CoolException("鏁版嵁閿欒锛氳鏌ョ湅璇疯揣鍖哄簱瀛樻槸鍚﹀瓨鍦紒锛�");
         }
-        Double workQty = Math.round((itemServiceOne.getWorkQty() - pakinItem.getAnfme()) * 100) / 100.0;
-        Double qty = Math.round((itemServiceOne.getQty() + pakinItem.getAnfme()) * 100) / 100.0;
+        Double workQty = Math.round((itemServiceOne.getWorkQty() - pakinItem.getAnfme()) * 1000000) / 1000000.0;
+        Double qty = Math.round((itemServiceOne.getQty() + pakinItem.getAnfme()) * 1000000) / 1000000.0;
         itemServiceOne.setWorkQty(workQty).setQty(qty);
-
-        if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
-                .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_DONE.val)
-                .set(WaitPakin::getUpdateBy, loginUserId)
-                .eq(WaitPakin::getId, pakinItem.getPakinId()))) {
-            throw new CoolException("缁勬嫋鐘舵�佷慨鏀瑰け璐ワ紒锛�");
-        }
 
         if (qty.compareTo(itemServiceOne.getAnfme()) == 0.00) {
             if (!warehouseAreasItemService.removeById(itemServiceOne.getId())) {
@@ -1666,7 +1673,7 @@
      * @time 2025/4/15 15:28
      */
     @Transactional(rollbackFor = Exception.class)
-    public synchronized void saveLocItem(List<TaskItem> items, Long taskId, Long loginUserId) throws Exception {
+    public synchronized void saveLocItem(List<TaskItem> items, Long taskId, Long loginUserId) {
         Task task = this.getById(taskId);
         if (Objects.isNull(task)) {
             throw new CoolException("浠诲姟涓嶅瓨鍦�!!");
@@ -1682,8 +1689,8 @@
             LocItem locItem = locItemService.getOne(new LambdaQueryWrapper<LocItem>()
                     .eq(LocItem::getMatnrId, taskItem.getMatnrId())
                     .eq(LocItem::getLocId, loc.getId())
-                    .eq(StringUtils.isNoneBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch())
-                    .eq(StringUtils.isNoneBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex()));
+                    .eq(StringUtils.isNotBlank(taskItem.getBatch()), LocItem::getBatch, taskItem.getBatch())
+                    .eq(StringUtils.isNotBlank(taskItem.getFieldsIndex()), LocItem::getFieldsIndex, taskItem.getFieldsIndex()));
             if (Objects.isNull(locItem)) {
                 BeanUtils.copyProperties(taskItem, item);
                 item.setLocCode(loc.getCode())
@@ -1692,16 +1699,20 @@
                         .setUpdateBy(loginUserId)
                         .setWkType(taskItem.getWkType())
                         .setType(taskItem.getOrderType());
-                locItems.add(item);
+                if (!locItemService.saveOrUpdate(item)) {
+                    throw new CoolException("搴撲綅鏄庣粏鏇存柊澶辫触锛侊紒");
+                }
             } else {
-                locItem.setAnfme(Math.round((locItem.getAnfme() + taskItem.getAnfme()) * 100) / 100.0)
-                        .setUpdateTime(new Date());
-                locItems.add(locItem);
+                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("搴撲綅鏄庣粏鏇存柊澶辫触锛侊紒");
+//                }
             }
         });
-        if (!locItemService.saveBatch(locItems)) {
-            throw new CoolException("搴撲綅鏄庣粏鏇存柊澶辫触锛侊紒");
-        }
+
     }
 
     /**

--
Gitblit v1.9.1