From 41a86ff7dc6bf5f9b5407ef635dd7aebbfcd34b6 Mon Sep 17 00:00:00 2001 From: paulGao Date: Tue, 22 Jun 2021 00:07:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=95=86=E5=93=81=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E5=8F=82=E6=95=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/goods/entity/dos/GoodsParams.java | 12 +++++ .../serviceimpl/GoodsParamsServiceImpl.java | 7 +++ .../serviceimpl/GoodsSkuServiceImpl.java | 43 ++++++----------- .../search/entity/dos/EsGoodsIndex.java | 46 +++++++++++++------ .../search/service/EsGoodsIndexService.java | 4 +- .../serviceimpl/EsGoodsIndexServiceImpl.java | 6 ++- .../other/ElasticsearchController.java | 13 +----- 7 files changed, 73 insertions(+), 58 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsParams.java b/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsParams.java index 54d59883..16045221 100644 --- a/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsParams.java +++ b/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsParams.java @@ -6,10 +6,14 @@ import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import lombok.EqualsAndHashCode; import org.hibernate.validator.constraints.Length; import javax.persistence.Entity; import javax.persistence.Table; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; /** * 商品关联参数 @@ -17,6 +21,7 @@ import javax.persistence.Table; * @author pikachu * @date 2020-02-23 9:14:33 */ +@EqualsAndHashCode(callSuper = true) @Data @Entity @Table(name = "li_goods_params") @@ -52,4 +57,11 @@ public class GoodsParams extends BaseEntity { @Length(max = 100, message = "参数值字符不能大于120") private String paramValue; + @TableField(value = "is_index") + @ApiModelProperty(value = "是否可索引,0 不显示 1 显示", required = true) + @NotNull(message = "是否可索引必选") + @Min(value = 0, message = "是否可索引传值不正确") + @Max(value = 1, message = "是否可索引传值不正确") + private Integer isIndex = 0; + } \ No newline at end of file diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsParamsServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsParamsServiceImpl.java index 729d1c32..4fae7285 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsParamsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsParamsServiceImpl.java @@ -2,11 +2,13 @@ package cn.lili.modules.goods.serviceimpl; import cn.lili.modules.goods.entity.dos.CategoryParameterGroup; import cn.lili.modules.goods.entity.dos.GoodsParams; +import cn.lili.modules.goods.entity.dos.Parameters; import cn.lili.modules.goods.entity.vos.GoodsParamsGroupVO; import cn.lili.modules.goods.entity.vos.GoodsParamsVO; import cn.lili.modules.goods.mapper.GoodsParamsMapper; import cn.lili.modules.goods.service.CategoryParameterGroupService; import cn.lili.modules.goods.service.GoodsParamsService; +import cn.lili.modules.goods.service.ParametersService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -36,6 +38,9 @@ public class GoodsParamsServiceImpl extends ServiceImpl paramList, String goodsId) { //先删除现有商品参数 @@ -43,10 +48,12 @@ public class GoodsParamsServiceImpl extends ServiceImpl i @Override public void add(List> skuList, Goods goods) { // 检查是否需要生成索引 - boolean needIndex = checkNeedIndex(goods); List newSkuList; // 如果有规格 if (skuList != null && !skuList.isEmpty()) { // 添加商品sku - newSkuList = this.addGoodsSku(skuList, goods, needIndex); + newSkuList = this.addGoodsSku(skuList, goods); } else { throw new ServiceException("规格必须要有一个!"); } @@ -103,23 +102,8 @@ public class GoodsSkuServiceImpl extends ServiceImpl i generateEsCheck(goods); } - private boolean checkNeedIndex(Goods goods) { - if (goods.getParams() != null && !goods.getParams().isEmpty()) { - List goodsParams = JSONUtil.toList(goods.getParams(), GoodsParams.class); - for (GoodsParams goodsParam : goodsParams) { - Parameters parameters = parametersService.getById(goodsParam.getParamId()); - if (parameters.getIsIndex() == 1) { - return true; - } - } - } - return false; - } - @Override public void update(List> skuList, Goods goods, Boolean regeneratorSkuFlag) { - // 检查是否需要生成索引 - boolean needIndex = checkNeedIndex(goods); // 是否存在规格 if (skuList == null || skuList.isEmpty()) { throw new ServiceException("规格必须要有一个!"); @@ -139,7 +123,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i //删除sku相册 goodsGalleryService.removeByIds(oldSkuIds); // 添加商品sku - newSkuList = this.addGoodsSku(skuList, goods, needIndex); + newSkuList = this.addGoodsSku(skuList, goods); //发送mq消息 String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.SKU_DELETE.name(); @@ -161,9 +145,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i this.updateBatchById(newSkuList); } this.updateStock(newSkuList); - if (Boolean.TRUE.equals(needIndex)) { - generateEsCheck(goods); - } + generateEsCheck(goods); } /** @@ -206,9 +188,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl i Map map = new HashMap<>(); GoodsSku goodsSku = this.getGoodsSkuByIdFromCache(skuId); + GoodsVO goodsVO = goodsService.getGoodsVO(goodsId); //如果规格为空则使用商品ID进行查询 if (goodsSku == null) { - GoodsVO goodsVO = goodsService.getGoodsVO(goodsId); skuId = goodsVO.getSkuList().get(0).getId(); goodsSku = this.getGoodsSkuByIdFromCache(skuId); //如果使用商品ID无法查询SKU则返回错误 @@ -219,8 +201,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i // 获取当前商品的索引信息 EsGoodsIndex goodsIndex = goodsIndexService.findById(skuId); if (goodsIndex == null) { - goodsIndex = new EsGoodsIndex(goodsSku); - goodsIndex.setPromotionMap(promotionService.getGoodsCurrentPromotionMap(goodsIndex)); + goodsIndex = goodsIndexService.resetEsGoodsIndex(goodsSku, goodsVO.getGoodsParamsList()); } //商品规格 GoodsSkuVO goodsSkuDetail = this.getGoodsSkuVO(goodsSku); @@ -456,6 +437,10 @@ public class GoodsSkuServiceImpl extends ServiceImpl i for (GoodsSku goodsSku : goodsSkuList) { EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId()); EsGoodsIndex goodsIndex = new EsGoodsIndex(goodsSku); + if (goods.getParams() != null && !goods.getParams().isEmpty()) { + List goodsParams = JSONUtil.toList(goods.getParams(), GoodsParams.class); + goodsIndex = new EsGoodsIndex(goodsSku, goodsParams); + } //如果商品库存不为0,并且es中有数据 if (goodsSku.getQuantity() > 0 && esGoodsOld == null) { goodsIndexService.addIndex(goodsIndex); @@ -501,7 +486,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i * @param skuList sku列表 * @param goods 商品信息 */ - private List addGoodsSku(List> skuList, Goods goods, Boolean needIndex) { + private List addGoodsSku(List> skuList, Goods goods) { List skus = new ArrayList<>(); List goodsIndices = new ArrayList<>(); for (Map skuVO : skuList) { @@ -517,11 +502,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl i stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); } this.saveBatch(skus); - if (Boolean.TRUE.equals(needIndex)) { - String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(); - //发送mq消息 - rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIndices), RocketmqSendCallbackBuilder.commonCallback()); - } + String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(); + //发送mq消息 + rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(goodsIndices), RocketmqSendCallbackBuilder.commonCallback()); return skus; } diff --git a/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java b/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java index a1baae33..07e5d6c2 100644 --- a/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java +++ b/framework/src/main/java/cn/lili/modules/search/entity/dos/EsGoodsIndex.java @@ -1,9 +1,11 @@ package cn.lili.modules.search.entity.dos; +import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import cn.lili.common.elasticsearch.EsSuffix; import cn.lili.common.utils.StringUtils; +import cn.lili.modules.goods.entity.dos.GoodsParams; import cn.lili.modules.goods.entity.dos.GoodsSku; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; @@ -275,22 +277,38 @@ public class EsGoodsIndex implements Serializable { this.intro = sku.getIntro(); this.grade = sku.getGrade(); this.releaseTime = new Date(); - if (StringUtils.isNotEmpty(sku.getSpecs())) { - List attributes = new ArrayList<>(); - JSONObject jsonObject = JSONUtil.parseObj(sku.getSpecs()); - for (Map.Entry entry : jsonObject.entrySet()) { - if (!entry.getKey().equals("images")) { - EsGoodsAttribute attribute = new EsGoodsAttribute(); - attribute.setType(0); - attribute.setName(entry.getKey()); - attribute.setValue(entry.getValue().toString()); - attributes.add(attribute); - } - } - this.attrList = attributes; - } +// if (CharSequenceUtil.isNotEmpty(sku.getSpecs())) { +// List attributes = new ArrayList<>(); +// JSONObject jsonObject = JSONUtil.parseObj(sku.getSpecs()); +// for (Map.Entry entry : jsonObject.entrySet()) { +// if (!entry.getKey().equals("images")) { +// EsGoodsAttribute attribute = new EsGoodsAttribute(); +// attribute.setType(1); +// attribute.setName(entry.getKey()); +// attribute.setValue(entry.getValue().toString()); +// attributes.add(attribute); +// } +// } +// this.attrList = attributes; +// } } + } + public EsGoodsIndex(GoodsSku sku, List goodsParams) { + this(sku); + if (goodsParams != null && !goodsParams.isEmpty()) { + List attributes = new ArrayList<>(); + for (GoodsParams goodsParam : goodsParams) { + EsGoodsAttribute attribute = new EsGoodsAttribute(); + if (goodsParam.getIsIndex() == 1) { + attribute.setType(1); + attribute.setName(goodsParam.getParamName()); + attribute.setValue(goodsParam.getParamValue()); + attributes.add(attribute); + } + } + this.attrList = attributes; + } } public void setGoodsSku(GoodsSku sku) { 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 6f737c1d..fbc7bc17 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 @@ -1,5 +1,6 @@ package cn.lili.modules.search.service; +import cn.lili.modules.goods.entity.dos.GoodsParams; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.promotion.entity.dos.PromotionGoods; import cn.lili.modules.promotion.entity.dto.BasePromotion; @@ -145,7 +146,8 @@ public interface EsGoodsIndexService { * 重置当前商品索引 * * @param goodsSku 商品sku信息 + * @param goodsParams 商品参数 * @return 商品索引 */ - EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku); + EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku, List goodsParams); } 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 d0df2bf7..082b3616 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 @@ -8,6 +8,7 @@ import cn.hutool.extra.pinyin.PinyinUtil; import cn.lili.common.elasticsearch.BaseElasticsearchService; import cn.lili.common.elasticsearch.EsSuffix; import cn.lili.config.elasticsearch.ElasticsearchProperties; +import cn.lili.modules.goods.entity.dos.GoodsParams; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.dos.GoodsWords; import cn.lili.modules.goods.entity.enums.GoodsWordsTypeEnum; @@ -367,11 +368,12 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements * 重置当前商品索引 * * @param goodsSku 商品sku信息 + * @param goodsParams 商品参数 * @return 商品索引 */ @Override - public EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku) { - EsGoodsIndex index = new EsGoodsIndex(goodsSku); + public EsGoodsIndex resetEsGoodsIndex(GoodsSku goodsSku, List goodsParams) { + EsGoodsIndex index = new EsGoodsIndex(goodsSku, goodsParams); //获取活动信息 Map goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index); //写入促销信息 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 7c2141e8..5ab07dbd 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 @@ -68,23 +68,14 @@ public class ElasticsearchController { String goodsId = null; //库存锁是在redis做的,所以生成索引,同时更新一下redis中的库存数量 for (GoodsSku goodsSku : list) { - boolean needIndex = false; if (goodsId == null || !goodsId.equals(goodsSku.getGoodsId())) { goodsId = goodsSku.getGoodsId(); Goods goods = goodsService.getById(goodsId); + EsGoodsIndex index = new EsGoodsIndex(goodsSku); if (goods.getParams() != null && !goods.getParams().isEmpty()) { List goodsParams = JSONUtil.toList(goods.getParams(), GoodsParams.class); - for (GoodsParams goodsParam : goodsParams) { - Parameters parameters = parametersService.getById(goodsParam.getParamId()); - if (parameters.getIsIndex() == 1) { - needIndex = true; - break; - } - } + index = new EsGoodsIndex(goodsSku, goodsParams); } - } - if (Boolean.TRUE.equals(needIndex)) { - EsGoodsIndex index = new EsGoodsIndex(goodsSku); Map goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index); index.setPromotionMap(goodsCurrentPromotionMap); esGoodsIndices.add(index);