From f2032c20736a4084c0b3b44f45d19b1054fba021 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Fri, 20 Dec 2024 02:52:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E5=95=86=E5=93=81?= =?UTF-8?q?=E7=B1=BB=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/good/CartRecordController.java | 22 +++++++++ .../controller/order/OrderController.java | 6 ++- .../dto/order/BuySingleGoodVerifyRequest.java | 29 +++++++++++ .../good/impl/CartRecordServiceImpl.java | 4 +- .../heritage/service/order/OrderService.java | 6 +++ .../service/order/impl/OrderServiceImpl.java | 21 ++++++++ .../service/wxpay/impl/WeChatServiceImpl.java | 48 +++++++++++++++++++ 7 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/model/dto/order/BuySingleGoodVerifyRequest.java diff --git a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java index e4de19e..a6384a1 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java @@ -8,6 +8,7 @@ import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.model.dto.cart.CartRecordAddRequest; import com.cultural.heritage.model.dto.cart.CartRecordUpdateRequest; +import com.cultural.heritage.model.dto.order.BuySingleGoodVerifyRequest; import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.model.entity.User; @@ -220,4 +221,25 @@ public class CartRecordController { + + /** + * (小程序端)用户点击立即购买或者支付订单触发的校验 + * @param buySingleGoodVerifyRequest 商品id和购买数量 + * @return 是否通过校验 + */ + @PostMapping("/verify/good") + @Operation(summary = "(小程序端)用户点击立即购买或者支付订单触发的校验", description = "参数:商品id和购买数量,权限:所有人,方法名:verifyPurchaseInfo") + public BaseResponse verifyPurchaseInfo(@RequestBody BuySingleGoodVerifyRequest buySingleGoodVerifyRequest, HttpServletRequest request) { + if (buySingleGoodVerifyRequest == null || buySingleGoodVerifyRequest.getGoodId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + userService.getLoginUser(request); + Long goodId = buySingleGoodVerifyRequest.getGoodId(); + Integer quantity = buySingleGoodVerifyRequest.getQuantity(); + Good good = goodService.getById(goodId); + ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.SYSTEM_ERROR, "商品已下架或不存在"); + ThrowUtils.throwIf(quantity > good.getInventory(), ErrorCode.SYSTEM_ERROR, "商品库存不足"); + return ResultUtils.success(true); + } + } diff --git a/src/main/java/com/cultural/heritage/controller/order/OrderController.java b/src/main/java/com/cultural/heritage/controller/order/OrderController.java index 20a1719..9475c39 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -132,7 +132,7 @@ public class OrderController { } // 填充到订单主要信息请求体 orderMainInfoAddRequest.setOrderItemMainInfoAddRequestList(orderItemMainInfoAddRequestList); - + // 创建通用订单(常规类和服务类商品) Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, true, cartIds); return ResultUtils.success(orderId); } @@ -140,7 +140,7 @@ public class OrderController { /** - * 用户创建订单 + * 小程序端创建用户订单 * @param orderMainInfoAddRequest 订单创建请求体 * @return 是否创建成功 */ @@ -154,6 +154,8 @@ public class OrderController { // 校验用户当前是否处于登录态 User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); + // 校验单独购买的商品数据是否准确 + orderService.validSingleGoodOrder(orderMainInfoAddRequest); // 创建通用订单(常规类和服务类商品) Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, false, null); return ResultUtils.success(orderId); diff --git a/src/main/java/com/cultural/heritage/model/dto/order/BuySingleGoodVerifyRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/BuySingleGoodVerifyRequest.java new file mode 100644 index 0000000..bbbadb7 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/order/BuySingleGoodVerifyRequest.java @@ -0,0 +1,29 @@ +package com.cultural.heritage.model.dto.order; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +@Schema(description = "购买单个商品校验请求体", requiredProperties = {"goodId", "quantity"}) +public class BuySingleGoodVerifyRequest implements Serializable { + + /** + * 商品id + */ + @Schema(description = "商品id", example = "2") + private Long goodId; + + + /** + * 商品数量 + */ + @Schema(description = "商品数量", example = "10") + private Integer quantity; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java index f9feede..334c6bb 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/CartRecordServiceImpl.java @@ -179,7 +179,9 @@ public class CartRecordServiceImpl extends ServiceImpl cartOrderItemAddRequestList) { - + if (cartOrderItemAddRequestList.size() == 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "购物车中没有商品"); + } // 封装map集合(键:购物车id, 值:购买数量) Map map = new HashMap<>(); for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) { diff --git a/src/main/java/com/cultural/heritage/service/order/OrderService.java b/src/main/java/com/cultural/heritage/service/order/OrderService.java index 921d4c5..6cd7a50 100644 --- a/src/main/java/com/cultural/heritage/service/order/OrderService.java +++ b/src/main/java/com/cultural/heritage/service/order/OrderService.java @@ -35,4 +35,10 @@ public interface OrderService extends IService { * 创建通用订单(常规类和服务类商品) */ Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List cartIds); + + + /** + * 校验单独购买的商品数据是否准确 + */ + void validSingleGoodOrder(OrderMainInfoAddRequest orderMainInfoAddRequest); } diff --git a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java index f7bad09..2953634 100644 --- a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java @@ -266,6 +266,27 @@ public class OrderServiceImpl extends ServiceImpl implements } + + /** + * 校验单独购买的商品数据是否准确 + */ + @Override + public void validSingleGoodOrder(OrderMainInfoAddRequest orderMainInfoAddRequest) { + List orderItemMainInfoAddRequestList = orderMainInfoAddRequest.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, "商品库存不足"); + } + + + + /** * 根据id获取订单(地址,联系人,优惠券,商品)信息 */ diff --git a/src/main/java/com/cultural/heritage/service/wxpay/impl/WeChatServiceImpl.java b/src/main/java/com/cultural/heritage/service/wxpay/impl/WeChatServiceImpl.java index efa1ab3..e0c5bbb 100644 --- a/src/main/java/com/cultural/heritage/service/wxpay/impl/WeChatServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/wxpay/impl/WeChatServiceImpl.java @@ -11,7 +11,12 @@ import com.cultural.heritage.config.WxPayConfig; import com.cultural.heritage.constant.OrderStatusConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; +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.service.good.GoodService; +import com.cultural.heritage.service.order.OrderItemService; import com.cultural.heritage.service.order.OrderService; import com.cultural.heritage.service.wxpay.WeChatService; import com.wechat.pay.java.core.notification.NotificationParser; @@ -35,7 +40,9 @@ import org.springframework.transaction.annotation.Transactional; import java.io.BufferedReader; import java.math.BigDecimal; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author 玄德 @@ -53,6 +60,12 @@ public class WeChatServiceImpl implements WeChatService { @Resource private OrderService orderService; + @Resource + private GoodService goodService; + + @Resource + private OrderItemService orderItemService; + /** * 请求参数 @@ -171,6 +184,41 @@ public class WeChatServiceImpl implements WeChatService { order.setOrderStatus(OrderStatusConstant.PAYMENT_REFUNDED); boolean update = orderService.updateById(order); ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "订单状态修改失败"); + + // 恢复商品库存 + QueryWrapper orderItemsQueryWrapper = new QueryWrapper<>(); + orderItemsQueryWrapper.eq("orderId", orderIdByString); + List orderItemsList = orderItemService.list(orderItemsQueryWrapper); + // 获取商品id列表 + List goodIds = orderItemsList.stream().map(orderItems -> { + GoodSnapshot goodSnapshot = orderItems.getGoodSnapshot(); + return goodSnapshot.getId(); + }).toList(); + QueryWrapper goodQueryWrapper = new QueryWrapper<>(); + goodQueryWrapper.in("id", goodIds); + // 获取商品列表 + List goodList = goodService.list(goodQueryWrapper); + // 封装为map集合(键:商品id, 值:库存量),用来存储购买的商品id对应的购买数量 + Map map = new HashMap<>(); + for (OrderItems orderItems : orderItemsList) { + GoodSnapshot goodSnapshot = orderItems.getGoodSnapshot(); + Long goodId = goodSnapshot.getId(); + Integer quantity = orderItems.getQuantity(); + map.put(goodId, quantity); + } + + // 批量更新商品库存 + for (Good good : goodList) { + Long goodId = good.getId(); + // 已经下单购买的数量 + Integer quantity = map.get(goodId); + // 原有的库存 + Integer inventory = good.getInventory(); + good.setInventory(quantity + inventory); + } + boolean updateBatch = goodService.updateBatchById(goodList); + ThrowUtils.throwIf(!updateBatch, ErrorCode.SYSTEM_ERROR, "商品库存恢复失败"); + System.out.println("---------------------------微信退款回调(结束)-------------------------------"); return true; }