Explorar o código

feat(coupon): 会员订单仅可使用会员优惠券

购买会员(orderType=MEMBERSHIP)时,CouponService.apply 仅放行
applicableTo=MEMBERSHIP 的优惠券,ALL/null/PRODUCT 等一律拒绝;
小程序会员支付页选券列表同步只展示会员优惠券。
新增 CouponServiceMembershipScopeTest 覆盖 5 个边界。
Xiaogang Liao hai 2 semanas
pai
achega
a02b1b0512

+ 12 - 5
cfc-backend/src/main/java/com/etotem/cfc/service/CouponService.java

@@ -241,11 +241,18 @@ public class CouponService {
         if (coupon.getValidUntil() != null && now.after(coupon.getValidUntil())) {
             return null;
         }
-        // 适用范围为空视为全场通用(COUPON-P2 ⑥:避免 null.equals() NPE)
-        String applicableTo = coupon.getApplicableTo();
-        if (applicableTo == null) applicableTo = "ALL";
-        if (!"ALL".equals(applicableTo) && !applicableTo.equals(orderType)) {
-            return null;
+        // 会员订单只能使用会员优惠券(applicableTo=MEMBERSHIP),全品类/商品券一律不可用
+        if ("MEMBERSHIP".equals(orderType)) {
+            if (!"MEMBERSHIP".equals(coupon.getApplicableTo())) {
+                return null;
+            }
+        } else {
+            // 非会员订单:适用范围为空视为全场通用(COUPON-P2 ⑥:避免 null.equals() NPE)
+            String applicableTo = coupon.getApplicableTo();
+            if (applicableTo == null) applicableTo = "ALL";
+            if (!"ALL".equals(applicableTo) && !applicableTo.equals(orderType)) {
+                return null;
+            }
         }
         // 商品券只能用于指定商品(防止商品券被用于非预期商品)
         if (coupon.getProductId() != null

+ 115 - 0
cfc-backend/src/test/java/com/etotem/cfc/service/CouponServiceMembershipScopeTest.java

@@ -0,0 +1,115 @@
+package com.etotem.cfc.service;
+
+import com.etotem.cfc.entity.Coupon;
+import com.etotem.cfc.entity.FamilyCoupon;
+import com.etotem.cfc.entity.User;
+import com.etotem.cfc.mapper.CouponMapper;
+import com.etotem.cfc.mapper.FamilyCouponGrantLogMapper;
+import com.etotem.cfc.mapper.FamilyCouponMapper;
+import com.etotem.cfc.mapper.UserMapper;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.when;
+
+/**
+ * 会员订单优惠券适用范围契约:购买会员只能使用会员优惠券(applicableTo=MEMBERSHIP),
+ * 全品类/商品/历史空值券一律不可用;非会员订单仍保持原有 ALL 通用逻辑。
+ */
+@ExtendWith(MockitoExtension.class)
+public class CouponServiceMembershipScopeTest {
+
+    private static final Long USER_ID = 1L;
+    private static final Long FAMILY_ID = 10L;
+    private static final Long USER_COUPON_ID = 500L;
+    private static final Long COUPON_ID = 100L;
+
+    @Mock
+    private CouponMapper couponMapper;
+
+    @Mock
+    private FamilyCouponMapper familyCouponMapper;
+
+    @Mock
+    private FamilyCouponGrantLogMapper familyCouponGrantLogMapper;
+
+    @Mock
+    private UserMapper userMapper;
+
+    @Mock
+    private MembershipService membershipService;
+
+    @Mock
+    private CouponGiftService couponGiftService;
+
+    @InjectMocks
+    private CouponService couponService;
+
+    @BeforeEach
+    void setUp() {
+        User user = new User();
+        user.setId(USER_ID);
+        user.setFamilyId(FAMILY_ID);
+        when(userMapper.selectById(USER_ID)).thenReturn(user);
+
+        FamilyCoupon fc = new FamilyCoupon();
+        fc.setId(USER_COUPON_ID);
+        fc.setFamilyId(FAMILY_ID);
+        fc.setCouponId(COUPON_ID);
+        fc.setStatus("AVAILABLE");
+        fc.setIsGift(0);
+        when(familyCouponMapper.selectById(USER_COUPON_ID)).thenReturn(fc);
+    }
+
+    private Coupon coupon(String applicableTo) {
+        Coupon c = new Coupon();
+        c.setId(COUPON_ID);
+        c.setStatus("ACTIVE");
+        c.setType("FIXED");
+        c.setValue(1000);
+        c.setApplicableTo(applicableTo);
+        return c;
+    }
+
+    @Test
+    void membershipOrder_rejectsAllScopeCoupon() {
+        when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("ALL"));
+        assertNull(couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
+    }
+
+    @Test
+    void membershipOrder_rejectsNullScopeCoupon() {
+        when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon(null));
+        assertNull(couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
+    }
+
+    @Test
+    void membershipOrder_rejectsProductScopeCoupon() {
+        when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("PRODUCT"));
+        assertNull(couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
+    }
+
+    @Test
+    void membershipOrder_acceptsMembershipCouponForMember() {
+        when(membershipService.getMemberLevel(USER_ID)).thenReturn("FAMILY");
+        when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("MEMBERSHIP"));
+        assertEquals(Integer.valueOf(1000),
+                couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
+    }
+
+    @Test
+    void productOrder_stillAcceptsAllScopeCoupon() {
+        when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("ALL"));
+        assertEquals(Integer.valueOf(1000),
+                couponService.apply(USER_ID, USER_COUPON_ID, "PRODUCT", 9900,
+                        Collections.singletonList(1L)));
+    }
+}

+ 2 - 1
cfc-frontend/pages/membership/pay.vue

@@ -151,7 +151,8 @@ export default {
       var self = this
       return this.coupons.filter(function(c) {
         if (c.status && c.status !== 'AVAILABLE') return false
-        if (c.applicableTo && c.applicableTo !== 'ALL' && c.applicableTo !== 'MEMBERSHIP') return false
+        // 购买会员只能使用会员优惠券(applicableTo=MEMBERSHIP),全品类/商品券一律不可用
+        if (c.applicableTo !== 'MEMBERSHIP') return false
         // 会员专属券:非会员不显示在可选列表中
         // 会员专属券(applicableTo=MEMBERSHIP)仅允许会员使用;手动发放券(grantType=MANUAL)例外,允许非会员购买时使用
         // 来自他人转赠的券(isGift=1)同样允许非会员用于购买会员