|
|
@@ -29,7 +29,6 @@ import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -72,17 +71,18 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
|
|
|
@Override
|
|
|
public void issueCoupons(UserCouponsIssueVo userCouponsIssue) {
|
|
|
- var coupons = lambdaQuery().in(Coupon::getId, userCouponsIssue.getCouponIds()).list();
|
|
|
+ var coupon = lambdaQuery().eq(Coupon::getId, userCouponsIssue.getCouponId()).one();
|
|
|
|
|
|
// 校验优惠券领取方式
|
|
|
- if (coupons.stream().anyMatch(coupon -> coupon.getReceiveType().equals(Coupon.RECEIVE_TYPE_主动领取))) {
|
|
|
- throw new BusinessException("包含限自主领取的优惠券");
|
|
|
+ if (coupon.getReceiveType().equals(Coupon.RECEIVE_TYPE_主动领取)) {
|
|
|
+ throw new BusinessException("优惠券仅限用户自主领取");
|
|
|
}
|
|
|
|
|
|
// 校验发放数量是否不大于券剩余数量(数量=0代表不限量)
|
|
|
- var flag = coupons.stream().findAny().filter(coupon -> coupon.getQuantity() > 0 && userCouponsIssue.getUserIds().size() > (coupon.getQuantity() - coupon.getClaimedQuantity()));
|
|
|
- if (flag.isPresent()) {
|
|
|
- throw new BusinessException("优惠券剩余数量不足,优惠券名称:%s,剩余:%d,发放数量:%d".formatted(flag.get().getName(), flag.get().getQuantity() - flag.get().getClaimedQuantity(), userCouponsIssue.getUserIds().size()));
|
|
|
+ var totalCouponAmount = userCouponsIssue.getUserIds().size() * userCouponsIssue.getCouponCount();
|
|
|
+ var flag = totalCouponAmount > (coupon.getQuantity() - coupon.getClaimedQuantity());
|
|
|
+ if (flag) {
|
|
|
+ throw new BusinessException("优惠券剩余数量不足,优惠券名称:%s,剩余:%d,待发放数量:%d".formatted(coupon.getName(), coupon.getQuantity() - coupon.getClaimedQuantity(), totalCouponAmount));
|
|
|
}
|
|
|
|
|
|
List<User> users = new ArrayList<>();
|
|
|
@@ -92,29 +92,33 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
users = userService.lambdaQuery().in(User::getMobilePhone, userCouponsIssue.getPhones()).list();
|
|
|
}
|
|
|
|
|
|
- var activityList = activityService.listByIds(coupons.stream().map(Coupon::getActivityId).toList());
|
|
|
+ var activity = activityService.getById(coupon.getActivityId());
|
|
|
|
|
|
// 更新优惠券发放数量
|
|
|
- lambdaUpdate().setSql("claimed_quantity = claimed_quantity + %d".formatted(users.size()))
|
|
|
- .in(Coupon::getId, coupons.stream().map(Coupon::getId).toList()).update();
|
|
|
+ lambdaUpdate().setSql("claimed_quantity = claimed_quantity + %d".formatted(totalCouponAmount))
|
|
|
+ .eq(Coupon::getId, coupon.getId()).update();
|
|
|
|
|
|
+ var startTime = LocalDateTime.now();
|
|
|
+
|
|
|
+ // 为每个用户发放优惠券
|
|
|
users.forEach(user -> {
|
|
|
- for (Coupon coupon : coupons) {
|
|
|
- var userCoupon = new UserCoupon();
|
|
|
- userCoupon.setActivityId(coupon.getActivityId());
|
|
|
- userCoupon.setActivityName(Objects.requireNonNull(activityList.stream().filter(activity -> activity.getId().equals(coupon.getActivityId())).findFirst().orElse(null)).getName());
|
|
|
- userCoupon.setCouponId(coupon.getId());
|
|
|
- userCoupon.setCouponName(coupon.getName());
|
|
|
- userCoupon.setUserId(user.getId());
|
|
|
- userCoupon.setStartTime(LocalDateTime.now());
|
|
|
- userCoupon.setEndTime(LocalDateTime.now().with(LocalTime.MAX).plusDays(coupon.getValidity() - 1));
|
|
|
- userCoupon.setValidity(coupon.getValidity());
|
|
|
- userCoupon.setCouponType(coupon.getCouponType());
|
|
|
- userCoupon.setDiscount(coupon.getDiscount());
|
|
|
- userCoupon.setMinServiceMoney(coupon.getMinServiceMoney());
|
|
|
- userCoupon.setAllowStacke(coupon.getAllowStacke());
|
|
|
- userCoupon.setRemark(coupon.getRemark());
|
|
|
- userCoupon.setStatus(coupon.getStatus());
|
|
|
+ var userCoupon = new UserCoupon();
|
|
|
+ userCoupon.setActivityId(coupon.getActivityId());
|
|
|
+ userCoupon.setActivityName(activity.getName());
|
|
|
+ userCoupon.setCouponId(coupon.getId());
|
|
|
+ userCoupon.setCouponName(coupon.getName());
|
|
|
+ userCoupon.setUserId(user.getId());
|
|
|
+ userCoupon.setStartTime(startTime);
|
|
|
+ userCoupon.setEndTime(startTime.with(LocalTime.MAX).plusDays(coupon.getValidity() - 1));
|
|
|
+ userCoupon.setValidity(coupon.getValidity());
|
|
|
+ userCoupon.setCouponType(coupon.getCouponType());
|
|
|
+ userCoupon.setDiscount(coupon.getDiscount());
|
|
|
+ userCoupon.setMinServiceMoney(coupon.getMinServiceMoney());
|
|
|
+ userCoupon.setAllowStacke(coupon.getAllowStacke());
|
|
|
+ userCoupon.setRemark(coupon.getRemark());
|
|
|
+ userCoupon.setStatus(coupon.getStatus());
|
|
|
+ int n = userCouponsIssue.getCouponCount();
|
|
|
+ for (int i = 0; i < n; i++) {
|
|
|
userCouponSender.sendMessage(userCoupon);
|
|
|
}
|
|
|
});
|
|
|
@@ -141,7 +145,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void usedQuantifyCount(Long couponId){
|
|
|
+ public void usedQuantifyCount(Long couponId) {
|
|
|
lambdaUpdate().setSql("used_quantity = used_quantity + 1").eq(Coupon::getId, couponId).update();
|
|
|
}
|
|
|
|