更新了优惠券模块

This commit is contained in:
chen-xin-zhi 2025-02-09 13:35:51 +08:00
parent 6b95ba86ff
commit 84c004d40c
8 changed files with 108 additions and 9 deletions

View File

@ -272,6 +272,7 @@ public class CouponController {
Long userId = loginUser.getId();
QueryWrapper<UserCoupon> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", userId);
queryWrapper.eq("isUsed", 0);
List<UserCoupon> userCouponList = userCouponService.list();
userCouponList = userCouponList.stream().filter(userCoupon -> userCoupon.getCouponVO().getStatus().equals(status)).toList();
@ -373,6 +374,7 @@ public class CouponController {
Long userId = loginUser.getId();
QueryWrapper<UserCoupon> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", userId);
queryWrapper.eq("isUsed", 0);
queryWrapper.eq("status", "可用");
List<UserCoupon> userCouponList = userCouponService.list(queryWrapper);
userCouponList = userCouponList.stream().filter(userCoupon -> userCoupon.getCouponVO().getStatus().equals("可用")).toList();

View File

@ -509,7 +509,10 @@ public class GoodController {
Set<Long> dateIds = dateMap.keySet();
for (Long dateId : dateIds) {
AppointmentDateVO appointmentDateVO = dateMap.get(dateId);
// 筛序掉已过期的预约日期
// 过滤掉没有空闲的预约日期
if (appointmentDateVO.getIsAvailable() == 0) continue;
// 过滤掉已过期的预约时间段
String specificDate = appointmentDateVO.getSpecificDate();
String today = DateUtil.today();
int result = DateUtil.compare(DateUtil.parse(specificDate), DateUtil.parse(today));

View File

@ -21,14 +21,12 @@ import com.cultural.heritage.model.dto.order.OrderUpdateRequest;
import com.cultural.heritage.model.dto.order.capital.GeneralGoodSingleBuyAddRequest;
import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.model.entity.Order;
import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.entity.*;
import com.cultural.heritage.model.vo.order.OrderVO;
import com.cultural.heritage.service.good.CartExperienceService;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.good.UserCouponService;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
import com.cultural.heritage.service.user.UserService;
@ -85,6 +83,10 @@ public class OrderController {
private GoodService goodService;
@Resource
private UserCouponService userCouponService;
@ -353,12 +355,12 @@ public class OrderController {
/**
* 小程序端用户取消订单
* 小程序端用户取消常规类商品订单
* @param commonRequest 订单id
* @return 是否取消成功
*/
@PostMapping ("/cancel/id")
@Operation(summary = "小程序端用户取消订单", description = "参数订单id权限所有人方法名cancelOrderById")
@Operation(summary = "小程序端用户取消常规类商品订单", description = "参数订单id权限所有人方法名cancelOrderById")
public BaseResponse<Boolean> cancelOrderById(@RequestBody CommonRequest commonRequest, HttpServletRequest request) {
if (commonRequest == null || commonRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
@ -405,6 +407,18 @@ public class OrderController {
}
boolean updateBatch = goodService.updateBatchById(goodList);
ThrowUtils.throwIf(!updateBatch, ErrorCode.SYSTEM_ERROR, "商品库存恢复失败");
// 如果使用了优惠券则退还优惠券
Long userCouponId = order.getCouponSnapshot().getId();
if (userCouponId != null) {
UserCoupon userCoupon = userCouponService.getById(userCouponId);
ThrowUtils.throwIf(userCoupon == null, ErrorCode.OPERATION_ERROR, "优惠券不存在");
// 更新优惠券状态
userCoupon.setIsUsed(0);
boolean result = userCouponService.updateById(userCoupon);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "优惠券状态更新失败");
}
return ResultUtils.success(true);
}
@ -412,4 +426,41 @@ public class OrderController {
/**
* 小程序端用户取消服务类商品订单
* @param commonRequest 订单id
* @return 是否取消成功
*/
@PostMapping ("/cancel/service/id")
@Operation(summary = "小程序端用户取消服务类商品订单", description = "参数订单id权限所有人方法名cancelOrderById")
public BaseResponse<Boolean> cancelServiceOrderById(@RequestBody CommonRequest commonRequest, HttpServletRequest request) {
if (commonRequest == null || commonRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = commonRequest.getId();
userService.getLoginUser(request);
Order order = orderService.getById(id);
ThrowUtils.throwIf(order == null, ErrorCode.OPERATION_ERROR, "订单不存在");
ThrowUtils.throwIf(!order.getOrderStatus().equals(OrderStatusConstant.PENDING_PAYMENT), ErrorCode.SYSTEM_ERROR, "订单状态错误");
order.setOrderStatus(OrderStatusConstant.TRANSACTION_CLOSED);
boolean update = orderService.updateById(order);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "订单状态更新失败");
// 如果使用了优惠券则退还优惠券
Long userCouponId = order.getCouponSnapshot().getId();
if (userCouponId != null) {
UserCoupon userCoupon = userCouponService.getById(userCouponId);
ThrowUtils.throwIf(userCoupon == null, ErrorCode.OPERATION_ERROR, "优惠券不存在");
// 更新优惠券状态
userCoupon.setIsUsed(0);
boolean result = userCouponService.updateById(userCoupon);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "优惠券状态更新失败");
}
return ResultUtils.success(true);
}
}

View File

@ -6,6 +6,8 @@ import com.cultural.heritage.constant.MqConstant;
import com.cultural.heritage.constant.OrderStatusConstant;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.entity.Order;
import com.cultural.heritage.model.entity.UserCoupon;
import com.cultural.heritage.service.good.UserCouponService;
import com.cultural.heritage.service.order.OrderService;
import com.cultural.heritage.utils.MultiDelayMessage;
import jakarta.annotation.Resource;
@ -28,6 +30,10 @@ public class OrderStatusListener {
private RabbitTemplate rabbitTemplate;
@Resource
private UserCouponService userCouponService;
@RabbitListener(bindings = @QueueBinding(
value = @Queue(MqConstant.DELAY_ORDER_QUEUE),
@ -62,6 +68,18 @@ public class OrderStatusListener {
updateWrapper.eq("id", orderId).set("orderStatus", OrderStatusConstant.TRANSACTION_CLOSED);
boolean update = orderService.update(updateWrapper);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "订单状态更新失败");
// 如果使用了优惠券则退还优惠券
Long userCouponId = order.getCouponSnapshot().getId();
if (userCouponId != null) {
UserCoupon userCoupon = userCouponService.getById(userCouponId);
ThrowUtils.throwIf(userCoupon == null, ErrorCode.OPERATION_ERROR, "优惠券不存在");
// 更新优惠券状态
userCoupon.setIsUsed(0);
boolean result = userCouponService.updateById(userCoupon);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "优惠券状态更新失败");
}
}

View File

@ -12,6 +12,13 @@ import java.math.BigDecimal;
public class CouponSnapshot implements Serializable {
/**
* 优惠券id
*/
@Schema(description = "优惠券id", example = "332")
private Long id;
/**
* 优惠券名称
*/

View File

@ -52,9 +52,9 @@ public class UserCoupon implements Serializable {
/**
* 是否删除
* 是否使用过
*/
private Integer isDelete;
private Integer isUsed;
@Serial

View File

@ -23,6 +23,12 @@ public class UserCouponVO implements Serializable {
private CouponVO couponVO;
/**
* 是否使用过
*/
private Integer isUsed;
@Serial
private static final long serialVersionUID = 1L;

View File

@ -351,6 +351,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
UserCoupon userCoupon = userCouponService.getById(userCouponId);
ThrowUtils.throwIf(userCoupon == null || userCoupon.getCouponVO().getStatus().equals("已过期"), ErrorCode.OPERATION_ERROR, "优惠券不存在或已过期");
conditionAmount = userCoupon.getCouponVO().getConditionAmount();
// 更新优惠券状态
userCoupon.setIsUsed(1);
boolean update = userCouponService.updateById(userCoupon);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "优惠券状态更新失败");
}
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
Long goodId = orderItemMainInfoAddRequest.getGoodId();
@ -439,6 +443,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
ThrowUtils.throwIf(userCoupon == null || userCoupon.getCouponVO().getStatus().equals("已过期"), ErrorCode.OPERATION_ERROR, "用户优惠券不存在或已过期");
BigDecimal conditionAmount = userCoupon.getCouponVO().getConditionAmount();
totalAmount = totalAmount.subtract(conditionAmount);
// 更新优惠券状态
userCoupon.setIsUsed(1);
boolean update = userCouponService.updateById(userCoupon);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "优惠券状态更新失败");
}
// 填充订单主要信息请求体金额
orderMainInfoAddRequest.setTotalAmount(totalAmount);
@ -509,6 +517,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
ThrowUtils.throwIf(userCoupon == null || userCoupon.getCouponVO().getStatus().equals("已过期"), ErrorCode.OPERATION_ERROR, "用户优惠券不存在或已过期");
BigDecimal conditionAmount = userCoupon.getCouponVO().getConditionAmount();
totalAmount = totalAmount.subtract(conditionAmount);
// 更新优惠券状态
userCoupon.setIsUsed(1);
boolean update = userCouponService.updateById(userCoupon);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "优惠券状态更新失败");
}
// 填充订单主要信息请求体金额
orderMainInfoAddRequest.setTotalAmount(totalAmount);