|
|
@@ -123,6 +123,54 @@ public class CouponService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 领券中心:返回该家庭「可领取」的券模板列表。
|
|
|
+ * 语义与 claim() 校验保持一致,保证「展示的券必可领取成功」:
|
|
|
+ * - 模板 status=ACTIVE
|
|
|
+ * - 在有效期内(validFrom/validUntil 为 NULL 视为不限)
|
|
|
+ * - 未领完(totalCount/usedCount 配额未耗尽)
|
|
|
+ * - 家庭尚未拥有该模板(family_coupon 中无 (familyId, couponId) 记录,任意状态均视为已领过,
|
|
|
+ * 与 claim() 去重口径一致)
|
|
|
+ */
|
|
|
+ public List<Map<String, Object>> 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());
|
|
|
+ List<Coupon> templates = couponMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<Coupon>()
|
|
|
+ .eq(Coupon::getStatus, "ACTIVE")
|
|
|
+ .and(w -> w.isNull(Coupon::getValidFrom).or().le(Coupon::getValidFrom, now))
|
|
|
+ .and(w -> w.isNull(Coupon::getValidUntil).or().ge(Coupon::getValidUntil, now))
|
|
|
+ .notIn(!ownedIds.isEmpty(), Coupon::getId, ownedIds)
|
|
|
+ .orderByDesc(Coupon::getValue));
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
+ for (Coupon c : templates) {
|
|
|
+ if (c.getTotalCount() != null && c.getUsedCount() != null
|
|
|
+ && c.getUsedCount() >= c.getTotalCount()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
+ item.put("id", c.getId());
|
|
|
+ item.put("couponId", c.getId());
|
|
|
+ item.put("name", c.getName());
|
|
|
+ item.put("type", c.getType());
|
|
|
+ item.put("value", c.getValue());
|
|
|
+ item.put("discountRate", c.getDiscountRate());
|
|
|
+ item.put("minSpend", c.getMinSpend());
|
|
|
+ item.put("applicableTo", c.getApplicableTo());
|
|
|
+ item.put("validFrom", c.getValidFrom());
|
|
|
+ item.put("validUntil", c.getValidUntil());
|
|
|
+ item.put("productId", c.getProductId());
|
|
|
+ item.put("status", "ACTIVE");
|
|
|
+ result.add(item);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
@Transactional
|
|
|
public String claim(Long userId, Long couponId) {
|
|
|
Long familyId = getFamilyId(userId);
|