tjx
昨天 979916a8922698475d43780ec72a63da618d6442
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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(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("【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);
                }
 
                // 获取cartonList数据
                List<CartonListItemDto> cartonList = response.getBody().getCartonList();
 
                if (cartonList != null && !cartonList.isEmpty()) {
                    log.info("【BCS101数据同步】获取到 {} 条箱数据,开始持久化...", cartonList.size());
 
                    // 转换DTO为Entity,并设置关联信息
                    List<CartonListItem> cartonEntityList = CartonListItemConverter.toEntityList(
                            cartonList,
                            deliveryMain.getId(),      // deliveryMainId - 送货单主表ID
                            deliveryMain.getZzasn()    // zzasn - 送货单号
                    );
 
                    // 先删除该送货单已有的箱数据(避免重复)
                    cartonListItemService.lambdaUpdate()
                            .eq(CartonListItem::getZzasn, deliveryMain.getZzasn())
                            .remove();
 
                    // 批量保存到数据库
                    boolean cartonSaved = cartonListItemService.saveBatch(cartonEntityList);
 
                    if (cartonSaved) {
                        log.info("【BCS101数据同步成功】成功保存 {} 条箱数据到数据库, 送货单: {}", cartonEntityList.size(), asn);
 
                        deliveryMainService.callPdaReceiptBtn1("送货单签收[BTNOK[PL017[" + deliveryMain.getZzasn(), "");
 
                    } else {
                        log.error("【BCS101数据同步失败】保存箱数据失败, 送货单: {}", asn);
                        throw new RuntimeException("保存箱数据失败");
                    }
                } else {
                    log.warn("【BCS101数据同步】响应中没有箱数据, 送货单: {}", 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);
        }
    }
}