|
|
@@ -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 "领取成功";
|
|
|
}
|
|
|
|