Skip to content

Commit

Permalink
pref: 优化es生成全部商品索引,减少数据库操作
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed Nov 2, 2022
1 parent ca23b5a commit 3b078cf
Showing 12 changed files with 188 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;
import java.util.Map;

/**
* 商品品牌业务层
@@ -40,6 +41,14 @@ public interface BrandService extends IService<Brand> {
*/
List<Brand> getBrandsByCategory(String categoryId);

/**
* 根据分类ID获取品牌列表
*
* @param categoryIds 分类ID
* @return 品牌列表
*/
List<Map<String, Object>> getBrandsMapsByCategory(List<String> categoryIds, String columns);

/**
* 添加品牌
*
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;
import java.util.Map;

/**
* 商品分类业务层
@@ -41,6 +42,14 @@ public interface CategoryService extends IService<Category> {
*/
List<Category> listByIdsOrderByLevel(List<String> ids);

/**
* 根据分类id集合获取所有分类根据层级排序
*
* @param ids 分类ID集合
* @return 商品分类列表
*/
List<Map<String, Object>> listMapsByIdsOrderByLevel(List<String> ids, String columns);

/**
* 获取分类树
*
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;
import java.util.Map;

/**
* 店铺商品分类业务层
@@ -30,6 +31,14 @@ public interface StoreGoodsLabelService extends IService<StoreGoodsLabel> {
*/
List<StoreGoodsLabel> listByStoreIds(List<String> ids);

/**
* 根据分类id集合获取所有店铺分类根据层级排序
*
* @param ids 商家ID
* @return 店铺分类列表
*/
List<Map<String, Object>> listMapsByStoreIds(List<String> ids, String columns);

/**
* 添加商品分类
*
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@@ -68,6 +69,14 @@ public List<Brand> getBrandsByCategory(String categoryId) {
return new ArrayList<>();
}

@Override
public List<Map<String, Object>> getBrandsMapsByCategory(List<String> categoryIds, String columns) {
QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
queryWrapper.select(columns);
queryWrapper.in("id", categoryIds);
return this.listMaps(queryWrapper);
}

@Override
public boolean addBrand(BrandVO brandVO) {

Original file line number Diff line number Diff line change
@@ -25,10 +25,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;


@@ -80,6 +77,14 @@ public List<Category> listByIdsOrderByLevel(List<String> ids) {
return this.list(new LambdaQueryWrapper<Category>().in(Category::getId, ids).orderByAsc(Category::getLevel));
}

@Override
public List<Map<String, Object>> listMapsByIdsOrderByLevel(List<String> ids, String columns) {
QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
queryWrapper.select(columns);
queryWrapper.in("id", ids).orderByAsc("level");
return this.listMaps(queryWrapper);
}

@Override
public List<CategoryVO> categoryTree() {
List<CategoryVO> categoryVOList = (List<CategoryVO>) cache.get(CachePrefix.CATEGORY.getPrefix());
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import cn.lili.modules.goods.mapper.StoreGoodsLabelMapper;
import cn.lili.modules.goods.service.StoreGoodsLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -22,6 +23,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
* 店铺商品分类业务层实现
@@ -83,6 +85,13 @@ public List<StoreGoodsLabel> listByStoreIds(List<String> ids) {
return this.list(new LambdaQueryWrapper<StoreGoodsLabel>().in(StoreGoodsLabel::getId, ids).orderByAsc(StoreGoodsLabel::getLevel));
}

@Override
public List<Map<String, Object>> listMapsByStoreIds(List<String> ids, String columns) {
QueryWrapper<StoreGoodsLabel> queryWrapper = new QueryWrapper<StoreGoodsLabel>().in("id", ids).orderByAsc("level");
queryWrapper.select(columns);
return this.listMaps(queryWrapper);
}

@Override
@Transactional(rollbackFor = Exception.class)
public StoreGoodsLabel addStoreGoodsLabel(StoreGoodsLabel storeGoodsLabel) {
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -43,6 +44,15 @@ static String getPromotionGoodsStockCacheKey(PromotionTypeEnum typeEnum, String
*/
List<PromotionGoods> findSkuValidPromotion(String skuId, String storeIds);


/**
* 获取sku所有有效活动
*
* @param skus 商品skuId
* @return 促销商品集合
*/
List<PromotionGoods> findSkuValidPromotions(List<GoodsSkuDTO> skus);

/**
* 分页获取促销商品信息
*
Original file line number Diff line number Diff line change
@@ -35,4 +35,12 @@ public interface PromotionService {
* @param goodsIdsJsonStr
*/
void removeByGoodsIds(String goodsIdsJsonStr);

/**
* 根据促销商品信息包装促销信息
*
* @param promotionGoodsList 促销商品信息
* @return 促销信息
*/
Map<String, Object> wrapperPromotionMapList(List<PromotionGoods> promotionGoodsList);
}
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
import cn.lili.modules.goods.entity.vos.GoodsVO;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.goods.service.GoodsSkuService;
@@ -35,6 +36,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
* 促销商品业务层实现
@@ -87,6 +89,22 @@ public List<PromotionGoods> findSkuValidPromotion(String skuId, String storeIds)
return this.list(queryWrapper);
}

@Override
public List<PromotionGoods> findSkuValidPromotions(List<GoodsSkuDTO> skus) {
List<String> categories = skus.stream().map(GoodsSku::getCategoryPath).collect(Collectors.toList());
List<String> skuIds = skus.stream().map(GoodsSku::getId).collect(Collectors.toList());
List<String> categoriesPath = new ArrayList<>();
categories.forEach(i -> categoriesPath.addAll(Arrays.asList(i.split(","))));
QueryWrapper<PromotionGoods> queryWrapper = new QueryWrapper<>();

queryWrapper.and(i -> i.or(j -> j.in(SKU_ID_COLUMN, skuIds))
.or(n -> n.eq("scope_type", PromotionsScopeTypeEnum.ALL.name()))
.or(n -> n.and(k -> k.eq("scope_type", PromotionsScopeTypeEnum.PORTION_GOODS_CATEGORY.name())
.and(l -> l.in("scope_id", categoriesPath)))));
queryWrapper.and(i -> i.or(PromotionTools.queryPromotionStatus(PromotionsStatusEnum.START)).or(PromotionTools.queryPromotionStatus(PromotionsStatusEnum.NEW)));
return this.list(queryWrapper);
}

@Override
public IPage<PromotionGoods> pageFindAll(PromotionGoodsSearchParams searchParams, PageVO pageVo) {
return this.page(PageUtil.initPage(pageVo), searchParams.queryWrapper());
Original file line number Diff line number Diff line change
@@ -89,8 +89,19 @@ public Map<String, List<PromotionGoods>> getCurrentPromotion() {
*/
public Map<String, Object> getGoodsSkuPromotionMap(String storeId, String goodsSkuId) {
String storeIds = storeId + "," + PromotionTools.PLATFORM_ID;
Map<String, Object> promotionMap = new HashMap<>();
List<PromotionGoods> promotionGoodsList = promotionGoodsService.findSkuValidPromotion(goodsSkuId, storeIds);
return wrapperPromotionMapList(promotionGoodsList);
}

@Override
public void removeByGoodsIds(String goodsIdsJsonStr) {
List<String> goodsIds = JSONUtil.toList(goodsIdsJsonStr, String.class);
promotionGoodsService.deletePromotionGoodsByGoods(goodsIds);
kanjiaActivityGoodsService.deleteByGoodsIds(goodsIds);
}

public Map<String, Object> wrapperPromotionMapList(List<PromotionGoods> promotionGoodsList) {
Map<String, Object> promotionMap = new HashMap<>();
for (PromotionGoods promotionGoods : promotionGoodsList) {
String esPromotionKey = promotionGoods.getPromotionType() + "-" + promotionGoods.getPromotionId();
switch (PromotionTypeEnum.valueOf(promotionGoods.getPromotionType())) {
@@ -120,13 +131,6 @@ public Map<String, Object> getGoodsSkuPromotionMap(String storeId, String goodsS
return promotionMap;
}

@Override
public void removeByGoodsIds(String goodsIdsJsonStr) {
List<String> goodsIds = JSONUtil.toList(goodsIdsJsonStr, String.class);
promotionGoodsService.deletePromotionGoodsByGoods(goodsIds);
kanjiaActivityGoodsService.deleteByGoodsIds(goodsIds);
}

private void getGoodsCurrentSeckill(String esPromotionKey, PromotionGoods promotionGoods, Map<String, Object> promotionMap) {
Seckill seckill = seckillService.getById(promotionGoods.getPromotionId());
SeckillSearchParams searchParams = new SeckillSearchParams();
Loading

0 comments on commit 3b078cf

Please sign in to comment.