add elasticsearch progress

This commit is contained in:
paulGao
2021-08-12 02:16:20 +08:00
parent 69d712c4b1
commit 48146edee9
7 changed files with 92 additions and 25 deletions

View File

@@ -1,6 +1,12 @@
package cn.lili.controller.other;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
@@ -15,6 +21,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -50,20 +57,28 @@ public class ElasticsearchController {
@Autowired
private PromotionService promotionService;
@GetMapping
public void init() {
//查询商品信息
LambdaQueryWrapper<GoodsSku> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(GoodsSku::getIsAuth, GoodsAuthEnum.PASS.name());
queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name());
@Autowired
private Cache cache;
List<GoodsSku> list = goodsSkuService.list(queryWrapper);
List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
String goodsId = null;
//库存锁是在redis做的所以生成索引同时更新一下redis中的库存数量
for (GoodsSku goodsSku : list) {
goodsId = goodsSku.getGoodsId();
Goods goods = goodsService.getById(goodsId);
@GetMapping
public ResultMessage<String> init() {
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
if (Boolean.TRUE.equals(flag)) {
return ResultUtil.error(100000, "当前有任务在执行");
}
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
ThreadUtil.execAsync(() -> {
//查询商品信息
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);
@@ -73,9 +88,18 @@ public class ElasticsearchController {
index.setPromotionMap(goodsCurrentPromotionMap);
esGoodsIndices.add(index);
stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString());
}
//初始化商品索引
esGoodsIndexService.initIndex(esGoodsIndices);
});
return ResultUtil.success();
}
}
//初始化商品索引
esGoodsIndexService.initIndex(esGoodsIndices);
@GetMapping("/progress")
public ResultMessage<Map<String, Integer>> getProgress() {
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);
}
}