From f8a5625a21f196d8a0cff47889fff9a50b08ff91 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Wed, 18 Dec 2024 13:28:59 +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 | 97 ++++++++++++++----- .../heritage/model/vo/cart/CartOrderVO.java | 2 +- .../model/vo/cart/CartRecordQueryVO.java | 26 ----- .../service/good/CartRecordService.java | 11 ++- .../heritage/service/good/GoodService.java | 1 + .../good/impl/CartRecordServiceImpl.java | 40 +++++--- .../service/good/impl/GoodServiceImpl.java | 2 + 7 files changed, 113 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/com/cultural/heritage/model/vo/cart/CartRecordQueryVO.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 083f50d..4763cab 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java @@ -11,10 +11,9 @@ import com.cultural.heritage.model.dto.cart.CartRecordUpdateRequest; import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.model.entity.User; -import com.cultural.heritage.model.vo.cart.CartOrderVO; -import com.cultural.heritage.model.vo.cart.CartRecordQueryVO; -import com.cultural.heritage.model.vo.cart.CartRecordVO; import com.cultural.heritage.model.vo.cart.CartGoodVO; +import com.cultural.heritage.model.vo.cart.CartOrderVO; +import com.cultural.heritage.model.vo.cart.CartRecordVO; import com.cultural.heritage.service.good.CartRecordService; import com.cultural.heritage.service.good.GoodService; import com.cultural.heritage.service.user.UserService; @@ -25,13 +24,13 @@ import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.util.CollectionUtils; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; @RestController @RequestMapping("/cart") @@ -134,8 +133,6 @@ 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); @@ -143,34 +140,82 @@ public class CartRecordController { + /** - * 展示用户批量购买后的订单信息 - * @param cartRecordQueryVOList 商品id和数量的集合 + * (小程序)从购物车中找出不合理的商品项 + * @return 存在问题的购物车id列表 + */ + @PostMapping("/cart/list/error") + @Operation(summary = "(小程序)从购物车中找出不合理的商品项", description = "参数:无,权限:所有人,方法名:listInvalidCartIds") + public BaseResponse> listInvalidCartIds (HttpServletRequest request) { + User loginUser = userService.getLoginUser(request); + Long userId = loginUser.getId(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("userId", userId); + List cartRecords = cartRecordService.list(queryWrapper); + // 找出存在问题的购物车id列表 + List invalidCartIds = cartRecordService.getInvalidCartIds(cartRecords); + return ResultUtils.success(invalidCartIds); + } + + + + + + /** + * (小程序)展示购物车提交订单页的商品信息 + * @param cartRecordIds 购物车id列表 * @return 订单商品信息列表 */ @PostMapping("/cart/list") - @Operation(summary = "展示用户批量购买后的订单信息", description = "参数:商品id和数量的集合,权限:所有人,方法名:listCartRecord") - public BaseResponse> listCartRecord(@RequestBody List cartRecordQueryVOList, HttpServletRequest request) { - if (cartRecordQueryVOList == null) { + @Operation(summary = "(小程序)展示购物车提交订单页的商品信息", description = "参数:商品id和购买数量信息,权限:所有人,方法名:listCartRecord") + public BaseResponse> listCartRecord(@RequestBody List cartRecordIds, HttpServletRequest request) { + if (CollectionUtils.isEmpty(cartRecordIds)) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } userService.getLoginUser(request); - List cartOrderVOList = cartRecordQueryVOList.stream().map(cartRecordQueryVO -> { - Long goodId = cartRecordQueryVO.getGoodId(); - Integer quantity = cartRecordQueryVO.getQuantity(); - Good good = goodService.getById(goodId); - ThrowUtils.throwIf(good == null || good.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "找不到该商品或者该商品已下架"); - CartOrderVO cartOrderVO = new CartOrderVO(); - CartGoodVO goodVO = new CartGoodVO(); - BeanUtils.copyProperties(good, goodVO); - - cartOrderVO.setGoodVO(goodVO); - cartOrderVO.setQuantity(quantity); - return cartOrderVO; + List cartRecordList = cartRecordService.list(); + // 获取购物车列表信息 + List cartRecords = cartRecordList.stream().filter(cartRecord -> { + Long id = cartRecord.getId(); + return cartRecordIds.contains(id); }).toList(); + + // 判断是否存在无效的购物车id + if (cartRecords.size() < cartRecordIds.size()) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "存在无效的购物车id"); + } + + // 提取出购物车中的商品id列表 + List goodIds = cartRecords.stream().map(CartRecord::getGoodId).toList(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("id", goodIds); + List goodList = goodService.list(queryWrapper); + + // 封装goodMap(根据商品id查询商品) + Map goodMap = new HashMap<>(); + for (Good good : goodList) { + goodMap.put(good.getId(), good); + } + + // 封装提交订单页的商品信息列表 + List cartOrderVOList = new ArrayList<>(); + for (CartRecord cartRecord : cartRecords) { + Integer quantity = cartRecord.getQuantity(); + Long goodId = cartRecord.getGoodId(); + Good good = goodMap.get(goodId); + CartGoodVO cartGoodVO = new CartGoodVO(); + BeanUtils.copyProperties(good, cartGoodVO); + CartOrderVO cartOrderVO = new CartOrderVO(); + cartOrderVO.setCartGoodVO(cartGoodVO); + cartOrderVO.setQuantity(quantity); + cartOrderVOList.add(cartOrderVO); + } return ResultUtils.success(cartOrderVOList); } + + } diff --git a/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java b/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java index 0eb3500..8b13ac5 100644 --- a/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java +++ b/src/main/java/com/cultural/heritage/model/vo/cart/CartOrderVO.java @@ -12,7 +12,7 @@ public class CartOrderVO implements Serializable { /** * 商品详细信息 */ - private CartGoodVO goodVO; + private CartGoodVO cartGoodVO; /** diff --git a/src/main/java/com/cultural/heritage/model/vo/cart/CartRecordQueryVO.java b/src/main/java/com/cultural/heritage/model/vo/cart/CartRecordQueryVO.java deleted file mode 100644 index 6387078..0000000 --- a/src/main/java/com/cultural/heritage/model/vo/cart/CartRecordQueryVO.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.cultural.heritage.model.vo.cart; - -import lombok.Data; - -import java.io.Serial; -import java.io.Serializable; - -@Data -public class CartRecordQueryVO implements Serializable { - - /** - * 商品id - */ - private Long goodId; - - - /** - * 商品数量 - */ - private Integer quantity; - - - - @Serial - private static final long serialVersionUID = 1L; -} 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 53d79c7..a7ba1a1 100644 --- a/src/main/java/com/cultural/heritage/service/good/CartRecordService.java +++ b/src/main/java/com/cultural/heritage/service/good/CartRecordService.java @@ -29,7 +29,14 @@ public interface CartRecordService extends IService { /** - * 校验购物车信息的准确性 + * 校验购物车商品项的准确性 */ - void validIsConsistent(List list); + boolean validIsConsistent(CartRecord cartRecord); + + + + /** + * 找出存在问题的购物车id列表 + */ + List getInvalidCartIds(List cartRecords); } diff --git a/src/main/java/com/cultural/heritage/service/good/GoodService.java b/src/main/java/com/cultural/heritage/service/good/GoodService.java index fb4e65c..a110880 100644 --- a/src/main/java/com/cultural/heritage/service/good/GoodService.java +++ b/src/main/java/com/cultural/heritage/service/good/GoodService.java @@ -25,4 +25,5 @@ public interface GoodService extends IService { * 校验 */ void validGood(Good good, boolean update); + } 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 97e3570..c612dcc 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 @@ -18,6 +18,7 @@ import com.cultural.heritage.service.good.GoodService; import com.cultural.heritage.service.user.UserService; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @@ -26,6 +27,7 @@ import java.math.BigDecimal; import java.util.List; @Service +@Slf4j public class CartRecordServiceImpl extends ServiceImpl implements CartRecordService { @@ -130,19 +132,35 @@ 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(); + public boolean validIsConsistent(CartRecord cartRecord) { + // 校验购物车中的商品是否存在 + Long goodId = cartRecord.getGoodId(); + Good good = goodService.getById(goodId); + if (good == null || good.getIsShelves() == 0) { + log.info("商品不存在或者已下架, 购物车id为" + cartRecord.getId()); + return false; } + // 校验购物车中的商品数量 + Integer inventory = good.getInventory(); + Integer quantity = cartRecord.getQuantity(); + if (quantity > inventory) { + log.info("商品库存量不足, 购物车id为" + cartRecord.getId()); + return false; + } + return true; + } + + + + /** + * 找出存在问题的购物车id列表 + */ + @Override + public List getInvalidCartIds(List cartRecords) { + List cartRecordList = cartRecords.stream().filter(cartRecord -> !validIsConsistent(cartRecord)).toList(); + return cartRecordList.stream().map(CartRecord::getId).toList(); } } diff --git a/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java index 5e7c3c2..f8191c5 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java @@ -106,4 +106,6 @@ public class GoodServiceImpl extends ServiceImpl implements Go } + + }