修正项目

This commit is contained in:
2024-01-07 01:10:08 +08:00
parent 2f29241806
commit b22014e976
943 changed files with 27699 additions and 28227 deletions

View File

@@ -0,0 +1,5 @@
FROM openjdk:8-jdk-alpine
LABEL authors="yovinchen"
VOLUME /tmp
ADD ./target/service-cart.jar service-cart.jar
ENTRYPOINT ["java","-jar","/service-cart.jar", "&"]

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yovinchen</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-cart</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.yovinchen</groupId>
<artifactId>service-product-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.yovinchen</groupId>
<artifactId>service-product-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.yovinchen</groupId>
<artifactId>service-activity-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.yovinchen</groupId>
<artifactId>rabbit_util</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,23 @@
package com.yovinchen.xlcs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* ClassName: ServiceCartApplication
* Package: com.yovinchen.xlcs
*
* @author yovinchen
* @Create 2023/10/8 22:26
*/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceCartApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceCartApplication.class, args);
}
}

View File

@@ -0,0 +1,117 @@
package com.yovinchen.xlcs.cart.controller;
import com.yovinchen.xlcs.cart.service.CartInfoService;
import com.yovinchen.xlcs.client.activity.ActivityFeignClient;
import com.yovinchen.xlcs.common.auth.AuthContextHolder;
import com.yovinchen.xlcs.common.result.Result;
import com.yovinchen.xlcs.model.order.CartInfo;
import com.yovinchen.xlcs.vo.order.OrderConfirmVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* ClassName: CartApiController
* Package: com.yovinchen.xlcs.cart.cart.controller
*
* @author yovinchen
* @Create 2023/10/8 22:32
*/
@RestController
@RequestMapping("/api/cart")
public class CartApiController {
@Autowired
private CartInfoService cartInfoService;
@Autowired
private ActivityFeignClient activityFeignClient;
//添加内容当前登录用户idskuId商品数量
@ApiOperation(value = "添加商品到购物车")
@GetMapping("addToCart/{skuId}/{skuNum}")
public Result addToCart(@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum) {
//获取当前登录用户Id
Long userId = AuthContextHolder.getUserId();
cartInfoService.addToCart(userId, skuId, skuNum);
return Result.ok(null);
}
@ApiOperation(value = "根据skuId删除购物车")
@DeleteMapping("deleteCart/{skuId}")
public Result deleteCart(@PathVariable("skuId") Long skuId) {
Long userId = AuthContextHolder.getUserId();
cartInfoService.deleteCart(skuId, userId);
return Result.ok(null);
}
@ApiOperation(value = "清空购物车")
@DeleteMapping("deleteAllCart")
public Result deleteAllCart() {
Long userId = AuthContextHolder.getUserId();
cartInfoService.deleteAllCart(userId);
return Result.ok(null);
}
@ApiOperation(value = "批量删除购物车 多个skuId")
@DeleteMapping("batchDeleteCart")
public Result batchDeleteCart(@RequestBody List<Long> skuIdList) {
Long userId = AuthContextHolder.getUserId();
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);
}
}

View File

@@ -0,0 +1,40 @@
package com.yovinchen.xlcs.cart.receiver;
import com.yovinchen.xlcs.cart.service.CartInfoService;
import com.yovinchen.xlcs.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.yovinchen.xlcs.cart.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);
}
}

View File

