更新了优惠券模块

This commit is contained in:
chen-xin-zhi 2025-02-08 13:01:22 +08:00
parent 50a5b81f72
commit 2ba3a71edd
5 changed files with 183 additions and 97 deletions

View File

@ -17,13 +17,14 @@ import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest;
import com.cultural.heritage.model.dto.order.OrderQueryRequest;
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.OrderItemMainInfoAddRequest;
import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import com.cultural.heritage.model.entity.*;
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.vo.order.OrderVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.CouponService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
@ -77,9 +78,6 @@ public class OrderController {
private GoodService goodService;
@Resource
private CouponService couponService;
@ -104,63 +102,11 @@ public class OrderController {
ThrowUtils.throwIf(!isAccurate, ErrorCode.OPERATION_ERROR, "当前购物车中的某些商品已下架或者库存不足,无法生成订单");
// 封装成订单主要信息请求体
OrderMainInfoAddRequest orderMainInfoAddRequest = new OrderMainInfoAddRequest();
BeanUtils.copyProperties(cartOrderAddRequest, orderMainInfoAddRequest);
OrderMainInfoAddRequest orderMainInfoAddRequest = orderService.packagedOrderMainInfoAddRequest(cartOrderAddRequest);
// 获取购物车id列表
List<Long> cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList();
// 根据id列表获取购物车商品信息
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", cartIds);
List<CartRecord> cartRecordList = cartRecordService.list(queryWrapper);
List<Long> goodIds = cartRecordList.stream().map(CartRecord::getGoodId).toList();
QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
goodQueryWrapper.in("id", goodIds);
List<Good> goodList = goodService.list(goodQueryWrapper);
// 封装map集合商品id, 商品信息
Map<Long, Good> goodMap = new HashMap<>();
for (Good good : goodList) {
goodMap.put(good.getId(), good);
}
// 封装成一个map集合购物车id, 购物车商品数量
Map<Long, Integer> map = new HashMap<>();
for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) {
Long cartRecordId = cartOrderItemAddRequest.getCartRecordId();
Integer quantity = cartOrderItemAddRequest.getQuantity();
map.put(cartRecordId, quantity);
}
// 计算订单总金额
BigDecimal totalAmount = BigDecimal.ZERO;
for (CartRecord cartRecord : cartRecordList) {
Long goodId = cartRecord.getGoodId();
Good good = goodMap.get(goodId);
Long cartRecordId = cartRecord.getId();
Integer quantity = map.get(cartRecordId);
totalAmount = totalAmount.add(good.getPrice().multiply(BigDecimal.valueOf(quantity)));
}
Long couponId = orderMainInfoAddRequest.getCouponId();
Coupon coupon = couponService.getById(couponId);
ThrowUtils.throwIf(coupon == null, ErrorCode.OPERATION_ERROR, "优惠券不存在");
BigDecimal conditionAmount = coupon.getConditionAmount();
totalAmount = totalAmount.subtract(conditionAmount);
orderMainInfoAddRequest.setTotalAmount(totalAmount);
// 封装成订单明细主要信息请求体列表
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = new ArrayList<>();
OrderItemMainInfoAddRequest orderItemMainInfoAddRequest;
for (CartRecord cartRecord : cartRecordList) {
Long goodId = cartRecord.getGoodId();
Long id = cartRecord.getId();
Integer quantity = map.get(id);
orderItemMainInfoAddRequest = new OrderItemMainInfoAddRequest(goodId, quantity, null, null);
orderItemMainInfoAddRequestList.add(orderItemMainInfoAddRequest);
}
// 填充到订单主要信息请求体
orderMainInfoAddRequest.setOrderItemMainInfoAddRequestList(orderItemMainInfoAddRequestList);
// 创建通用订单(常规类和服务类商品)
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, true, cartIds);
List<Long> cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList();
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, true, cartIds, false);
// 向消息队列中发送订单创建的消息
orderService.sendCreateOrderMessage(orderId);
@ -171,8 +117,8 @@ public class OrderController {
/**
* 小程序端用户创建常规类商品订单
* @param generalGoodSingleBuyAddRequest 常规类商品单独购买创建请求体
* 小程序端用户创建常规类(服务类商品订单
* @param generalGoodSingleBuyAddRequest 常规类商品单独购买创建请求体
* @return 是否创建成功
*/
@PostMapping("/add")
@ -186,8 +132,11 @@ public class OrderController {
User loginUser = userService.getLoginUser(request);
Long userId = loginUser.getId();
// 判断是否是常规类
boolean isGeneral = orderService.isGeneralGood(generalGoodSingleBuyAddRequest.getOrderType());
// 校验单独购买的商品数据是否准确
orderService.validSingleGoodOrder(generalGoodSingleBuyAddRequest);
orderService.validSingleGoodOrder(generalGoodSingleBuyAddRequest, isGeneral);
// 封装成订单主要信息请求体
OrderMainInfoAddRequest orderMainInfoAddRequest = new OrderMainInfoAddRequest();
@ -197,7 +146,7 @@ public class OrderController {
BigDecimal totalAmount = orderService.calculateTotalAmount(orderMainInfoAddRequest);
orderMainInfoAddRequest.setTotalAmount(totalAmount);
// 创建通用订单(常规类和服务类商品)
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, false, null);
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, false, null, isGeneral);
// 向消息队列中发送订单创建的消息
orderService.sendCreateOrderMessage(orderId);

View File

@ -62,6 +62,7 @@ public class OrderMainInfoAddRequest implements Serializable {
@Schema(description = "订单备注", example = "希望能完整体验非遗文化,保持产品原貌,妥善包装")
private String note;
/**
* 订单主要明细信息
*/

View File

@ -18,6 +18,14 @@ public class CouponSnapshot implements Serializable {
@Schema(description = "优惠券名称", example = "满200减50")
private String name;
/**
* 标准金额
*/
@Schema(description = "标准金额", example = "200")
private BigDecimal standardAmount;
/**
* 满减金额
*/

View File

@ -2,6 +2,7 @@ package com.cultural.heritage.service.order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.dto.cart.CartOrderAddRequest;
import com.cultural.heritage.model.dto.order.OrderAddRequest;
import com.cultural.heritage.model.dto.order.OrderQueryRequest;
import com.cultural.heritage.model.dto.order.capital.GeneralGoodSingleBuyAddRequest;
@ -36,13 +37,13 @@ public interface OrderService extends IService<Order> {
/**
* 创建通用订单(常规类和服务类商品)
*/
Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List<Long> cartIds);
Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List<Long> cartIds, boolean isGeneral);
/**
* 校验单独购买的商品数据是否准确
*/
void validSingleGoodOrder(GeneralGoodSingleBuyAddRequest generalGoodSingleBuyAddRequest);
void validSingleGoodOrder(GeneralGoodSingleBuyAddRequest generalGoodSingleBuyAddRequest, boolean isGeneral);
@ -57,4 +58,16 @@ public interface OrderService extends IService<Order> {
*/
void sendCreateOrderMessage(Long orderId);
/**
* 判断是否是常规类商品
*/
boolean isGeneralGood(String orderType);
/**
* 购物车封装订单主要信息请求体
*/
OrderMainInfoAddRequest packagedOrderMainInfoAddRequest(CartOrderAddRequest cartOrderAddRequest);
}

View File

@ -1,5 +1,6 @@
package com.cultural.heritage.service.order.impl;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -10,6 +11,8 @@ import com.cultural.heritage.constant.OrderStatusConstant;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.mapper.*;
import com.cultural.heritage.model.dto.cart.CartOrderAddRequest;
import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest;
import com.cultural.heritage.model.dto.order.OrderAddRequest;
import com.cultural.heritage.model.dto.order.OrderItemAddRequest;
import com.cultural.heritage.model.dto.order.OrderQueryRequest;
@ -20,14 +23,13 @@ import com.cultural.heritage.model.dto.snapshot.AddressSnapshot;
import com.cultural.heritage.model.dto.snapshot.ContactsSnapshot;
import com.cultural.heritage.model.dto.snapshot.CouponSnapshot;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import com.cultural.heritage.model.entity.Coupon;
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.*;
import com.cultural.heritage.model.enums.GoodTypeEnum;
import com.cultural.heritage.model.vo.coupon.CouponVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.CouponService;
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.utils.MultiDelayMessage;
@ -41,10 +43,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
@ -86,6 +85,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
private CouponService couponService;
@Resource
private UserCouponService userCouponService;
/**
* 获取查询条件
*/
@ -140,7 +143,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
ThrowUtils.throwIf(orderTypeEnum == null, ErrorCode.PARAMS_ERROR);
AddressSnapshot addressSnapshot = null;
ContactsSnapshot contactsSnapshot = null;
CouponSnapshot couponSnapshot = null;
CouponSnapshot couponSnapshot = new CouponSnapshot();
// 获取订单地址信息
if (orderTypeEnum.equals(GoodTypeEnum.PRODUCT)) {
@ -152,7 +155,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
// 获取订单优惠券信息
if (couponId != null) {
couponSnapshot = getSnapshot(couponId, couponMapper, CouponSnapshot.class);
UserCoupon userCoupon = userCouponService.getById(couponId);
ThrowUtils.throwIf(userCoupon == null || userCoupon.getCouponVO().getStatus().equals("已过期"), ErrorCode.OPERATION_ERROR, "用户优惠券不存在或者已过期");
CouponVO couponVO = userCoupon.getCouponVO();
BeanUtils.copyProperties(couponVO, couponSnapshot);
}
// 封装订单明细信息
List<OrderItemAddRequest> orderItemAddRequestList = orderItemMainInfoAddRequestList.stream().map(orderItemMainInfoAddRequest -> {
@ -217,7 +223,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
* 创建通用订单(常规类和服务类商品)
*/
@Override
public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List<Long> cartIds) {
public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List<Long> cartIds, boolean isGeneral) {
// 封装订单详细信息请求体
OrderAddRequest orderAddRequest = this.initOrderMainInfo(orderMainInfoAddRequest);
if (orderAddRequest == null) {
@ -237,7 +243,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
// 校验订单
this.validOrder(order);
boolean result = this.save(order);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "订单生成失败");
// 创建订单明细
Long id = order.getId();
List<OrderItemAddRequest> orderItemList = orderAddRequest.getOrderItemList();
@ -250,8 +256,27 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
boolean save = orderItemService.saveBatch(newOrderItemsList);
ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
if (isGeneral) {
// 扣减商品库存
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList();
this.deductionGeneralGoodInventory(orderItemMainInfoAddRequestList);
}
// 清空购物车
if (isCartOrder) {
boolean remove = cartRecordService.removeBatchByIds(cartIds);
ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "清空购物车失败");
}
return id;
}
/**
* 扣减常规类商品库存
*/
private void deductionGeneralGoodInventory(List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList) {
// 封装map集合商品id, 购买数量
Map<Long, Integer> map = new HashMap<>();
for (OrderItemMainInfoAddRequest orderItemMainInfoAddRequest : orderItemMainInfoAddRequestList) {
@ -270,13 +295,6 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
boolean update = goodService.updateBatchById(goodList);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "商品库存更新失败");
// 清空购物车
if (isCartOrder) {
boolean remove = cartRecordService.removeBatchByIds(cartIds);
ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "清空购物车失败");
}
return id;
}
@ -285,18 +303,34 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
* 校验单独购买的商品数据是否准确
*/
@Override
public void validSingleGoodOrder(GeneralGoodSingleBuyAddRequest generalGoodSingleBuyAddRequest) {
public void validSingleGoodOrder(GeneralGoodSingleBuyAddRequest generalGoodSingleBuyAddRequest, boolean isGeneral) {
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = generalGoodSingleBuyAddRequest.getOrderItemMainInfoAddRequestList();
if (orderItemMainInfoAddRequestList.size() != 1) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "购买了多种商品或未购买商品");
}
OrderItemMainInfoAddRequest orderItemMainInfoAddRequest = orderItemMainInfoAddRequestList.get(0);
Long goodId = orderItemMainInfoAddRequest.getGoodId();
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
Good good = goodService.getById(goodId);
ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.SYSTEM_ERROR, "商品已下架或者不存在");
ThrowUtils.throwIf(quantity > good.getInventory(), ErrorCode.SYSTEM_ERROR, "商品库存不足");
ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "商品已下架或者不存在");
if (isGeneral) {
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
ThrowUtils.throwIf(quantity > good.getInventory(), ErrorCode.OPERATION_ERROR, "商品库存不足");
} else {
String reservationDate = orderItemMainInfoAddRequest.getReservationDate();
String startTime = orderItemMainInfoAddRequest.getTimeSlot().split("-")[0];
String advanceDateTimeStr = reservationDate + " " + startTime + ":00";
String currentDateTimeStr = DateUtil.now();
Date advanceDateTime = DateUtil.parse(advanceDateTimeStr, "yyyy-MM-dd HH:mm:ss");
Date currentDateTime = DateUtil.parse(currentDateTimeStr, "yyyy-MM-dd HH:mm:ss");
int result = DateUtil.compare(advanceDateTime, currentDateTime);
ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, "当前时间段已过期");
}
}
@ -307,10 +341,14 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
public BigDecimal calculateTotalAmount(OrderMainInfoAddRequest orderMainInfoAddRequest) {
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList();
OrderItemMainInfoAddRequest orderItemMainInfoAddRequest = orderItemMainInfoAddRequestList.get(0);
Long couponId = orderMainInfoAddRequest.getCouponId();
Coupon coupon = couponService.getById(couponId);
ThrowUtils.throwIf(coupon == null, ErrorCode.OPERATION_ERROR, "优惠券不存在");
BigDecimal conditionAmount = coupon.getConditionAmount();
// 获取优惠券满减金额
Long userCouponId = orderMainInfoAddRequest.getCouponId();
BigDecimal conditionAmount = new BigDecimal("0.00");
if (userCouponId != null) {
UserCoupon userCoupon = userCouponService.getById(userCouponId);
ThrowUtils.throwIf(userCoupon == null || userCoupon.getCouponVO().getStatus().equals("已过期"), ErrorCode.OPERATION_ERROR, "优惠券不存在或已过期");
conditionAmount = userCoupon.getCouponVO().getConditionAmount();
}
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
Long goodId = orderItemMainInfoAddRequest.getGoodId();
Good good = goodService.getById(goodId);
@ -338,10 +376,87 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
/**
* 判断是否是常规类商品
*/
@Override
public boolean isGeneralGood(String orderType) {
GoodTypeEnum orderTypeEnum = GoodTypeEnum.getEnumByValue(orderType);
return GoodTypeEnum.PRODUCT.equals(orderTypeEnum);
}
/**
* 根据id获取订单(地址联系人优惠券商品)信息
* 购物车封装订单主要信息请求体
*/
@Override
public OrderMainInfoAddRequest packagedOrderMainInfoAddRequest(CartOrderAddRequest cartOrderAddRequest) {
// 封装成订单主要信息请求体
OrderMainInfoAddRequest orderMainInfoAddRequest = new OrderMainInfoAddRequest();
BeanUtils.copyProperties(cartOrderAddRequest, orderMainInfoAddRequest);
// 获取购物车id列表
List<CartOrderItemAddRequest> cartOrderItemAddRequestList = cartOrderAddRequest.getCartOrderItemAddRequestList();
List<Long> cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList();
// 根据id列表获取购物车商品信息
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", cartIds);
List<CartRecord> cartRecordList = cartRecordService.list(queryWrapper);
List<Long> goodIds = cartRecordList.stream().map(CartRecord::getGoodId).toList();
QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
goodQueryWrapper.in("id", goodIds);
List<Good> goodList = goodService.list(goodQueryWrapper);
// 封装map集合商品id, 商品信息
Map<Long, Good> goodMap = new HashMap<>();
for (Good good : goodList) {
goodMap.put(good.getId(), good);
}
// 封装成一个map集合购物车id, 购物车商品数量
Map<Long, Integer> map = new HashMap<>();
for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) {
Long cartRecordId = cartOrderItemAddRequest.getCartRecordId();
Integer quantity = cartOrderItemAddRequest.getQuantity();
map.put(cartRecordId, quantity);
}
// 计算订单总金额
BigDecimal totalAmount = BigDecimal.ZERO;
for (CartRecord cartRecord : cartRecordList) {
Long goodId = cartRecord.getGoodId();
Good good = goodMap.get(goodId);
Long cartRecordId = cartRecord.getId();
Integer quantity = map.get(cartRecordId);
totalAmount = totalAmount.add(good.getPrice().multiply(BigDecimal.valueOf(quantity)));
}
// 处理优惠券满减金额
Long couponId = orderMainInfoAddRequest.getCouponId();
if (couponId != null) {
UserCoupon userCoupon = userCouponService.getById(couponId);
ThrowUtils.throwIf(userCoupon == null || userCoupon.getCouponVO().getStatus().equals("已过期"), ErrorCode.OPERATION_ERROR, "用户优惠券不存在或已过期");
BigDecimal conditionAmount = userCoupon.getCouponVO().getConditionAmount();
totalAmount = totalAmount.subtract(conditionAmount);
}
// 填充订单主要信息请求体金额
orderMainInfoAddRequest.setTotalAmount(totalAmount);
// 封装成订单明细主要信息请求体列表
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = new ArrayList<>();
OrderItemMainInfoAddRequest orderItemMainInfoAddRequest;
for (CartRecord cartRecord : cartRecordList) {
Long goodId = cartRecord.getGoodId();
Long id = cartRecord.getId();
Integer quantity = map.get(id);
orderItemMainInfoAddRequest = new OrderItemMainInfoAddRequest(goodId, quantity, null, null);
orderItemMainInfoAddRequestList.add(orderItemMainInfoAddRequest);
}
// 填充到订单主要信息请求体
orderMainInfoAddRequest.setOrderItemMainInfoAddRequestList(orderItemMainInfoAddRequestList);
return orderMainInfoAddRequest;
}
/**
* 根据id获取订单(地址联系人商品)信息
*/
private static <T, S> S getSnapshot(Long id, BaseMapper<T> mapper, Class<S> snapshotClass) {
// 创建查询条件