From 48146edee9ad8fe7074388001d7becb5b31a6c45 Mon Sep 17 00:00:00 2001 From: paulGao Date: Thu, 12 Aug 2021 02:16:20 +0800 Subject: [PATCH 1/2] add elasticsearch progress --- .../main/java/cn/lili/cache/CachePrefix.java | 10 ++++ .../java/cn/lili/common/enums/ResultCode.java | 4 +- .../BaseElasticsearchService.java | 3 + .../search/entity/dto/EsGoodsSearchDTO.java | 3 + .../serviceimpl/EsGoodsIndexServiceImpl.java | 38 ++++++++++--- .../serviceimpl/EsGoodsSearchServiceImpl.java | 3 + .../other/ElasticsearchController.java | 56 +++++++++++++------ 7 files changed, 92 insertions(+), 25 deletions(-) diff --git a/framework/src/main/java/cn/lili/cache/CachePrefix.java b/framework/src/main/java/cn/lili/cache/CachePrefix.java index b867154e..40ca08c7 100644 --- a/framework/src/main/java/cn/lili/cache/CachePrefix.java +++ b/framework/src/main/java/cn/lili/cache/CachePrefix.java @@ -455,6 +455,16 @@ public enum CachePrefix { */ ARTICLE_CACHE, + /** + * 初始化索引 + */ + INIT_INDEX_PROCESS, + + /** + * 初始化索引标示 + */ + INIT_INDEX_FLAG, + /** * 店铺分类 */ diff --git a/framework/src/main/java/cn/lili/common/enums/ResultCode.java b/framework/src/main/java/cn/lili/common/enums/ResultCode.java index 5b364610..82d4ac10 100644 --- a/framework/src/main/java/cn/lili/common/enums/ResultCode.java +++ b/framework/src/main/java/cn/lili/common/enums/ResultCode.java @@ -438,8 +438,8 @@ public enum ResultCode { CUSTOM_WORDS_EXIST_ERROR(90000, "当前自定义分词已存在!"), CUSTOM_WORDS_NOT_EXIST_ERROR(90001, "当前自定义分词不存在!"), CUSTOM_WORDS_SECRET_KEY_ERROR(90002, "秘钥验证失败!"), - CONNECT_NOT_EXIST(90000, "登录方式不存在"), - + CONNECT_NOT_EXIST(90000, "登录方式不存在!"), + ELASTICSEARCH_INDEX_INIT_ERROR(90003, "索引初始化失败!"), ; private final Integer code; diff --git a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java index dc04ee94..a3b1f14d 100644 --- a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java +++ b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java @@ -253,6 +253,9 @@ public abstract class BaseElasticsearchService { " }\n" + " }\n" + " },\n" + + " \"recommend\": {\n" + + " \"type\": \"boolean\"\n" + + " },\n" + " \"selfOperated\": {\n" + " \"type\": \"boolean\"\n" + " },\n" + diff --git a/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java b/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java index c9f046bd..564d0ecd 100644 --- a/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java +++ b/framework/src/main/java/cn/lili/modules/search/entity/dto/EsGoodsSearchDTO.java @@ -23,6 +23,9 @@ public class EsGoodsSearchDTO { @ApiModelProperty(value = "品牌,可以多选 品牌Id@品牌Id@品牌Id") private String brandId; + @ApiModelProperty("是否为推荐商品") + private Boolean recommend; + @ApiModelProperty(value = "价格", example = "10_30") private String price; diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index 768b0362..c3c889f3 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -6,18 +6,20 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.pinyin.PinyinUtil; +import cn.lili.cache.Cache; +import cn.lili.cache.CachePrefix; +import cn.lili.common.enums.PromotionTypeEnum; import cn.lili.elasticsearch.BaseElasticsearchService; import cn.lili.elasticsearch.EsSuffix; import cn.lili.elasticsearch.config.ElasticsearchProperties; -import cn.lili.modules.goods.entity.dto.GoodsParamsDTO; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.dos.GoodsWords; +import cn.lili.modules.goods.entity.dto.GoodsParamsDTO; import cn.lili.modules.goods.entity.enums.GoodsWordsTypeEnum; import cn.lili.modules.goods.service.GoodsWordsService; import cn.lili.modules.promotion.entity.dos.PromotionGoods; import cn.lili.modules.promotion.entity.dto.BasePromotion; import cn.lili.modules.promotion.entity.enums.PromotionStatusEnum; -import cn.lili.common.enums.PromotionTypeEnum; import cn.lili.modules.promotion.service.PromotionService; import cn.lili.modules.search.entity.dos.EsGoodsAttribute; import cn.lili.modules.search.entity.dos.EsGoodsIndex; @@ -36,7 +38,6 @@ import org.elasticsearch.search.SearchHit; import org.mybatis.spring.MyBatisSystemException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @@ -65,7 +66,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements @Autowired private PromotionService promotionService; @Autowired - private ElasticsearchRestTemplate elasticsearchRestTemplate; + private Cache cache; @Override public void addIndex(EsGoodsIndex goods) { @@ -152,6 +153,9 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements @Override public void initIndex(List goodsIndexList) { + if (goodsIndexList == null || goodsIndexList.isEmpty()) { + return; + } //索引名称拼接 String indexName = elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME; @@ -165,12 +169,32 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements //如果索引不存在,则创建索引 createIndexRequest(indexName); - if (goodsIndexList != null && !goodsIndexList.isEmpty()) { + Map resultMap = new HashMap<>(); + final String KEY_SUCCESS = "success"; + final String KEY_FAIL = "fail"; + final String KEY_PROCESSED = "processed"; + resultMap.put("total", goodsIndexList.size()); + resultMap.put(KEY_SUCCESS, 0); + resultMap.put(KEY_FAIL, 0); + resultMap.put(KEY_PROCESSED, 0); + cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix() + "", resultMap); + cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), true); + if (!goodsIndexList.isEmpty()) { goodsIndexRepository.deleteAll(); for (EsGoodsIndex goodsIndex : goodsIndexList) { - addIndex(goodsIndex); + try { + addIndex(goodsIndex); + resultMap.put(KEY_SUCCESS, resultMap.get(KEY_SUCCESS) + 1); + } catch (Exception e) { + log.error("商品{}生成索引错误!", goodsIndex); + resultMap.put(KEY_FAIL, resultMap.get(KEY_FAIL) + 1); + } + resultMap.put(KEY_PROCESSED, resultMap.get(KEY_PROCESSED) + 1); + cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), resultMap); } } + cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), resultMap); + cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false); } @Override @@ -440,7 +464,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements } } //移除促销信息 - promotionMap.keySet().removeAll(removeKeys); + removeKeys.forEach(promotionMap.keySet()::remove); } } diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java index a4aea352..fa48b879 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java @@ -342,6 +342,9 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService { String[] brands = searchDTO.getBrandId().split("@"); filterBuilder.must(QueryBuilders.termsQuery("brandId", brands)); } + if (searchDTO.getRecommend() != null) { + filterBuilder.filter(QueryBuilders.termQuery("storeId", searchDTO.getRecommend())); + } //规格项判定 if (searchDTO.getNameIds() != null && !searchDTO.getNameIds().isEmpty()) { filterBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.termsQuery("attrList.nameId", searchDTO.getNameIds()), ScoreMode.None)); diff --git a/manager-api/src/main/java/cn/lili/controller/other/ElasticsearchController.java b/manager-api/src/main/java/cn/lili/controller/other/ElasticsearchController.java index e5d9ba79..3455d199 100644 --- a/manager-api/src/main/java/cn/lili/controller/other/ElasticsearchController.java +++ b/manager-api/src/main/java/cn/lili/controller/other/ElasticsearchController.java @@ -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 queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(GoodsSku::getIsAuth, GoodsAuthEnum.PASS.name()); - queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name()); + @Autowired + private Cache cache; - List list = goodsSkuService.list(queryWrapper); - List esGoodsIndices = new ArrayList<>(); - String goodsId = null; - //库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量 - for (GoodsSku goodsSku : list) { - goodsId = goodsSku.getGoodsId(); - Goods goods = goodsService.getById(goodsId); + @GetMapping + public ResultMessage 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 queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(GoodsSku::getIsAuth, GoodsAuthEnum.PASS.name()); + queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name()); + + List list = goodsSkuService.list(queryWrapper); + List 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 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> getProgress() { + Map map = (Map) 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); } } From ee77ee673151f9cf0ae312cc64c2a0a34a86ffe1 Mon Sep 17 00:00:00 2001 From: Chopper Date: Thu, 12 Aug 2021 17:30:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8F=98=E5=8A=A8=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E4=B8=BA0=EF=BC=8C=E5=88=99=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/aop/interceptor/PointLogInterceptor.java | 8 ++++++-- .../modules/member/entity/dos/MemberPointsHistory.java | 4 +--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/member/entity/aop/interceptor/PointLogInterceptor.java b/framework/src/main/java/cn/lili/modules/member/entity/aop/interceptor/PointLogInterceptor.java index b378e1c3..45a56040 100644 --- a/framework/src/main/java/cn/lili/modules/member/entity/aop/interceptor/PointLogInterceptor.java +++ b/framework/src/main/java/cn/lili/modules/member/entity/aop/interceptor/PointLogInterceptor.java @@ -1,6 +1,5 @@ package cn.lili.modules.member.entity.aop.interceptor; -import cn.lili.common.utils.CurrencyUtil; import cn.lili.modules.member.entity.dos.Member; import cn.lili.modules.member.entity.dos.MemberPointsHistory; import cn.lili.modules.member.entity.enums.PointTypeEnum; @@ -45,11 +44,16 @@ public class PointLogInterceptor { if (obj[1] != null) { type = obj[1].toString(); } - //会员ID + // 会员ID String memberId = ""; if (obj[2] != null) { memberId = obj[2].toString(); } + // 变动积分为0,则直接返回 + if (point == 0) { + return; + } + //根据会员id查询会员信息 Member member = memberService.getById(memberId); if (member != null) { diff --git a/framework/src/main/java/cn/lili/modules/member/entity/dos/MemberPointsHistory.java b/framework/src/main/java/cn/lili/modules/member/entity/dos/MemberPointsHistory.java index 481f1add..8728889e 100644 --- a/framework/src/main/java/cn/lili/modules/member/entity/dos/MemberPointsHistory.java +++ b/framework/src/main/java/cn/lili/modules/member/entity/dos/MemberPointsHistory.java @@ -17,8 +17,6 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; import java.util.Date; /** @@ -50,7 +48,7 @@ public class MemberPointsHistory { @CreatedDate @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") - @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @TableField(fill = FieldFill.INSERT) @ApiModelProperty(value = "创建时间", hidden = true) private Date createTime;