From f2b62569198d9afd1604a1617ab1e0a3e11f74bf Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期四, 30 十月 2025 18:40:22 +0800
Subject: [PATCH] 增加条码的持久化逻辑
---
src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java | 360 +++++++++++++++++------------------------------------------
1 files changed, 103 insertions(+), 257 deletions(-)
diff --git a/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java b/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java
index 297f9b1..955ab01 100644
--- a/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java
+++ b/src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java
@@ -1,35 +1,58 @@
package com.gs.xiaomi;
-import cn.hutool.core.date.DateUtil;
-import cn.hutool.crypto.SmUtil;
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.config.DataAcquisitionConfiguration;
import com.gs.xiaomi.dto.BCS101Request;
+import com.gs.xiaomi.dto.BCS101Response;
+import com.gs.xiaomi.dto.NumbericalDto;
+import com.gs.xiaomi.dto.SnListItemDto;
+import com.gs.xiaomi.entity.DeliveryMain;
+import com.gs.xiaomi.entity.SnListItem;
import com.gs.xiaomi.service.BCS101ApiService;
-import com.gs.xiaomi.service.XM104Service;
+import com.gs.xiaomi.service.BCS101Service;
+import com.gs.xiaomi.service.DeliveryMainService;
+import com.gs.xiaomi.service.SnListItemService;
+import com.gs.xiaomi.service.Xm104Service;
+import com.gs.xiaomi.util.SnListItemConverter;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.util.Base64;
-import java.util.Date;
+import java.io.IOException;
+import java.util.List;
+import java.util.stream.Collectors;
@SpringBootTest
class XiaomiApplicationTests {
@Autowired
- private XM104Service xm104Service;
+ private Xm104Service xm104Service;
@Autowired
private BCS101ApiService bcs101ApiService;
+ @Autowired
+ private DeliveryMainService deliveryMainService;
+
+ @Autowired
+ private SnListItemService snListItemService;
+
+ @Autowired
+ private BCS101Service bcs101Service;
+
@Test
void contextLoads() throws Exception {
xm104Service.getDb();
+ }
+
+ //manualSynchronization
+ @Test
+ void contextLoads1() throws Exception {
+ NumbericalDto barcode = new NumbericalDto();
+ barcode.setAsn("9316702418");
+ xm104Service.manualSynchronization(barcode);
}
@Test
@@ -44,262 +67,85 @@
@Test
void testBCS101ApiService() throws Exception {
- // 鍒涘缓娴嬭瘯璇锋眰鍙傛暟
- BCS101Request request = new BCS101Request();
- request.setSupplierId("177301");
- request.setDocNo("9311036039");
- request.setDocType("ASNGR");
- request.setPageNo(1);
- request.setPageSize(1000);
- //Error: ERROR_IN_MODULECHAIN;ERROR_IN_MODULECHAIN, Sender Channel 'CC_MI_OEM_HTTP_OUT' (ID: a58369adbaa03aafb76d2c02c2ae7cd7): Catching exception calling messaging system
- System.out.println("=== 娴嬭瘯BCS101 API鏈嶅姟锛圔asic Auth鐗堟湰锛� ===");
- try {
- // 璋冪敤鏍囧噯BCS101 API鏈嶅姟锛堝彧浣跨敤Basic Auth锛�
- String response = bcs101ApiService.getBCS101Data(request);
- System.out.println("BCS101 API Response: " + response);
- } catch (Exception e) {
- System.out.println("BCS101 API璋冪敤寮傚父: " + e.getMessage());
- e.printStackTrace();
- }
+ List<DeliveryMain> list = deliveryMainService.list();
- //Error: ERROR_IN_MODULECHAIN;ERROR_IN_MODULECHAIN, Sender Channel 'CC_MI_OEM_HTTP_OUT' (ID: a58369adbaa03aafb76d2c02c2ae7cd7): Catching exception calling messaging system
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛堝甫X5鍗忚鐗堟湰锛� ===");
- try {
- // 璋冪敤甯5鍗忚鐨凚CS101 API鏈嶅姟
- String responseWithX5 = bcs101ApiService.getBCS101DataWithX5(request, true);
- System.out.println("BCS101 API Response (X5): " + responseWithX5);
- } catch (Exception e) {
- System.out.println("BCS101 API X5璋冪敤寮傚父: " + e.getMessage());
- e.printStackTrace();
- }
+ //List<String> collect = list.stream().map(DeliveryMain::getZzasn).collect(Collectors.toList());
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛堣皟璇曠増鏈級 ===");
- try {
- // 璋冪敤璋冭瘯鐗堟湰API鏈嶅姟
- String debugResponse = bcs101ApiService.getBCS101DataDebug(request);
- System.out.println("BCS101 API Debug Response: " + debugResponse);
- } catch (Exception e) {
- System.out.println("BCS101 API 璋冭瘯鐗堟湰璋冪敤寮傚父: " + e.getMessage());
- e.printStackTrace();
- }
+ list.forEach(s -> {
+ System.out.println(String.valueOf(Integer.parseInt(s.getLifnr())) + ":" + s.getZzasn());
+ // 鍒涘缓娴嬭瘯璇锋眰鍙傛暟
+ BCS101Request request = new BCS101Request();
+ request.setSupplierId(String.valueOf(Integer.parseInt(s.getHubLifnr())));
+ request.setDocNo(s.getZzasn());
+ request.setDocType("ASNGR");
+ request.setPageNo(1);
+ request.setPageSize(1000);
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛堢畝鍖栫増鏈級 ===");
- try {
- // 璋冪敤绠�鍖栫増鏈珹PI鏈嶅姟
- String simpleResponse = bcs101ApiService.getBCS101DataSimple(request);
- System.out.println("BCS101 API Simple Response: " + simpleResponse);
- } catch (Exception e) {
- System.out.println("BCS101 API 绠�鍖栫増鏈皟鐢ㄥ紓甯�: " + e.getMessage());
- e.printStackTrace();
- }
+ // 璋冪敤API鑾峰彇鍘熷JSON瀛楃涓�
+ String bcs101Data = null;
+ try {
+ bcs101Data = bcs101ApiService.getBCS101Data(request);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ System.out.println("鍘熷鍝嶅簲: " + bcs101Data);
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛圶5鍗忚鏍囧噯鐗堟湰锛� ===");
- try {
- // 璋冪敤X5鍗忚鏍囧噯鐗堟湰
- String x5StandardResponse = bcs101ApiService.getBCS101DataX5Standard(request);
- System.out.println("BCS101 API X5Standard Response: " + x5StandardResponse);
- } catch (Exception e) {
- System.out.println("BCS101 API X5鏍囧噯鐗堟湰璋冪敤寮傚父: " + e.getMessage());
- e.printStackTrace();
- }
+ // 瑙f瀽JSON涓築CS101Response瀵硅薄
+ ObjectMapper objectMapper = new ObjectMapper();
+ try {
+ BCS101Response response = objectMapper.readValue(bcs101Data, BCS101Response.class);
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛圶5鍗忚鍙樹綋鐗堟湰锛� ===");
- try {
- // 璋冪敤X5鍗忚鍙樹綋鐗堟湰
- String x5VariantResponse = bcs101ApiService.getBCS101DataX5Variant(request);
- System.out.println("BCS101 API X5Variant Response: " + x5VariantResponse);
- } catch (Exception e) {
- System.out.println("BCS101 API X5鍙樹綋鐗堟湰璋冪敤寮傚父: " + e.getMessage());
- e.printStackTrace();
- }
+ // 妫�鏌ュ搷搴旀槸鍚︽垚鍔�
+ if (response.isSuccess()) {
+ // 鑾峰彇snList鏁版嵁
+ List<SnListItemDto> snList = response.getBody().getSnList();
+
+ if (snList != null && !snList.isEmpty()) {
+ System.out.println("鑾峰彇鍒� " + snList.size() + " 鏉N鏁版嵁锛屽紑濮嬫寔涔呭寲...");
+
+ // 杞崲DTO涓篍ntity锛屽苟璁剧疆鍏宠仈淇℃伅
+ List<SnListItem> entityList = SnListItemConverter.toEntityList(
+ snList,
+ s.getId(), // deliveryMainId - 閫佽揣鍗曚富琛↖D
+ s.getZzasn() // zzasn - 閫佽揣鍗曞彿
+ );
+
+ // 鍏堝垹闄よ閫佽揣鍗曞凡鏈夌殑SN鏁版嵁锛堥伩鍏嶉噸澶嶏級
+ snListItemService.lambdaUpdate()
+ .eq(SnListItem::getZzasn, s.getZzasn())
+ .remove();
+
+ // 鎵归噺淇濆瓨鍒版暟鎹簱
+ boolean saved = snListItemService.saveBatch(entityList);
+
+ if (saved) {
+ System.out.println("鎴愬姛淇濆瓨 " + entityList.size() + " 鏉N鏁版嵁鍒版暟鎹簱");
+ } else {
+ System.err.println("淇濆瓨SN鏁版嵁澶辫触锛�");
+ }
+ } else {
+ System.out.println("鍝嶅簲涓病鏈塖N鏁版嵁");
+ }
+ } else {
+ System.err.println("BCS101鎺ュ彛璋冪敤澶辫触: " + response.getErrorDesc());
+ }
+
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
+ });
}
+
+ /**
+ * 娴嬭瘯BCS101鏁版嵁鍚屾 - 浣跨敤BCS101Service
+ * @param asn 閫佽揣鍗曞彿
+ * @throws Exception 寮傚父
+ */
@Test
- void test1() throws UnsupportedEncodingException, JsonProcessingException {
- // 鍒涘缓娴嬭瘯璇锋眰瀵硅薄
- BCS101Request request = new BCS101Request();
- request.setSupplierId("177301");
- request.setDocNo("9311036039");
- request.setDocType("ASNGR");
- request.setPageNo(1);
- request.setPageSize(1000);
-
-
- ObjectMapper objectMapper = new ObjectMapper();
-
- System.out.println("=== BCS101 API 瀹為檯璇锋眰鍙傛暟璇︽儏 ===\n");
-
- //鏃堕棿
- String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss");
- System.out.println(yyyyMMddHHmmss);
-
- // 1. 鍘熷JSON鏁版嵁
- String jsonData = objectMapper.writeValueAsString(request);
- System.out.println("1. 鍘熷JSON鏁版嵁:");
- System.out.println(jsonData);
- System.out.println();
-
- // 2. Base64缂栫爜
- String base64Data = Base64.getEncoder().encodeToString(jsonData.getBytes("UTF-8"));
- System.out.println("2. Base64缂栫爜鍚�:");
- System.out.println(base64Data);
- System.out.println();
-
- // 3. URL缂栫爜
- String urlEncodedData = URLEncoder.encode(base64Data, "UTF-8");
- System.out.println("3. URL缂栫爜鍚�:");
- System.out.println(urlEncodedData);
- System.out.println();
-
- // 4. Form鏁版嵁
- String formData = "data=" + urlEncodedData;
- System.out.println("4. Form鏁版嵁 (璇锋眰Body):");
- System.out.println(formData);
- System.out.println();
-
- // 5. 璇锋眰URL
- System.out.println("5. 璇锋眰URL:");
- System.out.println(DataAcquisitionConfiguration.BCS_101_URL);
- System.out.println();
-
- // 6. Basic Auth
- String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD;
- String auth = Base64.getEncoder().encodeToString(credentials.getBytes("UTF-8"));
- System.out.println("6. Basic Auth淇℃伅:");
- System.out.println("鐢ㄦ埛鍚�: " + DataAcquisitionConfiguration.BCS_USER_NAME);
- System.out.println("瀵嗙爜: " + DataAcquisitionConfiguration.BCS_PWD);
- System.out.println("Base64缂栫爜: " + auth);
- System.out.println();
-
- // 7. X5鍗忚鍙傛暟
- String appId = DataAcquisitionConfiguration.BCS_APP_ID;
- String appKey = DataAcquisitionConfiguration.BCS_APP_Key;
- long timestamp = System.currentTimeMillis() / 1000;
-
- System.out.println("7. X5鍗忚鍙傛暟:");
- System.out.println("App ID: " + appId);
- System.out.println("App Key: " + appKey);
- System.out.println("Timestamp: " + timestamp);
- System.out.println();
-
- // 8. X5绛惧悕璁$畻
- String signString = appId + appKey + timestamp + formData;
- String signature;
- try {
- signature = SmUtil.sm3(signString).toUpperCase();
- System.out.println("8. X5绛惧悕 (浣跨敤SM3):");
- } catch (Exception e) {
- signature = DigestUtil.md5Hex(signString).toUpperCase();
- System.out.println("8. X5绛惧悕 (浣跨敤MD5):");
- }
- System.out.println("绛惧悕瀛楃涓�: " + signString);
- System.out.println("绛惧悕鍊�: " + signature);
- System.out.println();
-
- // 9. 瀹屾暣鐨凥TTP璇锋眰澶�
- System.out.println("9. 瀹屾暣鐨凥TTP璇锋眰澶�:");
- System.out.println("Content-Type: application/x-www-form-urlencoded");
- System.out.println("Authorization: Basic " + auth);
- System.out.println("X-App-Id: " + appId);
- System.out.println("X-Timestamp: " + timestamp);
- System.out.println("X-Signature: " + signature);
- System.out.println();
-
- // 10. 瀹屾暣鐨刢URL鍛戒护绀轰緥
- System.out.println("10. 瀹屾暣鐨刢URL鍛戒护绀轰緥:");
- System.out.println("curl -X POST \\");
- System.out.println(" '" + DataAcquisitionConfiguration.BCS_101_URL + "' \\");
- System.out.println(" -H 'Content-Type: application/x-www-form-urlencoded' \\");
- System.out.println(" -H 'Authorization: Basic " + auth + "' \\");
- System.out.println(" -H 'X-App-Id: " + appId + "' \\");
- System.out.println(" -H 'X-Timestamp: " + timestamp + "' \\");
- System.out.println(" -H 'X-Signature: " + signature + "' \\");
- System.out.println(" -d '" + formData + "'");
- System.out.println();
-
- // 11. Postman鏍煎紡
- System.out.println("11. Postman娴嬭瘯鏍煎紡:");
- System.out.println("Method: POST");
- System.out.println("URL: " + DataAcquisitionConfiguration.BCS_101_URL);
- System.out.println("Headers:");
- System.out.println(" Content-Type: application/x-www-form-urlencoded");
- System.out.println(" Authorization: Basic " + auth);
- System.out.println(" X-App-Id: " + appId);
- System.out.println(" X-Timestamp: " + timestamp);
- System.out.println(" X-Signature: " + signature);
- System.out.println("Body (x-www-form-urlencoded):");
- System.out.println(" data: " + urlEncodedData);
- System.out.println();
-
- // 12. 瀹為檯璋冪敤API骞惰幏鍙栧搷搴�
- System.out.println("12. === 瀹為檯API璋冪敤鍜屽搷搴� ===");
- StringBuilder responseResult = new StringBuilder();
-
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛圔asic Auth鐗堟湰锛� ===");
- try {
- String response = bcs101ApiService.getBCS101Data(request);
- System.out.println("BCS101 API Response: " + response);
- responseResult.append("Basic Auth鍝嶅簲: ").append(response.length()).append("瀛楃; ");
- } catch (Exception e) {
- System.out.println("BCS101 API璋冪敤寮傚父: " + e.getMessage());
- responseResult.append("Basic Auth寮傚父: ").append(e.getMessage()).append("; ");
- }
-
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛堝甫X5鍗忚鐗堟湰锛� ===");
- try {
- String responseWithX5 = bcs101ApiService.getBCS101DataWithX5(request, true);
- System.out.println("BCS101 API Response (X5): " + responseWithX5);
- responseResult.append("X5鍗忚鍝嶅簲: ").append(responseWithX5.length()).append("瀛楃; ");
- } catch (Exception e) {
- System.out.println("BCS101 API X5璋冪敤寮傚父: " + e.getMessage());
- responseResult.append("X5鍗忚寮傚父: ").append(e.getMessage()).append("; ");
- }
-
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛堣皟璇曠増鏈級 ===");
- try {
- String debugResponse = bcs101ApiService.getBCS101DataDebug(request);
- System.out.println("BCS101 API Debug Response: " + debugResponse);
- responseResult.append("璋冭瘯鐗堟湰鍝嶅簲: ").append(debugResponse.length()).append("瀛楃; ");
- } catch (Exception e) {
- System.out.println("BCS101 API 璋冭瘯鐗堟湰璋冪敤寮傚父: " + e.getMessage());
- responseResult.append("璋冭瘯鐗堟湰寮傚父: ").append(e.getMessage()).append("; ");
- }
-
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛堢畝鍖栫増鏈級 ===");
- try {
- String simpleResponse = bcs101ApiService.getBCS101DataSimple(request);
- System.out.println("BCS101 API Simple Response: " + simpleResponse);
- responseResult.append("绠�鍖栫増鏈搷搴�: ").append(simpleResponse.length()).append("瀛楃; ");
- } catch (Exception e) {
- System.out.println("BCS101 API 绠�鍖栫増鏈皟鐢ㄥ紓甯�: " + e.getMessage());
- responseResult.append("绠�鍖栫増鏈紓甯�: ").append(e.getMessage()).append("; ");
- }
-
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛圶5鍗忚鏍囧噯鐗堟湰锛� ===");
- try {
- String x5StandardResponse = bcs101ApiService.getBCS101DataX5Standard(request);
- System.out.println("BCS101 API X5Standard Response: " + x5StandardResponse);
- responseResult.append("X5鏍囧噯鐗堟湰鍝嶅簲: ").append(x5StandardResponse.length()).append("瀛楃; ");
- } catch (Exception e) {
- System.out.println("BCS101 API X5鏍囧噯鐗堟湰璋冪敤寮傚父: " + e.getMessage());
- responseResult.append("X5鏍囧噯鐗堟湰寮傚父: ").append(e.getMessage()).append("; ");
- }
-
- System.out.println("\n=== 娴嬭瘯BCS101 API鏈嶅姟锛圶5鍗忚鍙樹綋鐗堟湰锛� ===");
- try {
- String x5VariantResponse = bcs101ApiService.getBCS101DataX5Variant(request);
- System.out.println("BCS101 API X5Variant Response: " + x5VariantResponse);
- responseResult.append("X5鍙樹綋鐗堟湰鍝嶅簲: ").append(x5VariantResponse.length()).append("瀛楃; ");
- } catch (Exception e) {
- System.out.println("BCS101 API X5鍙樹綋鐗堟湰璋冪敤寮傚父: " + e.getMessage());
- responseResult.append("X5鍙樹綋鐗堟湰寮傚父: ").append(e.getMessage()).append("; ");
- }
-
- // 杩斿洖姹囨�讳俊鎭�
- String summary = String.format("璇锋眰鍙傛暟[渚涘簲鍟咺D: %s, 鍗曟嵁鍙�: %s, 绛惧悕: %s, 鏃堕棿鎴�: %d] | 鍝嶅簲姹囨�籟%s]",
- request.getSupplierId(), request.getDocNo(), signature, timestamp, responseResult.toString());
- System.out.println("\n=== 娴嬭瘯姹囨�� ===");
- System.out.println(summary);
+ void testGetBCS101() throws Exception {
+ // 鐩存帴璋冪敤BCS101Service杩涜鏁版嵁鍚屾
+ bcs101Service.syncBCS101DataByAsn("9316702418");
}
}
--
Gitblit v1.9.3