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

feat(product-gift): 商品详情附带当前用户等级赠品配置

E2E Test Bot 2 недель назад
Родитель
Сommit
1444153b14

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

@@ -47,10 +47,11 @@ public class ProductController {
     }
 
     @PostMapping("/detail")
-    public Result<ProductDTO> detail(@RequestBody Map<String, Object> params) {
+    public Result<ProductDTO> detail(@RequestBody Map<String, Object> params,
+                                     @RequestAttribute(value = "userId", required = false) Long userId) {
         if (params.get("id") == null) return Result.error("缺少商品ID");
         Long id = ParamUtils.getLong(params.get("id"));
-        return productService.detail(id);
+        return productService.detail(id, userId);
     }
 
     @PostMapping("/create")

+ 3 - 0
cfc-backend/src/main/java/com/etotem/cfc/dto/ProductDTO.java

@@ -49,6 +49,9 @@ public class ProductDTO {
     private String giftLevelCode;
     private String giftLevelName;
 
+    // 商品可选赠品配置(当前用户等级对应,空=无赠品)
+    private List<GiftConfigVO> giftConfigs;
+
     // 配送与履约配置(按 deliveryMethod 附带 pickupLocations/servicePersons/onlineSlots)
     private Map<String, Object> deliveryConfig;
 

+ 25 - 1
cfc-backend/src/main/java/com/etotem/cfc/service/ProductService.java

@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.etotem.cfc.common.Result;
+import com.etotem.cfc.dto.GiftConfigVO;
+import com.etotem.cfc.dto.MembershipLevelDTO;
 import com.etotem.cfc.dto.ProductDTO;
 import com.etotem.cfc.dto.ProductListQueryDTO;
 import com.etotem.cfc.entity.AssessmentProduct;
@@ -45,6 +47,9 @@ public class ProductService {
     @Resource
     private ProductGiftRuleService productGiftRuleService;
 
+    @Resource
+    private ProductGiftService productGiftService;
+
     @Resource
     private AssessmentProductService assessmentProductService;
 
@@ -128,7 +133,7 @@ wrapper.in(Product::getId, productIds)
         return Result.success(data);
     }
 
-    public Result<ProductDTO> detail(Long id) {
+    public Result<ProductDTO> detail(Long id, Long userId) {
         Product product = productMapper.selectById(id);
         if (product == null) {
             return Result.error("商品不存在");
@@ -165,6 +170,25 @@ wrapper.in(Product::getId, productIds)
             dto.setGiftLevelCode(giftRule.getLevelCode());
             dto.setGiftLevelName(productGiftRuleService.getLevelName(giftRule.getLevelCode()));
         }
+        // 可选赠品配置(当前用户等级)
+        if (userId != null) {
+            try {
+                User buyer = userMapper.selectById(userId);
+                if (buyer != null && buyer.getFamilyId() != null) {
+                    MembershipLevelDTO currentLevel = membershipService.getCurrentLevel(buyer.getFamilyId());
+                    String userLevel = currentLevel != null ? currentLevel.getLevelCode() : null;
+                    if (userLevel != null && !"FREE".equals(userLevel)) {
+                        List<GiftConfigVO> activeConfigs = productGiftService
+                                .getActiveConfigsByProductAndLevel(id, userLevel);
+                        if (!activeConfigs.isEmpty()) {
+                            dto.setGiftConfigs(activeConfigs);
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                // 赠品配置加载失败不影响商品详情
+            }
+        }
         // 配送与履约配置(按 deliveryMethod 附带自取地点/服务者/线上时间段)
         Map<String, Object> deliveryConfig = new HashMap<>();
         productDeliveryConfigService.enrichProductDetail(id, product.getDeliveryMethod(), deliveryConfig);

+ 6 - 6
cfc-backend/src/test/java/com/etotem/cfc/integration/controller/ProductControllerTest.java

@@ -145,9 +145,9 @@ public class ProductControllerTest {
         mockProduct.setStock(100);
         mockProduct.setSalesCount(58);
 
-        when(productService.detail(1L)).thenReturn(Result.success(mockProduct));
+        when(productService.detail(1L, null)).thenReturn(Result.success(mockProduct));
 
-        Result<ProductDTO> result = controller.detail(params);
+        Result<ProductDTO> result = controller.detail(params, null);
 
         assertEquals(200, result.getCode());
         assertNotNull(result.getData());
@@ -160,9 +160,9 @@ public class ProductControllerTest {
         Map<String, Object> params = new HashMap<>();
         params.put("id", 999L);
 
-        when(productService.detail(999L)).thenReturn(Result.error("商品不存在"));
+        when(productService.detail(999L, null)).thenReturn(Result.error("商品不存在"));
 
-        Result<ProductDTO> result = controller.detail(params);
+        Result<ProductDTO> result = controller.detail(params, null);
 
         assertEquals(500, result.getCode());
     }
@@ -175,9 +175,9 @@ public class ProductControllerTest {
         ProductDTO mockProduct = createProductDTO(10L, "待发布任务模板", 9800, "draft");
         mockProduct.setStock(0);
 
-        when(productService.detail(10L)).thenReturn(Result.success(mockProduct));
+        when(productService.detail(10L, null)).thenReturn(Result.success(mockProduct));
 
-        Result<ProductDTO> result = controller.detail(params);
+        Result<ProductDTO> result = controller.detail(params, null);
 
         assertEquals(200, result.getCode());
         assertEquals("draft", result.getData().getStatus());