查看购物车接口开发

This commit is contained in:
jieyuu 2024-08-17 11:27:24 +08:00
parent 723d16e812
commit bfa1377737
6 changed files with 110 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import net.jieyuu.request.CartItemRequest;
import net.jieyuu.service.CartService;
import net.jieyuu.service.ProductService;
import net.jieyuu.utils.JsonData;
import net.jieyuu.vo.CartVO;
import net.jieyuu.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -34,7 +35,7 @@ public class CartController {
@ApiOperation("添加到购物车")
@PostMapping("add")
@PostMapping("/add")
public JsonData addToCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
cartService.addToCart(cartItemRequest);
return JsonData.buildSuccess();
@ -42,10 +43,16 @@ public class CartController {
@ApiOperation("清空购物车")
@DeleteMapping("clear")
@DeleteMapping("/clear")
public JsonData clearMyCart() {
cartService.clear();
return JsonData.buildSuccess();
}
@ApiOperation("查找购物车")
@GetMapping("/mycart")
public JsonData findMyCart() {
CartVO cartVO = cartService.getMyCart();
return JsonData.buildSuccess(cartVO);
}
}

View File

@ -1,10 +1,12 @@
package net.jieyuu.service;
import net.jieyuu.request.CartItemRequest;
import net.jieyuu.vo.CartVO;
public interface CartService {
/**
* 添加商品到购物车
*
* @param cartItemRequest
*/
public void addToCart(CartItemRequest cartItemRequest);
@ -13,4 +15,11 @@ public interface CartService {
* 清空购物车
*/
void clear();
/**
* 查看购物车
*
* @return
*/
CartVO getMyCart();
}

View File

@ -4,6 +4,7 @@ import net.jieyuu.model.ProductDO;
import com.baomidou.mybatisplus.extension.service.IService;
import net.jieyuu.vo.ProductVO;
import java.util.List;
import java.util.Map;
/**
@ -18,6 +19,7 @@ public interface ProductService {
/**
* 分页查询商品列表
*
* @param page
* @param size
* @return
@ -26,8 +28,17 @@ public interface ProductService {
/**
* 根据id查找商品详情
*
* @param productId
* @return
*/
ProductVO findDetailById(long productId);
/**
* 根据id批量查询商品
*
* @param productIdList
* @return
*/
List<ProductVO> findProductByIdBatch(List<Long> productIdList);
}

View File

@ -8,10 +8,12 @@ import net.jieyuu.enums.BizCodeEnum;
import net.jieyuu.exception.BizException;
import net.jieyuu.interceptor.LoginInterceptor;
import net.jieyuu.model.LoginUser;
import net.jieyuu.model.ProductDO;
import net.jieyuu.request.CartItemRequest;
import net.jieyuu.service.CartService;
import net.jieyuu.service.ProductService;
import net.jieyuu.vo.CartItemVO;
import net.jieyuu.vo.CartVO;
import net.jieyuu.vo.ProductVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,6 +21,12 @@ import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@Slf4j
public class CartServiceImpl implements CartService {
@ -77,6 +85,67 @@ public class CartServiceImpl implements CartService {
redisTemplate.delete(cartKey);
}
@Override
public CartVO getMyCart() {
// 获取全部cart
List<CartItemVO> cartItemVOList = buildCartItem(false);
//拼装cartVO
CartVO cartVO = new CartVO();
cartVO.setCartItems(cartItemVOList);
return cartVO;
}
/**
* 获取最新购物项
*
* @param latestPrice
* @return
*/
private List<CartItemVO> buildCartItem(boolean latestPrice) {
BoundHashOperations<String, Object, Object> myCartOps = getMyCartOps();
List<Object> itemList = myCartOps.values();
List<CartItemVO> cartItemVOList = new ArrayList<>();
//拼接id列表查询
List<Long> productIdList = new ArrayList<>();
for (Object object : itemList) {
CartItemVO cartItemVO = JSON.parseObject((String) object, CartItemVO.class);
cartItemVOList.add(cartItemVO);
productIdList.add(cartItemVO.getProductId());
}
// 查询最新价格
// 假如用户 查看购物车 不需要最新价格
// 当下单结算时,才查看最新价格
if (latestPrice) {
setProductLatestPrice(cartItemVOList, productIdList);
}
return cartItemVOList;
}
/**
* 设置商品最新价格
*
* @param cartItemVOList
* @param productIdList
*/
private void setProductLatestPrice(List<CartItemVO> cartItemVOList, List<Long> productIdList) {
//批量查询商品最新数据
List<ProductVO> productVOList = productService.findProductByIdBatch(productIdList);
//根据id进行分组
Map<Long, ProductVO> maps = productVOList.stream().collect(Collectors.toMap(ProductVO::getId, Function.identity()));
cartItemVOList.stream().forEach(item -> {
// 拿到最新的数据
ProductVO productVO = maps.get(item.getProductId());
// 遍历更新item
item.setProductTitle(productVO.getTitle());
item.setProductImg(productVO.getCoverImg());
item.setAmount(productVO.getAmount());
});
}
/**
* 抽取购物车 通用方法
*

View File

@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -53,6 +54,13 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
return beanProcess(productDO);
}
@Override
public List<ProductVO> findProductByIdBatch(List<Long> productIdList) {
List<ProductDO> productDOList = productMapper.selectList(new QueryWrapper<ProductDO>().in("id", productIdList));
List<ProductVO> productVOList = productDOList.stream().map(obj -> beanProcess(obj)).collect(Collectors.toList());
return productVOList;
}
private ProductVO beanProcess(ProductDO obj) {
ProductVO productVO = new ProductVO();
BeanUtils.copyProperties(obj, productVO);

View File

@ -60,7 +60,7 @@ public class CartVO {
BigDecimal amount = new BigDecimal("0");
if (this.cartItems != null) {
for (CartItemVO cartItem : cartItems) {
BigDecimal itemTotalAmount = cartItem.getAmount();
BigDecimal itemTotalAmount = cartItem.getTotalAmount();
amount = amount.add(itemTotalAmount);
}
}