查看购物车接口开发
This commit is contained in:
parent
723d16e812
commit
bfa1377737
@ -8,6 +8,7 @@ import net.jieyuu.request.CartItemRequest;
|
|||||||
import net.jieyuu.service.CartService;
|
import net.jieyuu.service.CartService;
|
||||||
import net.jieyuu.service.ProductService;
|
import net.jieyuu.service.ProductService;
|
||||||
import net.jieyuu.utils.JsonData;
|
import net.jieyuu.utils.JsonData;
|
||||||
|
import net.jieyuu.vo.CartVO;
|
||||||
import net.jieyuu.vo.ProductVO;
|
import net.jieyuu.vo.ProductVO;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@ -34,7 +35,7 @@ public class CartController {
|
|||||||
|
|
||||||
|
|
||||||
@ApiOperation("添加到购物车")
|
@ApiOperation("添加到购物车")
|
||||||
@PostMapping("add")
|
@PostMapping("/add")
|
||||||
public JsonData addToCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
|
public JsonData addToCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
|
||||||
cartService.addToCart(cartItemRequest);
|
cartService.addToCart(cartItemRequest);
|
||||||
return JsonData.buildSuccess();
|
return JsonData.buildSuccess();
|
||||||
@ -42,10 +43,16 @@ public class CartController {
|
|||||||
|
|
||||||
|
|
||||||
@ApiOperation("清空购物车")
|
@ApiOperation("清空购物车")
|
||||||
@DeleteMapping("clear")
|
@DeleteMapping("/clear")
|
||||||
public JsonData clearMyCart() {
|
public JsonData clearMyCart() {
|
||||||
cartService.clear();
|
cartService.clear();
|
||||||
return JsonData.buildSuccess();
|
return JsonData.buildSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("查找购物车")
|
||||||
|
@GetMapping("/mycart")
|
||||||
|
public JsonData findMyCart() {
|
||||||
|
CartVO cartVO = cartService.getMyCart();
|
||||||
|
return JsonData.buildSuccess(cartVO);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,10 +1,12 @@
|
|||||||
package net.jieyuu.service;
|
package net.jieyuu.service;
|
||||||
|
|
||||||
import net.jieyuu.request.CartItemRequest;
|
import net.jieyuu.request.CartItemRequest;
|
||||||
|
import net.jieyuu.vo.CartVO;
|
||||||
|
|
||||||
public interface CartService {
|
public interface CartService {
|
||||||
/**
|
/**
|
||||||
* 添加商品到购物车
|
* 添加商品到购物车
|
||||||
|
*
|
||||||
* @param cartItemRequest
|
* @param cartItemRequest
|
||||||
*/
|
*/
|
||||||
public void addToCart(CartItemRequest cartItemRequest);
|
public void addToCart(CartItemRequest cartItemRequest);
|
||||||
@ -13,4 +15,11 @@ public interface CartService {
|
|||||||
* 清空购物车
|
* 清空购物车
|
||||||
*/
|
*/
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看购物车
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
CartVO getMyCart();
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import net.jieyuu.model.ProductDO;
|
|||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import net.jieyuu.vo.ProductVO;
|
import net.jieyuu.vo.ProductVO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,6 +19,7 @@ public interface ProductService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页查询商品列表
|
* 分页查询商品列表
|
||||||
|
*
|
||||||
* @param page
|
* @param page
|
||||||
* @param size
|
* @param size
|
||||||
* @return
|
* @return
|
||||||
@ -26,8 +28,17 @@ public interface ProductService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id查找商品详情
|
* 根据id查找商品详情
|
||||||
|
*
|
||||||
* @param productId
|
* @param productId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
ProductVO findDetailById(long productId);
|
ProductVO findDetailById(long productId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id批量查询商品
|
||||||
|
*
|
||||||
|
* @param productIdList
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<ProductVO> findProductByIdBatch(List<Long> productIdList);
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,12 @@ import net.jieyuu.enums.BizCodeEnum;
|
|||||||
import net.jieyuu.exception.BizException;
|
import net.jieyuu.exception.BizException;
|
||||||
import net.jieyuu.interceptor.LoginInterceptor;
|
import net.jieyuu.interceptor.LoginInterceptor;
|
||||||
import net.jieyuu.model.LoginUser;
|
import net.jieyuu.model.LoginUser;
|
||||||
|
import net.jieyuu.model.ProductDO;
|
||||||
import net.jieyuu.request.CartItemRequest;
|
import net.jieyuu.request.CartItemRequest;
|
||||||
import net.jieyuu.service.CartService;
|
import net.jieyuu.service.CartService;
|
||||||
import net.jieyuu.service.ProductService;
|
import net.jieyuu.service.ProductService;
|
||||||
import net.jieyuu.vo.CartItemVO;
|
import net.jieyuu.vo.CartItemVO;
|
||||||
|
import net.jieyuu.vo.CartVO;
|
||||||
import net.jieyuu.vo.ProductVO;
|
import net.jieyuu.vo.ProductVO;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
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
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class CartServiceImpl implements CartService {
|
public class CartServiceImpl implements CartService {
|
||||||
@ -77,6 +85,67 @@ public class CartServiceImpl implements CartService {
|
|||||||
redisTemplate.delete(cartKey);
|
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());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 抽取购物车 通用方法
|
* 抽取购物车 通用方法
|
||||||
*
|
*
|
||||||
|
@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -53,6 +54,13 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
|
|||||||
return beanProcess(productDO);
|
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) {
|
private ProductVO beanProcess(ProductDO obj) {
|
||||||
ProductVO productVO = new ProductVO();
|
ProductVO productVO = new ProductVO();
|
||||||
BeanUtils.copyProperties(obj, productVO);
|
BeanUtils.copyProperties(obj, productVO);
|
||||||
|
@ -60,7 +60,7 @@ public class CartVO {
|
|||||||
BigDecimal amount = new BigDecimal("0");
|
BigDecimal amount = new BigDecimal("0");
|
||||||
if (this.cartItems != null) {
|
if (this.cartItems != null) {
|
||||||
for (CartItemVO cartItem : cartItems) {
|
for (CartItemVO cartItem : cartItems) {
|
||||||
BigDecimal itemTotalAmount = cartItem.getAmount();
|
BigDecimal itemTotalAmount = cartItem.getTotalAmount();
|
||||||
amount = amount.add(itemTotalAmount);
|
amount = amount.add(itemTotalAmount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user