tjx
2025-10-30 f2b62569198d9afd1604a1617ab1e0a3e11f74bf
增加条码的持久化逻辑
已修改3个文件
141 ■■■■■ 文件已修改
src/main/java/com/gs/xiaomi/service/BCS101Service.java 103 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/gs/xiaomi/service/Xm104Service.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/java/com/gs/xiaomi/XiaomiApplicationTests.java 27 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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("【BCS101数据同步】开始同步送货单: {}", asn);
        // 查询送货单主表信息
        LambdaQueryWrapper<DeliveryMain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliveryMain::getZzasn, asn);
        DeliveryMain deliveryMain = deliveryMainService.getOne(queryWrapper, false);
        if (deliveryMain == null) {
            log.error("【BCS101数据同步失败】送货单不存在: {}", 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("【BCS101数据同步】原始响应: {}", bcs101Data);
        } catch (IOException e) {
            log.error("【BCS101数据同步失败】调用API异常, 送货单: {}, 异常: {}", asn, e.getMessage(), e);
            throw new RuntimeException("调用BCS101接口失败: " + e.getMessage(), e);
        }
        // 解析JSON为BCS101Response对象
        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("【BCS101数据同步】获取到 {} 条SN数据,开始持久化...", snList.size());
                    // 转换DTO为Entity,并设置关联信息
                    List<SnListItem> entityList = SnListItemConverter.toEntityList(
                            snList,
                            deliveryMain.getId(),      // deliveryMainId - 送货单主表ID
                            deliveryMain.getZzasn()    // zzasn - 送货单号
                    );
                    // 先删除该送货单已有的SN数据(避免重复)
                    snListItemService.lambdaUpdate()
                            .eq(SnListItem::getZzasn, deliveryMain.getZzasn())
                            .remove();
                    // 批量保存到数据库
                    boolean saved = snListItemService.saveBatch(entityList);
                    if (saved) {
                        log.info("【BCS101数据同步成功】成功保存 {} 条SN数据到数据库, 送货单: {}", entityList.size(), asn);
                    } else {
                        log.error("【BCS101数据同步失败】保存SN数据失败, 送货单: {}", asn);
                        throw new RuntimeException("保存SN数据失败");
                    }
                } else {
                    log.warn("【BCS101数据同步】响应中没有SN数据, 送货单: {}", asn);
                }
            } else {
                log.error("【BCS101数据同步失败】接口调用失败, 送货单: {}, 错误: {}", asn, response.getErrorDesc());
                throw new RuntimeException("BCS101接口调用失败: " + response.getErrorDesc());
            }
        } catch (JsonProcessingException e) {
            log.error("【BCS101数据同步失败】JSON解析异常, 送货单: {}, 异常: {}", asn, e.getMessage(), e);
            throw new RuntimeException("JSON解析失败: " + e.getMessage(), e);
        }
    }
}
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("【BCS101数据同步失败】送货单: {}, 异常: {}", asn, e.getMessage(), e);
                    // 不影响后续送货单的处理,继续执行
                }
            }
            return null; // 返回null表示成功
        } catch (Exception e) {
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为Entity,并设置关联信息
                        List<SnListItem> entityList = SnListItemConverter.toEntityList(
                            snList,
                            s.getId(),      // deliveryMainId - 送货单主表ID
                            s.getZzasn()    // zzasn - 送货单号
                                snList,
                                s.getId(),      // deliveryMainId - 送货单主表ID
                                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");
    }
}