tjx
2025-10-30 f2b62569198d9afd1604a1617ab1e0a3e11f74bf
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服务(Basic 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 &#39;CC_MI_OEM_HTTP_OUT&#39; (ID: a58369adbaa03aafb76d2c02c2ae7cd7): Catching exception calling messaging system
        System.out.println("\n=== 测试BCS101 API服务(带X5协议版本) ===");
        try {
            // 调用带X5协议的BCS101 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 {
            // 调用简化版本API服务
            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服务(X5协议标准版本) ===");
        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();
        }
            // 解析JSON为BCS101Response对象
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                BCS101Response response = objectMapper.readValue(bcs101Data, BCS101Response.class);
        System.out.println("\n=== 测试BCS101 API服务(X5协议变体版本) ===");
        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() + " 条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 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. 完整的HTTP请求头
        System.out.println("9. 完整的HTTP请求头:");
        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. 完整的cURL命令示例
        System.out.println("10. 完整的cURL命令示例:");
        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服务(Basic 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服务(X5协议标准版本) ===");
        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服务(X5协议变体版本) ===");
        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("请求参数[供应商ID: %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");
    }
}