merge origin master
This commit is contained in:
@@ -1,41 +1,15 @@
|
||||
package cn.lili.controller.other;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.lili.cache.Cache;
|
||||
import cn.lili.cache.CachePrefix;
|
||||
import cn.lili.common.enums.ResultUtil;
|
||||
import cn.lili.common.vo.ResultMessage;
|
||||
import cn.lili.modules.goods.entity.dos.Brand;
|
||||
import cn.lili.modules.goods.entity.dos.Category;
|
||||
import cn.lili.modules.goods.entity.dos.Goods;
|
||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
|
||||
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
||||
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
||||
import cn.lili.modules.goods.service.BrandService;
|
||||
import cn.lili.modules.goods.service.CategoryService;
|
||||
import cn.lili.modules.goods.service.GoodsService;
|
||||
import cn.lili.modules.goods.service.GoodsSkuService;
|
||||
import cn.lili.modules.promotion.service.PromotionService;
|
||||
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
|
||||
import cn.lili.modules.search.service.EsGoodsIndexService;
|
||||
import cn.lili.modules.store.entity.dos.StoreGoodsLabel;
|
||||
import cn.lili.modules.store.service.StoreGoodsLabelService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -48,108 +22,22 @@ import java.util.Map;
|
||||
@RestController
|
||||
@Api(tags = "ES初始化接口")
|
||||
@RequestMapping("/manager/elasticsearch")
|
||||
@Slf4j
|
||||
public class ElasticsearchController {
|
||||
|
||||
@Autowired
|
||||
private EsGoodsIndexService esGoodsIndexService;
|
||||
|
||||
@Autowired
|
||||
private GoodsSkuService goodsSkuService;
|
||||
|
||||
@Autowired
|
||||
private GoodsService goodsService;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Autowired
|
||||
private PromotionService promotionService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private BrandService brandService;
|
||||
|
||||
@Autowired
|
||||
private StoreGoodsLabelService storeGoodsLabelService;
|
||||
|
||||
@Autowired
|
||||
private Cache cache;
|
||||
|
||||
@GetMapping
|
||||
public ResultMessage<String> init() {
|
||||
|
||||
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
|
||||
if (flag == null) {
|
||||
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
|
||||
}
|
||||
if (Boolean.TRUE.equals(flag)) {
|
||||
return ResultUtil.error(100000, "当前有任务在执行");
|
||||
}
|
||||
|
||||
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), null);
|
||||
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), true);
|
||||
ThreadUtil.execAsync(() -> {
|
||||
try {
|
||||
//查询商品信息
|
||||
LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(GoodsSku::getIsAuth, GoodsAuthEnum.PASS.name());
|
||||
queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
|
||||
|
||||
List<GoodsSku> list = goodsSkuService.list(queryWrapper);
|
||||
List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
|
||||
//库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量
|
||||
for (GoodsSku goodsSku : list) {
|
||||
Goods goods = goodsService.getById(goodsSku.getGoodsId());
|
||||
EsGoodsIndex index = new EsGoodsIndex(goodsSku);
|
||||
if (goods.getParams() != null && !goods.getParams().isEmpty()) {
|
||||
List<GoodsParamsDTO> goodsParamDTOS = JSONUtil.toList(goods.getParams(), GoodsParamsDTO.class);
|
||||
index = new EsGoodsIndex(goodsSku, goodsParamDTOS);
|
||||
}
|
||||
if (goods.getCategoryPath() != null) {
|
||||
List<Category> categories = categoryService.listByIdsOrderByLevel(Arrays.asList(goods.getCategoryPath().split(",")));
|
||||
if (!categories.isEmpty()) {
|
||||
index.setCategoryNamePath(ArrayUtil.join(categories.stream().map(Category::getName).toArray(), ","));
|
||||
}
|
||||
}
|
||||
Brand brand = brandService.getById(goods.getBrandId());
|
||||
if (brand != null) {
|
||||
index.setBrandName(brand.getName());
|
||||
index.setBrandUrl(brand.getLogo());
|
||||
}
|
||||
if (goods.getStoreCategoryPath() != null && CharSequenceUtil.isNotEmpty(goods.getStoreCategoryPath())) {
|
||||
List<StoreGoodsLabel> storeGoodsLabels = storeGoodsLabelService.listByStoreIds(Arrays.asList(goods.getStoreCategoryPath().split(",")));
|
||||
if (!storeGoodsLabels.isEmpty()) {
|
||||
index.setStoreCategoryNamePath(ArrayUtil.join(storeGoodsLabels.stream().map(StoreGoodsLabel::getLabelName).toArray(), ","));
|
||||
}
|
||||
}
|
||||
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
|
||||
index.setPromotionMap(goodsCurrentPromotionMap);
|
||||
esGoodsIndices.add(index);
|
||||
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
|
||||
}
|
||||
//初始化商品索引
|
||||
esGoodsIndexService.initIndex(esGoodsIndices);
|
||||
} catch (Exception e) {
|
||||
cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), null);
|
||||
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
|
||||
log.error("初始化索引异常", e);
|
||||
}
|
||||
});
|
||||
esGoodsIndexService.init();
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
||||
@GetMapping("/progress")
|
||||
public ResultMessage<Map<String, Integer>> getProgress() {
|
||||
try {
|
||||
Map<String, Integer> map = (Map<String, Integer>) cache.get(CachePrefix.INIT_INDEX_PROCESS.getPrefix());
|
||||
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
|
||||
map.put("flag", Boolean.TRUE.equals(flag) ? 1 : 0);
|
||||
return ResultUtil.data(map);
|
||||
} catch (Exception e) {
|
||||
return ResultUtil.data(null);
|
||||
}
|
||||
return ResultUtil.data(esGoodsIndexService.getProgress());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ spring:
|
||||
props:
|
||||
#是否打印逻辑SQL语句和实际SQL语句,建议调试时打印,在生产环境关闭
|
||||
sql:
|
||||
show: false
|
||||
show: true
|
||||
|
||||
# 忽略鉴权url
|
||||
ignored:
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.lili.test.elasticsearch;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.lili.cache.Cache;
|
||||
import cn.lili.common.vo.PageVO;
|
||||
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
||||
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
|
||||
@@ -22,7 +23,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.elasticsearch.core.SearchPage;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@@ -52,7 +52,7 @@ class EsTest {
|
||||
private GoodsSkuService goodsSkuService;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
private Cache cache;
|
||||
|
||||
@Autowired
|
||||
private PromotionService promotionService;
|
||||
@@ -161,7 +161,7 @@ class EsTest {
|
||||
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index);
|
||||
index.setPromotionMap(goodsCurrentPromotionMap);
|
||||
esGoodsIndices.add(index);
|
||||
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
|
||||
cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity());
|
||||
}
|
||||
esGoodsIndexService.initIndex(esGoodsIndices);
|
||||
Assertions.assertTrue(true);
|
||||
|
||||
Reference in New Issue
Block a user