优惠券发放和展示功能优化
This commit is contained in:
+1
@@ -93,6 +93,7 @@ public class CartServiceImpl implements CartService {
|
||||
cartVO.setPrice(product.getPrice());
|
||||
cartVO.setQuantity(cart.getQuantity());
|
||||
cartVO.setTotalPrice(product.getPrice().multiply(new BigDecimal(cart.getQuantity())));
|
||||
cartVO.setTags(product.getTags());
|
||||
cartVOList.add(cartVO);
|
||||
}
|
||||
}
|
||||
|
||||
+42
-20
@@ -287,31 +287,33 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
}
|
||||
|
||||
// 检查优惠券数量是否足够
|
||||
if (couponDO.getUsedCount() + userIds.size() * quantity > couponDO.getTotalCount()) {
|
||||
int totalNeeded = userIds.size() * quantity;
|
||||
if (couponDO.getUsedCount() + totalNeeded > couponDO.getTotalCount()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 发放优惠券
|
||||
int distributedCount = 0;
|
||||
for (Long userId : userIds) {
|
||||
for (int i = 0; i < quantity; i++) {
|
||||
// 检查是否已经分配
|
||||
UserCouponDO existing = userCouponMapper.selectOne(new LambdaQueryWrapper<UserCouponDO>()
|
||||
.eq(UserCouponDO::getUserId, userId)
|
||||
.eq(UserCouponDO::getCouponId, couponId)
|
||||
.eq(UserCouponDO::getIsDelete, 0)
|
||||
);
|
||||
if (existing == null) {
|
||||
// 分配优惠券
|
||||
UserCouponDO userCouponDO = new UserCouponDO();
|
||||
userCouponDO.setUserId(userId);
|
||||
userCouponDO.setCouponId(couponId);
|
||||
userCouponDO.setStatus(1);
|
||||
userCouponDO.setObtainTime(LocalDateTime.now());
|
||||
userCouponMapper.insert(userCouponDO);
|
||||
}
|
||||
// 分配优惠券(不检查是否已存在,允许用户拥有多张相同优惠券)
|
||||
UserCouponDO userCouponDO = new UserCouponDO();
|
||||
userCouponDO.setUserId(userId);
|
||||
userCouponDO.setCouponId(couponId);
|
||||
userCouponDO.setStatus(1);
|
||||
userCouponDO.setObtainTime(LocalDateTime.now());
|
||||
userCouponMapper.insert(userCouponDO);
|
||||
distributedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新优惠券使用数量
|
||||
couponDO.setUsedCount(couponDO.getUsedCount() + distributedCount);
|
||||
if (couponDO.getUsedCount() >= couponDO.getTotalCount()) {
|
||||
couponDO.setStatus(3); // 已用完
|
||||
}
|
||||
updateById(couponDO);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -378,18 +380,38 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
return true;
|
||||
}
|
||||
|
||||
// 查询优惠券的所有标签关联
|
||||
List<CouponTagDO> couponTagDOs = couponTagMapper.selectList(new LambdaQueryWrapper<CouponTagDO>()
|
||||
.eq(CouponTagDO::getCouponId, couponId)
|
||||
.in(CouponTagDO::getTagId, productTagIds)
|
||||
);
|
||||
|
||||
// 如果有不可用标签,则返回false
|
||||
for (CouponTagDO couponTagDO : couponTagDOs) {
|
||||
if (couponTagDO.getIsAvailable() == 0) {
|
||||
// 分离可用标签和不可用标签
|
||||
List<Long> availableTagIds = couponTagDOs.stream()
|
||||
.filter(tag -> tag.getIsAvailable() == 1)
|
||||
.map(tag -> tag.getTagId())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<Long> unavailableTagIds = couponTagDOs.stream()
|
||||
.filter(tag -> tag.getIsAvailable() == 0)
|
||||
.map(tag -> tag.getTagId())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 检查是否有不可用标签
|
||||
for (Long tagId : productTagIds) {
|
||||
if (unavailableTagIds.contains(tagId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否只包含可用标签(如果有可用标签配置)
|
||||
if (!availableTagIds.isEmpty()) {
|
||||
for (Long tagId : productTagIds) {
|
||||
if (!availableTagIds.contains(tagId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,4 +13,5 @@ public class CartVO {
|
||||
private BigDecimal price;
|
||||
private Integer quantity;
|
||||
private BigDecimal totalPrice;
|
||||
private String tags;
|
||||
}
|
||||
Reference in New Issue
Block a user