wuchen hai 2 meses
pai
achega
eaf93bc6a4

+ 9 - 0
alien-entity/src/main/java/shop/alien/entity/store/StoreInfo.java

@@ -359,5 +359,14 @@ public class StoreInfo {
     @TableField("tableware_fee")
     private Integer tablewareFee;
 
+    /** 好评送券活动:每用户可参与次数,0或null表示不限制(需表 store_info 存在列 good_rating_participation_limit) */
+    @ApiModelProperty(value = "好评送券活动每用户可参与次数,0或null不限制")
+    @TableField("good_rating_participation_limit")
+    private Integer goodRatingParticipationLimit;
+
+    /** 好评送券发券类型:1=只发优惠券,2=只发代金券,null=都发(不落库,exist=false,由调用方或配置注入) */
+    @ApiModelProperty(value = "好评送券发券类型:1只发优惠券,2只发代金券,null都发")
+    @TableField(exist = false)
+    private Integer goodRatingRewardType;
 
 }

+ 12 - 17
alien-store/src/main/java/shop/alien/store/service/impl/CommonRatingServiceImpl.java

@@ -21,10 +21,10 @@ import org.springframework.transaction.annotation.Transactional;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.*;
 import shop.alien.entity.store.vo.CommonCommentVo;
-import shop.alien.entity.storePlatform.StoreOperationalActivity;
 import shop.alien.entity.store.vo.CommonRatingVo;
 import shop.alien.entity.store.vo.StoreInfoScoreVo;
 import shop.alien.entity.store.vo.WebSocketVo;
+import shop.alien.entity.storePlatform.StoreOperationalActivity;
 import shop.alien.mapper.*;
 import shop.alien.mapper.storePlantform.StoreOperationalActivityMapper;
 import shop.alien.store.config.WebSocketProcess;
@@ -224,7 +224,7 @@ public class CommonRatingServiceImpl extends ServiceImpl<CommonRatingMapper, Com
                 websocketVo.setText(JSONObject.from(lifeMessage).toJSONString());
                 webSocketProcess.sendMessage("store_" + storeUser.getPhone(), JSONObject.from(websocketVo).toJSONString());
             }
