|
|
@@ -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();
|