Sfoglia il codice sorgente

fix: ProductController spec map JSON.parseArray type inference for Java 8

Xiaogang Liao 3 mesi fa
parent
commit
499b41dc0c

+ 54 - 3
cfc-backend/src/main/java/com/etotem/cfc/controller/product/ProductController.java

@@ -105,9 +105,60 @@ public class ProductController {
             for (ProductSku sku : skus) {
                 if (sku.getSpecs() == null) continue;
                 try {
-                    @SuppressWarnings("unchecked")
-                    List<Map<String, String>> specs = (List<Map<String, String>>) JSON.parseArray(sku.getSpecs());
-                    for (Map<String, String> spec : specs) {
+                    com.alibaba.fastjson.JSONArray arr = JSON.parseArray(sku.getSpecs());
+                    if (arr == null) continue;
+                    for (int i = 0; i < arr.size(); i++) {
+                        com.alibaba.fastjson.JSONObject specObj = arr.getJSONObject(i);
+                        if (specObj == null) continue;
+                        String name = specObj.getString("name");
+                        String value = specObj.getString("value");
+                        if (name == null || value == null) continue;
+
+                        if (!nameToGroupId.containsKey(name)) {
+                            nameToGroupId.put(name, groupId++);
+                            Map<String, Object> grp = new HashMap<>();
+                            grp.put("groupId", nameToGroupId.get(name));
+                            grp.put("groupName", name);
+                            grp.put("options", new ArrayList<Map<String, Object>>());
+                            groups.add(grp);
+                        }
+
+                        Map<String, Object> option = new HashMap<>();
+                        option.put("id", sku.getId());
+                        option.put("groupId", nameToGroupId.get(name));
+                        option.put("name", value);
+                        option.put("skuId", sku.getId());
+                        option.put("price", sku.getPrice());
+                        option.put("stock", sku.getStock());
+                        option.put("enabled", sku.getEnabled());
+
+                        for (Map<String, Object> g : groups) {
+                            if (g.get("groupId").equals(nameToGroupId.get(name))) {
+                                @SuppressWarnings("unchecked")
+                                List<Map<String, Object>> opts = (List<Map<String, Object>>) g.get("options");
+                                boolean exists = false;
+                                for (Map<String, Object> o : opts) {
+                                    if (value.equals(o.get("name")) && sku.getId().equals(o.get("skuId"))) {
+                                        exists = true;
+                                        break;
+                                    }
+                                }
+                                if (!exists) {
+                                    opts.add(option);
+                                }
+                            }
+                        }
+                    }
+                } catch (Exception e) {
+                    log.warn("parse specs JSON failed for sku {}: {}", sku.getId(), e.getMessage());
+                }
+            }
+
+            result.put(productId.toString(), groups);
+        }
+        return Result.success(result);
+    }
+}
                         String name = spec.get("name");
                         String value = spec.get("value");
                         if (name == null || value == null) continue;