Prechádzať zdrojové kódy

修改BUG 添加店铺名称 新增收藏店铺时 自动获取优惠券功能 表添加字段-- 1. 在 life_collect 表中添加优惠券发放标记字段
ALTER TABLE life_collect
ADD COLUMN coupon_issued_flag INT DEFAULT 0 COMMENT '优惠券发放标记:0-未发放,1-已发放';

-- 2. 在 life_discount_coupon_user 表中添加发放来源字段
ALTER TABLE life_discount_coupon_user
ADD COLUMN issue_source INT COMMENT '发放来源:1-手动领取,2-收藏店铺,3-好评送券,4-好友赠送,5-平台发放';

lutong 1 mesiac pred
rodič
commit
e9ec196de9

+ 4 - 0
alien-entity/src/main/java/shop/alien/entity/store/LifeCollect.java

@@ -53,4 +53,8 @@ public class LifeCollect {
     @ApiModelProperty(value = "修改人ID")
     @TableField("updated_user_id")
     private Integer updatedUserId;
+
+    @ApiModelProperty(value = "优惠券发放标记:0-未发放,1-已发放")
+    @TableField("coupon_issued_flag")
+    private Integer couponIssuedFlag;
 }

+ 4 - 0
alien-entity/src/main/java/shop/alien/entity/store/LifeDiscountCouponUser.java

@@ -92,6 +92,10 @@ public class LifeDiscountCouponUser extends Model<LifeDiscountCouponUser> {
     @TableField("voucher_id")
     private String voucherId;
 
+    @ApiModelProperty(value = "发放来源:1-手动领取,2-收藏店铺,3-好评送券,4-好友赠送,5-平台发放")
+    @TableField("issue_source")
+    private Integer issueSource;
+
     @Override
     protected Serializable pkVal() {
         return this.id;

+ 20 - 0
alien-store/src/main/java/shop/alien/store/controller/LifeCollectController.java

@@ -19,6 +19,7 @@ import shop.alien.mapper.*;
 import shop.alien.mapper.second.SecondGoodsMapper;
 import shop.alien.store.annotation.TrackEvent;
 import shop.alien.store.config.GaoDeMapUtil;
+import shop.alien.store.service.LifeDiscountCouponService;
 import shop.alien.util.common.ListToPage;
 
 import java.text.DecimalFormat;
@@ -54,6 +55,8 @@ public class LifeCollectController {
 
     private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;
 
+    private final LifeDiscountCouponService lifeDiscountCouponService;
+
     @ApiOperation("收藏列表")
     @ApiOperationSupport(order = 1)
     @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
@@ -230,6 +233,23 @@ public class LifeCollectController {
             LambdaQueryWrapper<LifeCollect> queryWrapper = new LambdaQueryWrapper<>();
             queryWrapper.eq(LifeCollect::getStoreId, lifeCollect.getStoreId());
             storeInfoMapper.update(null, new LambdaUpdateWrapper<StoreInfo>().eq(StoreInfo::getId, lifeCollect.getStoreId()).set(StoreInfo::getCollectNum, lifeCollectMapper.selectCount(queryWrapper)));
+            
+            // 收藏店铺时自动发放优惠券(每种类型一张)
+            if (StringUtils.hasText(lifeCollect.getUserId())) {
+                try {
+                    Integer userId = Integer.parseInt(lifeCollect.getUserId());
+                    // 传入收藏记录ID,用于更新发放标记
+                    int issuedCount = lifeDiscountCouponService.issueCouponsForStoreCollect(userId, lifeCollect.getStoreId(), lifeCollect.getId());
+                    if (issuedCount > 0) {
+                        log.info("收藏店铺自动发放优惠券成功,userId={}, storeId={}, collectId={}, issuedCount={}", 
+                                userId, lifeCollect.getStoreId(), lifeCollect.getId(), issuedCount);
+                    }
+                } catch (NumberFormatException e) {
+                    log.warn("收藏店铺发放优惠券失败:用户ID格式错误,userId={}, storeId={}", lifeCollect.getUserId(), lifeCollect.getStoreId());
+                } catch (Exception e) {
+                    log.error("收藏店铺发放优惠券异常,userId={}, storeId={}, error={}", lifeCollect.getUserId(), lifeCollect.getStoreId(), e.getMessage(), e);
+                }
+            }
         }
 
 

+ 10 - 0
alien-store/src/main/java/shop/alien/store/service/LifeDiscountCouponService.java

@@ -168,4 +168,14 @@ public interface LifeDiscountCouponService extends IService<LifeDiscountCoupon>
      * @return List<LifeDiscountCoupon>
      */
     List<LifeDiscountCoupon> getPlatformCoupon(Integer couponId, Integer couponType);
+
+    /**
+     * 收藏店铺时自动发放优惠券(每种类型一张)
+     *
+     * @param userId   用户ID
+     * @param storeId  店铺ID
+     * @param collectId 收藏记录ID(用于更新发放标记)
+     * @return 发放的优惠券数量
+     */
+    int issueCouponsForStoreCollect(Integer userId, String storeId, String collectId);
 }

+ 150 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponServiceImpl.java

@@ -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;
+        }
+    }
 }

