From 979916a8922698475d43780ec72a63da618d6442 Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期一, 15 十二月 2025 15:50:24 +0800
Subject: [PATCH] 增加条码的转换逻辑
---
src/main/java/com/gs/xiaomi/service/BCS101Service.java | 141 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 140 insertions(+), 1 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..b4bf307 100644
--- a/src/main/java/com/gs/xiaomi/service/BCS101Service.java
+++ b/src/main/java/com/gs/xiaomi/service/BCS101Service.java
@@ -1,16 +1,155 @@
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.CartonListItemDto;
+import com.gs.xiaomi.dto.SnListItemDto;
+import com.gs.xiaomi.entity.CartonListItem;
+import com.gs.xiaomi.entity.DeliveryMain;
+import com.gs.xiaomi.entity.SnListItem;
+import com.gs.xiaomi.util.CartonListItemConverter;
+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;
+ private final CartonListItemService cartonListItemService;
+
+ /**
+ * 鏍规嵁閫佽揣鍗曞彿鑾峰彇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);
+ }
+
+ // 鑾峰彇cartonList鏁版嵁
+ List<CartonListItemDto> cartonList = response.getBody().getCartonList();
+
+ if (cartonList != null && !cartonList.isEmpty()) {
+ log.info("銆怋CS101鏁版嵁鍚屾銆戣幏鍙栧埌 {} 鏉$鏁版嵁锛屽紑濮嬫寔涔呭寲...", cartonList.size());
+
+ // 杞崲DTO涓篍ntity锛屽苟璁剧疆鍏宠仈淇℃伅
+ List<CartonListItem> cartonEntityList = CartonListItemConverter.toEntityList(
+ cartonList,
+ deliveryMain.getId(), // deliveryMainId - 閫佽揣鍗曚富琛↖D
+ deliveryMain.getZzasn() // zzasn - 閫佽揣鍗曞彿
+ );
+
+ // 鍏堝垹闄よ閫佽揣鍗曞凡鏈夌殑绠辨暟鎹紙閬垮厤閲嶅锛�
+ cartonListItemService.lambdaUpdate()
+ .eq(CartonListItem::getZzasn, deliveryMain.getZzasn())
+ .remove();
+
+ // 鎵归噺淇濆瓨鍒版暟鎹簱
+ boolean cartonSaved = cartonListItemService.saveBatch(cartonEntityList);
+
+ if (cartonSaved) {
+ log.info("銆怋CS101鏁版嵁鍚屾鎴愬姛銆戞垚鍔熶繚瀛� {} 鏉$鏁版嵁鍒版暟鎹簱, 閫佽揣鍗�: {}", cartonEntityList.size(), asn);
+
+ deliveryMainService.callPdaReceiptBtn1("閫佽揣鍗曠鏀禰BTNOK[PL017[" + deliveryMain.getZzasn(), "");
+
+ } else {
+ log.error("銆怋CS101鏁版嵁鍚屾澶辫触銆戜繚瀛樼鏁版嵁澶辫触, 閫佽揣鍗�: {}", asn);
+ throw new RuntimeException("淇濆瓨绠辨暟鎹け璐�");
+ }
+ } else {
+ log.warn("銆怋CS101鏁版嵁鍚屾銆戝搷搴斾腑娌℃湁绠辨暟鎹�, 閫佽揣鍗�: {}", 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);
+ }
+ }
}
--
Gitblit v1.9.3