1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、application.xml文章来源:https://www.toymoban.com/news/detail-657948.html
spring:
characterEncoding=utf-8&useSSL=false
redis:
host: 127.0.0.1
port: 6379
3、CartServiceImpl.java文章来源地址https://www.toymoban.com/news/detail-657948.html
@Service
public class CartServiceImpl implements ICartService {
private final static String CART_REDIS_KEY_TEMPLATE = "cart_%d";
@Autowired
private ProductMapper productMapper;
@Autowired
private StringRedisTemplate redisTemplate;
private Gson gson = new Gson();
@Override
public ResponseVo<CartVo> add(Integer uid, CartAddForm form) {
Integer quantity = 1;
Product product = productMapper.selectByPrimaryKey(form.getProductId());
//商品是否存在
if (product == null) {
return ResponseVo.error(ResponseEnum.PRODUCT_NOT_EXIST);
}
//商品是否正常在售
if (!product.getStatus().equals(ProductStatusEnum.ON_SALE.getCode())) {
return ResponseVo.error(ResponseEnum.PRODUCT_OFF_SALE_OR_DELETE);
}
//商品库存是否充足
if (product.getStock() <= 0) {
return ResponseVo.error(ResponseEnum.PROODUCT_STOCK_ERROR);
}
//写入到redis
//key: cart_1
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, uid);
Cart cart;
String value = opsForHash.get(redisKey, String.valueOf(product.getId()));
if (StringUtils.isEmpty(value)) {
//没有该商品, 新增
cart = new Cart(product.getId(), quantity, form.getSelected());
}else {
//已经有了,数量+1
cart = gson.fromJson(value, Cart.class);
cart.setQuantity(cart.getQuantity() + quantity);
}
opsForHash.put(redisKey,
String.valueOf(product.getId()),
gson.toJson(cart));
return list(uid);
}
@Override
public ResponseVo<CartVo> list(Integer uid) {
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, uid);
Map<String, String> entries = opsForHash.entries(redisKey);
boolean selectAll = true;
Integer cartTotalQuantity = 0;
BigDecimal cartTotalPrice = BigDecimal.ZERO;
CartVo cartVo = new CartVo();
List<CartProductVo> cartProductVoList = new ArrayList<>();
for (Map.Entry<String, String> entry : entries.entrySet()) {
Integer productId = Integer.valueOf(entry.getKey());
Cart cart = gson.fromJson(entry.getValue(), Cart.class);
//TODO 需要优化,使用mysql里的in
Product product = productMapper.selectByPrimaryKey(productId);
if (product != null) {
CartProductVo cartProductVo = new CartProductVo(productId,
cart.getQuantity(),
product.getName(),
product.getSubtitle(),
product.getMainImage(),
product.getPrice(),
product.getStatus(),
product.getPrice().multiply(BigDecimal.valueOf(cart.getQuantity())),
product.getStock(),
cart.getProductSelected()
);
cartProductVoList.add(cartProductVo);
if (!cart.getProductSelected()) {
selectAll = false;
}
//计算总价(只计算选中的)
if (cart.getProductSelected()) {
cartTotalPrice = cartTotalPrice.add(cartProductVo.getProductTotalPrice());
}
}
cartTotalQuantity += cart.getQuantity();
}
//有一个没有选中,就不叫全选
cartVo.setSelectedAll(selectAll);
cartVo.setCartTotalQuantity(cartTotalQuantity);
cartVo.setCartTotalPrice(cartTotalPrice);
cartVo.setCartProductVoList(cartProductVoList);
return ResponseVo.success(cartVo);
}
@Override
public ResponseVo<CartVo> update(Integer uid, Integer productId, CartUpdateForm form) {
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, uid);
String value = opsForHash.get(redisKey, String.valueOf(productId));
if (StringUtils.isEmpty(value)) {
//没有该商品, 报错
return ResponseVo.error(ResponseEnum.CART_PRODUCT_NOT_EXIST);
}
//已经有了,修改内容
Cart cart = gson.fromJson(value, Cart.class);
if (form.getQuantity() != null
&& form.getQuantity() >= 0) {
cart.setQuantity(form.getQuantity());
}
if (form.getSelected() != null) {
cart.setProductSelected(form.getSelected());
}
opsForHash.put(redisKey, String.valueOf(productId), gson.toJson(cart));
return list(uid);
}
@Override
public ResponseVo<CartVo> delete(Integer uid, Integer productId) {
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, uid);
String value = opsForHash.get(redisKey, String.valueOf(productId));
if (StringUtils.isEmpty(value)) {
//没有该商品, 报错
return ResponseVo.error(ResponseEnum.CART_PRODUCT_NOT_EXIST);
}
opsForHash.delete(redisKey, String.valueOf(productId));
return list(uid);
}
@Override
public ResponseVo<CartVo> selectAll(Integer uid) {
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, uid);
for (Cart cart : listForCart(uid)) {
cart.setProductSelected(true);
opsForHash.put(redisKey,
String.valueOf(cart.getProductId()),
gson.toJson(cart));
}
return list(uid);
}
@Override
public ResponseVo<CartVo> unSelectAll(Integer uid) {
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, uid);
for (Cart cart : listForCart(uid)) {
cart.setProductSelected(false);
opsForHash.put(redisKey,
String.valueOf(cart.getProductId()),
gson.toJson(cart));
}
return list(uid);
}
@Override
public ResponseVo<Integer> sum(Integer uid) {
Integer sum = listForCart(uid).stream()
.map(Cart::getQuantity)
.reduce(0, Integer::sum);
return ResponseVo.success(sum);
}
public List<Cart> listForCart(Integer uid) {
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, uid);
Map<String, String> entries = opsForHash.entries(redisKey);
List<Cart> cartList = new ArrayList<>();
for (Map.Entry<String, String> entry : entries.entrySet()) {
cartList.add(gson.fromJson(entry.getValue(), Cart.class));
}
return cartList;
}
}
到了这里,关于SpringBoot + redis处理购物车逻辑的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!