Parcourir la source

feat(backend): specMap 扩展返回 linked 商品摘要,库存为0/已下架不显示

asus il y a 1 mois
Parent
commit
60d995d5e3

+ 45 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/product/ProductController.java

@@ -107,6 +107,50 @@ public class ProductController {
             int groupId = 1;
 
             for (ProductSku sku : skus) {
+                boolean isLinked = sku.getLinkedProductId() != null;
+
+                if (isLinked) {
+                    // Linked SKU: 查询关联商品摘要,库存为0/不存在则跳过
+                    Map<String, Object> linkedSummary = productSkuService.getLinkedProductSummary(sku.getLinkedProductId());
+                    if (linkedSummary == null) continue;
+
+                    // 选项名:label 优先,否则用 linked 商品名
+                    String optionName = sku.getLabel();
+                    if (optionName == null || optionName.trim().isEmpty()) {
+                        optionName = (String) linkedSummary.get("name");
+                    }
+
+                    // 用 "关联商品" 作为分组名
+                    String groupName = "关联商品";
+                    if (!nameToGroupId.containsKey(groupName)) {
+                        nameToGroupId.put(groupName, groupId++);
+                        Map<String, Object> grp = new HashMap<>();
+                        grp.put("groupId", nameToGroupId.get(groupName));
+                        grp.put("groupName", groupName);
+                        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(groupName));
+                    option.put("name", optionName);
+                    option.put("skuId", sku.getId());
+                    option.put("linkedProductId", sku.getLinkedProductId());
+                    option.put("isLinked", true);
+                    option.put("linked", linkedSummary);
+
+                    for (Map<String, Object> g : groups) {
+                        if (g.get("groupId").equals(nameToGroupId.get(groupName))) {
+                            @SuppressWarnings("unchecked")
+                            List<Map<String, Object>> opts = (List<Map<String, Object>>) g.get("options");
+                            opts.add(option);
+                        }
+                    }
+                    continue;
+                }
+
+                // Legacy SKU: 原有 specs 解析逻辑
                 if (sku.getSpecs() == null) continue;
                 if (sku.getSpecs().toString().trim().isEmpty()) continue;
                 try {
@@ -133,6 +177,7 @@ public class ProductController {
                         option.put("price", sku.getPrice());
                         option.put("stock", sku.getStock());
                         option.put("enabled", sku.getEnabled());
+                        option.put("isLinked", false);
 
                         for (Map<String, Object> g : groups) {
                             if (g.get("groupId").equals(nameToGroupId.get(name))) {

+ 24 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/ProductSkuService.java

@@ -1,7 +1,9 @@
 package com.etotem.cfc.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.etotem.cfc.entity.Product;
 import com.etotem.cfc.entity.ProductSku;
+import com.etotem.cfc.mapper.ProductMapper;
 import com.etotem.cfc.mapper.ProductSkuMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -22,6 +24,9 @@ public class ProductSkuService {
     @Resource
     private ProductSkuMapper skuMapper;
 
+    @Resource
+    private ProductMapper productMapper;
+
     public List<ProductSku> listByProductId(Long productId) {
         return skuMapper.findByProductId(productId);
     }
@@ -96,6 +101,25 @@ public class ProductSkuService {
         return sku.getStock() >= quantity;
     }
 
+    /**
+     * 查询 linked 商品摘要(用于 specMap 扩展)
+     * @return null 如果商品不存在或库存为0
+     */
+    public java.util.Map<String, Object> getLinkedProductSummary(Long linkedProductId) {
+        if (linkedProductId == null) return null;
+        Product product = productMapper.selectById(linkedProductId);
+        if (product == null) return null;
+        if (product.getStock() != null && product.getStock() == 0) return null;
+        java.util.Map<String, Object> summary = new java.util.HashMap<>();
+        summary.put("id", product.getId());
+        summary.put("name", product.getName());
+        summary.put("price", product.getPrice());
+        summary.put("stock", product.getStock());
+        summary.put("image", product.getCoverImage());
+        summary.put("brief", product.getIntro());
+        return summary;
+    }
+
     /**
      * 将 specs 规格字符串规范化为合法 JSON 对象。
      *