更新了商品类别

This commit is contained in:
chen-xin-zhi 2024-12-20 02:52:19 +08:00
parent c5565b97ac
commit f2032c2073
7 changed files with 133 additions and 3 deletions

View File

@ -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<Boolean> 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);
}
}

View File

@ -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);

View File

@ -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;
}

View File

@ -179,7 +179,9 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
*/
@Override
public boolean validIsConsistent(List<CartOrderItemAddRequest> cartOrderItemAddRequestList) {
if (cartOrderItemAddRequestList.size() == 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "购物车中没有商品");
}
// 封装map集合购物车id, 购买数量
Map<Long, Integer> map = new HashMap<>();
for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) {

View File

@ -35,4 +35,10 @@ public interface OrderService extends IService<Order> {
* 创建通用订单(常规类和服务类商品)
*/
Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId, boolean isCartOrder, List<Long> cartIds);
/**
* 校验单独购买的商品数据是否准确
*/
void validSingleGoodOrder(OrderMainInfoAddRequest orderMainInfoAddRequest);
}

View File

@ -266,6 +266,27 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
/**
* 校验单独购买的商品数据是否准确
*/
@Override
public void validSingleGoodOrder(OrderMainInfoAddRequest orderMainInfoAddRequest) {
List<OrderItemMainInfoAddRequest> 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获取订单(地址联系人优惠券商品)信息
*/

View File

@ -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<OrderItems> orderItemsQueryWrapper = new QueryWrapper<>();
orderItemsQueryWrapper.eq("orderId", orderIdByString);
List<OrderItems> orderItemsList = orderItemService.list(orderItemsQueryWrapper);
// 获取商品id列表
List<Long> goodIds = orderItemsList.stream().map(orderItems -> {
GoodSnapshot goodSnapshot = orderItems.getGoodSnapshot();
return goodSnapshot.getId();
}).toList();
QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
goodQueryWrapper.in("id", goodIds);
// 获取商品列表
List<Good> goodList = goodService.list(goodQueryWrapper);
// 封装为map集合商品id, 库存量,用来存储购买的商品id对应的购买数量
Map<Long, Integer> 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;
}