|
|
|
@ -0,0 +1,246 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
*
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
*
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.yami.shop.service.impl;
|
|
|
|
|
|
|
|
|
|
// 导入所需的类和包
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import com.yami.shop.bean.app.dto.*;
|
|
|
|
|
import com.yami.shop.bean.app.param.ChangeShopCartParam;
|
|
|
|
|
import com.yami.shop.bean.app.param.OrderItemParam;
|
|
|
|
|
import com.yami.shop.bean.app.param.ShopCartParam;
|
|
|
|
|
import com.yami.shop.bean.event.ShopCartEvent;
|
|
|
|
|
import com.yami.shop.bean.model.Basket;
|
|
|
|
|
import com.yami.shop.bean.model.Product;
|
|
|
|
|
import com.yami.shop.bean.model.ShopDetail;
|
|
|
|
|
import com.yami.shop.bean.model.Sku;
|
|
|
|
|
import com.yami.shop.common.util.Arith;
|
|
|
|
|
import com.yami.shop.common.util.CacheManagerUtil;
|
|
|
|
|
import com.yami.shop.dao.BasketMapper;
|
|
|
|
|
import com.yami.shop.service.BasketService;
|
|
|
|
|
import com.yami.shop.service.ProductService;
|
|
|
|
|
import com.yami.shop.service.ShopDetailService;
|
|
|
|
|
import com.yami.shop.service.SkuService;
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 购物车服务实现类
|
|
|
|
|
*
|
|
|
|
|
* @author lgh on 2018/10/18.
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
public class BasketServiceImpl extends ServiceImpl<BasketMapper, Basket> implements BasketService {
|
|
|
|
|
|
|
|
|
|
// 注入的组件
|
|
|
|
|
private final BasketMapper basketMapper;
|
|
|
|
|
private final CacheManagerUtil cacheManagerUtil;
|
|
|
|
|
private final ApplicationContext applicationContext;
|
|
|
|
|
private final SkuService skuService;
|
|
|
|
|
private final ShopDetailService shopDetailService;
|
|
|
|
|
private final ProductService productService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据购物车ID删除购物车商品
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
* @param basketIds 购物车ID列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
|
|
|
|
|
public void deleteShopCartItemsByBasketIds(String userId, List<Long> basketIds) {
|
|
|
|
|
basketMapper.deleteShopCartItemsByBasketIds(userId, basketIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加购物车商品
|
|
|
|
|
* @param param 购物车参数
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
|
|
|
|
|
public void addShopCartItem(ChangeShopCartParam param, String userId) {
|
|
|
|
|
Basket basket = new Basket();
|
|
|
|
|
basket.setBasketCount(param.getCount());
|
|
|
|
|
basket.setBasketDate(new Date());
|
|
|
|
|
basket.setProdId(param.getProdId());
|
|
|
|
|
basket.setShopId(param.getShopId());
|
|
|
|
|
basket.setUserId(userId);
|
|
|
|
|
basket.setSkuId(param.getSkuId());
|
|
|
|
|
basket.setDistributionCardNo(param.getDistributionCardNo());
|
|
|
|
|
basketMapper.insert(basket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新购物车商品
|
|
|
|
|
* @param basket 购物车对象
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@CacheEvict(cacheNames = "ShopCartItems", key = "#basket.userId")
|
|
|
|
|
public void updateShopCartItem(Basket basket) {
|
|
|
|
|
basketMapper.updateById(basket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除用户所有购物车商品
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
|
|
|
|
|
public void deleteAllShopCartItems(String userId) {
|
|
|
|
|
basketMapper.deleteAllShopCartItems(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户购物车商品列表
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
* @return 购物车商品列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<ShopCartItemDto> getShopCartItems(String userId) {
|
|
|
|
|
// 从缓存中获取购物车商品列表,如果缓存中没有则从数据库中获取
|
|
|
|
|
List<ShopCartItemDto> shopCartItemDtoList = cacheManagerUtil.getCache("ShopCartItems", userId);
|
|
|
|
|
if (shopCartItemDtoList != null) {
|
|
|
|
|
return shopCartItemDtoList;
|
|
|
|
|
}
|
|
|
|
|
shopCartItemDtoList = basketMapper.getShopCartItems(userId);
|
|
|
|
|
for (ShopCartItemDto shopCartItemDto : shopCartItemDtoList) {
|
|
|
|
|
shopCartItemDto.setProductTotalAmount(Arith.mul(shopCartItemDto.getProdCount(), shopCartItemDto.getPrice()));
|
|
|
|
|
}
|
|
|
|
|
cacheManagerUtil.putCache("ShopCartItems", userId, shopCartItemDtoList);
|
|
|
|
|
return shopCartItemDtoList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取过期的购物车商品列表
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
* @return 过期的购物车商品列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<ShopCartItemDto> getShopCartExpiryItems(String userId) {
|
|
|
|
|
return basketMapper.getShopCartExpiryItems(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清除过期商品列表
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
|
|
|
|
|
public void cleanExpiryProdList(String userId) {
|
|
|
|
|
basketMapper.cleanExpiryProdList(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据购物车参数更新购物车
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
* @param basketIdShopCartParamMap 购物车ID和参数的映射
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
|
|
|
|
|
public void updateBasketByShopCartParam(String userId, Map<Long, ShopCartParam> basketIdShopCartParamMap) {
|
|
|
|
|
basketMapper.updateDiscountItemId(userId, basketIdShopCartParamMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据用户ID移除购物车缓存
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
|
|
|
|
|
public void removeShopCartItemsCacheByUserId(String userId) {
|
|
|
|
|
// 该方法体为空,可能是待实现的方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据商品ID列出所有用户的ID
|
|
|
|
|
* @param prodId 商品ID
|
|
|
|
|
* @return 用户ID列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<String> listUserIdByProdId(Long prodId) {
|
|
|
|
|
return basketMapper.listUserIdByProdId(prodId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据购物车商品项获取购物车信息
|
|
|
|
|
* @param shopCartItems 购物车商品项列表
|
|
|
|
|
* @return 购物车信息列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<ShopCartDto> getShopCarts(List<ShopCartItemDto> shopCartItems) {
|
|
|
|
|
// 根据店铺ID分组购物车商品项
|
|
|
|
|
Map<Long, List<ShopCartItemDto>> shopCartMap = shopCartItems.stream().collect(Collectors.groupingBy(ShopCartItemDto::getShopId));
|
|
|
|
|
|
|
|
|
|
// 构建购物车信息列表
|
|
|
|
|
List<ShopCartDto> shopCartDtos = Lists.newArrayList();
|
|
|
|
|
for (Long shopId : shopCartMap.keySet()) {
|
|
|
|
|
// 获取店铺的所有商品项
|
|
|
|
|
List<ShopCartItemDto> shopCartItemDtoList = shopCartMap.get(shopId);
|
|
|
|
|
|
|
|
|
|
// 构建每个店铺的购物车信息
|
|
|
|
|
ShopCartDto shopCart = new ShopCartDto();
|
|
|
|
|
shopCart.setShopId(shopId);
|
|
|
|
|
shopCart.setShopName(shopCartItemDtoList.get(0).getShopName());
|
|
|
|
|
|
|
|
|
|
// 发布购物车事件
|
|
|
|
|
applicationContext.publishEvent(new ShopCartEvent(shopCart, shopCartItemDtoList));
|
|
|
|
|
|
|
|
|
|
shopCartDtos.add(shopCart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return shopCartDtos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据订单项和用户ID获取购物车商品项
|
|
|
|
|
* @param basketId 购物车ID列表
|
|
|
|
|
* @param orderItem 订单项参数
|
|
|
|
|
* @param userId 用户ID
|
|
|
|
|
* @return 购物车商品项列表
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<ShopCartItemDto> getShopCartItemsByOrderItems(List<Long> basketId, OrderItemParam orderItem, String userId) {
|
|
|
|
|
if (orderItem == null && CollectionUtil.isEmpty(basketId)) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当立即购买时,没有提交的订单是没有购物车信息的
|
|
|
|
|
if (CollectionUtil.isEmpty(basketId) && orderItem != null) {
|
|
|
|
|
Sku sku = skuService.getSkuBySkuId(orderItem.getSkuId());
|
|
|
|
|
if (sku == null) {
|
|
|
|
|
throw new RuntimeException("订单包含无法识别的商品");
|
|
|
|
|
}
|
|
|
|
|
Product prod = productService.getProductByProdId(orderItem.getProdId());
|
|
|
|
|
if (prod == null) {
|
|
|
|
|
throw new RuntimeException("订单包含无法识别的商品");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建购物车商品项
|
|
|
|
|
ShopCartItemDto shopCartItemDto = new ShopCartItemDto();
|
|
|
|
|
shopCartItemDto.setBasketId(-1L);
|
|
|
|
|
shopCartItemDto.setSkuId(orderItem.getSkuId());
|
|
|
|
|
shopCartItemDto.setProdCount(orderItem.getProdCount());
|
|
|
|
|
shopCartItemDto.setProdId(orderItem.getProdId());
|
|
|
|
|
shopCartItemDto.setSkuName(sku.getSkuName());
|
|
|
|
|
shopCartItemDto.setPic(StrUtil.isBlank(sku.getPic()) ? prod.getPic() : sku.getPic());
|
|
|
|
|
shopCartItemDto.setProdName(sku.getProdName());
|
|
|
|
|
shopCartItemDto.set
|
|
|
|
|
}
|