@@ -0,0 +1,90 @@
package com.yovinchen.xlcs.cart.service;
import com.yovinchen.xlcs.model.order.CartInfo;
import java.util.List;
/**
* ClassName: CartInfoService
* Package: com.yovinchen.xlcs.cart.cart.service
*
* @author yovinchen
* @Create 2023/10/8 22:33
*/
public interface CartInfoService {
/**
* 添加商品到购物车
*
* @param userId
* @param skuId
* @param skuNum
*/
void addToCart(Long userId, Long skuId, Integer skuNum);
/**
* 根据skuId删除购物车
*
* @param skuId
* @param userId
*/
void deleteCart(Long skuId, Long userId);
/**
* 清空购物车
*
* @param userId
*/
void deleteAllCart(Long userId);
/**
* 批量删除购物车
*
* @param skuIdList
* @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);
}

View File

@@ -0,0 +1,303 @@
package com.yovinchen.xlcs.cart.service.impl;
import com.yovinchen.xlcs.cart.service.CartInfoService;
import com.yovinchen.xlcs.client.product.ProductFeignClient;
import com.yovinchen.xlcs.common.constant.RedisConst;
import com.yovinchen.xlcs.common.exception.xlcsException;
import com.yovinchen.xlcs.common.result.ResultCodeEnum;
import com.yovinchen.xlcs.enums.SkuType;
import com.yovinchen.xlcs.model.order.CartInfo;
import com.yovinchen.xlcs.model.product.SkuInfo;
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
* Package: com.yovinchen.xlcs.cart.cart.service.impl
*
* @author yovinchen
* @Create 2023/10/8 22:34
*/
@Service
public class CartInfoServiceImpl implements CartInfoService {
@Autowired
private ProductFeignClient productFeignClient;
@Autowired
private RedisTemplate redisTemplate;
//返回购物车在redis的key
private String getCartKey(Long userId) {
return RedisConst.USER_KEY_PREFIX + userId + RedisConst.USER_CART_KEY_SUFFIX;
}
//设置key 过期时间
private void setCartKeyExpire(String key) {
redisTemplate.expire(key, RedisConst.USER_CART_EXPIRE, TimeUnit.SECONDS);
}
/**
* 添加商品到购物车
*
* @param userId
* @param skuId
* @param skuNum
*/
@Override
public void addToCart(Long userId, Long skuId, Integer skuNum) {
//1 因为购物车数据存储到redis里面
// 从redis里面根据key获取数据这个key包含userId
String cartKey = this.getCartKey(userId);
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
//2 根据第一步查询出来的结果得到是skuId + skuNum关系
CartInfo cartInfo = null;
//目的:判断是否是第一次添加这个商品到购物车
// 进行判断判断结果里面是否有skuId
if (hashOperations.hasKey(skuId.toString())) {
//3 如果结果里面包含skuId不是第一次添加
//3.1 根据skuId获取对应数量更新数量
cartInfo = hashOperations.get(skuId.toString());
//把购物车存在商品之前数量获取数量,在进行数量更新操作
Integer currentSkuNum = cartInfo.getSkuNum() + skuNum;
if (currentSkuNum < 1) {
return;
}
//更新cartInfo对象
cartInfo.setSkuNum(currentSkuNum);
cartInfo.setCurrentBuyNum(currentSkuNum);
//判断商品数量不能大于限购数量
Integer perLimit = cartInfo.getPerLimit();
if (currentSkuNum > perLimit) {
throw new xlcsException(ResultCodeEnum.SKU_LIMIT_ERROR);
}
//更新其他值
cartInfo.setIsChecked(1);
cartInfo.setUpdateTime(new Date());
} else {
//4 如果结果里面没有skuId就是第一次添加
//4.1 直接添加
skuNum = 1;
//远程调用根据skuId获取skuInfo
SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
if (skuInfo == null) {
throw new xlcsException(ResultCodeEnum.DATA_ERROR);
}
//封装cartInfo对象
cartInfo = new CartInfo();
cartInfo.setSkuId(skuId);
cartInfo.setCategoryId(skuInfo.getCategoryId());
cartInfo.setSkuType(skuInfo.getSkuType());
cartInfo.setIsNewPerson(skuInfo.getIsNewPerson());
cartInfo.setUserId(userId);
cartInfo.setCartPrice(skuInfo.getPrice());
cartInfo.setSkuNum(skuNum);
cartInfo.setCurrentBuyNum(skuNum);
cartInfo.setSkuType(SkuType.COMMON.getCode());
cartInfo.setPerLimit(skuInfo.getPerLimit());
cartInfo.setImgUrl(skuInfo.getImgUrl());
cartInfo.setSkuName(skuInfo.getSkuName());
cartInfo.setWareId(skuInfo.getWareId());
cartInfo.setIsChecked(1);
cartInfo.setStatus(1);
cartInfo.setCreateTime(new Date());
cartInfo.setUpdateTime(new Date());
}
//5 更新redis缓存
hashOperations.put(skuId.toString(), cartInfo);
//6 设置有效时间
this.setCartKeyExpire(cartKey);
}
/**
* 根据skuId删除购物车
*
* @param skuId
* @param userId
*/
@Override
public void deleteCart(Long skuId, Long userId) {
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(this.getCartKey(userId));
if (hashOperations.hasKey(skuId.toString())) {
hashOperations.delete(skuId.toString());
}
}
/**
* 清空购物车
*
* @param userId
*/
@Override
public void deleteAllCart(Long userId) {
String cartKey = this.getCartKey(userId);
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
List<CartInfo> cartInfoList = hashOperations.values();
for (CartInfo cartInfo : cartInfoList) {
hashOperations.delete(cartInfo.getSkuId().toString());
}
}
/**
* 批量删除购物车
*
* @param skuIdList
* @param userId
*/
@Override
public void batchDeleteCart(List<Long> skuIdList, Long userId) {
String cartKey = this.getCartKey(userId);
BoundHashOperations<String, String, CartInfo> hashOperations = redisTemplate.boundHashOps(cartKey);
skuIdList.forEach(skuId -> {
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);
//根据fieldskuId获取valueCartInfo
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);
//根据filedskuId删除redis数据
skuIdList.forEach(skuId -> {
hashOperations.delete(skuId.toString());
});
}
}

View File

@@ -0,0 +1,11 @@
server:
port: 8208
spring:
application:
name: service-cart
cloud:
nacos:
discovery:
server-addr: 82.157.68.223:8848
username: nacos
password: nacos

View File

@@ -0,0 +1,24 @@
spring:
cloud:
nacos:
config:
namespace: dd5265c5-8290-45bc-9d07-395c14c977d3
server-addr: 82.157.68.223:8848
group: service
username: nacos
password: nacos
enabled: true
file-extension: yml
extension-configs:
- data-id: common.yml
group: common
refresh: true
- data-id: service-redis.yml
group: common
refresh: true
- data-id: service-rabbitmq.yml
group: common
refresh: true
- data-id: service-openfeign.yml
group: common
refresh: true