订单模块
This commit is contained in:
@@ -29,6 +29,18 @@
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.atguigu</groupId>
|
||||
<artifactId>service-activity-client</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.atguigu</groupId>
|
||||
<artifactId>rabbit_util</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@@ -1,8 +1,11 @@
|
||||
package com.atguigu.ssyx.cart.controller;
|
||||
|
||||
import com.atguigu.ssyx.cart.service.CartInfoService;
|
||||
import com.atguigu.ssyx.client.activity.ActivityFeignClient;
|
||||
import com.atguigu.ssyx.common.auth.AuthContextHolder;
|
||||
import com.atguigu.ssyx.common.result.Result;
|
||||
import com.atguigu.ssyx.model.order.CartInfo;
|
||||
import com.atguigu.ssyx.vo.order.OrderConfirmVo;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -23,6 +26,9 @@ public class CartApiController {
|
||||
@Autowired
|
||||
private CartInfoService cartInfoService;
|
||||
|
||||
@Autowired
|
||||
private ActivityFeignClient activityFeignClient;
|
||||
|
||||
//添加内容:当前登录用户id,skuId,商品数量
|
||||
@ApiOperation(value = "添加商品到购物车")
|
||||
@GetMapping("addToCart/{skuId}/{skuNum}")
|
||||
@@ -56,4 +62,56 @@ public class CartApiController {
|
||||
cartInfoService.batchDeleteCart(skuIdList, userId);
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "购物车列表")
|
||||
@GetMapping("cartList")
|
||||
public Result cartList() {
|
||||
//获取userId
|
||||
Long userId = AuthContextHolder.getUserId();
|
||||
List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);
|
||||
return Result.ok(cartInfoList);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "查询带优惠卷的购物车")
|
||||
@GetMapping("activityCartList")
|
||||
public Result activityCartList() {
|
||||
// 获取用户Id
|
||||
Long userId = AuthContextHolder.getUserId();
|
||||
List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);
|
||||
|
||||
OrderConfirmVo orderTradeVo = activityFeignClient.findCartActivityAndCoupon(cartInfoList, userId);
|
||||
return Result.ok(orderTradeVo);
|
||||
}
|
||||
|
||||
@ApiOperation("根据skuId选中")
|
||||
@GetMapping("checkCart/{skuId}/{isChecked}")
|
||||
public Result checkCart(@PathVariable("skuId") Long skuId, @PathVariable("isChecked") Integer isChecked) {
|
||||
//获取userId
|
||||
Long userId = AuthContextHolder.getUserId();
|
||||
//调用方法
|
||||
cartInfoService.checkCart(userId, skuId, isChecked);
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
@ApiOperation("全选")
|
||||
@GetMapping("checkAllCart/{isChecked}")
|
||||
public Result checkAllCart(@PathVariable("isChecked") Integer isChecked) {
|
||||
Long userId = AuthContextHolder.getUserId();
|
||||
cartInfoService.checkAllCart(userId, isChecked);
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
@ApiOperation("批量选中")
|
||||
@PostMapping("batchCheckCart/{isChecked}")
|
||||
public Result batchCheckCart(@RequestBody List<Long> skuIdList, @PathVariable("isChecked") Integer isChecked) {
|
||||
Long userId = AuthContextHolder.getUserId();
|
||||
cartInfoService.batchCheckCart(skuIdList, userId, isChecked);
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "根据用户Id查询购物车列表")
|
||||
@PostMapping("inner/getCartCheckedList/{userId}")
|
||||
public List<CartInfo> getCartCheckedList(@PathVariable("userId") Long userId) {
|
||||
return cartInfoService.getCartCheckedList(userId);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,40 @@
|
||||
package com.atguigu.ssyx.cart.receiver;
|
||||
|
||||
import com.atguigu.ssyx.cart.service.CartInfoService;
|
||||
import com.atguigu.ssyx.mq.constant.MqConst;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* ClassName: CartReceiver
|
||||
* Package: com.atguigu.ssyx.cart.receiver
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 08:56
|
||||
*/
|
||||
@Component
|
||||
public class CartReceiver {
|
||||
|
||||
@Autowired
|
||||
private CartInfoService cartInfoService;
|
||||
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
value = @Queue(value = MqConst.QUEUE_DELETE_CART, durable = "true"),
|
||||
exchange = @Exchange(value = MqConst.EXCHANGE_ORDER_DIRECT),
|
||||
key = {MqConst.ROUTING_DELETE_CART}
|
||||
))
|
||||
public void deleteCart(Long userId, Message message, Channel channel) throws IOException {
|
||||
if (userId != null) {
|
||||
cartInfoService.deleteCartChecked(userId);
|
||||
}
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
}
|
||||
}
|
@@ -1,5 +1,7 @@
|
||||
package com.atguigu.ssyx.cart.service;
|
||||
|
||||
import com.atguigu.ssyx.model.order.CartInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -41,4 +43,48 @@ public interface CartInfoService {
|
||||
* @param userId
|
||||
*/
|
||||
void batchDeleteCart(List<Long> skuIdList, Long userId);
|
||||
|
||||
/**
|
||||
* 购物车列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<CartInfo> getCartList(Long userId);
|
||||
|
||||
/**
|
||||
* 根据skuId选中
|
||||
*
|
||||
* @param userId
|
||||
* @param skuId
|
||||
* @param isChecked
|
||||
*/
|
||||
void checkCart(Long userId, Long skuId, Integer isChecked);
|
||||
|
||||
/**
|
||||
* 全选
|
||||
*
|
||||
* @param userId
|
||||
* @param isChecked
|
||||
*/
|
||||
void checkAllCart(Long userId, Integer isChecked);
|
||||
|
||||
/**
|
||||
* 批量选中
|
||||
*
|
||||
* @param skuIdList
|
||||
* @param userId
|
||||
* @param isChecked
|
||||
*/
|
||||
void batchCheckCart(List<Long> skuIdList, Long userId, Integer isChecked);
|
||||
|
||||
/**
|
||||
* 根据用户Id查询购物车列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<CartInfo> getCartCheckedList(Long userId);
|
||||
|
||||
void deleteCartChecked(Long userId);
|
||||
}
|
||||
|
@@ -12,10 +12,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.BoundHashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* ClassName: CartInfoServiceImpl
|
||||
@@ -53,7 +57,7 @@ public class CartInfoServiceImpl implements CartInfoService {
|
||||
*/
|
||||
@Override
|
||||
public void addToCart(Long userId, Long skuId, Integer skuNum) {
|
||||
//1 因为购物车数据存储到redis里面,
|
||||
//1 因为购物车数据存储到redis里面,
|
||||
// 从redis里面根据key获取数据,这个key包含userId
|
||||
String cartKey = this.getCartKey(userId);
|
||||
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
@@ -149,8 +153,7 @@ public class CartInfoServiceImpl implements CartInfoService {
|
||||
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
List<CartInfo> cartInfoList = hashOperations.values();
|
||||
for (CartInfo cartInfo : cartInfoList) {
|
||||
hashOperations.delete(cartInfo.getSkuId()
|
||||
.toString());
|
||||
hashOperations.delete(cartInfo.getSkuId().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,4 +171,133 @@ public class CartInfoServiceImpl implements CartInfoService {
|
||||
hashOperations.delete(skuId.toString());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 购物车列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
//购物车列表
|
||||
@Override
|
||||
public List<CartInfo> getCartList(Long userId) {
|
||||
//判断userId
|
||||
List<CartInfo> cartInfoList = new ArrayList<>();
|
||||
if (StringUtils.isEmpty(userId)) {
|
||||
return cartInfoList;
|
||||
}
|
||||
//从redis获取购物车数据
|
||||
String cartKey = this.getCartKey(userId);
|
||||
BoundHashOperations<String, String, CartInfo> boundHashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
cartInfoList = boundHashOperations.values();
|
||||
if (!CollectionUtils.isEmpty(cartInfoList)) {
|
||||
//根据商品添加时间,降序
|
||||
cartInfoList.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()));
|
||||
}
|
||||
return cartInfoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据skuId选中
|
||||
*
|
||||
* @param userId
|
||||
* @param skuId
|
||||
* @param isChecked
|
||||
*/
|
||||
@Override
|
||||
public void checkCart(Long userId, Long skuId, Integer isChecked) {
|
||||
//获取redis的key
|
||||
String cartKey = this.getCartKey(userId);
|
||||
//cartKey获取field-value
|
||||
BoundHashOperations<String, String, CartInfo> boundHashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
//根据field(skuId)获取value(CartInfo)
|
||||
CartInfo cartInfo = boundHashOperations.get(skuId.toString());
|
||||
if (cartInfo != null) {
|
||||
cartInfo.setIsChecked(isChecked);
|
||||
//更新
|
||||
boundHashOperations.put(skuId.toString(), cartInfo);
|
||||
//设置key过期时间
|
||||
this.setCartKeyExpire(cartKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全选
|
||||
*
|
||||
* @param userId
|
||||
* @param isChecked
|
||||
*/
|
||||
@Override
|
||||
public void checkAllCart(Long userId, Integer isChecked) {
|
||||
String cartKey = this.getCartKey(userId);
|
||||
BoundHashOperations<String, String, CartInfo> boundHashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
List<CartInfo> cartInfoList = boundHashOperations.values();
|
||||
cartInfoList.forEach(cartInfo -> {
|
||||
cartInfo.setIsChecked(isChecked);
|
||||
boundHashOperations.put(cartInfo.getSkuId().toString(), cartInfo);
|
||||
});
|
||||
this.setCartKeyExpire(cartKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量选中
|
||||
*
|
||||
* @param skuIdList
|
||||
* @param userId
|
||||
* @param isChecked
|
||||
*/
|
||||
@Override
|
||||
public void batchCheckCart(List<Long> skuIdList, Long userId, Integer isChecked) {
|
||||
String cartKey = this.getCartKey(userId);
|
||||
BoundHashOperations<String, String, CartInfo> boundHashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
skuIdList.forEach(skuId -> {
|
||||
CartInfo cartInfo = boundHashOperations.get(skuId.toString());
|
||||
cartInfo.setIsChecked(isChecked);
|
||||
boundHashOperations.put(cartInfo.getSkuId().toString(), cartInfo);
|
||||
});
|
||||
this.setCartKeyExpire(cartKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户Id查询购物车列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<CartInfo> getCartCheckedList(Long userId) {
|
||||
String cartKey = this.getCartKey(userId);
|
||||
BoundHashOperations<String, String, CartInfo> boundHashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
List<CartInfo> cartInfoList = boundHashOperations.values();
|
||||
//isChecked = 1购物项选中
|
||||
return cartInfoList.stream().filter(cartInfo -> {
|
||||
return cartInfo.getIsChecked().intValue() == 1;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据userId删除选中购物车记录
|
||||
*
|
||||
* @param userId
|
||||
*/
|
||||
@Override
|
||||
public void deleteCartChecked(Long userId) {
|
||||
//根据userid查询选中购物车记录
|
||||
List<CartInfo> cartInfoList = this.getCartCheckedList(userId);
|
||||
|
||||
//查询list数据处理,得到skuId集合
|
||||
List<Long> skuIdList = cartInfoList.stream().map(item -> item.getSkuId()).collect(Collectors.toList());
|
||||
|
||||
//构建redis的key值
|
||||
// hash类型 key filed-value
|
||||
String cartKey = this.getCartKey(userId);
|
||||
|
||||
//根据key查询filed-value结构
|
||||
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
|
||||
|
||||
//根据filed(skuId)删除redis数据
|
||||
skuIdList.forEach(skuId -> {
|
||||
hashOperations.delete(skuId.toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user