-            // 用户好评且 AI 审核通过后:按运营活动表(评论有礼)配置的 coupon_id/voucher_id 发放,参与次数以活动 participation_limit 为准,发放成功后扣减库存,按优惠券/代金券分别发通知
+            // 用户好评且 AI 审核通过后:按运营活动表(评论有礼)配置的 coupon_id/voucher_id 发放,用 participation_limit 限制参与次数,发放成功后扣减库存,按优惠券/代金券发对应通知
             if (score != null && score >= 4.5 && commonRating.getUserId() != null && commonRating.getBusinessId() != null) {
                 try {
                     Date now = new Date();
@@ -233,30 +233,25 @@ public class CommonRatingServiceImpl extends ServiceImpl<CommonRatingMapper, Com
                             .eq(StoreOperationalActivity::getActivityType, "COMMENT")
                             .in(StoreOperationalActivity::getStatus, 5, 8)
                             .le(StoreOperationalActivity::getStartTime, now)
-                            .ge(StoreOperationalActivity::getEndTime, now);
-                    activityWrapper.last("LIMIT 1");
+                            .ge(StoreOperationalActivity::getEndTime, now)
+                            .orderByDesc(StoreOperationalActivity::getId)
+                            .last("LIMIT 1");
                     StoreOperationalActivity activity = storeOperationalActivityMapper.selectOne(activityWrapper);
-                    if (activity == null) {
-                        log.debug("CommonRatingService 好评送券跳过:店铺无进行中的评论有礼活动 storeId={}", businessId);
+                    if (activity == null || (activity.getCouponId() == null && activity.getVoucherId() == null)) {
                         return;
                     }
                     Integer limit = activity.getParticipationLimit();
                     if (limit != null && limit > 0) {
                         int passedCount = commonRatingMapper.countPassedGoodRatingsByUserAndStore(commonRating.getUserId(), businessId);
-                        if (passedCount >= limit) {
-                            log.info("CommonRatingService 好评送券跳过:已达运营活动参与次数上限 userId={}, storeId={}, activityId={}, limit={}, count={}",
-                                    commonRating.getUserId(), businessId, activity.getId(), limit, passedCount);
+                        if (passedCount > limit) {
+                            log.info("CommonRatingService 好评送券跳过:超过运营活动参与次数 participation_limit={}, count={}, userId={}, storeId={}",
+                                    limit, passedCount, commonRating.getUserId(), businessId);
                             return;
                         }
                     }
-                    if (activity.getCouponId() != null || activity.getVoucherId() != null) {
-                        lifeDiscountCouponStoreFriendService.issueCouponForGoodRating(
-                                Math.toIntExact(commonRating.getUserId()),
-                                businessId,
-                                activity.getCouponId(),
-                                activity.getVoucherId(),
-                                activity.getCouponQuantity() != null ? activity.getCouponQuantity() : 1);
-                    }
+//                    lifeDiscountCouponStoreFriendService.issueCouponForGoodRating(
+//                            Math.toIntExact(commonRating.getUserId()), businessId,
+//                            activity.getCouponId(), activity.getVoucherId());
                 } catch (Exception ex) {
                     log.warn("CommonRatingService 好评送券异常 userId={}, storeId={}, msg={}", commonRating.getUserId(), businessId, ex.getMessage());
                 }

+ 25 - 36
alien-store/src/main/java/shop/alien/store/service/impl/LifeDiscountCouponStoreFriendServiceImpl.java

@@ -649,40 +649,27 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
     }
 
     /**
-     * 好评送券:用户对店铺好评且通过审核后,将店铺可用优惠券和代金券发放到用户卡包,通知按优惠券/代金券分开发送
+     * 好评送券:按优惠券/代金券ID发放到用户券包,发放成功后扣减库存;根据是优惠券还是代金券发送对应通知。
      */
     @Override
-    public int issueCouponForGoodRating(Integer userId, Integer storeId) {
+    public int issueCouponForGoodRating(Integer userId, Integer storeId, Integer couponId, Integer voucherId) {
         if (userId == null || storeId == null) {
             return 0;
         }
-        LocalDate currentDate = LocalDate.now();
-        LambdaQueryWrapper<LifeDiscountCoupon> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeDiscountCoupon::getStoreId, String.valueOf(storeId))
-                .eq(LifeDiscountCoupon::getGetStatus, DiscountCouponEnum.CAN_GET.getValue())
-                .ge(LifeDiscountCoupon::getValidDate, currentDate)
-                .gt(LifeDiscountCoupon::getSingleQty, 0);
-        List<LifeDiscountCoupon> coupons = lifeDiscountCouponMapper.selectList(queryWrapper);
-
-        LambdaQueryWrapper<LifeCoupon> wrapper = new LambdaQueryWrapper<LifeCoupon>();
-        wrapper.eq(LifeCoupon::getStoreId, String.valueOf(storeId))
-                .eq(LifeCoupon::getStatus, 5)
-                .gt(LifeCoupon::getSingleQty, 0);
-        List<LifeCoupon> voucherList = lifeCouponMapper.selectList(wrapper);
-
-        if (CollectionUtils.isEmpty(coupons) && CollectionUtils.isEmpty(voucherList)) {
+        if ((couponId == null || couponId <= 0) && (voucherId == null || voucherId <= 0)) {
             return 0;
         }
-
         int commenterUserId = userId.intValue();
         StoreInfo storeInfo = storeInfoMapper.selectById(storeId);
         LifeUser lifeUser = lifeUserMapper.selectById(commenterUserId);
         int grantedCoupon = 0;
         int grantedVoucher = 0;
 
-        // 发放优惠券到用户卡包
-        if (ObjectUtils.isNotEmpty(coupons)) {
-            for (LifeDiscountCoupon coupon : coupons) {
+        // 按优惠券ID发放:发放1张,扣减库存,发优惠券通知
+        if (couponId != null && couponId > 0) {
+            LifeDiscountCoupon coupon = lifeDiscountCouponMapper.selectById(couponId);
+            if (coupon != null && String.valueOf(storeId).equals(coupon.getStoreId())
+                    && coupon.getSingleQty() != null && coupon.getSingleQty() > 0) {
                 try {
                     LifeDiscountCouponUser lifeDiscountCouponUser = new LifeDiscountCouponUser();
                     lifeDiscountCouponUser.setCouponId(coupon.getId());
@@ -694,43 +681,45 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
                     lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
                     coupon.setSingleQty(coupon.getSingleQty() - 1);
                     lifeDiscountCouponMapper.updateById(coupon);
-                    grantedCoupon++;
+                    grantedCoupon = 1;
                 } catch (Exception e) {
-                    // 单张发放失败不影响其余
+
                 }
             }
         }
 
-        // 发放代金券到用户卡包
-        if (ObjectUtils.isNotEmpty(voucherList)) {
-            for (LifeCoupon life : voucherList) {
+        // 按代金券ID发放:发放1张,扣减库存,发代金券通知
+        if (voucherId != null && voucherId > 0) {
+            LifeCoupon lifeCoupon = lifeCouponMapper.selectById(voucherId);
+            if (lifeCoupon != null && String.valueOf(storeId).equals(lifeCoupon.getStoreId())
+                    && lifeCoupon.getSingleQty() != null && lifeCoupon.getSingleQty() > 0) {
                 try {
                     LifeDiscountCouponUser lifeDiscountCouponUser = new LifeDiscountCouponUser();
-                    lifeDiscountCouponUser.setVoucherId(life.getId());
+                    lifeDiscountCouponUser.setVoucherId(lifeCoupon.getId());
                     lifeDiscountCouponUser.setUserId(commenterUserId);
                     lifeDiscountCouponUser.setReceiveTime(new Date());
-                    if (life.getEndDate() != null) {
-                        lifeDiscountCouponUser.setExpirationTime(life.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
+                    if (lifeCoupon.getEndDate() != null) {
+                        lifeDiscountCouponUser.setExpirationTime(lifeCoupon.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                     }
                     lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
                     lifeDiscountCouponUser.setDeleteFlag(0);
                     lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
-                    life.setSingleQty(life.getSingleQty() - 1);
-                    lifeCouponMapper.updateById(life);
-                    grantedVoucher++;
+                    lifeCoupon.setSingleQty(lifeCoupon.getSingleQty() - 1);
+                    lifeCouponMapper.updateById(lifeCoupon);
+                    grantedVoucher = 1;
                 } catch (Exception e) {
-                    // 单张发放失败不影响其余
+
                 }
             }
         }
 
-        // 通知分开:优惠券一条,代金券一条
+        // 根据发放的是优惠券还是代金券发送对应通知
         if (lifeUser != null && storeInfo != null) {
             if (grantedCoupon > 0) {
                 LifeNotice couponNotice = new LifeNotice();
                 couponNotice.setSenderId("system");
                 couponNotice.setReceiverId("user_" + lifeUser.getUserPhone());
-                String couponText = "您对店铺「" + storeInfo.getStoreName() + "」的好评已通过审核,已为您发放" + grantedCoupon + "张优惠券,快去我的券包查看吧~";
+                String couponText = "您对店铺「" + storeInfo.getStoreName() + "」的好评已通过审核,已为您发放优惠券,快去我的券包查看吧~";
                 JSONObject couponJson = new JSONObject();
                 couponJson.put("message", couponText);
                 couponNotice.setContext(couponJson.toJSONString());
@@ -744,7 +733,7 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
                 LifeNotice voucherNotice = new LifeNotice();
                 voucherNotice.setSenderId("system");
                 voucherNotice.setReceiverId("user_" + lifeUser.getUserPhone());
-                String voucherText = "您对店铺「" + storeInfo.getStoreName() + "」的好评已通过审核,已为您发放" + grantedVoucher + "张代金券,快去我的券包查看吧~";
+                String voucherText = "您对店铺「" + storeInfo.getStoreName() + "」的好评已通过审核,已为您发放代金券,快去我的券包查看吧~";
                 JSONObject voucherJson = new JSONObject();
                 voucherJson.put("message", voucherText);
                 voucherNotice.setContext(voucherJson.toJSONString());