|
|
@@ -70,6 +70,8 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
|
|
|
|
|
|
private final LifeCouponMapper lifeCouponMapper;
|
|
|
|
|
|
+ private final LifeCollectMapper lifeCollectMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public boolean addDiscountCoupon(LifeDiscountCouponDto lifeDiscountCouponDto) {
|
|
|
try {
|
|
|
@@ -382,6 +384,20 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
|
|
|
}
|
|
|
lifeDiscountCouponVo.setAvailableTimeQuantum(availableTimeQuantumList);
|
|
|
lifeDiscountCouponVo.setCustomizeUnavailableTimeQuantum(customizeUnavailableTimeQuantumList);
|
|
|
+
|
|
|
+ // 查询店铺名称
|
|
|
+ if (lifeDiscountCoupon != null && !StringUtils.isEmpty(lifeDiscountCoupon.getStoreId())) {
|
|
|
+ try {
|
|
|
+ Integer storeId = Integer.parseInt(lifeDiscountCoupon.getStoreId());
|
|
|
+ StoreInfo storeInfo = storeInfoMapper.selectById(storeId);
|
|
|
+ if (storeInfo != null && !StringUtils.isEmpty(storeInfo.getStoreName())) {
|
|
|
+ lifeDiscountCouponVo.setStoreName(storeInfo.getStoreName());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 查询店铺信息失败,不影响主流程,静默处理
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return lifeDiscountCouponVo;
|
|
|
}
|
|
|
|
|
|
@@ -1764,6 +1780,7 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
|
|
|
lifeDiscountCouponUser.setStatus(0);
|
|
|
lifeDiscountCouponUser.setDeleteFlag(0);
|
|
|
lifeDiscountCouponUser.setCreatedTime(new Date());
|
|
|
+ lifeDiscountCouponUser.setIssueSource(5); // 5-平台发放
|
|
|
lifeDiscountCouponUsers.add(lifeDiscountCouponUser);
|
|
|
}
|
|
|
return lifeDiscountCouponUserService.saveBatch(lifeDiscountCouponUsers);
|
|
|
@@ -1828,4 +1845,137 @@ public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCoupo
|
|
|
lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getGetStatus, 1);
|
|
|
return lifeDiscountCouponMapper.selectList(lifeDiscountCouponLambdaQueryWrapper);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 收藏店铺时自动发放优惠券(每种类型一张)
|
|
|
+ *
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @param storeId 店铺ID
|
|
|
+ * @param collectId 收藏记录ID(用于更新发放标记)
|
|
|
+ * @return 发放的优惠券数量
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int issueCouponsForStoreCollect(Integer userId, String storeId, String collectId) {
|
|
|
+ if (userId == null || StringUtils.isEmpty(storeId)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果提供了收藏记录ID,检查是否已发放过
|
|
|
+ if (!StringUtils.isEmpty(collectId)) {
|
|
|
+ LifeCollect lifeCollect = lifeCollectMapper.selectById(collectId);
|
|
|
+ if (lifeCollect != null && lifeCollect.getCouponIssuedFlag() != null && lifeCollect.getCouponIssuedFlag() == 1) {
|
|
|
+ // 该收藏记录已发放过优惠券,不再重复发放
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
+
|
|
|
+ // 查询该店铺所有开启领取的优惠券(有效期内、有库存、未删除)
|
|
|
+ LambdaQueryWrapper<LifeDiscountCoupon> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(LifeDiscountCoupon::getStoreId, storeId)
|
|
|
+ .eq(LifeDiscountCoupon::getGetStatus, 1) // 开启领取
|
|
|
+ .eq(LifeDiscountCoupon::getDeleteFlag, 0) // 未删除
|
|
|
+ .gt(LifeDiscountCoupon::getSingleQty, 0) // 有库存
|
|
|
+ .ge(LifeDiscountCoupon::getEndGetDate, now) // 未过期
|
|
|
+ .eq(LifeDiscountCoupon::getCouponStatus, 1) // 正式状态(非草稿)
|
|
|
+ .isNotNull(LifeDiscountCoupon::getCouponType) // 必须有优惠券类型
|
|
|
+ .orderByDesc(LifeDiscountCoupon::getCreatedTime); // 按创建时间降序
|
|
|
+
|
|
|
+ List<LifeDiscountCoupon> allCoupons = lifeDiscountCouponMapper.selectList(queryWrapper);
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(allCoupons)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按 couponType 分组,每种类型只取第一个(最新的)
|
|
|
+ Map<Integer, LifeDiscountCoupon> couponByType = new HashMap<>();
|
|
|
+ for (LifeDiscountCoupon coupon : allCoupons) {
|
|
|
+ Integer couponType = coupon.getCouponType();
|
|
|
+ if (couponType != null && !couponByType.containsKey(couponType)) {
|
|
|
+ couponByType.put(couponType, coupon);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (couponByType.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查用户已领取的优惠券ID列表(避免重复发放)
|
|
|
+ LambdaQueryWrapper<LifeDiscountCouponUser> userCouponWrapper = new LambdaQueryWrapper<>();
|
|
|
+ userCouponWrapper.eq(LifeDiscountCouponUser::getUserId, userId)
|
|
|
+ .eq(LifeDiscountCouponUser::getDeleteFlag, 0)
|
|
|
+ .in(LifeDiscountCouponUser::getCouponId,
|
|
|
+ couponByType.values().stream().map(LifeDiscountCoupon::getId).collect(Collectors.toList()));
|
|
|
+ List<LifeDiscountCouponUser> existingCoupons = lifeDiscountCouponUserMapper.selectList(userCouponWrapper);
|
|
|
+ Set<Integer> existingCouponIds = existingCoupons.stream()
|
|
|
+ .map(LifeDiscountCouponUser::getCouponId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ // 发放每种类型的优惠券(每种一张)
|
|
|
+ int issuedCount = 0;
|
|
|
+ for (Map.Entry<Integer, LifeDiscountCoupon> entry : couponByType.entrySet()) {
|
|
|
+ LifeDiscountCoupon coupon = entry.getValue();
|
|
|
+
|
|
|
+ // 如果用户已领取过该优惠券,跳过
|
|
|
+ if (existingCouponIds.contains(coupon.getId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 再次检查库存(防止并发问题)
|
|
|
+ if (coupon.getSingleQty() == null || coupon.getSingleQty() <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 创建用户优惠券记录
|
|
|
+ LifeDiscountCouponUser lifeDiscountCouponUser = new LifeDiscountCouponUser();
|
|
|
+ lifeDiscountCouponUser.setCouponId(coupon.getId());
|
|
|
+ lifeDiscountCouponUser.setUserId(userId);
|
|
|
+ lifeDiscountCouponUser.setReceiveTime(new Date());
|
|
|
+
|
|
|
+ // 设置过期时间:优先使用validDate,如果为null则使用endDate
|
|
|
+ LocalDate expirationTime = coupon.getValidDate();
|
|
|
+ if (expirationTime == null && coupon.getEndDate() != null) {
|
|
|
+ expirationTime = coupon.getEndDate();
|
|
|
+ }
|
|
|
+ lifeDiscountCouponUser.setExpirationTime(expirationTime);
|
|
|
+ lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
|
|
|
+ lifeDiscountCouponUser.setDeleteFlag(0);
|
|
|
+ lifeDiscountCouponUser.setIssueSource(2); // 2-收藏店铺
|
|
|
+
|
|
|
+ // 插入用户优惠券记录
|
|
|
+ lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
|
|
|
+
|
|
|
+ // 扣减库存
|
|
|
+ coupon.setSingleQty(coupon.getSingleQty() - 1);
|
|
|
+ lifeDiscountCouponMapper.updateById(coupon);
|
|
|
+
|
|
|
+ issuedCount++;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 单个优惠券发放失败不影响其他优惠券的发放
|
|
|
+ // 静默处理,不记录日志,避免日志过多
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果发放成功且提供了收藏记录ID,更新收藏记录的发放标记
|
|
|
+ if (issuedCount > 0 && !StringUtils.isEmpty(collectId)) {
|
|
|
+ try {
|
|
|
+ LifeCollect lifeCollect = new LifeCollect();
|
|
|
+ lifeCollect.setId(collectId);
|
|
|
+ lifeCollect.setCouponIssuedFlag(1); // 标记为已发放
|
|
|
+ lifeCollectMapper.updateById(lifeCollect);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 更新标记失败不影响主流程
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return issuedCount;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 异常时静默处理,不影响收藏功能
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|