feat(新增): 查询购物车列表
This commit is contained in:
parent
5b6a1a910c
commit
d92705d68b
|
@ -23,5 +23,10 @@
|
||||||
<artifactId>service-product-client</artifactId>
|
<artifactId>service-product-client</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.atguigu</groupId>
|
||||||
|
<artifactId>service-activity-client</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package com.atguigu.ssyx.cart.controller;
|
package com.atguigu.ssyx.cart.controller;
|
||||||
|
|
||||||
import com.atguigu.ssyx.cart.service.CartInfoService;
|
import com.atguigu.ssyx.cart.service.CartInfoService;
|
||||||
|
import com.atguigu.ssyx.client.ActivityFeignClient;
|
||||||
import com.atguigu.ssyx.common.context.BaseContext;
|
import com.atguigu.ssyx.common.context.BaseContext;
|
||||||
import com.atguigu.ssyx.common.result.Result;
|
import com.atguigu.ssyx.common.result.Result;
|
||||||
import com.atguigu.ssyx.model.order.CartInfo;
|
import com.atguigu.ssyx.model.order.CartInfo;
|
||||||
|
import com.atguigu.ssyx.vo.order.OrderConfirmVo;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -17,6 +19,8 @@ import java.util.List;
|
||||||
public class CartApiController {
|
public class CartApiController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private CartInfoService cartInfoService;
|
private CartInfoService cartInfoService;
|
||||||
|
@Autowired
|
||||||
|
private ActivityFeignClient activityFeignClient;
|
||||||
|
|
||||||
@ApiOperation(value = "添加购物车")
|
@ApiOperation(value = "添加购物车")
|
||||||
@GetMapping("addToCart/{skuId}/{skuNum}")
|
@GetMapping("addToCart/{skuId}/{skuNum}")
|
||||||
|
@ -49,4 +53,22 @@ public class CartApiController {
|
||||||
cartInfoService.batchDeleteCart(userId, skuIdList);
|
cartInfoService.batchDeleteCart(userId, skuIdList);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "查询购物车列表")
|
||||||
|
@GetMapping("cartList")
|
||||||
|
public Result<List<CartInfo>> cartList() {
|
||||||
|
Long userId = BaseContext.getUserId();
|
||||||
|
List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);
|
||||||
|
return Result.success(cartInfoList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "查询带优惠卷的购物车")
|
||||||
|
@GetMapping("activityCartList")
|
||||||
|
public Result<OrderConfirmVo> activityCartList() {
|
||||||
|
Long userId = BaseContext.getUserId();
|
||||||
|
List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);
|
||||||
|
OrderConfirmVo orderTradeVo = activityFeignClient.findCartActivityAndCoupon(cartInfoList, userId);
|
||||||
|
return Result.success(orderTradeVo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.atguigu.ssyx.cart.service;
|
package com.atguigu.ssyx.cart.service;
|
||||||
|
|
||||||
|
import com.atguigu.ssyx.model.order.CartInfo;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface CartInfoService {
|
public interface CartInfoService {
|
||||||
|
@ -35,4 +37,12 @@ public interface CartInfoService {
|
||||||
* @param skuIdList 商品的id
|
* @param skuIdList 商品的id
|
||||||
*/
|
*/
|
||||||
void batchDeleteCart(Long userId, List<Long> skuIdList);
|
void batchDeleteCart(Long userId, List<Long> skuIdList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 查询购物车列表
|
||||||
|
*
|
||||||
|
* @param userId 用户id
|
||||||
|
* @return 购物车信息列表
|
||||||
|
*/
|
||||||
|
List<CartInfo> getCartList(Long userId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,17 @@ import com.atguigu.ssyx.common.constant.MessageConstant;
|
||||||
import com.atguigu.ssyx.common.exception.BunnyException;
|
import com.atguigu.ssyx.common.exception.BunnyException;
|
||||||
import com.atguigu.ssyx.common.result.ResultCodeEnum;
|
import com.atguigu.ssyx.common.result.ResultCodeEnum;
|
||||||
import com.atguigu.ssyx.enums.SkuType;
|
import com.atguigu.ssyx.enums.SkuType;
|
||||||
|
import com.atguigu.ssyx.model.base.BaseEntity;
|
||||||
import com.atguigu.ssyx.model.order.CartInfo;
|
import com.atguigu.ssyx.model.order.CartInfo;
|
||||||
import com.atguigu.ssyx.model.product.SkuInfo;
|
import com.atguigu.ssyx.model.product.SkuInfo;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.BoundHashOperations;
|
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.Comparator;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -131,4 +135,28 @@ public class CartInfoServiceImpl implements CartInfoService {
|
||||||
skuIdList.forEach(skuId -> hashOperations.delete(skuId.toString()));
|
skuIdList.forEach(skuId -> hashOperations.delete(skuId.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 查询购物车列表
|
||||||
|
*
|
||||||
|
* @param userId 用户id
|
||||||
|
* @return 购物车信息列表
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<CartInfo> getCartList(Long userId) {
|
||||||
|
List<CartInfo> cartInfoList = new ArrayList<>();
|
||||||
|
// 用户ID不存在返回空数组
|
||||||
|
if (StringUtils.isEmpty(userId.toString())) {
|
||||||
|
return cartInfoList;
|
||||||
|
}
|
||||||
|
// 查询Redis之后做排序
|
||||||
|
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(module.getCartKey(userId));
|
||||||
|
cartInfoList = hashOperations.values();
|
||||||
|
// 当购物车数据不为空时,做降序排列
|
||||||
|
if (cartInfoList != null) {
|
||||||
|
cartInfoList.sort(Comparator.comparing(BaseEntity::getCreateTime));
|
||||||
|
}
|
||||||
|
// 返回结果
|
||||||
|
return cartInfoList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue