From f2b62569198d9afd1604a1617ab1e0a3e11f74bf Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期四, 30 十月 2025 18:40:22 +0800
Subject: [PATCH] 增加条码的持久化逻辑

---
 src/main/java/com/gs/xiaomi/service/Xm104Service.java   |   11 +++
 src/main/java/com/gs/xiaomi/service/BCS101Service.java  |  103 ++++++++++++++++++++++++++++++++++
 src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java |   27 +++++++-
 3 files changed, 135 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/gs/xiaomi/service/BCS101Service.java b/src/main/java/com/gs/xiaomi/service/BCS101Service.java
index 11f37ba..d7baca5 100644
--- a/src/main/java/com/gs/xiaomi/service/BCS101Service.java
+++ b/src/main/java/com/gs/xiaomi/service/BCS101Service.java
@@ -1,16 +1,117 @@
 package com.gs.xiaomi.service;
 
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.gs.xiaomi.dto.BCS101Request;
+import com.gs.xiaomi.dto.BCS101Response;
+import com.gs.xiaomi.dto.SnListItemDto;
+import com.gs.xiaomi.entity.DeliveryMain;
+import com.gs.xiaomi.entity.SnListItem;
+import com.gs.xiaomi.util.SnListItemConverter;
 import lombok.RequiredArgsConstructor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.io.IOException;
+import java.util.List;
+
 @Service
 @Transactional(rollbackFor = Exception.class)
 @RequiredArgsConstructor
 public class BCS101Service {
 
-    private static final Logger log = LoggerFactory.getLogger(Xm104Service.class);
+    private static final Logger log = LoggerFactory.getLogger(BCS101Service.class);
+
+    private final BCS101ApiService bcs101ApiService;
+    private final DeliveryMainService deliveryMainService;
+    private final SnListItemService snListItemService;
+
+    /**
+     * 鏍规嵁閫佽揣鍗曞彿鑾峰彇BCS101鏉$爜鏁版嵁骞舵寔涔呭寲
+     *
+     * @param asn 閫佽揣鍗曞彿
+     * @throws Exception 澶勭悊寮傚父
+     */
+    public void syncBCS101DataByAsn(String asn) throws Exception {
+        log.info("銆怋CS101鏁版嵁鍚屾銆戝紑濮嬪悓姝ラ�佽揣鍗�: {}", asn);
+
+        // 鏌ヨ閫佽揣鍗曚富琛ㄤ俊鎭�
+        LambdaQueryWrapper<DeliveryMain> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(DeliveryMain::getZzasn, asn);
+        DeliveryMain deliveryMain = deliveryMainService.getOne(queryWrapper, false);
+
+        if (deliveryMain == null) {
+            log.error("銆怋CS101鏁版嵁鍚屾澶辫触銆戦�佽揣鍗曚笉瀛樺湪: {}", asn);
+            throw new RuntimeException("閫佽揣鍗曚笉瀛樺湪: " + asn);
+        }
+
+        // 鍒涘缓BCS101璇锋眰鍙傛暟
+        BCS101Request request = new BCS101Request();
+        request.setSupplierId(String.valueOf(Integer.parseInt(deliveryMain.getHubLifnr())));
+        request.setDocNo(deliveryMain.getZzasn());
+        request.setDocType("ASNGR");
+        request.setPageNo(1);
+        request.setPageSize(1000);
+
+        // 璋冪敤API鑾峰彇鍘熷JSON瀛楃涓�
+        String bcs101Data;
+        try {
+            bcs101Data = bcs101ApiService.getBCS101Data(request);
+            log.debug("銆怋CS101鏁版嵁鍚屾銆戝師濮嬪搷搴�: {}", bcs101Data);
+        } catch (IOException e) {
+            log.error("銆怋CS101鏁版嵁鍚屾澶辫触銆戣皟鐢ˋPI寮傚父, 閫佽揣鍗�: {}, 寮傚父: {}", asn, e.getMessage(), e);
+            throw new RuntimeException("璋冪敤BCS101鎺ュ彛澶辫触: " + e.getMessage(), e);
+        }
+
+        // 瑙f瀽JSON涓築CS101Response瀵硅薄
+        ObjectMapper objectMapper = new ObjectMapper();
+        try {
+            BCS101Response response = objectMapper.readValue(bcs101Data, BCS101Response.class);
+
+            // 妫�鏌ュ搷搴旀槸鍚︽垚鍔�
+            if (response.isSuccess()) {
+                // 鑾峰彇snList鏁版嵁
+                List<SnListItemDto> snList = response.getBody().getSnList();
+
+                if (snList != null && !snList.isEmpty()) {
+                    log.info("銆怋CS101鏁版嵁鍚屾銆戣幏鍙栧埌 {} 鏉N鏁版嵁锛屽紑濮嬫寔涔呭寲...", snList.size());
+
+                    // 杞崲DTO涓篍ntity锛屽苟璁剧疆鍏宠仈淇℃伅
+                    List<SnListItem> entityList = SnListItemConverter.toEntityList(
+                            snList,
+                            deliveryMain.getId(),      // deliveryMainId - 閫佽揣鍗曚富琛↖D
+                            deliveryMain.getZzasn()    // zzasn - 閫佽揣鍗曞彿
+                    );
+
+                    // 鍏堝垹闄よ閫佽揣鍗曞凡鏈夌殑SN鏁版嵁锛堥伩鍏嶉噸澶嶏級
+                    snListItemService.lambdaUpdate()
+                            .eq(SnListItem::getZzasn, deliveryMain.getZzasn())
+                            .remove();
+
+                    // 鎵归噺淇濆瓨鍒版暟鎹簱
+                    boolean saved = snListItemService.saveBatch(entityList);
+
+                    if (saved) {
+                        log.info("銆怋CS101鏁版嵁鍚屾鎴愬姛銆戞垚鍔熶繚瀛� {} 鏉N鏁版嵁鍒版暟鎹簱, 閫佽揣鍗�: {}", entityList.size(), asn);
+                    } else {
+                        log.error("銆怋CS101鏁版嵁鍚屾澶辫触銆戜繚瀛楽N鏁版嵁澶辫触, 閫佽揣鍗�: {}", asn);
+                        throw new RuntimeException("淇濆瓨SN鏁版嵁澶辫触");
+                    }
+                } else {
+                    log.warn("銆怋CS101鏁版嵁鍚屾銆戝搷搴斾腑娌℃湁SN鏁版嵁, 閫佽揣鍗�: {}", asn);
+                }
+            } else {
+                log.error("銆怋CS101鏁版嵁鍚屾澶辫触銆戞帴鍙h皟鐢ㄥけ璐�, 閫佽揣鍗�: {}, 閿欒: {}", asn, response.getErrorDesc());
+                throw new RuntimeException("BCS101鎺ュ彛璋冪敤澶辫触: " + response.getErrorDesc());
+            }
+
+        } catch (JsonProcessingException e) {
+            log.error("銆怋CS101鏁版嵁鍚屾澶辫触銆慗SON瑙f瀽寮傚父, 閫佽揣鍗�: {}, 寮傚父: {}", asn, e.getMessage(), e);
+            throw new RuntimeException("JSON瑙f瀽澶辫触: " + e.getMessage(), e);
+        }
+    }
 }
diff --git a/src/main/java/com/gs/xiaomi/service/Xm104Service.java b/src/main/java/com/gs/xiaomi/service/Xm104Service.java
index dfa014a..572c7f2 100644
--- a/src/main/java/com/gs/xiaomi/service/Xm104Service.java
+++ b/src/main/java/com/gs/xiaomi/service/Xm104Service.java
@@ -45,6 +45,8 @@
 
     private final LogisticsPackageService logisticsPackageService;
 
