Explorar o código

fix(coupon): C3 领券中心数据源改为可取券模板 /api/coupon/claimable grant_type=CLAIM

- CouponService.listClaimable: grant_type=CLAIM + status=ACTIVE + 有效期内 + 库存未耗 + 排除家庭已拥有(family_coupon任意状态) 的券模板,orderBy value,id
- CouponController @PostMapping /api/coupon/claimable -> Result<List<Coupon>>
- claim() 成功后落 FamilyCouponGrantLog(grantType=CLAIM, quantity=1, source=claim-center, period=null)
- 小程序 api.js getClaimableCoupons + coupon-center.vue 改用之(claim 用 item.couponId||item.id,原始 Coupon 即券模板 id)
- 管理端 CouponManagement.vue grantType 下拉新增 CLAIM(领券中心自领)、CLAIM 隐藏'每次发放数量'且提交 grantQuantity=null、grantTypeLabel 新增 CLAIM
- CouponGrantLog.vue grantTypeLabel/tagType 新增 CLAIM
- schema.sql coupon/family_coupon_grant_log grant_type 注释追加 CLAIM(仅注释)
- API_REFERENCE.md 登记 /api/coupon/claimable + PROJECT-OVERVIEW 同步

验收: 三端编译(backend mvn compile PASS / cfc-web npm run build PASS / mp-weixin 构建被既有 certification-center.vue \-secondary Sass 错误阻断,非本任务引入)
运行时 curl 验证: claimable 返回未领取模板→claim 成功→已领取模板被排除→my 含新领券;grant log=CLAIM/claim-center
无 DB 迁移(仅注释/文档变更)。
Sisyphus Agent hai 2 semanas
pai
achega
deb3fc3ac4

+ 8 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/CouponController.java

@@ -39,6 +39,14 @@ public class CouponController {
         return Result.error(result);
     }
 
