tjx
2025-10-30 f2b62569198d9afd1604a1617ab1e0a3e11f74bf
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
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;
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.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.IOException;
import java.util.List;
import java.util.stream.Collectors;
 
@SpringBootTest
class XiaomiApplicationTests {
 
    @Autowired
    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
    void test() {
//        String str = ",,";
//        String[] parts = str.split(",");
//        for (String part : parts) {
//            System.out.println(part);
//        }
        System.out.println(DigestUtil.md5Hex("VDATA.XM104/177301/c5Kl}xN&i(").toUpperCase());
    }
 
    @Test
    void testBCS101ApiService() throws Exception {
 
        List<DeliveryMain> list = deliveryMainService.list();
 
        //List<String> collect = list.stream().map(DeliveryMain::getZzasn).collect(Collectors.toList());
 
        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);
 
            // 调用API获取原始JSON字符串
            String bcs101Data = null;
            try {
                bcs101Data = bcs101ApiService.getBCS101Data(request);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            System.out.println("原始响应: " + bcs101Data);
 
            // 解析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()) {
                        System.out.println("获取到 " + snList.size() + " 条SN数据,开始持久化...");
 
                        // 转换DTO为Entity,并设置关联信息
                        List<SnListItem> entityList = SnListItemConverter.toEntityList(
                                snList,
                                s.getId(),      // deliveryMainId - 送货单主表ID
                                s.getZzasn()    // zzasn - 送货单号
                        );
 
                        // 先删除该送货单已有的SN数据(避免重复)
                        snListItemService.lambdaUpdate()
                                .eq(SnListItem::getZzasn, s.getZzasn())
                                .remove();
 
                        // 批量保存到数据库
                        boolean saved = snListItemService.saveBatch(entityList);
 
                        if (saved) {
                            System.out.println("成功保存 " + entityList.size() + " 条SN数据到数据库");
                        } else {
                            System.err.println("保存SN数据失败!");
                        }
                    } else {
                        System.out.println("响应中没有SN数据");
                    }
                } else {
                    System.err.println("BCS101接口调用失败: " + response.getErrorDesc());
                }
 
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });
    }
 
 
    /**
     * 测试BCS101数据同步 - 使用BCS101Service
     * @param asn 送货单号
     * @throws Exception 异常
     */
    @Test
    void testGetBCS101() throws Exception {
        // 直接调用BCS101Service进行数据同步
        bcs101Service.syncBCS101DataByAsn("9316702418");
    }
}