From 55c9a63301c92d6df5c877ec6543fa9836609ae5 Mon Sep 17 00:00:00 2001 From: paulGao Date: Thu, 14 Oct 2021 15:37:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=95=86=E5=93=81=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E6=9B=B4=E6=96=B0=E5=AD=97=E6=AE=B5=E6=96=B9=E6=B3=95?= =?UTF-8?q?;=20=E4=BF=AE=E5=A4=8D=E5=BA=97=E9=93=BA=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8E=EF=BC=8C=E5=95=86=E5=93=81=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E7=9A=84=E5=BA=97=E9=93=BA=E4=BF=A1=E6=81=AF=E4=B8=8D?= =?UTF-8?q?=E4=BC=9A=E9=9A=8F=E4=B9=8B=E6=9B=B4=E6=94=B9;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lili/listener/GoodsMessageListener.java | 21 ++++-- .../search/service/EsGoodsIndexService.java | 16 +++- .../serviceimpl/EsGoodsIndexServiceImpl.java | 58 +++++++++++++-- .../modules/search/utils/EsIndexUtil.java | 45 ++++++++++++ .../store/service/StoreDetailService.java | 11 ++- .../serviceimpl/StoreDetailServiceImpl.java | 51 ++++++++++++- .../store/serviceimpl/StoreServiceImpl.java | 24 +++--- .../cn/lili/rocketmq/tags/GoodsTagsEnum.java | 5 ++ .../other/ElasticsearchController.java | 73 ++++++++++--------- .../store/StoreManagerController.java | 4 +- 10 files changed, 246 insertions(+), 62 deletions(-) create mode 100644 framework/src/main/java/cn/lili/modules/search/utils/EsIndexUtil.java diff --git a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java index ab36fce2..845967f0 100644 --- a/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java +++ b/consumer/src/main/java/cn/lili/listener/GoodsMessageListener.java @@ -2,6 +2,7 @@ package cn.lili.listener; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import cn.lili.event.GoodsCommentCompleteEvent; import cn.lili.modules.distribution.entity.dos.DistributionGoods; @@ -42,6 +43,7 @@ import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; /** * 商品消息 @@ -137,16 +139,25 @@ public class GoodsMessageListener implements RocketMQListener { case UPDATE_GOODS_INDEX: try { String goodsIdsJsonStr = new String(messageExt.getBody()); - List goodsList = new ArrayList<>(); - for (String goodsId : JSONUtil.toList(goodsIdsJsonStr, String.class)) { - Goods goods = goodsService.getById(goodsId); - goodsList.add(goods); - } + List goodsList = goodsService.list(new LambdaQueryWrapper().in(Goods::getId, JSONUtil.toList(goodsIdsJsonStr, String.class))); this.updateGoodsIndex(goodsList); } catch (Exception e) { log.error("更新商品索引事件执行异常,商品信息 {}", new String(messageExt.getBody())); } break; + case UPDATE_GOODS_INDEX_FIELD: + try { + String updateIndexFieldsJsonStr = new String(messageExt.getBody()); + JSONObject updateIndexFields = JSONUtil.parseObj(updateIndexFieldsJsonStr); + @SuppressWarnings("unchecked") + Map queryFields = updateIndexFields.get("queryFields", Map.class); + @SuppressWarnings("unchecked") + Map updateFields = updateIndexFields.get("updateFields", Map.class); + goodsIndexService.updateIndex(queryFields, updateFields); + } catch (Exception e) { + log.error("更新商品索引事件执行异常,商品信息 {}", new String(messageExt.getBody())); + } + break; case RESET_GOODS_INDEX: try { String goodsIdsJsonStr = new String(messageExt.getBody()); diff --git a/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java b/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java index 5172d66b..a33c8928 100644 --- a/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java +++ b/framework/src/main/java/cn/lili/modules/search/service/EsGoodsIndexService.java @@ -35,11 +35,19 @@ public interface EsGoodsIndexService { /** * 更新商品索引的的部分属性(只填写更新的字段,不需要更新的字段不要填写) * - * @param id 商品索引id + * @param id 商品索引id * @param goods 更新后的购买数量 */ void updateIndex(String id, EsGoodsIndex goods); + /** + * 更新商品索引的的部分属性 + * + * @param queryFields 查询字段 + * @param updateFields 更新字段 + */ + void updateIndex(Map queryFields, Map updateFields); + /** * 批量商品索引的的属性(ID 必填, 其他字段只填写更新的字段,不需要更新的字段不要填写。) * @@ -105,10 +113,12 @@ public interface EsGoodsIndexService { /** * 删除索引中指定的促销活动id的促销活动 - * @param skuId 商品skuId + * + * @param skuId 商品skuId * @param promotionId 促销活动Id */ void deleteEsGoodsPromotionByPromotionId(String skuId, String promotionId); + /** * 清除所以商品索引的无效促销活动 */ @@ -142,7 +152,7 @@ public interface EsGoodsIndexService { /** * 重置当前商品索引 * - * @param goodsSku 商品sku信息 + * @param goodsSku 商品sku信息 * @param goodsParamDTOS 商品参数 * @return 商品索引 */ 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 32b9c45c..af57ba99 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 @@ -30,11 +30,18 @@ import cn.lili.modules.search.service.EsGoodsSearchService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.extern.slf4j.Slf4j; import org.assertj.core.util.IterableUtil; +import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.AnalyzeResponse; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.TermQueryBuilder; +import org.elasticsearch.index.reindex.BulkByScrollResponse; +import org.elasticsearch.index.reindex.UpdateByQueryRequest; +import org.elasticsearch.script.Script; import org.mybatis.spring.MyBatisSystemException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.SearchHit; @@ -76,11 +83,9 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements @Override public void addIndex(EsGoodsIndex goods) { - //索引名称拼接 - String indexName = elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME; try { //分词器分词 - AnalyzeRequest analyzeRequest = AnalyzeRequest.withIndexAnalyzer(indexName, "ik_max_word", goods.getGoodsName()); + AnalyzeRequest analyzeRequest = AnalyzeRequest.withIndexAnalyzer(getIndexName(), "ik_max_word", goods.getGoodsName()); AnalyzeResponse analyze = client.indices().analyze(analyzeRequest, RequestOptions.DEFAULT); List tokens = analyze.getTokens(); @@ -126,6 +131,29 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements goodsIndexRepository.save(goods); } + /** + * 更新商品索引的的部分属性(只填写更新的字段,不需要更新的字段不要填写) + * + * @param queryFields 查询字段 + * @param updateFields 更新字段 + */ + @Override + public void updateIndex(Map queryFields, Map updateFields) { + UpdateByQueryRequest update = new UpdateByQueryRequest(getIndexName()); + BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); + for (Map.Entry entry : queryFields.entrySet()) { + TermQueryBuilder termQueryBuilder = new TermQueryBuilder(entry.getKey(), entry.getValue()); + queryBuilder.filter(termQueryBuilder); + } + update.setQuery(queryBuilder); + StringBuilder script = new StringBuilder(); + for (Map.Entry entry : updateFields.entrySet()) { + script.append("ctx._source.").append(entry.getKey()).append("=").append("'").append(entry.getValue()).append("'").append(";"); + } + update.setScript(new Script(script.toString())); + client.updateByQueryAsync(update, RequestOptions.DEFAULT, this.actionListener()); + } + /** * 批量商品索引的的属性(ID 必填, 其他字段只填写更新的字段,不需要更新的字段不要填写。) * @@ -135,7 +163,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements public void updateBulkIndex(List goodsIndices) { try { //索引名称拼接 - String indexName = elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME; + String indexName = getIndexName(); BulkRequest request = new BulkRequest(); @@ -178,7 +206,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements return; } //索引名称拼接 - String indexName = elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME; + String indexName = this.getIndexName(); //索引初始化,因为mapping结构问题: //但是如果索引已经自动生成过,这里就不会创建索引,设置mapping,所以这里决定在初始化索引的同时,将已有索引删除,重新创建 @@ -383,7 +411,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements } return promotionMap; } - return null; + return new HashMap<>(); } /** @@ -519,4 +547,22 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements } } + private String getIndexName() { + //索引名称拼接 + return elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME; + } + + private ActionListener actionListener() { + return new ActionListener() { + @Override + public void onResponse(BulkByScrollResponse bulkByScrollResponse) { + log.debug("UpdateByQueryResponse: {}", bulkByScrollResponse); + } + + @Override + public void onFailure(Exception e) { + log.error("UpdateByQueryRequestFailure: ", e); + } + }; + } } diff --git a/framework/src/main/java/cn/lili/modules/search/utils/EsIndexUtil.java b/framework/src/main/java/cn/lili/modules/search/utils/EsIndexUtil.java new file mode 100644 index 00000000..e514f011 --- /dev/null +++ b/framework/src/main/java/cn/lili/modules/search/utils/EsIndexUtil.java @@ -0,0 +1,45 @@ +package cn.lili.modules.search.utils; + +import cn.hutool.core.util.ReflectUtil; +import cn.lili.modules.search.entity.dos.EsGoodsIndex; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +/** + * @author paulG + * @since 2021/10/11 + **/ +public class EsIndexUtil { + + private static final String IGNORE_FIELD = "serialVersionUID,promotionMap,id,goodsId"; + + public static Map getUpdateIndexFieldsMap(EsGoodsIndex queryGoodsIndex, EsGoodsIndex updateGoodsIndex) { + Map queryFieldsMap = new HashMap<>(); + Map updateFieldsMap = new HashMap<>(); + + for (Map.Entry entry : ReflectUtil.getFieldMap(EsGoodsIndex.class).entrySet()) { + Object queryFieldValue = ReflectUtil.getFieldValue(queryGoodsIndex, entry.getValue()); + Object updateFieldValue = ReflectUtil.getFieldValue(updateGoodsIndex, entry.getValue()); + if (queryFieldValue != null && !IGNORE_FIELD.contains(entry.getKey())) { + ReflectUtil.setFieldValue(queryFieldsMap, entry.getValue(), queryFieldValue); + } + if (updateFieldValue != null && !IGNORE_FIELD.contains(entry.getKey())) { + ReflectUtil.setFieldValue(updateFieldsMap, entry.getValue(), updateFieldValue); + } + } + + return getUpdateIndexFieldsMap(queryFieldsMap, updateFieldsMap); + } + + public static Map getUpdateIndexFieldsMap(Map queryFieldsMap, Map updateFieldsMap) { + Map updateIndexMap = new HashMap<>(); + + updateIndexMap.put("queryFields", queryFieldsMap); + updateIndexMap.put("updateFields", updateFieldsMap); + return updateIndexMap; + } + + +} diff --git a/framework/src/main/java/cn/lili/modules/store/service/StoreDetailService.java b/framework/src/main/java/cn/lili/modules/store/service/StoreDetailService.java index cf7c311c..8232dd2f 100644 --- a/framework/src/main/java/cn/lili/modules/store/service/StoreDetailService.java +++ b/framework/src/main/java/cn/lili/modules/store/service/StoreDetailService.java @@ -1,10 +1,12 @@ package cn.lili.modules.store.service; +import cn.lili.modules.store.entity.dos.Store; import cn.lili.modules.store.entity.dos.StoreDetail; import cn.lili.modules.store.entity.dto.StoreAfterSaleAddressDTO; import cn.lili.modules.store.entity.dto.StoreSettingDTO; import cn.lili.modules.store.entity.vos.StoreBasicInfoVO; import cn.lili.modules.store.entity.vos.StoreDetailVO; +import cn.lili.modules.store.entity.vos.StoreManagementCategoryVO; import cn.lili.modules.store.entity.vos.StoreOtherVO; import com.baomidou.mybatisplus.extension.service.IService; @@ -95,7 +97,7 @@ public interface StoreDetailService extends IService { * @param storeId 店铺ID * @return 店铺经营范围 */ - List goodsManagementCategory(String storeId); + List goodsManagementCategory(String storeId); /** * 获取店铺其他信息 @@ -104,4 +106,11 @@ public interface StoreDetailService extends IService { * @return 店铺其他信息 */ StoreOtherVO getStoreOtherVO(String storeId); + + /** + * 更新店铺内所有商品信息 + * + * @param store 店铺信息 + */ + void updateStoreGoodsInfo(Store store); } \ No newline at end of file diff --git a/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreDetailServiceImpl.java b/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreDetailServiceImpl.java index b0c8a2ad..956a8bb9 100644 --- a/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreDetailServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreDetailServiceImpl.java @@ -1,10 +1,18 @@ package cn.lili.modules.store.serviceimpl; +import cn.hutool.core.map.MapUtil; +import cn.hutool.json.JSONUtil; +import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.common.security.AuthUser; import cn.lili.common.security.context.UserContext; import cn.lili.common.utils.BeanUtil; 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.service.CategoryService; +import cn.lili.modules.goods.service.GoodsService; +import cn.lili.modules.goods.service.GoodsSkuService; +import cn.lili.modules.search.utils.EsIndexUtil; import cn.lili.modules.store.entity.dos.Store; import cn.lili.modules.store.entity.dos.StoreDetail; import cn.lili.modules.store.entity.dto.StoreAfterSaleAddressDTO; @@ -16,16 +24,20 @@ import cn.lili.modules.store.entity.vos.StoreOtherVO; import cn.lili.modules.store.mapper.StoreDetailMapper; import cn.lili.modules.store.service.StoreDetailService; import cn.lili.modules.store.service.StoreService; +import cn.lili.rocketmq.RocketmqSendCallbackBuilder; +import cn.lili.rocketmq.tags.GoodsTagsEnum; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -49,6 +61,18 @@ public class StoreDetailServiceImpl extends ServiceImpl() + .eq(Goods::getStoreId, store.getId()) + .set(Goods::getStoreName, store.getStoreName()) + .set(Goods::getSelfOperated, store.getSelfOperated())); + goodsSkuService.update(new LambdaUpdateWrapper() + .eq(GoodsSku::getStoreId, store.getId()) + .set(GoodsSku::getStoreName, store.getStoreName()) + .set(GoodsSku::getSelfOperated, store.getSelfOperated())); + + Map updateIndexFieldsMap = EsIndexUtil.getUpdateIndexFieldsMap( + MapUtil.builder().put("storeId", store.getId()).build(), + MapUtil.builder().put("storeName", store.getStoreName()).put("selfOperated", store.getSelfOperated()).build()); + String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_FIELD.name(); + //发送mq消息 + rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(updateIndexFieldsMap), RocketmqSendCallbackBuilder.commonCallback()); } @Override @@ -118,7 +165,7 @@ public class StoreDetailServiceImpl extends ServiceImpl goodsManagementCategory(String storeId) { //获取顶部分类列表 List categoryList = categoryService.firstCategory(); diff --git a/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreServiceImpl.java b/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreServiceImpl.java index a12892d7..df337b0f 100644 --- a/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/store/serviceimpl/StoreServiceImpl.java @@ -2,12 +2,12 @@ package cn.lili.modules.store.serviceimpl; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; +import cn.hutool.core.text.CharSequenceUtil; import cn.lili.common.enums.ResultCode; import cn.lili.common.exception.ServiceException; import cn.lili.common.security.AuthUser; import cn.lili.common.security.context.UserContext; import cn.lili.common.utils.BeanUtil; -import cn.lili.common.utils.StringUtils; import cn.lili.common.vo.PageVO; import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.enums.GoodsAuthEnum; @@ -39,6 +39,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Objects; import java.util.Optional; /** @@ -89,7 +90,7 @@ public class StoreServiceImpl extends ServiceImpl implements @Override public StoreVO getStoreDetail() { - AuthUser currentUser = UserContext.getCurrentUser(); + AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser()); StoreVO storeVO = this.baseMapper.getStoreDetail(currentUser.getStoreId()); storeVO.setNickName(currentUser.getNickName()); return storeVO; @@ -99,7 +100,7 @@ public class StoreServiceImpl extends ServiceImpl implements public Store add(AdminStoreApplyDTO adminStoreApplyDTO) { //判断店铺名称是否存在 - QueryWrapper queryWrapper = Wrappers.query(); + QueryWrapper queryWrapper = Wrappers.query(); queryWrapper.eq("store_name", adminStoreApplyDTO.getStoreName()); if (this.getOne(queryWrapper) != null) { throw new ServiceException(ResultCode.STORE_NAME_EXIST_ERROR); @@ -111,7 +112,7 @@ public class StoreServiceImpl extends ServiceImpl implements throw new ServiceException(ResultCode.USER_NOT_EXIST); } //判断是否拥有店铺 - if (member.getHaveStore()) { + if (Boolean.TRUE.equals(member.getHaveStore())) { throw new ServiceException(ResultCode.STORE_APPLY_DOUBLE_ERROR); } @@ -138,7 +139,7 @@ public class StoreServiceImpl extends ServiceImpl implements if (storeEditDTO != null) { //判断店铺名是否唯一 Store storeTmp = getOne(new QueryWrapper().eq("store_name", storeEditDTO.getStoreName())); - if (storeTmp != null && !StringUtils.equals(storeTmp.getId(), storeEditDTO.getStoreId())) { + if (storeTmp != null && !CharSequenceUtil.equals(storeTmp.getId(), storeEditDTO.getStoreId())) { throw new ServiceException(ResultCode.STORE_NAME_EXIST_ERROR); } //修改店铺详细信息 @@ -160,7 +161,10 @@ public class StoreServiceImpl extends ServiceImpl implements if (store != null) { BeanUtil.copyProperties(storeEditDTO, store); store.setId(storeEditDTO.getStoreId()); - this.updateById(store); + boolean result = this.updateById(store); + if (result) { + storeDetailService.updateStoreGoodsInfo(store); + } } return store; } @@ -232,7 +236,8 @@ public class StoreServiceImpl extends ServiceImpl implements Store store = getStoreByMember(); //如果没有申请过店铺,新增店铺 if (!Optional.ofNullable(store).isPresent()) { - Member member = memberService.getById(UserContext.getCurrentUser().getId()); + AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser()); + Member member = memberService.getById(authUser.getId()); store = new Store(member); BeanUtil.copyProperties(storeCompanyDTO, store); this.save(store); @@ -336,11 +341,12 @@ public class StoreServiceImpl extends ServiceImpl implements /** * 获取当前登录操作的店铺 * - * @return + * @return 店铺信息 */ private Store getStoreByMember() { + AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser()); LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery(); - lambdaQueryWrapper.eq(Store::getMemberId, UserContext.getCurrentUser().getId()); + lambdaQueryWrapper.eq(Store::getMemberId, authUser.getId()); return this.getOne(lambdaQueryWrapper); } diff --git a/framework/src/main/java/cn/lili/rocketmq/tags/GoodsTagsEnum.java b/framework/src/main/java/cn/lili/rocketmq/tags/GoodsTagsEnum.java index dedf7052..73e3b7d6 100644 --- a/framework/src/main/java/cn/lili/rocketmq/tags/GoodsTagsEnum.java +++ b/framework/src/main/java/cn/lili/rocketmq/tags/GoodsTagsEnum.java @@ -14,6 +14,11 @@ public enum GoodsTagsEnum { * "更新商品索引" */ UPDATE_GOODS_INDEX("更新商品索引"), + + /** + * "更新商品索引部分字段" + */ + UPDATE_GOODS_INDEX_FIELD("更新商品索引部分字段"), /** * "重置商品索引" */ 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 31ce2559..4770c933 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 @@ -90,45 +90,50 @@ public class ElasticsearchController { cache.put(CachePrefix.INIT_INDEX_PROCESS.getPrefix(), null); cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), true); ThreadUtil.execAsync(() -> { - //查询商品信息 - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(GoodsSku::getIsAuth, GoodsAuthEnum.PASS.name()); - queryWrapper.eq(GoodsSku::getMarketEnable, GoodsStatusEnum.UPPER.name()); + try { + //查询商品信息 + 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); - index = new EsGoodsIndex(goodsSku, goodsParamDTOS); - } - if (goods.getCategoryPath() != null) { - List categories = categoryService.listByIdsOrderByLevel(Arrays.asList(goods.getCategoryPath().split(","))); - if (!categories.isEmpty()) { - index.setCategoryNamePath(ArrayUtil.join(categories.stream().map(Category::getName).toArray(), ",")); + 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); + index = new EsGoodsIndex(goodsSku, goodsParamDTOS); } - } - 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 storeGoodsLabels = storeGoodsLabelService.listByStoreIds(Arrays.asList(goods.getStoreCategoryPath().split(","))); - if (!storeGoodsLabels.isEmpty()) { - index.setStoreCategoryNamePath(ArrayUtil.join(storeGoodsLabels.stream().map(StoreGoodsLabel::getLabelName).toArray(), ",")); + if (goods.getCategoryPath() != null) { + List 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 storeGoodsLabels = storeGoodsLabelService.listByStoreIds(Arrays.asList(goods.getStoreCategoryPath().split(","))); + if (!storeGoodsLabels.isEmpty()) { + index.setStoreCategoryNamePath(ArrayUtil.join(storeGoodsLabels.stream().map(StoreGoodsLabel::getLabelName).toArray(), ",")); + } + } + Map goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index); + index.setPromotionMap(goodsCurrentPromotionMap); + esGoodsIndices.add(index); + stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); } - Map 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); } - //初始化商品索引 - esGoodsIndexService.initIndex(esGoodsIndices); }); return ResultUtil.success(); } diff --git a/manager-api/src/main/java/cn/lili/controller/store/StoreManagerController.java b/manager-api/src/main/java/cn/lili/controller/store/StoreManagerController.java index 841d0464..e73b4fd9 100644 --- a/manager-api/src/main/java/cn/lili/controller/store/StoreManagerController.java +++ b/manager-api/src/main/java/cn/lili/controller/store/StoreManagerController.java @@ -3,11 +3,11 @@ package cn.lili.controller.store; import cn.lili.common.enums.ResultUtil; import cn.lili.common.vo.PageVO; import cn.lili.common.vo.ResultMessage; -import cn.lili.modules.goods.entity.vos.CategoryVO; import cn.lili.modules.store.entity.dos.Store; import cn.lili.modules.store.entity.dto.AdminStoreApplyDTO; import cn.lili.modules.store.entity.dto.StoreEditDTO; import cn.lili.modules.store.entity.vos.StoreDetailVO; +import cn.lili.modules.store.entity.vos.StoreManagementCategoryVO; import cn.lili.modules.store.entity.vos.StoreSearchParams; import cn.lili.modules.store.entity.vos.StoreVO; import cn.lili.modules.store.service.StoreDetailService; @@ -112,7 +112,7 @@ public class StoreManagerController { @ApiOperation(value = "查询一级分类列表") @ApiImplicitParam(name = "storeId", value = "店铺id", required = true, dataType = "String", paramType = "path") @GetMapping(value = "/managementCategory/{storeId}") - public ResultMessage> firstCategory(@PathVariable String storeId) { + public ResultMessage> firstCategory(@PathVariable String storeId) { return ResultUtil.data(this.storeDetailService.goodsManagementCategory(storeId)); }