Просмотр исходного кода

feat: CartService.buildCartItemDTO builds specDesc from ProductSku.specs JSON when skuId is present

Xiaogang Liao 2 месяцев назад
Родитель
Сommit
7c7c810b58
1 измененных файлов с 30 добавлено и 0 удалено
  1. 30 0
      cfc-backend/src/main/java/com/etotem/cfc/service/CartService.java

+ 30 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/CartService.java

@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.etotem.cfc.dto.CartItemDTO;
 import com.etotem.cfc.entity.Cart;
 import com.etotem.cfc.entity.Product;
+import com.etotem.cfc.entity.ProductSku;
+import com.etotem.cfc.service.ProductSkuService;
 import com.etotem.cfc.mapper.CartMapper;
 import com.etotem.cfc.mapper.ProductMapper;
 import lombok.extern.slf4j.Slf4j;
@@ -14,6 +16,8 @@ import javax.annotation.Resource;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 
 @Slf4j
 @Service
@@ -25,6 +29,9 @@ public class CartService {
     @Resource
     private ProductMapper productMapper;
 
+    @Resource
+    private ProductSkuService productSkuService;
+
     @Transactional
     public CartItemDTO addToCart(Long userId, Long productId, Integer quantity, Long skuId) {
         Product product = productMapper.selectById(productId);
@@ -141,6 +148,29 @@ public class CartService {
             dto.setUnitPrice(product.getPrice() != null ? product.getPrice() : 0);
             dto.setSupplierName(product.getVendorName());
         }
+        if (cart.getSkuId() != null) {
+            ProductSku sku = productSkuService.getById(cart.getSkuId());
+            if (sku != null && sku.getSpecs() != null) {
+                try {
+                    JSONArray specsArr = JSON.parseArray(sku.getSpecs());
+                    if (specsArr != null) {
+                        StringBuilder specDesc = new StringBuilder();
+                        for (int i = 0; i < specsArr.size(); i++) {
+                            com.alibaba.fastjson.JSONObject specObj = specsArr.getJSONObject(i);
+                            if (specObj == null) continue;
+                            String name = specObj.getString("name");
+                            String value = specObj.getString("value");
+                            if (name == null || value == null) continue;
+                            if (specDesc.length() > 0) specDesc.append(";");
+                            specDesc.append(name).append(":").append(value);
+                        }
+                        dto.setSpecDesc(specDesc.toString());
+                    }
+                } catch (Exception e) {
+                    log.warn("解析SKU specs失败: skuId={}", cart.getSkuId(), e);
+                }
+            }
+        }
         return dto;
     }
 }