فهرست منبع

feat(bundle): 下单金额由服务端权威重算套餐优惠(createMultiItem + 单件create)\n\n- createMultiItem 收集解析后身份行项,服务端算套餐折扣计入 multiDiscountAmount\n- 单件 create() 按下单最终单价(含履约覆盖价)算单行套餐折扣\n- 前端 discountAmount 语义改为仅规则优惠,不做套餐;3 组单测验证防篡改\n\n注:现存 3 个并发会话测试失败(ServiceRoleApprove/TaskPlan/TaskProgress),非本次改动引入

Xiaogang Liao 3 هفته پیش
والد
کامیت
9d62cc354f

+ 15 - 2
cfc-backend/src/main/java/com/etotem/cfc/service/ProductOrderService.java

@@ -12,6 +12,7 @@ import com.etotem.cfc.entity.AssessmentProduct;
 import com.etotem.cfc.entity.Consignee;
 import com.etotem.cfc.entity.UserAddress;
 import com.etotem.cfc.mapper.UserAddressMapper;
+import com.etotem.cfc.pricing.LineItem;
 import com.etotem.cfc.entity.Coupon;
 import com.etotem.cfc.entity.FamilyCoupon;
 import com.etotem.cfc.entity.Product;
@@ -97,6 +98,8 @@ public class ProductOrderService {
     private ProductSkuService productSkuService;
     @Resource
     private MemberPriceResolver memberPriceResolver;
+    @Resource
+    private BundlePricingService bundlePricingService;
 
     @Resource
     private InventoryService inventoryService;
@@ -272,7 +275,12 @@ public class ProductOrderService {
         order.setPointsUsed(dto.getPointsUsed() != null ? dto.getPointsUsed() : 0);
         int pointsCost = dto.getPointsUsed() != null ? dto.getPointsUsed() : 0;
         order.setPointsCost(pointsCost);
-        int discountAmount = dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0;
+        // 套餐自动定价优惠(服务端权威):按下单最终单价(含履约覆盖价)计算;前端 discountAmount 仅含规则优惠
+        int baseUnitPrice = memberPriceResolver.resolveEligibleMemberPrice(product, buyerId);
+        int effectiveUnitPrice = fv.unitPrice != null ? fv.unitPrice : baseUnitPrice;
+        int bundleDiscount = bundlePricingService.compute(java.util.Collections.singletonList(
+                new LineItem(product.getId(), null, dto.getQuantity(), effectiveUnitPrice))).getDiscount();
+        int discountAmount = (dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0) + bundleDiscount;
         // 优惠券抵扣:校验并计入优惠金额(orderType=PRODUCT)
         Long appliedCouponId = null;
         if (dto.getCouponId() != null) {
@@ -747,6 +755,8 @@ public class ProductOrderService {
         java.util.List<OrderItem> orderItemRecords = new java.util.ArrayList<>();
         Product firstProduct = null;
         int firstUnitPrice = 0;
+        // 收集下单口径行项(解析后身份 + 结算单价),用于服务端权威的套餐自动定价
+        java.util.List<LineItem> bundleLines = new java.util.ArrayList<>();
 
         for (OrderItemVO item : dto.getItems()) {
             Product product = productMapper.selectById(item.getProductId());
@@ -816,6 +826,7 @@ public class ProductOrderService {
             itemRecord.setQuantity(item.getQuantity());
             itemRecord.setSubtotal(java.math.BigDecimal.valueOf((long) unitPrice * item.getQuantity()));
             orderItemRecords.add(itemRecord);
+            bundleLines.add(new LineItem(product.getId(), item.getSkuId(), item.getQuantity(), unitPrice));
         }
 
         User buyer = userMapper.selectById(buyerId);
@@ -833,7 +844,9 @@ public class ProductOrderService {
         order.setPointsUsed(dto.getPointsUsed() != null ? dto.getPointsUsed() : 0);
         int pointsCost = dto.getPointsUsed() != null ? dto.getPointsUsed() : 0;
         order.setPointsCost(pointsCost);
-        int multiDiscountAmount = dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0;
+        // 套餐自动定价优惠(服务端权威):前端 discountAmount 仅含规则优惠,套餐部分由服务端重算加入
+        int bundleDiscount = bundlePricingService.compute(bundleLines).getDiscount();
+        int multiDiscountAmount = (dto.getDiscountAmount() != null ? dto.getDiscountAmount() : 0) + bundleDiscount;
         // 优惠券抵扣:校验并计入优惠金额(orderType=PRODUCT)
         Long appliedCouponId = null;
         if (dto.getCouponId() != null) {

+ 140 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/ProductOrderServiceTest.java

@@ -0,0 +1,140 @@
+package com.etotem.cfc.unit;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.dto.CreateProductOrderDTO;
+import com.etotem.cfc.dto.OrderItemVO;
+import com.etotem.cfc.dto.ProductOrderDTO;
+import com.etotem.cfc.entity.Product;
+import com.etotem.cfc.entity.User;
+import com.etotem.cfc.mapper.OrderItemMapper;
+import com.etotem.cfc.mapper.ProductMapper;
+import com.etotem.cfc.mapper.ProductOrderMapper;
+import com.etotem.cfc.mapper.UserMapper;
+import com.etotem.cfc.pricing.BundleDiscountResult;
+import com.etotem.cfc.pricing.LineItem;
+import com.etotem.cfc.service.BundlePricingService;
+import com.etotem.cfc.service.CouponService;
+import com.etotem.cfc.service.MemberPriceResolver;
+import com.etotem.cfc.service.ProductGiftRuleService;
+import com.etotem.cfc.service.ProductOrderService;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * ProductOrderService.createMultiItem 单测:服务端权威重算套餐优惠计入 moneyAmount(前端 discountAmount 仅含规则优惠)。
+ */
+@ExtendWith(MockitoExtension.class)
+class ProductOrderServiceTest {
+
+    @Mock private ProductMapper productMapper;
+    @Mock private UserMapper userMapper;
+    @Mock private ProductOrderMapper orderMapper;
+    @Mock private OrderItemMapper orderItemMapper;
+    @Mock private MemberPriceResolver memberPriceResolver;
+    @Mock private BundlePricingService bundlePricingService;
+    @Mock private ProductGiftRuleService productGiftRuleService;
+    @Mock private CouponService couponService;
+    @InjectMocks private ProductOrderService orderService;
+
+    private Product product(Long id, String name, int price) {
+        Product p = new Product();
+        p.setId(id);
+        p.setName(name);
+        p.setPrice(price);
+        p.setProductType("assessment");
+        p.setStatus("on_shelf");
+        return p;
+    }
+
+    private CreateProductOrderDTO dto(long productId, int qty, Integer discountAmount) {
+        CreateProductOrderDTO dto = new CreateProductOrderDTO();
+        OrderItemVO item = new OrderItemVO();
+        item.setProductId(productId);
+        item.setQuantity(qty);
+        dto.setItems(Collections.singletonList(item));
+        dto.setDiscountAmount(discountAmount);
+        return dto;
+    }
+
+    @Test
+    void createMultiItem_addsServerBundleDiscountToMoneyAmount() {
+        // 2×69900 配套餐省11800 → moneyAmount = 139800 - 0 - 11800 = 128000
+        when(productMapper.selectById(10L)).thenReturn(product(10L, "DAN测评", 69900));
+        when(memberPriceResolver.resolveEligibleMemberPrice(any(Product.class), anyLong())).thenReturn(69900);
+        BundleDiscountResult r = new BundleDiscountResult();
+        r.setActive(true);
+        r.setOriginalTotal(139800);
+        r.setPayPrice(128000);
+        r.setDiscount(11800);
+        r.setBundleList(new ArrayList<>());
+        when(bundlePricingService.compute(anyList())).thenReturn(r);
+        when(userMapper.selectById(anyLong())).thenReturn(new User());
+        when(orderMapper.insert(any())).thenReturn(1);
+        when(orderItemMapper.insert(any())).thenReturn(1);
+
+        Result<ProductOrderDTO> result = orderService.create(dto(10L, 2, null), 1L);
+
+        assertTrue(result.getCode() == 200, "下单应成功: " + result.getMessage());
+        assertEquals(128000, result.getData().getMoneyAmount());
+    }
+
+    @Test
+    void createMultiItem_bundleLinesCollectResolvedIdentityAndPrice() {
+        // 收集行项:解析后 productId、skuId=null、quantity、unitPrice=69900
+        when(productMapper.selectById(10L)).thenReturn(product(10L, "DAN测评", 69900));
+        when(memberPriceResolver.resolveEligibleMemberPrice(any(Product.class), anyLong())).thenReturn(69900);
+        when(bundlePricingService.compute(anyList())).thenReturn(BundleDiscountResult.inactive());
+        when(userMapper.selectById(anyLong())).thenReturn(new User());
+        when(orderMapper.insert(any())).thenReturn(1);
+        when(orderItemMapper.insert(any())).thenReturn(1);
+
+        orderService.create(dto(10L, 2, null), 1L);
+
+        ArgumentCaptor<List<LineItem>> captor = ArgumentCaptor.forClass(List.class);
+        verify(bundlePricingService).compute(captor.capture());
+        List<LineItem> lines = captor.getValue();
+        assertEquals(1, lines.size());
+        assertEquals(10L, lines.get(0).getProductId());
+        assertEquals(null, lines.get(0).getSkuId());
+        assertEquals(2, lines.get(0).getQuantity());
+        assertEquals(69900, lines.get(0).getUnitPrice());
+    }
+
+    @Test
+    void createMultiItem_frontendRuleDiscountNotMixedWithBundle() {
+        // 前端传入规则优惠5000(不含套餐)+ 套餐11800 → moneyAmount = 139800 - 5000 - 11800 = 123000
+        when(productMapper.selectById(10L)).thenReturn(product(10L, "DAN测评", 69900));
+        when(memberPriceResolver.resolveEligibleMemberPrice(any(Product.class), anyLong())).thenReturn(69900);
+        BundleDiscountResult r = new BundleDiscountResult();
+        r.setActive(true);
+        r.setOriginalTotal(139800);
+        r.setPayPrice(128000);
+        r.setDiscount(11800);
+        r.setBundleList(new ArrayList<>());
+        when(bundlePricingService.compute(anyList())).thenReturn(r);
+        when(userMapper.selectById(anyLong())).thenReturn(new User());
+        when(orderMapper.insert(any())).thenReturn(1);
+        when(orderItemMapper.insert(any())).thenReturn(1);
+
+        Result<ProductOrderDTO> result = orderService.create(dto(10L, 2, 5000), 1L);
+
+        assertTrue(result.getCode() == 200);
+        assertEquals(123000, result.getData().getMoneyAmount());
+    }
+}