瀏覽代碼

线上预订 核销增加发送好有店铺优惠卷

qinxuyang 1 天之前
父節點
當前提交
34ea6569d8

+ 6 - 0
alien-store/src/main/java/shop/alien/store/service/LifeDiscountCouponStoreFriendService.java

@@ -36,6 +36,12 @@ public interface LifeDiscountCouponStoreFriendService extends IService<LifeDisco
      */
     List<LifeDiscountCouponStoreFriendVo> issueFriendCoupon(LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto);
 
+
+    /**
+     * (新)消费后发放好友优惠券并返回列表
+     */
+    List<LifeDiscountCouponStoreFriendVo> newIssueFriendCoupon(LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto);
+
     /**
      * 获取该订单发放的好友列表
      */

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

@@ -84,6 +84,8 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
 
     private final CommonRatingMapper commonRatingMapper;
 
+    private final UserReservationOrderMapper userReservationOrderMapper;
+
     @Override
     public List<LifeDiscountCouponStoreFriendVo> getFriendCouponList(UserLoginInfo userLoginInfo, String friendUserId) {
         List<LifeDiscountCouponStoreFriendVo> result = new ArrayList<>();
@@ -463,6 +465,155 @@ public class LifeDiscountCouponStoreFriendServiceImpl extends ServiceImpl<LifeDi
     }
 
     @Override
