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 b4193bc..083f50d 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java @@ -30,6 +30,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.math.BigDecimal; import java.util.List; @RestController @@ -68,12 +69,12 @@ public class CartRecordController { /** - * 用户更新商品至购物车 + * 小程序端用户更新购物车中的商品数量 * @param cartRecordUpdateRequestList 购物车记录列表更新请求体 * @return 是否更新成功 */ @PostMapping("/update") - @Operation(summary = "小程序端用户更新商品至购物车", description = "参数:购物车记录更新请求体,权限:所有人,方法名:updateCartRecord") + @Operation(summary = "小程序端用户更新购物车中的商品数量", description = "参数:购物车记录更新请求体,权限:所有人,方法名:updateCartRecord") public BaseResponse updateCartRecord(@RequestBody List cartRecordUpdateRequestList, HttpServletRequest request) { if (CollectionUtils.isEmpty(cartRecordUpdateRequestList)) { throw new BusinessException(ErrorCode.PARAMS_ERROR); @@ -81,9 +82,17 @@ public class CartRecordController { User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); List cartRecordList = cartRecordUpdateRequestList.stream().map(cartRecordUpdateRequest -> { + Long id = cartRecordUpdateRequest.getId(); + CartRecord cart = cartRecordService.getById(id); + ThrowUtils.throwIf(cart == null, ErrorCode.OPERATION_ERROR, "该项商品已被移除"); CartRecord cartRecord = new CartRecord(); BeanUtils.copyProperties(cartRecordUpdateRequest, cartRecord); + Long goodId = cart.getGoodId(); + Good good = goodService.getById(goodId); + ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "购物车商品更新失败"); + cartRecord.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(cartRecord.getQuantity()))); cartRecord.setUserId(userId); + cartRecord.setGoodId(goodId); // 校验 cartRecordService.validCart(cartRecord, true); return cartRecord; @@ -125,6 +134,9 @@ public class CartRecordController { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", userId); List list = cartRecordService.list(queryWrapper); + // 校验购物车信息的准确性 + cartRecordService.validIsConsistent(list); + // 封装用户购物车中的商品信息 List cartRecordVOList = cartRecordService.transformToCartRecordVOList(list); return ResultUtils.success(cartRecordVOList); } diff --git a/src/main/java/com/cultural/heritage/controller/user/UserController.java b/src/main/java/com/cultural/heritage/controller/user/UserController.java index 16a02db..e5aefe8 100644 --- a/src/main/java/com/cultural/heritage/controller/user/UserController.java +++ b/src/main/java/com/cultural/heritage/controller/user/UserController.java @@ -118,12 +118,12 @@ public class UserController { /** - * 获取当前登录用户 + * 小程序端(Web端)用户获取登录信息 * @param request http * @return 用户登录信息 */ @GetMapping("/get/login") - @Operation(summary = "小程序端用户获取登录信息", description = "参数:无,权限:所有人, 方法名:getLoginUser") + @Operation(summary = "小程序端(Web端)用户获取登录信息", description = "参数:无,权限:所有人, 方法名:getLoginUser") public BaseResponse getLoginUser(HttpServletRequest request) { User user = userService.getLoginUser(request); return ResultUtils.success(userService.getUserVO(user)); diff --git a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java index 45a0ff4..afe8afe 100644 --- a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordAddRequest.java @@ -5,10 +5,9 @@ import lombok.Data; import java.io.Serial; import java.io.Serializable; -import java.math.BigDecimal; @Data -@Schema(description = "购物车记录添加请求体", requiredProperties = {"userId", "goodId", "quantity", "subtotal"}) +@Schema(description = "购物车记录添加请求体", requiredProperties = {"goodId", "quantity"}) public class CartRecordAddRequest implements Serializable { /** @@ -23,13 +22,6 @@ public class CartRecordAddRequest implements Serializable { @Schema(description = "商品数量(quantity > 0)", example = "3") private Integer quantity; - /** - * 小计 - */ - @Schema(description = "小计(商品单价 * 数量)", example = "60") - private BigDecimal subtotal; - - @Serial private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java index eefcb95..7281c87 100644 --- a/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/cart/CartRecordUpdateRequest.java @@ -5,10 +5,9 @@ import lombok.Data; import java.io.Serial; import java.io.Serializable; -import java.math.BigDecimal; @Data -@Schema(description = "购物车记录更新请求体", requiredProperties = {"id", "userId", "goodId", "quantity", "subtotal"}) +@Schema(description = "购物车记录更新请求体", requiredProperties = {"id", "quantity"}) public class CartRecordUpdateRequest implements Serializable { /** @@ -18,23 +17,12 @@ public class CartRecordUpdateRequest implements Serializable { private Long id; - /** - * 商品id - */ - @Schema(description = "商品id(id > 0)", example = "20") - private Long goodId; - /** * 数量 */ @Schema(description = "商品数量(quantity > 0)", example = "3") private Integer quantity; - /** - * 小计 - */ - @Schema(description = "小计(商品单价 * 数量)", example = "60") - private BigDecimal subtotal; @Serial diff --git a/src/main/java/com/cultural/heritage/service/good/CartRecordService.java b/src/main/java/com/cultural/heritage/service/good/CartRecordService.java index ba1b10c..53d79c7 100644 --- a/src/main/java/com/cultural/heritage/service/good/CartRecordService.java +++ b/src/main/java/com/cultural/heritage/service/good/CartRecordService.java @@ -26,4 +26,10 @@ public interface CartRecordService extends IService { * 校验购物车中的商品信息 */ void validCart(CartRecord cartRecord, boolean update); + + + /** + * 校验购物车信息的准确性 + */ + void validIsConsistent(List list); } 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 4006ea9..97e3570 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 @@ -48,6 +48,8 @@ public class CartRecordServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userId", userId); queryWrapper.eq("goodId", goodId); @@ -55,7 +57,7 @@ public class CartRecordServiceImpl extends ServiceImpl list) { + for (CartRecord cartRecord : list) { + // 校验购物车中的商品是否存在 + Long goodId = cartRecord.getGoodId(); + Good good = goodService.getById(goodId); + ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "商品不存在或者已下架"); + + // 校验购物车中的商品数量 + Integer inventory = good.getInventory(); + Integer quantity = cartRecord.getQuantity(); + } + } }