From c6938bcb89091596edab2740f7bf0b218956b4b0 Mon Sep 17 00:00:00 2001
From: cl <1442464845@qq.com>
Date: 星期二, 12 五月 2026 15:12:37 +0800
Subject: [PATCH] #

---
 rsf-server/src/main/java/com/vincent/rsf/server/manager/service/CusBarcodeSyncMatnrService.java |  110 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 65 insertions(+), 45 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/CusBarcodeSyncMatnrService.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/CusBarcodeSyncMatnrService.java
index 51a3c03..0800fc9 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/CusBarcodeSyncMatnrService.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/CusBarcodeSyncMatnrService.java
@@ -12,6 +12,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
@@ -118,37 +119,48 @@
             syncMatnrNonForceFromView(cfg, matnrCodes, orderItemByCode, loginUserId);
             return;
         }
-        List<Map<String, Object>> viewItems = cusBarcodeSyncViewQueryService.listByItemNos(matnrCodes);
-        log.info("[cus_barcode_sync] FORCE_VIEW 鍒嗘敮 CUS_ITEM_SYNC_MODE.val={} viewRows={} viewBarcodes=[{}]",
-                formatCfgVal(cfg.rawVal),
-                viewItems == null ? 0 : viewItems.size(),
-                summarizeViewBarcodes(viewItems));
+        List<Map<String, Object>> viewItems = new ArrayList<>(matnrCodes.size());
         for (String code : matnrCodes) {
-            boolean hit = CusBarcodeSyncViewQueryService.orderMatnrHitsBarcodeView(code, viewItems);
+            List<Map<String, Object>> one = cusBarcodeSyncViewQueryService.listMapsForBarcode(code);
+            boolean hit = CusBarcodeSyncViewQueryService.orderMatnrHitsBarcodeView(code, one);
             log.info("[cus_barcode_sync] 寮哄埗鏍¢獙 code={} viewHit={}", code, hit);
             if (!hit) {
                 throw new CoolException("鐗╂枡鏈湪瑙嗗浘 cus_barcode_sync_view 涓細" + code);
             }
+            viewItems.addAll(one);
         }
+        log.info("[cus_barcode_sync] FORCE_VIEW 鍒嗘敮 CUS_ITEM_SYNC_MODE.val={} viewRows={} viewBarcodes=[{}]",
+                formatCfgVal(cfg.rawVal),
+                viewItems.size(),
+                summarizeViewBarcodes(viewItems));
         cusBarcodeSyncMatnrApplyService.applyFromViewRows(viewItems, orderItemByCode, loginUserId);
     }
 
     private void syncMatnrNonForceFromView(CusItemSyncConfigSnapshot cfg, List<String> matnrCodes, Map<String, SyncOrdersItem> orderItemByCode, Long loginUserId) {
-        List<Map<String, Object>> viewItems = null;
+        Map<String, List<Map<String, Object>>> viewByCode = null;
         try {
-            viewItems = cusBarcodeSyncViewQueryService.listByItemNos(matnrCodes);
+            viewByCode = new LinkedHashMap<>();
+            for (String code : matnrCodes) {
+                viewByCode.put(code, cusBarcodeSyncViewQueryService.listMapsForBarcode(code));
+            }
         } catch (Exception ex) {
             log.warn("[cus_barcode_sync] 鏌ヨ cus_barcode_sync_view 寮傚父锛屽皢浠呮寜鐗╂枡琛ㄦ牎楠�", ex);
         }
+        int viewRowTotal = viewByCode == null ? 0 : viewByCode.values().stream().mapToInt(l -> l == null ? 0 : l.size()).sum();
         log.info("[cus_barcode_sync] NONE 鍒嗘敮 CUS_ITEM_SYNC_MODE.val={} 鍓簱瑙嗗浘 rows={} barcodesInView=[{}]",
                 formatCfgVal(cfg.rawVal),
-                viewItems == null ? 0 : viewItems.size(),
-                summarizeViewBarcodes(viewItems));
-        if (viewItems != null && !viewItems.isEmpty()) {
+                viewRowTotal,
+                summarizeBarcodesWithHits(viewByCode));
+        if (viewByCode != null) {
             try {
-                cusBarcodeSyncMatnrApplyService.applyFromViewRows(viewItems, orderItemByCode, loginUserId);
+                for (String code : matnrCodes) {
+                    List<Map<String, Object>> one = viewByCode.get(code);
+                    if (one != null && !one.isEmpty()) {
+                        cusBarcodeSyncMatnrApplyService.applyFromViewRows(one, orderItemByCode, loginUserId);
+                    }
+                }
             } catch (Exception ex) {
-                log.warn("[cus_barcode_sync] 鎵归噺 applyFromViewRows 澶辫触", ex);
+                log.warn("[cus_barcode_sync] 鎸夋潯鐮� applyFromViewRows 澶辫触", ex);
             }
         }
         // 瑙嗗浘鏈夋潯鐮佷絾鏈湴浠嶆棤锛氭寜琛岃ˉ寤烘。
@@ -158,53 +170,49 @@
                 log.info("[cus_barcode_sync] 鏍¢獙閫氳繃 code={} localMatnrId={}", code, m.getId());
                 continue;
             }
-            boolean viewHit = viewItems != null && CusBarcodeSyncViewQueryService.orderMatnrHitsBarcodeView(code, viewItems);
-            int viewRowCount = viewItems == null ? 0 : viewItems.size();
+            List<Map<String, Object>> one = viewByCode == null
+                    ? Collections.emptyList()
+                    : viewByCode.getOrDefault(code, Collections.emptyList());
+            boolean viewHit = CusBarcodeSyncViewQueryService.orderMatnrHitsBarcodeView(code, one);
+            int viewRowCount = one.size();
             log.info("[cus_barcode_sync] 鏈湴鏃犺褰� code={} viewHit={} viewRowCount={}", code, viewHit, viewRowCount);
             Exception applyEx = null;