+    public List<LifeDiscountCouponStoreFriendVo> newIssueFriendCoupon(LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto) {
+        // 用于存储最终成功发放的优惠券信息的列表
+        List<LifeDiscountCouponStoreFriendVo> result = new ArrayList<>();
+
+        //判断该订单是否已经发放过好友店铺优惠券
+//        LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectById(lifeDiscountCouponStoreFriendDto.getOrderId());
+//        if (lifeUserOrder.getSendDiscountCouponFlag() == 1) {
+//            throw new RuntimeException("已发放优惠券");
+//        }
+        UserReservationOrder userReservationOrder = userReservationOrderMapper.selectById(lifeDiscountCouponStoreFriendDto.getOrderId());
+
+
+        //送券规则
+        List<Integer> couponList = new ArrayList<>();
+        LambdaQueryWrapper<LifeDiscountCouponFriendRule> ruleLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        ruleLambdaQueryWrapper.eq(LifeDiscountCouponFriendRule::getStoreId, userReservationOrder.getStoreId())
+                .eq(LifeDiscountCouponFriendRule::getDeleteFlag, 0);
+        List<LifeDiscountCouponFriendRule> lifeDiscountCouponFriendRules = lifeDiscountCouponFriendRuleMapper.selectList(ruleLambdaQueryWrapper);
+        lifeDiscountCouponFriendRules = lifeDiscountCouponFriendRules.stream().filter(i -> i.getMoneyLow().compareTo(userReservationOrder.getDepositAmount()) <= 0 && i.getMoneyHigh().compareTo(userReservationOrder.getDepositAmount()) >= 0).collect(Collectors.toList());
+        if (ObjectUtils.isNotEmpty(lifeDiscountCouponFriendRules)) {
+            LambdaQueryWrapper<LifeDiscountCouponFriendRuleDetail> detailLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            detailLambdaQueryWrapper.in(LifeDiscountCouponFriendRuleDetail::getRuleId, lifeDiscountCouponFriendRules.stream().map(LifeDiscountCouponFriendRule::getId).collect(Collectors.toList()));
+            List<LifeDiscountCouponFriendRuleDetail> lifeDiscountCouponFriendRuleDetails = lifeDiscountCouponFriendRuleDetailMapper.selectList(detailLambdaQueryWrapper);
+            couponList = lifeDiscountCouponFriendRuleDetails.stream().map(LifeDiscountCouponFriendRuleDetail::getCouponId).collect(Collectors.toList());
+        }
+
+
+//        lifeUserOrder.setSendDiscountCouponFlag(1);
+//        lifeUserOrderMapper.updateById(lifeUserOrder);
+
+        //有符合规则的优惠券
+        if (ObjectUtils.isNotEmpty(couponList) && !couponList.isEmpty()) {
+            // 获取当前消费用户的ID
+            int userId = Integer.parseInt(userReservationOrder.getUserId().toString());
+            LifeUser lifeUser = lifeUserMapper.selectById(userId);
+            // 从传入的DTO对象中获取店铺ID
+            StoreUser storeUser =
+                    storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, userReservationOrder.getStoreId()));
+            Integer storeUserId = storeUser.getId();
+            Integer storeId = storeUser.getStoreId();
+            StoreInfo storeInfo = storeInfoMapper.selectById(storeUser.getStoreId());
+
+            // 根据店铺ID从数据库中查询该店铺为好友设定的所有优惠券信息
+            List<LifeDiscountCouponStoreFriend> lifeDiscountCouponStoreFriends = lifeDiscountCouponStoreFriendMapper.selectList(
+                    // 使用LambdaQueryWrapper构建查询条件,筛选出店铺ID等于指定店铺ID的优惠券记录,并且发布状态为已发布
+                    new LambdaQueryWrapper<LifeDiscountCouponStoreFriend>().eq(LifeDiscountCouponStoreFriend::getStoreUserId, storeId)
+                            .eq(LifeDiscountCouponStoreFriend::getReleaseType, 1)
+                            .in(LifeDiscountCouponStoreFriend::getCouponId, couponList));
+
+            // 获取当前日期,用于后续判断优惠券是否在有效期内
+            LocalDate currentDate = LocalDate.now();
+            if (!lifeDiscountCouponStoreFriends.isEmpty()) {
+                // 遍历该店铺为好友设定的所有优惠券信息
+                for (LifeDiscountCouponStoreFriend coupon : lifeDiscountCouponStoreFriends) {
+                    // 创建一个Lambda查询包装器,用于构建查询优惠券的条件
+                    LambdaQueryWrapper<LifeDiscountCoupon> queryWrapper = new LambdaQueryWrapper<>();
+                    // 设置查询条件:优惠券的领取状态为可领取
+                    queryWrapper.eq(LifeDiscountCoupon::getGetStatus, DiscountCouponEnum.CAN_GET.getValue());
+                    // 设置查询条件:优惠券的ID等于当前遍历到的优惠券ID
+                    queryWrapper.eq(LifeDiscountCoupon::getId, coupon.getCouponId());
+                    // 设置查询条件:优惠券的结束日期大于等于当前日期,即优惠券在有效期内
+                    queryWrapper.ge(LifeDiscountCoupon::getValidDate, currentDate);
+                    // 设置查询条件:优惠券还有库存
+                    queryWrapper.gt(LifeDiscountCoupon::getSingleQty, 0);
+
+                    // 根据上述查询条件从数据库中查询符合条件的优惠券信息
+                    LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectOne(queryWrapper);
+
+                    // 如果查询到符合条件的优惠券,说明该优惠券可领
+                    if (lifeDiscountCoupon != null) {
+                        // 设定本次领取优惠券的数量,这里固定为1张
+                        Integer receiveQuantity = 1;
+
+                        // 循环领取优惠券,根据设定的领取数量进行多次领取操作
+                        for (int i = 0; i < receiveQuantity; i++) {
+                            // 创建一个新的用户优惠券记录对象
+                            LifeDiscountCouponUser lifeDiscountCouponUser = new LifeDiscountCouponUser();
+                            // 设置该优惠券记录的优惠券ID
+                            lifeDiscountCouponUser.setCouponId(coupon.getCouponId());
+                            // 设置该优惠券记录的用户ID为当前用户ID
+                            lifeDiscountCouponUser.setUserId(userId);
+                            // 设置该优惠券的领取时间为当前时间
+                            lifeDiscountCouponUser.setReceiveTime(new Date());
+                            // 设置该优惠券的过期时间为优惠券本身的结束日期
+                            lifeDiscountCouponUser.setExpirationTime(lifeDiscountCoupon.getValidDate());
+                            // 设置该优惠券的状态为待使用
+                            lifeDiscountCouponUser.setStatus(Integer.parseInt(DiscountCouponEnum.WAITING_USED.getValue()));
+                            lifeDiscountCouponUser.setIssueSource(4); // 4-好友赠送
+                            // 将该用户优惠券记录插入到数据库中
+                            lifeDiscountCouponUserMapper.insert(lifeDiscountCouponUser);
+                        }
+
+                        // 削减该优惠券的库存,根据领取数量减少库存
+                        coupon.setSingleQty(coupon.getSingleQty() - receiveQuantity);
+                        // 更新数据库中该优惠券的库存信息
+                        lifeDiscountCouponStoreFriendMapper.updateById(coupon);
+
+                        // 创建一个用于存储优惠券信息的VO对象
+                        LifeDiscountCouponStoreFriendVo lifeDiscountCouponStoreFriendVo = new LifeDiscountCouponStoreFriendVo();
+                        // 将优惠券信息复制到VO对象中
+                        BeanUtils.copyProperties(coupon, lifeDiscountCouponStoreFriendVo);
+                        // 将该VO对象添加到最终结果列表中
+                        result.add(lifeDiscountCouponStoreFriendVo);
+                    }
+                }
+
+                //存储发放日志
+                LifeDiscountCouponStoreFriendSendRecord lifeDiscountCouponStoreFriendSendRecord = new LifeDiscountCouponStoreFriendSendRecord();
+                //存入门店id
+                lifeDiscountCouponStoreFriendSendRecord.setStoreId(Integer.parseInt(userReservationOrder.getStoreId().toString()));
+                //存入接收人id
+                lifeDiscountCouponStoreFriendSendRecord.setUserId(userId);
+                //存入发放人id
+                lifeDiscountCouponStoreFriendSendRecord.setStoreUserId(storeUserId);
+                //存入订单id
+                lifeDiscountCouponStoreFriendSendRecord.setOrderId(Integer.parseInt(userReservationOrder.getId().toString()));
+                //存入优惠券ids
+                String[] couponIds = result.stream()
+                        .map(LifeDiscountCouponStoreFriendVo::getCouponId)
+                        .map(String::valueOf)  // 将 Integer 转换为 String
+                        .toArray(String[]::new);
+
+                String join = String.join(",", couponIds);
+                lifeDiscountCouponStoreFriendSendRecord.setDiscountCouponIds(join);
+                lifeDiscountCouponStoreFriendSendRecordMapper.insert(lifeDiscountCouponStoreFriendSendRecord);
+
+                //发送公告消息
+                LifeNotice lifeNotice = new LifeNotice();
+                //存入发送人
+                lifeNotice.setSenderId("system");
+                //存入接收人
+                lifeNotice.setReceiverId("user_" + lifeUser.getUserPhone());
+                //存入信息
+                String text = storeInfo.getStoreName() + "赠送了您他的好友店铺优惠券,快去我的券包查看吧~";
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("message", text);
+                lifeNotice.setContext(jsonObject.toJSONString());
+                //存入类型
+                lifeNotice.setNoticeType(1);
+                lifeNotice.setTitle("赠劵通知");
+                lifeNoticeMapper.insert(lifeNotice);
+
+            }
+        }
+            // 返回成功发放的优惠券信息列表
+            return result;
+    }
+
+    @Override
     public List<LifeDiscountCouponStoreFriendVo> getSendCoupons(LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto) {
         List<LifeDiscountCouponStoreFriendVo> result = new ArrayList<>();
         Integer orderId = lifeDiscountCouponStoreFriendDto.getOrderId();

+ 8 - 7
alien-store/src/main/java/shop/alien/store/service/impl/StoreReservationServiceImpl.java

@@ -12,6 +12,7 @@ import shop.alien.entity.store.StoreInfo;
 import shop.alien.entity.store.UserReservation;
 import shop.alien.entity.store.UserReservationTable;
 import shop.alien.entity.store.UserReservationOrder;
+import shop.alien.entity.store.dto.LifeDiscountCouponStoreFriendDto;
 import shop.alien.entity.store.vo.StoreReservationListVo;
 import shop.alien.entity.store.StoreBookingBusinessHours;
 import shop.alien.entity.store.StoreBookingSettings;
@@ -22,13 +23,7 @@ import shop.alien.mapper.UserReservationTableMapper;
 import shop.alien.mapper.EssentialHolidayComparisonMapper;
 import shop.alien.store.config.BaseRedisService;
 import shop.alien.store.listener.RedisKeyExpirationHandler;
-import shop.alien.store.service.StoreBookingTableService;
-import shop.alien.store.service.StoreInfoService;
-import shop.alien.store.service.MerchantPaymentOrderService;
-import shop.alien.store.service.StoreReservationService;
-import shop.alien.store.service.UserReservationOrderService;
-import shop.alien.store.service.StoreBookingBusinessHoursService;
-import shop.alien.store.service.StoreBookingSettingsService;
+import shop.alien.store.service.*;
 import shop.alien.store.strategy.merchantPayment.MerchantPaymentStrategy;
 import shop.alien.store.strategy.merchantPayment.MerchantPaymentStrategyFactory;
 import shop.alien.store.util.ali.AliSms;
@@ -78,6 +73,7 @@ public class StoreReservationServiceImpl extends ServiceImpl<StoreReservationMap
     private final StoreBookingSettingsService storeBookingSettingsService;
     private final EssentialHolidayComparisonMapper essentialHolidayComparisonMapper;
     private final MerchantPaymentOrderService merchantPaymentOrderService;
+    private final LifeDiscountCouponStoreFriendService lifeDiscountCouponStoreFriendService;
 
     /** 预约状态:已取消 */
     private static final int STATUS_CANCELLED = 3;
@@ -788,6 +784,11 @@ public class StoreReservationServiceImpl extends ServiceImpl<StoreReservationMap
             throw new RuntimeException("更新订单状态失败");
         }
 
+        //发放好友优惠券
+        LifeDiscountCouponStoreFriendDto lifeDiscountCouponStoreFriendDto = new LifeDiscountCouponStoreFriendDto();
+        lifeDiscountCouponStoreFriendDto.setOrderId(Integer.parseInt(order.getId().toString()));
+        lifeDiscountCouponStoreFriendService.newIssueFriendCoupon(lifeDiscountCouponStoreFriendDto);
+
         // 核销成功后,发送订金退款短信和通知
         try {
             sendDepositRefundSmsAndNoticeInternal(reservation, order);