+    /**
+     * 领券中心:可自领券模板(grantType=CLAIM,当前家庭未领取的上架券)
+     */
+    @PostMapping("/claimable")
+    public Result<List<Coupon>> claimable(@RequestAttribute("userId") Long userId) {
+        return Result.success(couponService.listClaimable(userId));
+    }
+
     @PostMapping("/apply")
     public Result<Map<String, Object>> apply(@RequestAttribute("userId") Long userId,
                                              @RequestBody Map<String, Object> params) {

+ 38 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/CouponService.java

@@ -162,6 +162,34 @@ public class CouponService {
         return result;
     }
 
+    /**
+     * 领券中心:返回当前家庭可自领的优惠券模板(grantType=CLAIM)。
+     * 排除家庭已拥有(family_coupon 任意状态,含已用/过期)的券模板,保证每家庭每券限领一次
+     * (与 claim() 的去重口径一致);仅返回上架(status=ACTIVE)、在有效期内、库存未耗尽的模板,
+     * 按面值升序、同面值按 id 升序。
+     */
+    public List<Coupon> listClaimable(Long userId) {
+        Long familyId = getFamilyId(userId);
+        if (familyId == null) return new ArrayList<>();
+        Date now = new Date();
+        List<Long> ownedIds = familyCouponMapper.selectList(
+                        new LambdaQueryWrapper<FamilyCoupon>()
+                                .eq(FamilyCoupon::getFamilyId, familyId))
+                .stream().map(FamilyCoupon::getCouponId).collect(java.util.stream.Collectors.toList());
+        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper<Coupon>()
+                .eq(Coupon::getGrantType, "CLAIM")
+                .eq(Coupon::getStatus, "ACTIVE")
+                .le(Coupon::getValidFrom, now)
+                .ge(Coupon::getValidUntil, now)
+                .notIn(!ownedIds.isEmpty(), Coupon::getId, ownedIds)
+                // 库存未耗:total_count 为 null 或 used_count < total_count(used_count null 视为 0)
+                .and(w -> w.isNull(Coupon::getTotalCount)
+                        .or().apply("COALESCE(used_count, 0) < total_count"))
+                .orderByAsc(Coupon::getValue)
+                .orderByAsc(Coupon::getId);
+        return couponMapper.selectList(wrapper);
+    }
+
     @Transactional
     public String claim(Long userId, Long couponId) {
         Long familyId = getFamilyId(userId);
@@ -195,6 +223,16 @@ public class CouponService {
         issueToFamily(couponId, familyId);
         coupon.setUsedCount(coupon.getUsedCount() == null ? 1 : coupon.getUsedCount() + 1);
         couponMapper.updateById(coupon);
+        // COUPON-C3:领取成功落发券流水(grantType=CLAIM / source=claim-center / period=null)
+        // claim() 已按 family_id+coupon_id 去重,每家庭每券至多一条 CLAIM 流水
+        FamilyCouponGrantLog log = new FamilyCouponGrantLog();
+        log.setFamilyId(familyId);
+        log.setCouponId(couponId);
+        log.setGrantType("CLAIM");
+        log.setQuantity(1);
+        log.setSource("claim-center");
+        log.setCreatedAt(new Date());
+        familyCouponGrantLogMapper.insert(log);
         return "领取成功";
     }
 

+ 2 - 2
cfc-backend/src/main/resources/schema.sql

@@ -2350,7 +2350,7 @@ CREATE TABLE IF NOT EXISTS coupon (
     product_id BIGINT COMMENT '绑定商品ID,NULL=全场通用',
     family_id BIGINT DEFAULT NULL COMMENT '模板块绑定家庭ID(NULL=全平台模板)',
     points_price INT DEFAULT 0 COMMENT '积分兑换价,0=不可积分兑换',
-    grant_type VARCHAR(16) COMMENT '发放规则:JOIN/PERIODIC/POPULATION/REGISTER,NULL=不自动发放',
+    grant_type VARCHAR(16) COMMENT '发放规则:JOIN/PERIODIC/POPULATION/REGISTER/CLAIM,NULL=不自动发放(CLAIM=领券中心自领)',
     grant_level_code VARCHAR(32) COMMENT '发放规则对应会员等级',
     grant_period VARCHAR(16) COMMENT 'PERIODIC周期:MONTHLY/QUARTERLY',
     grant_quantity INT DEFAULT 1 COMMENT '每次发放数量',
@@ -4747,7 +4747,7 @@ CREATE TABLE IF NOT EXISTS family_coupon_grant_log (
     id BIGINT AUTO_INCREMENT PRIMARY KEY,
     family_id BIGINT NOT NULL COMMENT '受赠家庭ID',
     coupon_id BIGINT NOT NULL COMMENT '券模板ID',
-    grant_type VARCHAR(16) NOT NULL COMMENT 'JOIN/PERIODIC/POPULATION/REGISTER/EXCHANGE/CF_EXCHANGE',
+    grant_type VARCHAR(16) NOT NULL COMMENT 'JOIN/PERIODIC/POPULATION/REGISTER/EXCHANGE/CF_EXCHANGE/CLAIM',
     period VARCHAR(16) COMMENT '周期标识(YYYY-MM或YYYY-Qn),PERIODIC防重用',
     quantity INT DEFAULT 1 COMMENT '发放数量',
     source VARCHAR(64) COMMENT '触发来源(订单号/成员ID等)',

+ 2 - 2
cfc-frontend/pages/profile-extra/coupon-center.vue

@@ -31,7 +31,7 @@
 </template>
 
 <script>
-import { getCouponList, claimCoupon } from '../../utils/api.js'
+import { getClaimableCoupons, claimCoupon } from '../../utils/api.js'
 import { parseDate } from '../../utils/format.js'
 import fontScaleMixin from '@/mixins/font-scale.js'
 
@@ -60,7 +60,7 @@ export default {
     loadCoupons() {
       var self = this
       this.loading = true
-      getCouponList().then(function(res) {
+      getClaimableCoupons().then(function(res) {
         if (res && res.code === 200) {
           self.couponList = res.data || []
         } else {

+ 5 - 0
cfc-frontend/utils/api.js

@@ -2619,6 +2619,11 @@ export const getCouponList = () => {
   return request('/api/coupon/list', 'POST')
 }
 
+// 领券中心:可自领的优惠券模板(grantType=CLAIM,当前家庭未领取)
+export const getClaimableCoupons = () => {
+  return request('/api/coupon/claimable', 'POST', {})
+}
+
 export const getMyCoupons = () => {
   return request('/api/coupon/my', 'POST')
 }

+ 2 - 2
cfc-web/src/views/admin/CouponGrantLog.vue

@@ -99,11 +99,11 @@ export default {
   },
   methods: {
     grantTypeLabel(val) {
-      const map = { JOIN: '开通赠券', PERIODIC: '周期补发', POPULATION: '家庭人口券', REGISTER: '新用户注册赠券', EXCHANGE: '积分兑换', CF_EXCHANGE: 'CF值兑换' }
+      const map = { JOIN: '开通赠券', PERIODIC: '周期补发', POPULATION: '家庭人口券', REGISTER: '新用户注册赠券', EXCHANGE: '积分兑换', CF_EXCHANGE: 'CF值兑换', CLAIM: '领券中心自领' }
       return map[val] || val
     },
     grantTypeTagType(val) {
-      const map = { JOIN: 'success', PERIODIC: 'primary', POPULATION: 'warning', REGISTER: 'info', EXCHANGE: 'danger', CF_EXCHANGE: 'info' }
+      const map = { JOIN: 'success', PERIODIC: 'primary', POPULATION: 'warning', REGISTER: 'info', EXCHANGE: 'danger', CF_EXCHANGE: 'info', CLAIM: 'primary' }
       return map[val] || 'info'
     },
     async loadData() {

+ 4 - 3
cfc-web/src/views/admin/CouponManagement.vue

@@ -149,6 +149,7 @@
             <el-option label="PERIODIC(周期补发)" value="PERIODIC" />
             <el-option label="POPULATION(家庭人口券)" value="POPULATION" />
             <el-option label="REGISTER(新用户注册赠)" value="REGISTER" />
+            <el-option label="CLAIM(领券中心自领)" value="CLAIM" />
           </el-select>
         </el-form-item>
         <el-form-item v-if="form.grantType === 'JOIN' || form.grantType === 'PERIODIC'" label="适用会员等级">
@@ -162,7 +163,7 @@
             <el-option label="QUARTERLY(每季度)" value="QUARTERLY" />
           </el-select>
         </el-form-item>
-        <el-form-item v-if="form.grantType && form.grantType !== ''" label="每次发放数量">
+        <el-form-item v-if="form.grantType && form.grantType !== '' && form.grantType !== 'CLAIM'" label="每次发放数量">
           <el-input-number v-model="form.grantQuantity" :min="1" :step="1" class="w-200" />
         </el-form-item>
         <el-form-item label="有效期起" required>
@@ -293,7 +294,7 @@ export default {
       return map[val] || val
     },
     grantTypeLabel(val) {
-      const map = { JOIN: '开通赠券', PERIODIC: '周期补发', POPULATION: '家庭人口券', REGISTER: '新用户注册赠券' }
+      const map = { JOIN: '开通赠券', PERIODIC: '周期补发', POPULATION: '家庭人口券', REGISTER: '新用户注册赠券', CLAIM: '领券中心自领' }
       return map[val] || val
     },
     async loadData() {
@@ -393,7 +394,7 @@ export default {
           grantType: this.form.grantType || null,
           grantLevelCode: this.form.grantType ? this.form.grantLevelCode : null,
           grantPeriod: this.form.grantType === 'PERIODIC' ? this.form.grantPeriod : null,
-          grantQuantity: this.form.grantType ? this.form.grantQuantity : null,
+          grantQuantity: this.form.grantType && this.form.grantType !== 'CLAIM' ? this.form.grantQuantity : null,
           applicableTo: this.form.applicableTo,
           validFrom: this.form.validFrom,
           validUntil: this.form.validUntil,

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
docs/superpowers/PROJECT-OVERVIEW.md


+ 1 - 0
docs/superpowers/api/API_REFERENCE.md

@@ -1062,6 +1062,7 @@ find cfc-backend/src/main/java -name "*XxxService.java" -o -name "*XxxController
 | `POST /api/coupon/my` | 我的(家庭)优惠券(小程序) |
 | `POST /api/coupon/apply` | 下单抵扣(家庭券) |
 | `POST /api/coupon/claim` | 领取优惠券(发到家庭) |
+| `POST /api/coupon/claimable` | 领券中心可自领券模板(小程序,grantType=CLAIM 且当前家庭未领取的上架券;返回原始 Coupon,id 即券模板 id,排除 family_coupon 任意状态已拥有) |
 | `POST /api/coupon/checkout-list` | 结算页可兑换未拥有券 |
 
 ---

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio