use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.
the class GoodsSkuServiceImpl method getGoodsSkuVOList.
@Override
public List<GoodsSkuVO> getGoodsSkuVOList(List<GoodsSku> list) {
List<GoodsSkuVO> goodsSkuVOS = new ArrayList<>();
for (GoodsSku goodsSku : list) {
GoodsSkuVO goodsSkuVO = this.getGoodsSkuVO(goodsSku);
goodsSkuVOS.add(goodsSkuVO);
}
return goodsSkuVOS;
}
use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.
the class GoodsSkuServiceImpl method getGoodsSkuDetail.
@Override
public Map<String, Object> getGoodsSkuDetail(String goodsId, String skuId) {
Map<String, Object> map = new HashMap<>(16);
// 获取商品VO
GoodsVO goodsVO = goodsService.getGoodsVO(goodsId);
// 如果skuid为空,则使用商品VO中sku信息获取
if (CharSequenceUtil.isEmpty(skuId) || "undefined".equals(skuId)) {
skuId = goodsVO.getSkuList().get(0).getId();
}
// 从缓存拿商品Sku
GoodsSku goodsSku = this.getGoodsSkuByIdFromCache(skuId);
// 如果使用商品ID无法查询SKU则返回错误
if (goodsVO == null || goodsSku == null) {
throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
}
// 商品下架||商品未审核通过||商品删除,则提示:商品已下架
if (GoodsStatusEnum.DOWN.name().equals(goodsVO.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goodsVO.getAuthFlag()) || Boolean.TRUE.equals(goodsVO.getDeleteFlag())) {
throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
}
// 获取当前商品的索引信息
EsGoodsIndex goodsIndex = goodsIndexService.findById(skuId);
if (goodsIndex == null) {
goodsIndex = goodsIndexService.getResetEsGoodsIndex(goodsSku, goodsVO.getGoodsParamsDTOList());
}
// 商品规格
GoodsSkuVO goodsSkuDetail = this.getGoodsSkuVO(goodsSku);
Map<String, Object> promotionMap = goodsIndex.getPromotionMap();
// 设置当前商品的促销价格
if (promotionMap != null && !promotionMap.isEmpty()) {
promotionMap = promotionMap.entrySet().stream().parallel().filter(i -> {
JSONObject jsonObject = JSONUtil.parseObj(i.getValue());
// 过滤活动赠送优惠券和无效时间的活动
return (jsonObject.get("getType") == null || jsonObject.get("getType", String.class).equals(CouponGetEnum.FREE.name())) && (jsonObject.get("startTime") != null && jsonObject.get("startTime", Date.class).getTime() <= System.currentTimeMillis()) && (jsonObject.get("endTime") == null || jsonObject.get("endTime", Date.class).getTime() >= System.currentTimeMillis());
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Optional<Map.Entry<String, Object>> containsPromotion = promotionMap.entrySet().stream().filter(i -> i.getKey().contains(PromotionTypeEnum.SECKILL.name()) || i.getKey().contains(PromotionTypeEnum.PINTUAN.name())).findFirst();
if (containsPromotion.isPresent()) {
JSONObject jsonObject = JSONUtil.parseObj(containsPromotion.get().getValue());
PromotionGoodsSearchParams searchParams = new PromotionGoodsSearchParams();
searchParams.setSkuId(skuId);
searchParams.setPromotionId(jsonObject.get("id").toString());
PromotionGoods promotionsGoods = promotionGoodsService.getPromotionsGoods(searchParams);
if (promotionsGoods != null && promotionsGoods.getPrice() != null) {
goodsSkuDetail.setPromotionFlag(true);
goodsSkuDetail.setPromotionPrice(promotionsGoods.getPrice());
}
} else {
goodsSkuDetail.setPromotionFlag(false);
goodsSkuDetail.setPromotionPrice(null);
}
}
map.put("data", goodsSkuDetail);
// 获取分类
String[] split = goodsSkuDetail.getCategoryPath().split(",");
map.put("categoryName", categoryService.getCategoryNameByIds(Arrays.asList(split)));
// 获取规格信息
map.put("specs", this.groupBySkuAndSpec(goodsVO.getSkuList()));
map.put("promotionMap", promotionMap);
// 获取参数信息
if (goodsVO.getGoodsParamsDTOList() != null && !goodsVO.getGoodsParamsDTOList().isEmpty()) {
map.put("goodsParamsDTOList", goodsVO.getGoodsParamsDTOList());
}
// 记录用户足迹
if (UserContext.getCurrentUser() != null) {
FootPrint footPrint = new FootPrint(UserContext.getCurrentUser().getId(), goodsId, skuId);
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.VIEW_GOODS.name();
rocketMQTemplate.asyncSend(destination, footPrint, RocketmqSendCallbackBuilder.commonCallback());
}
return map;
}
use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.
the class GoodsSkuServiceImpl method updateStock.
@Override
@Transactional(rollbackFor = Exception.class)
public void updateStock(String skuId, Integer quantity) {
GoodsSku goodsSku = getGoodsSkuByIdFromCache(skuId);
if (goodsSku != null) {
if (quantity <= 0) {
goodsIndexService.deleteIndexById(goodsSku.getId());
}
goodsSku.setQuantity(quantity);
boolean update = this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity));
if (update) {
cache.remove(CachePrefix.GOODS.getPrefix() + goodsSku.getGoodsId());
}
cache.put(GoodsSkuService.getCacheKeys(skuId), goodsSku);
cache.put(GoodsSkuService.getStockCacheKey(skuId), quantity);
// 更新商品库存
List<GoodsSku> goodsSkus = new ArrayList<>();
goodsSkus.add(goodsSku);
this.updateGoodsStuck(goodsSkus);
}
}
use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.
the class GoodsSkuServiceImpl method getGoodsSkuByIdFromCache.
@Override
public GoodsSku getGoodsSkuByIdFromCache(String id) {
// 获取缓存中的sku
GoodsSku goodsSku = (GoodsSku) cache.get(GoodsSkuService.getCacheKeys(id));
// 如果缓存中没有信息,则查询数据库,然后写入缓存
if (goodsSku == null) {
goodsSku = this.getById(id);
if (goodsSku == null) {
return null;
}
cache.put(GoodsSkuService.getCacheKeys(id), goodsSku);
}
// 获取商品库存
Integer integer = (Integer) cache.get(GoodsSkuService.getStockCacheKey(id));
// 库存不为空,库存与缓存中不一致
if (integer != null && !goodsSku.getQuantity().equals(integer)) {
// 写入最新的库存信息
goodsSku.setQuantity(integer);
cache.put(GoodsSkuService.getCacheKeys(goodsSku.getId()), goodsSku);
}
return goodsSku;
}
use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.
the class GoodsSkuServiceImpl method update.
@Override
@Transactional(rollbackFor = Exception.class)
public void update(List<Map<String, Object>> skuList, Goods goods, Boolean regeneratorSkuFlag) {
// 是否存在规格
if (skuList == null || skuList.isEmpty()) {
throw new ServiceException(ResultCode.MUST_HAVE_GOODS_SKU);
}
List<GoodsSku> newSkuList;
// 删除旧的sku信息
if (Boolean.TRUE.equals(regeneratorSkuFlag)) {
List<GoodsSkuVO> goodsListByGoodsId = getGoodsListByGoodsId(goods.getId());
List<String> oldSkuIds = new ArrayList<>();
// 删除旧索引
for (GoodsSkuVO goodsSkuVO : goodsListByGoodsId) {
oldSkuIds.add(goodsSkuVO.getId());
cache.remove(GoodsSkuService.getCacheKeys(goodsSkuVO.getId()));
}
this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
// 删除sku相册
goodsGalleryService.removeByGoodsId(goods.getId());
getGoodsListByGoodsId(goods.getId());
// 添加商品sku
newSkuList = this.addGoodsSku(skuList, goods);
// 发送mq消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.SKU_DELETE.name();
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(oldSkuIds), RocketmqSendCallbackBuilder.commonCallback());
} else {
newSkuList = new ArrayList<>();
for (Map<String, Object> map : skuList) {
GoodsSku sku = null;
if (map.get("id") != null) {
sku = this.getGoodsSkuByIdFromCache(map.get("id").toString());
}
if (sku == null || map.get("id") == null) {
sku = new GoodsSku();
}
// 设置商品信息
goodsInfo(sku, goods);
// 设置商品规格信息
skuInfo(sku, goods, map, null);
newSkuList.add(sku);
// 如果商品状态值不对,则es索引移除
if (goods.getAuthFlag().equals(GoodsAuthEnum.PASS.name()) && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name())) {
goodsIndexService.deleteIndexById(sku.getId());
this.clearCache(sku.getId());
}
}
this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
this.saveOrUpdateBatch(newSkuList);
}
this.updateStock(newSkuList);
if (GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag()) && !newSkuList.isEmpty()) {
generateEs(goods);
}
}
Aggregations