+ 3 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponStoreFriendServiceImpl.java

@@ -390,6 +390,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
                             lifeDiscountCouponUser.setExpirationTime(lifeDiscountCoupon.getValidDate());
                             // 设置该优惠券的状态为待使用
                             lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
+                            lifeDiscountCouponUser.setIssueSource(4); // 4-好友赠送
                             // 将该用户优惠券记录插入到数据库中
                             lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
                         }
@@ -966,6 +967,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
                     lifeDiscountCouponUser.setExpirationTime(expirationTime);
                     lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
                     lifeDiscountCouponUser.setDeleteFlag(0);
+                    lifeDiscountCouponUser.setIssueSource(3); // 3-好评送券
                     lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
                     coupon.setSingleQty(coupon.getSingleQty() - 1);
                     lifeDiscountCouponMapper.updateById(coupon);
@@ -993,6 +995,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
                     }
                     lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
                     lifeDiscountCouponUser.setDeleteFlag(0);
+                    lifeDiscountCouponUser.setIssueSource(3); // 3-好评送券(代金券)
                     lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
                     lifeCoupon.setSingleQty(lifeCoupon.getSingleQty() - 1);
                     lifeCouponMapper.updateById(lifeCoupon);

+ 1 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponUserServiceImpl.java

@@ -108,6 +108,7 @@ public class LifeDiscountCouponUserServiceImpl extends ServiceImpl<LifeDiscountC
 
                 //存入状态  待使用:0
                 lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
+                lifeDiscountCouponUser.setIssueSource(1); // 1-手动领取
                 lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
             }
             //削减该优惠券库存

+ 2 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LifeGroupPackageServiceImpl.java

@@ -153,6 +153,7 @@ public class LifeGroupPackageServiceImpl extends ServiceImpl<LifeGroupPackageMap
                                     lifeDiscountCouponUser.setExpirationTime(insertedCoupon.getEndDate());
                                     // 设置该优惠券的状态为待使用
                                     lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
+                                    lifeDiscountCouponUser.setIssueSource(5); // 5-平台发放(团购套餐补偿)
                                     // 将该用户优惠券记录插入到数据库中
                                     if (lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser) > 0) {
                                         // 发送通知
@@ -283,6 +284,7 @@ public class LifeGroupPackageServiceImpl extends ServiceImpl<LifeGroupPackageMap
                                     lifeDiscountCouponUser.setExpirationTime(insertedCoupon.getEndDate());
                                     // 设置该优惠券的状态为待使用
                                     lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
+                                    lifeDiscountCouponUser.setIssueSource(5); // 5-平台发放(团购套餐补偿)
                                     // 将该用户优惠券记录插入到数据库中
                                     if (lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser) > 0) {
                                         // 发送通知

+ 1 - 0
alien-store/src/main/java/shop/alien/store/service/impl/StorePlatformBenefitsServiceImpl.java

@@ -264,6 +264,7 @@ public class StorePlatformBenefitsServiceImpl extends ServiceImpl<StorePlatformB
         // 设置该优惠券的状态为待使用
         lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
         lifeDiscountCouponUser.setWelfareId(benefitsId);
+        lifeDiscountCouponUser.setIssueSource(5); // 5-平台发放(平台福利)
         lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
 
         if (updateResult > 0) {