From c45f2e980ff53c2e0d8a0a05480d347132a6cd3c Mon Sep 17 00:00:00 2001
From: Junjie <540245094@qq.com>
Date: 星期五, 29 八月 2025 16:06:51 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/task/handler/OrderSyncHandler.java |  206 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 146 insertions(+), 60 deletions(-)

diff --git a/src/main/java/com/zy/asrs/task/handler/OrderSyncHandler.java b/src/main/java/com/zy/asrs/task/handler/OrderSyncHandler.java
index 675f182..422306c 100644
--- a/src/main/java/com/zy/asrs/task/handler/OrderSyncHandler.java
+++ b/src/main/java/com/zy/asrs/task/handler/OrderSyncHandler.java
@@ -405,77 +405,163 @@
             if (!orderDetlService.delete(new EntityWrapper<OrderDetl>().eq("order_no", order.getOrderNo()))) {
                 throw new CoolException("閲嶆暣鍑哄簱璁㈠崟銆恛rderNo = " + order.getOrderNo() + "銆戞槑缁嗗け璐�");
             }
+
+            HashMap<String, List<NccSaleDbddWms>> matnrMap = new HashMap<>();
+            for (OrderDetl orderDetl : old) {
+                String remark = orderDetl.getRemark();
+                if(Cools.isEmpty(remark)) {
+                    continue;
+                }
+
+                NccSaleDbddWms currentData = JSONObject.parseObject(orderDetl.getRemark(), NccSaleDbddWms.class);
+                if (currentData == null) {
+                    continue;
+                }
+
+                List<NccSaleDbddWms> list = new ArrayList<>();
+                if (matnrMap.containsKey(orderDetl.getMatnr())) {
+                    list = matnrMap.get(orderDetl.getMatnr());
+                    list.add(currentData);
+                }else {
+                    list.add(currentData);
+                }
+                matnrMap.put(orderDetl.getMatnr(), list);
+            }
+
+            for (Map.Entry<String, List<NccSaleDbddWms>> entry : matnrMap.entrySet()) {
+                String matnr = entry.getKey();
+                List<NccSaleDbddWms> list = entry.getValue();
+                list.sort(Comparator.comparing(NccSaleDbddWms::getFsl).reversed());
+                matnrMap.put(matnr, list);
+            }
+
             List<WrkDetl> wrkDetls = wrkDetlService.selectAndLogByOrderNo(order.getOrderNo());
             for (WrkDetl wrkDetl : wrkDetls) {
-                OrderDetl orderDetl = new OrderDetl();
-                orderDetl.sync(wrkDetl);
-                orderDetl.setQty(wrkDetl.getAnfme());
-                orderDetl.setOrderId(orderSou.getId());
-                orderDetl.setOrderNo(orderSou.getOrderNo());
-                orderDetl.setStatus(1);
-                orderDetl.setCreateTime(orderSou.getCreateTime());
-                orderDetl.setCreateBy(orderSou.getCreateBy());
-                orderDetl.setUpdateTime(orderSou.getUpdateTime());
-                orderDetl.setUpdateBy(orderSou.getUpdateBy());
-                orderDetl.setId(null);
-                //鍏堟寜鐓ф壒娆″尮閰嶏紝鍐嶆寜鐓ф棤鎵规鍙锋壒娆★紝鍥犱负閿�鍞彂璐э紝鎵规鍙峰彲鑳藉甫锛屾湁鍙兘涓嶆惡甯︼紝鎵�浠ュ氨浼氭湁闂锛涜�屼笖鑰冭檻鍒颁竴鏉¤鍗曢噷濡傛灉鐩稿悓鐗╂枡锛屼竴鏉″甫鎵规锛屼竴鏉′笉甯︽壒娆★紝鎯呭喌
-                for (OrderDetl xxx : old) {
-                    if (xxx.beSimilar(wrkDetl)) {
-                        if(Cools.isEmpty(orderDetl.getRemark())) {
-                            orderDetl.setRemark(xxx.getRemark());
+                List<OrderDetl> insertList = new ArrayList<>();
+
+                Double isused = wrkDetl.getWeight();
+                while (isused > 0D) {
+                    List<NccSaleDbddWms> list = matnrMap.get(wrkDetl.getMatnr());
+                    if (!list.isEmpty()) {
+                        NccSaleDbddWms nccSaleDbddWms = list.get(0);
+                        BigDecimal orderStock = nccSaleDbddWms.getFsl();
+
+                        //瀹為檯鍑哄簱鏁伴噺
+                        double realWeight = 0D;
+                        BigDecimal diff = orderStock.subtract(BigDecimal.valueOf(isused));
+                        if (diff.doubleValue() >= 0) {
+                            nccSaleDbddWms.setFsl(diff);
+                            list.set(0, nccSaleDbddWms);
+                            matnrMap.put(wrkDetl.getMatnr(), list);
+                            realWeight = isused;
                         }else {
-                            NccSaleDbddWms currentData = JSONObject.parseObject(orderDetl.getRemark(), NccSaleDbddWms.class);
-                            if (currentData == null) {
-                                continue;
-                            }
-
-                            NccSaleDbddWms nextData = JSONObject.parseObject(xxx.getRemark(), NccSaleDbddWms.class);
-                            if (nextData == null) {
-                                continue;
-                            }
-
-                            BigDecimal currentFslDecimal = currentData.getFsl();
-                            BigDecimal nextFslDecimal = nextData.getFsl();
-                            if(currentFslDecimal.doubleValue() < nextFslDecimal.doubleValue()) {
-                                orderDetl.setRemark(xxx.getRemark());
-                            }
+                            list.remove(0);
+                            matnrMap.put(wrkDetl.getMatnr(), list);
+                            realWeight = orderStock.doubleValue();
                         }
+
+                        //璁$畻涓绘暟閲�
+                        Double anfme = MatUtils.calcAnfmeFromWeight(wrkDetl.getMatnr(), realWeight);
+
+                        OrderDetl orderDetl = new OrderDetl();
+                        orderDetl.sync(wrkDetl);
+                        orderDetl.setQty(anfme);
+                        orderDetl.setWeight(realWeight);
+                        orderDetl.setOrderId(orderSou.getId());
+                        orderDetl.setOrderNo(orderSou.getOrderNo());
+                        orderDetl.setStatus(1);
+                        orderDetl.setCreateTime(orderSou.getCreateTime());
+                        orderDetl.setCreateBy(orderSou.getCreateBy());
+                        orderDetl.setUpdateTime(orderSou.getUpdateTime());
+                        orderDetl.setUpdateBy(orderSou.getUpdateBy());
+                        orderDetl.setId(null);
+                        orderDetl.setRemark(JSON.toJSONString(nccSaleDbddWms));
+                        insertList.add(orderDetl);
+
+                        isused -= orderStock.doubleValue();
+                    }else {
+                        throw new CoolException("閲嶆暣璋冩嫧鍑哄簱璁㈠崟銆恛rderNo = " + order.getOrderNo() + "銆戞槑缁嗗け璐ワ紝ERP鍘熷鏁版嵁涓虹┖");
                     }
                 }
-                if (Cools.isEmpty(orderDetl.getRemark())) {
-                    for (OrderDetl xxx : old) {
-                        if (xxx.beSimilarByWrkDetl(wrkDetl)) {
-                            if(Cools.isEmpty(orderDetl.getRemark())) {
-                                orderDetl.setRemark(xxx.getRemark());
-                            }else {
-                                NccSaleDbddWms currentData = JSONObject.parseObject(orderDetl.getRemark(), NccSaleDbddWms.class);
-                                if (currentData == null) {
-                                    continue;
-                                }
 
-                                NccSaleDbddWms nextData = JSONObject.parseObject(xxx.getRemark(), NccSaleDbddWms.class);
-                                if (nextData == null) {
-                                    continue;
-                                }
-
-                                BigDecimal currentFslDecimal = currentData.getFsl();
-                                BigDecimal nextFslDecimal = nextData.getFsl();
-                                if(currentFslDecimal.doubleValue() < nextFslDecimal.doubleValue()) {
-                                    orderDetl.setRemark(xxx.getRemark());
-                                }
-                            }
-                        }
+                for (OrderDetl orderDetl : insertList) {
+                    if (!orderDetlService.insert(orderDetl)) {
+                        throw new CoolException("閲嶆暣璋冩嫧鍑哄簱璁㈠崟銆恛rderNo = " + order.getOrderNo() + "銆戞槑缁嗗け璐�");
                     }
-
-                    if (Cools.isEmpty(orderDetl.getRemark())) {
-                        throw new CoolException("閲嶆暣鍑哄簱璁㈠崟銆恛rderNo = " + order.getOrderNo() + "銆戞槑缁嗗け璐ワ紝ERP鍘熷鏁版嵁涓虹┖");
-                    }
-                }
-                if (!orderDetlService.insert(orderDetl)) {
-                    throw new CoolException("閲嶆暣鍑哄簱璁㈠崟銆恛rderNo = " + order.getOrderNo() + "銆戞槑缁嗗け璐�");
                 }
             }
 
+//            List<WrkDetl> wrkDetls = wrkDetlService.selectAndLogByOrderNo(order.getOrderNo());
+//            for (WrkDetl wrkDetl : wrkDetls) {
+//                OrderDetl orderDetl = new OrderDetl();
+//                orderDetl.sync(wrkDetl);
+//                orderDetl.setQty(wrkDetl.getAnfme());
+//                orderDetl.setOrderId(orderSou.getId());
+//                orderDetl.setOrderNo(orderSou.getOrderNo());
+//                orderDetl.setStatus(1);
+//                orderDetl.setCreateTime(orderSou.getCreateTime());
+//                orderDetl.setCreateBy(orderSou.getCreateBy());
+//                orderDetl.setUpdateTime(orderSou.getUpdateTime());
+//                orderDetl.setUpdateBy(orderSou.getUpdateBy());
+//                orderDetl.setId(null);
+//                //鍏堟寜鐓ф壒娆″尮閰嶏紝鍐嶆寜鐓ф棤鎵规鍙锋壒娆★紝鍥犱负閿�鍞彂璐э紝鎵规鍙峰彲鑳藉甫锛屾湁鍙兘涓嶆惡甯︼紝鎵�浠ュ氨浼氭湁闂锛涜�屼笖鑰冭檻鍒颁竴鏉¤鍗曢噷濡傛灉鐩稿悓鐗╂枡锛屼竴鏉″甫鎵规锛屼竴鏉′笉甯︽壒娆★紝鎯呭喌
+//                for (OrderDetl xxx : old) {
+//                    if (xxx.beSimilar(wrkDetl)) {
+//                        if(Cools.isEmpty(orderDetl.getRemark())) {
+//                            orderDetl.setRemark(xxx.getRemark());
+//                        }else {
+//                            NccSaleDbddWms currentData = JSONObject.parseObject(orderDetl.getRemark(), NccSaleDbddWms.class);
+//                            if (currentData == null) {
+//                                continue;
+//                            }
+//
+//                            NccSaleDbddWms nextData = JSONObject.parseObject(xxx.getRemark(), NccSaleDbddWms.class);
+//                            if (nextData == null) {
+//                                continue;
+//                            }
+//
+//                            BigDecimal currentFslDecimal = currentData.getFsl();
+//                            BigDecimal nextFslDecimal = nextData.getFsl();
+//                            if(currentFslDecimal.doubleValue() < nextFslDecimal.doubleValue()) {
+//                                orderDetl.setRemark(xxx.getRemark());
+//                            }
+//                        }
+//                    }
+//                }
+//                if (Cools.isEmpty(orderDetl.getRemark())) {
+//                    for (OrderDetl xxx : old) {
+//                        if (xxx.beSimilarByWrkDetl(wrkDetl)) {
+//                            if(Cools.isEmpty(orderDetl.getRemark())) {
+//                                orderDetl.setRemark(xxx.getRemark());
+//                            }else {
+//                                NccSaleDbddWms currentData = JSONObject.parseObject(orderDetl.getRemark(), NccSaleDbddWms.class);
+//                                if (currentData == null) {
+//                                    continue;
+//                                }
+//
+//                                NccSaleDbddWms nextData = JSONObject.parseObject(xxx.getRemark(), NccSaleDbddWms.class);
+//                                if (nextData == null) {
+//                                    continue;
+//                                }
+//
+//                                BigDecimal currentFslDecimal = currentData.getFsl();
+//                                BigDecimal nextFslDecimal = nextData.getFsl();
+//                                if(currentFslDecimal.doubleValue() < nextFslDecimal.doubleValue()) {
+//                                    orderDetl.setRemark(xxx.getRemark());
+//                                }
+//                            }
+//                        }
+//                    }
+//
+//                    if (Cools.isEmpty(orderDetl.getRemark())) {
+//                        throw new CoolException("閲嶆暣鍑哄簱璁㈠崟銆恛rderNo = " + order.getOrderNo() + "銆戞槑缁嗗け璐ワ紝ERP鍘熷鏁版嵁涓虹┖");
+//                    }
+//                }
+//                if (!orderDetlService.insert(orderDetl)) {
+//                    throw new CoolException("閲嶆暣鍑哄簱璁㈠崟銆恛rderNo = " + order.getOrderNo() + "銆戞槑缁嗗け璐�");
+//                }
+//            }
+
             // 淇敼璁㈠崟鐘舵�� 2.浣滀笟涓� ===>> 4.寰呬笂鎶�
             if (!orderService.updateSettle(orderSou.getId(), 4L, null)) {
                 throw new CoolException("鏈嶅姟鍣ㄥ唴閮ㄩ敊璇紝璇疯仈绯荤鐞嗗憳");

--
Gitblit v1.9.1