فهرست منبع

fix: 设备可售卖商品列表 API 数据解析修复

- data key: "list" → "dataInfo"(与 API 实际返回结构匹配)
- API 返回 dataInfo 为分类数组,需展开 goods 子数组获取商品列表
- 商品字段映射修正: product_name→name, 去除不存在的 bar_code
- 价格字段: price(Number)→c_price(String),兼容字符串解析

Co-Authored-By: Claude <noreply@anthropic.com>
skyline 2 روز پیش
والد
کامیت
b7e5e89233
1فایلهای تغییر یافته به همراه23 افزوده شده و 12 حذف شده
  1. 23 12
      haha-service/src/main/java/com/haha/service/impl/ProductServiceImpl.java

+ 23 - 12
haha-service/src/main/java/com/haha/service/impl/ProductServiceImpl.java

@@ -137,19 +137,30 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
     public List<Product> getProductListBydeviceId(String deviceId) {
         try {
             Map<String, Object> result = hahaClient.getGoodsApi().getDeviceGoodsList(deviceId, null);
-            if (result != null && result.containsKey("list")) {
+            if (result != null && result.containsKey("dataInfo")) {
                 @SuppressWarnings("unchecked")
-                List<Map<String, Object>> apiList = (List<Map<String, Object>>) result.get("list");
-                return apiList.stream().map(item -> {
-                    Product product = new Product();
-                    product.setCode((String) item.get("code"));
-                    product.setName((String) item.get("product_name"));
-                    product.setBarcode((String) item.get("bar_code"));
-                    if (item.get("price") != null) {
-                        product.setPrice(((Number) item.get("price")).doubleValue());
-                    }
-                    return product;
-                }).collect(Collectors.toList());
+                List<Map<String, Object>> dataInfoList = (List<Map<String, Object>>) result.get("dataInfo");
+                return dataInfoList.stream()
+                    .flatMap(category -> {
+                        @SuppressWarnings("unchecked")
+                        List<Map<String, Object>> goodsList = (List<Map<String, Object>>) category.get("goods");
+                        if (goodsList == null) return java.util.stream.Stream.empty();
+                        return goodsList.stream().map(item -> {
+                            Product product = new Product();
+                            product.setCode((String) item.get("code"));
+                            product.setName((String) item.get("name"));
+                            product.setPic((String) item.get("pic"));
+                            if (item.get("c_price") != null) {
+                                try {
+                                    product.setPrice(Double.parseDouble(item.get("c_price").toString()));
+                                } catch (NumberFormatException e) {
+                                    log.debug("价格解析失败: {}", item.get("c_price"));
+                                }
+                            }
+                            return product;
+                        });
+                    })
+                    .collect(Collectors.toList());
             }
         } catch (Exception e) {
             log.error("获取设备商品列表失败 - deviceId: {}, error: {}", deviceId, e.getMessage());