-            if (viewHit && viewItems != null) {
-                List<Map<String, Object>> rowsForCode = viewItems.stream()
-                        .filter(r -> CusBarcodeSyncViewQueryService.rowMatchesOrderMatnr(code, Objects.toString(r.get("barcode"), null)))
-                        .collect(Collectors.toList());
-                if (!rowsForCode.isEmpty()) {
-                    try {
-                        log.info("[cus_barcode_sync] 鎸夋潯鐮佽ˉ妗� apply rows={} code={}", rowsForCode.size(), code);
-                        cusBarcodeSyncMatnrApplyService.applyFromViewRows(rowsForCode, orderItemByCode, loginUserId);
-                    } catch (Exception ex) {
-                        applyEx = ex;
-                        log.warn("[cus_barcode_sync] 鎸夎鍥捐ˉ鍏ㄧ墿鏂欏け璐� code={}", code, ex);
-                    }
-                    m = findLocalMatnrForOrderCode(code);
+            if (viewHit && !one.isEmpty()) {
+                try {
+                    log.info("[cus_barcode_sync] 鎸夋潯鐮佽ˉ妗� apply rows={} code={}", one.size(), code);
+                    cusBarcodeSyncMatnrApplyService.applyFromViewRows(one, orderItemByCode, loginUserId);
+                } catch (Exception ex) {
+                    applyEx = ex;
+                    log.warn("[cus_barcode_sync] 鎸夎鍥捐ˉ鍏ㄧ墿鏂欏け璐� code={}", code, ex);
                 }
+                m = findLocalMatnrForOrderCode(code);
             }
             if (m == null) {
-                ManMatnrFailReason fr = resolveManMatnrFailReason(viewItems, viewHit, applyEx);
+                ManMatnrFailReason fr = resolveManMatnrFailReason(viewByCode, viewHit, applyEx);
                 log.warn("[cus_barcode_sync] 鏃犳硶钀藉湴 man_matnr code={} reason={} viewRowCount={} viewBarcodes=[{}]",
-                        code, fr.name(), viewRowCount, summarizeViewBarcodes(viewItems));
-                throw new CoolException("鐗╂枡涓嶅瓨鍦細" + code + "锛�" + fr.userMessage);
+                        code, fr.name(), viewRowCount, summarizeViewBarcodes(one));
+                throw new CoolException("鐗╂枡涓嶅瓨鍦細" + code);
             }
         }
     }
 
     private enum ManMatnrFailReason {
-        VIEW_QUERY_ERROR("鍓簱瑙嗗浘鏌ヨ寮傚父锛屾湭鎷垮埌缁撴灉闆�"),
-        VIEW_ZERO_ROWS("鍓簱瑙嗗浘鏈鏌ヨ 0 琛岋紝璇锋牳瀵� dj-cloud-wms 涓庤鍥炬暟鎹�"),
-        VIEW_CODE_NOT_IN_RESULT("鍓簱瑙嗗浘鏈夎繑鍥炶浣嗘棤涓庢湰鍗� matnr 鐩哥瓑鐨� barcode"),
-        APPLY_ERROR("瑙嗗浘宸插懡涓絾鍐欏叆 man_matnr 澶辫触"),
-        APPLY_STILL_EMPTY("瑙嗗浘宸插懡涓笖宸� apply 浠嶆棤涓诲簱鐗╂枡璁板綍");
-
-        final String userMessage;
-
-        ManMatnrFailReason(String userMessage) {
-            this.userMessage = userMessage;
-        }
+        VIEW_QUERY_ERROR,
+        VIEW_ZERO_ROWS,
+        VIEW_CODE_NOT_IN_RESULT,
+        APPLY_ERROR,
+        APPLY_STILL_EMPTY
     }
 
-    private static ManMatnrFailReason resolveManMatnrFailReason(List<Map<String, Object>> viewItems, boolean viewHit, Exception applyEx) {
-        if (viewItems == null) {
+    private static ManMatnrFailReason resolveManMatnrFailReason(
+            Map<String, List<Map<String, Object>>> viewByCode,
+            boolean viewHit,
+            Exception applyEx) {
+        if (viewByCode == null) {
             return ManMatnrFailReason.VIEW_QUERY_ERROR;
         }
-        if (viewItems.isEmpty()) {
+        boolean anyHit = viewByCode.values().stream().anyMatch(list -> list != null && !list.isEmpty());
+        if (!anyHit) {
             return ManMatnrFailReason.VIEW_ZERO_ROWS;
         }
         if (!viewHit) {
@@ -236,6 +244,18 @@
         return s.length() > 1200 ? s.substring(0, 1200) + "..." : s;
     }
 
+    /** 浠呮眹鎬绘湰娆℃煡璇㈡湁鍛戒腑鐨勫崟鎹墿鏂欏彿 */
+    private static String summarizeBarcodesWithHits(Map<String, List<Map<String, Object>>> viewByCode) {
+        if (viewByCode == null || viewByCode.isEmpty()) {
+            return "";
+        }
+        String s = viewByCode.entrySet().stream()
+                .filter(e -> e.getValue() != null && !e.getValue().isEmpty())
+                .map(Map.Entry::getKey)
+                .collect(Collectors.joining(","));
+        return s.length() > 1200 ? s.substring(0, 1200) + "..." : s;
+    }
+
     private Matnr findLocalMatnrForOrderCode(String orderMatnr) {
         String t = StringUtils.trimToNull(orderMatnr);
         if (t == null) {

--
Gitblit v1.9.1