更新了商品类别

This commit is contained in:
chen-xin-zhi 2024-12-18 13:28:59 +08:00
parent 79f8972538
commit f8a5625a21
7 changed files with 113 additions and 66 deletions

View File

@ -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<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", userId);
List<CartRecord> list = cartRecordService.list(queryWrapper);
// 校验购物车信息的准确性
cartRecordService.validIsConsistent(list);
// 封装用户购物车中的商品信息
List<CartRecordVO> 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<List<Long>> listInvalidCartIds (HttpServletRequest request) {
User loginUser = userService.getLoginUser(request);
Long userId = loginUser.getId();
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", userId);
List<CartRecord> cartRecords = cartRecordService.list(queryWrapper);
// 找出存在问题的购物车id列表
List<Long> invalidCartIds = cartRecordService.getInvalidCartIds(cartRecords);
return ResultUtils.success(invalidCartIds);
}
/**
* (小程序)展示购物车提交订单页的商品信息
* @param cartRecordIds 购物车id列表
* @return 订单商品信息列表
*/
@PostMapping("/cart/list")
@Operation(summary = "展示用户批量购买后的订单信息", description = "参数:商品id和数量的集合权限所有人方法名listCartRecord")
public BaseResponse<List<CartOrderVO>> listCartRecord(@RequestBody List<CartRecordQueryVO> cartRecordQueryVOList, HttpServletRequest request) {
if (cartRecordQueryVOList == null) {
@Operation(summary = "(小程序)展示购物车提交订单页的商品信息", description = "参数:商品id和购买数量信息权限所有人方法名listCartRecord")
public BaseResponse<List<CartOrderVO>> listCartRecord(@RequestBody List<Long> cartRecordIds, HttpServletRequest request) {
if (CollectionUtils.isEmpty(cartRecordIds)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
userService.getLoginUser(request);
List<CartOrderVO> 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<CartRecord> cartRecordList = cartRecordService.list();
// 获取购物车列表信息
List<CartRecord> 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<Long> goodIds = cartRecords.stream().map(CartRecord::getGoodId).toList();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", goodIds);
List<Good> goodList = goodService.list(queryWrapper);
// 封装goodMap(根据商品id查询商品)
Map<Long, Good> goodMap = new HashMap<>();
for (Good good : goodList) {
goodMap.put(good.getId(), good);
}
// 封装提交订单页的商品信息列表
List<CartOrderVO> 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);
}
}

View File

@ -12,7 +12,7 @@ public class CartOrderVO implements Serializable {
/**
* 商品详细信息
*/
private CartGoodVO goodVO;
private CartGoodVO cartGoodVO;
/**

View File

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

View File

@ -29,7 +29,14 @@ public interface CartRecordService extends IService<CartRecord> {
/**
* 校验购物车信息的准确性
* 校验购物车商品项的准确性
*/
void validIsConsistent(List<CartRecord> list);
boolean validIsConsistent(CartRecord cartRecord);
/**
* 找出存在问题的购物车id列表
*/
List<Long> getInvalidCartIds(List<CartRecord> cartRecords);
}

View File

@ -25,4 +25,5 @@ public interface GoodService extends IService<Good> {
* 校验
*/
void validGood(Good good, boolean update);
}

View File

@ -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<CartRecordMapper, CartRecord> implements CartRecordService {
@ -130,19 +132,35 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
/**
* 校验购物车信息的准确性
* 校验购物车商品项的准确性
*/
@Override
public void validIsConsistent(List<CartRecord> 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<Long> getInvalidCartIds(List<CartRecord> cartRecords) {
List<CartRecord> cartRecordList = cartRecords.stream().filter(cartRecord -> !validIsConsistent(cartRecord)).toList();
return cartRecordList.stream().map(CartRecord::getId).toList();
}
}

View File

@ -106,4 +106,6 @@ public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements Go
}
}