优惠券的使用功能优化
This commit is contained in:
+48
-14
@@ -321,32 +321,66 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
* 处理优惠券标签关联
|
||||
*/
|
||||
private void handleCouponTags(Long couponId, List<Long> availableTagIds, List<Long> unavailableTagIds) {
|
||||
// 删除原有标签关联
|
||||
couponTagMapper.delete(new LambdaQueryWrapper<CouponTagDO>()
|
||||
// 先查询所有现有的标签关联
|
||||
List<CouponTagDO> existingTags = couponTagMapper.selectList(new LambdaQueryWrapper<CouponTagDO>()
|
||||
.eq(CouponTagDO::getCouponId, couponId)
|
||||
.eq(CouponTagDO::getIsDelete, 0)
|
||||
);
|
||||
|
||||
// 添加可用标签
|
||||
// 构建现有标签的映射,方便快速查找
|
||||
java.util.Map<Long, CouponTagDO> existingTagMap = new java.util.HashMap<>();
|
||||
for (CouponTagDO tag : existingTags) {
|
||||
existingTagMap.put(tag.getTagId(), tag);
|
||||
}
|
||||
|
||||
// 处理可用标签
|
||||
if (availableTagIds != null) {
|
||||
for (Long tagId : availableTagIds) {
|
||||
CouponTagDO couponTagDO = new CouponTagDO();
|
||||
couponTagDO.setCouponId(couponId);
|
||||
couponTagDO.setTagId(tagId);
|
||||
couponTagDO.setIsAvailable(1);
|
||||
couponTagMapper.insert(couponTagDO);
|
||||
if (existingTagMap.containsKey(tagId)) {
|
||||
// 如果标签已存在,更新其状态
|
||||
CouponTagDO existingTag = existingTagMap.get(tagId);
|
||||
if (existingTag.getIsAvailable() != 1) {
|
||||
existingTag.setIsAvailable(1);
|
||||
couponTagMapper.updateById(existingTag);
|
||||
}
|
||||
existingTagMap.remove(tagId); // 标记为已处理
|
||||
} else {
|
||||
// 如果标签不存在,创建新的
|
||||
CouponTagDO couponTagDO = new CouponTagDO();
|
||||
couponTagDO.setCouponId(couponId);
|
||||
couponTagDO.setTagId(tagId);
|
||||
couponTagDO.setIsAvailable(1);
|
||||
couponTagMapper.insert(couponTagDO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加不可用标签
|
||||
// 处理不可用标签
|
||||
if (unavailableTagIds != null) {
|
||||
for (Long tagId : unavailableTagIds) {
|
||||
CouponTagDO couponTagDO = new CouponTagDO();
|
||||
couponTagDO.setCouponId(couponId);
|
||||
couponTagDO.setTagId(tagId);
|
||||
couponTagDO.setIsAvailable(0);
|
||||
couponTagMapper.insert(couponTagDO);
|
||||
if (existingTagMap.containsKey(tagId)) {
|
||||
// 如果标签已存在,更新其状态
|
||||
CouponTagDO existingTag = existingTagMap.get(tagId);
|
||||
if (existingTag.getIsAvailable() != 0) {
|
||||
existingTag.setIsAvailable(0);
|
||||
couponTagMapper.updateById(existingTag);
|
||||
}
|
||||
existingTagMap.remove(tagId); // 标记为已处理
|
||||
} else {
|
||||
// 如果标签不存在,创建新的
|
||||
CouponTagDO couponTagDO = new CouponTagDO();
|
||||
couponTagDO.setCouponId(couponId);
|
||||
couponTagDO.setTagId(tagId);
|
||||
couponTagDO.setIsAvailable(0);
|
||||
couponTagMapper.insert(couponTagDO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除剩余的标签关联(这些是不在新列表中的标签)
|
||||
for (CouponTagDO tagToDelete : existingTagMap.values()) {
|
||||
couponTagMapper.deleteById(tagToDelete.getId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+21
-8
@@ -74,22 +74,25 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
// 处理优惠券
|
||||
BigDecimal actualAmount = totalAmount;
|
||||
Long couponId = orderDTO.getCouponId();
|
||||
if (couponId != null && couponId > 0) {
|
||||
Long userCouponId = orderDTO.getCouponId();
|
||||
Long couponId = null;
|
||||
if (userCouponId != null && userCouponId > 0) {
|
||||
// 检查优惠券是否存在且可用
|
||||
UserCouponDO userCoupon = userCouponMapper.selectOne(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("id", userCouponId)
|
||||
.eq("user_id", userId)
|
||||
.eq("coupon_id", couponId)
|
||||
.eq("status", 1)
|
||||
);
|
||||
|
||||
if (userCoupon != null) {
|
||||
couponId = userCoupon.getCouponId();
|
||||
CouponDO coupon = couponMapper.selectById(couponId);
|
||||
if (coupon != null && coupon.getStatus() == 1) {
|
||||
// 计算优惠金额
|
||||
if (coupon.getCouponType() == 1) { // 折扣券
|
||||
actualAmount = totalAmount.multiply(coupon.getDiscountValue()).divide(new BigDecimal(10), 2, BigDecimal.ROUND_HALF_UP);
|
||||
actualAmount = totalAmount.multiply(coupon.getDiscountValue())
|
||||
.divide(new BigDecimal(10), 2, BigDecimal.ROUND_HALF_UP);
|
||||
} else if (coupon.getCouponType() == 2) { // 满减券
|
||||
if (totalAmount.compareTo(coupon.getMinAmount()) >= 0) {
|
||||
actualAmount = totalAmount.subtract(coupon.getReduceAmount());
|
||||
@@ -181,14 +184,19 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
// 返还优惠券
|
||||
if (order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
UserCouponDO userCoupon = userCouponMapper.selectOne(
|
||||
// 查询用户的已使用优惠券,找到与订单中couponId匹配的记录
|
||||
List<UserCouponDO> userCoupons = userCouponMapper.selectList(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("user_id", userId)
|
||||
.eq("coupon_id", order.getCouponId())
|
||||
.eq("status", 2)
|
||||
);
|
||||
|
||||
if (userCoupon != null) {
|
||||
// 找到最近使用的优惠券
|
||||
if (!userCoupons.isEmpty()) {
|
||||
// 按使用时间倒序排序,取最近使用的
|
||||
userCoupons.sort((a, b) -> b.getUseTime().compareTo(a.getUseTime()));
|
||||
UserCouponDO userCoupon = userCoupons.get(0);
|
||||
userCoupon.setStatus(1); // 未使用
|
||||
userCoupon.setUseTime(null);
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
@@ -218,14 +226,19 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
// 如果取消订单,返还优惠券
|
||||
if (status == 3 && order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
UserCouponDO userCoupon = userCouponMapper.selectOne(
|
||||
// 查询用户的已使用优惠券,找到与订单中couponId匹配的记录
|
||||
List<UserCouponDO> userCoupons = userCouponMapper.selectList(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("user_id", order.getUserId())
|
||||
.eq("coupon_id", order.getCouponId())
|
||||
.eq("status", 2)
|
||||
);
|
||||
|
||||
if (userCoupon != null) {
|
||||
// 找到最近使用的优惠券
|
||||
if (!userCoupons.isEmpty()) {
|
||||
// 按使用时间倒序排序,取最近使用的
|
||||
userCoupons.sort((a, b) -> b.getUseTime().compareTo(a.getUseTime()));
|
||||
UserCouponDO userCoupon = userCoupons.get(0);
|
||||
userCoupon.setStatus(1); // 未使用
|
||||
userCoupon.setUseTime(null);
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
|
||||
Reference in New Issue
Block a user