+    private final BCS101Service bcs101Service;
+
     public void getDb() {
         String format = DateUtil.format(new Date(), "yyyy-MM-dd");
         getXM104Save(format);
@@ -234,10 +236,19 @@
         try {
             String[] asns = numbericalDto.getAsn().split(",");
             for (String asn : asns) {
+                // 鍚屾XM104閫佽揣鍗曟暟鎹�
                 BizDocument doc = new BizDocument();
                 doc.setLifnr(DataAcquisitionConfiguration.LIFNR);
                 doc.setZzasn(asn);
                 getXM104(doc);
+
+                // 鍚屾BCS101鏉$爜鏁版嵁
+                try {
+                    bcs101Service.syncBCS101DataByAsn(asn);
+                } catch (Exception e) {
+                    log.error("銆怋CS101鏁版嵁鍚屾澶辫触銆戦�佽揣鍗�: {}, 寮傚父: {}", asn, e.getMessage(), e);
+                    // 涓嶅奖鍝嶅悗缁�佽揣鍗曠殑澶勭悊锛岀户缁墽琛�
+                }
             }
             return null; // 杩斿洖null琛ㄧず鎴愬姛
         } catch (Exception e) {
diff --git a/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java b/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java
index 3e6e966..955ab01 100644
--- a/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java
+++ b/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java
@@ -1,6 +1,7 @@
 package com.gs.xiaomi;
 
 import cn.hutool.crypto.digest.DigestUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.gs.xiaomi.dto.BCS101Request;
@@ -10,6 +11,7 @@
 import com.gs.xiaomi.entity.DeliveryMain;
 import com.gs.xiaomi.entity.SnListItem;
 import com.gs.xiaomi.service.BCS101ApiService;
+import com.gs.xiaomi.service.BCS101Service;
 import com.gs.xiaomi.service.DeliveryMainService;
 import com.gs.xiaomi.service.SnListItemService;
 import com.gs.xiaomi.service.Xm104Service;
@@ -36,6 +38,9 @@
 
     @Autowired
     private SnListItemService snListItemService;
+
+    @Autowired
+    private BCS101Service bcs101Service;
 
     @Test
     void contextLoads() throws Exception {
@@ -101,15 +106,15 @@
 
                         // 杞崲DTO涓篍ntity锛屽苟璁剧疆鍏宠仈淇℃伅
                         List<SnListItem> entityList = SnListItemConverter.toEntityList(
-                            snList,
-                            s.getId(),      // deliveryMainId - 閫佽揣鍗曚富琛↖D
-                            s.getZzasn()    // zzasn - 閫佽揣鍗曞彿
+                                snList,
+                                s.getId(),      // deliveryMainId - 閫佽揣鍗曚富琛↖D
+                                s.getZzasn()    // zzasn - 閫佽揣鍗曞彿
                         );
 
                         // 鍏堝垹闄よ閫佽揣鍗曞凡鏈夌殑SN鏁版嵁锛堥伩鍏嶉噸澶嶏級
                         snListItemService.lambdaUpdate()
-                            .eq(SnListItem::getZzasn, s.getZzasn())
-                            .remove();
+                                .eq(SnListItem::getZzasn, s.getZzasn())
+                                .remove();
 
                         // 鎵归噺淇濆瓨鍒版暟鎹簱
                         boolean saved = snListItemService.saveBatch(entityList);
@@ -131,4 +136,16 @@
             }
         });
     }
+
+
+    /**
+     * 娴嬭瘯BCS101鏁版嵁鍚屾 - 浣跨敤BCS101Service
+     * @param asn 閫佽揣鍗曞彿
+     * @throws Exception 寮傚父
+     */
+    @Test
+    void testGetBCS101() throws Exception {
+        // 鐩存帴璋冪敤BCS101Service杩涜鏁版嵁鍚屾
+        bcs101Service.syncBCS101DataByAsn("9316702418");
+    }
 }

--
Gitblit v1.9.3