优化初始化商品索引,增加编辑商品时,保留原商品的销量、好评率和评价量

This commit is contained in:
paulGao 2022-06-09 16:43:16 +08:00
parent d14763d52c
commit 4316fa8953
3 changed files with 32 additions and 8 deletions

View File

@ -14,6 +14,7 @@ import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsGallery;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
@ -166,8 +167,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
} else {
skuList = new ArrayList<>();
for (Map<String, Object> map : goodsOperationDTO.getSkuList()) {
GoodsSku sku = null;
sku = GoodsSkuBuilder.build(goods, map, goodsOperationDTO);
GoodsSku sku = GoodsSkuBuilder.build(goods, map, goodsOperationDTO);
renderGoodsSku(sku, goodsOperationDTO);
skuList.add(sku);
//如果商品状态值不对则es索引移除
@ -636,6 +636,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
// 商品销售模式渲染器
salesModelRenders.stream().filter(i -> i.getSalesMode().equals(goodsOperationDTO.getSalesModel())).findFirst().ifPresent(i -> i.renderBatch(goodsSkuList, goodsOperationDTO));
for (GoodsSku goodsSku : goodsSkuList) {
extendOldSkuValue(goodsSku);
this.renderImages(goodsSku);
}
}
@ -647,22 +648,43 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
* @param goodsOperationDTO 商品操作DTO
*/
void renderGoodsSku(GoodsSku goodsSku, GoodsOperationDTO goodsOperationDTO) {
extendOldSkuValue(goodsSku);
// 商品销售模式渲染器
salesModelRenders.stream().filter(i -> i.getSalesMode().equals(goodsOperationDTO.getSalesModel())).findFirst().ifPresent(i -> i.renderSingle(goodsSku, goodsOperationDTO));
this.renderImages(goodsSku);
}
/**
* 将原sku的一些不会直接传递的值放到新的sku中
*
* @param goodsSku 商品sku
*/
private void extendOldSkuValue(GoodsSku goodsSku) {
if (CharSequenceUtil.isNotEmpty(goodsSku.getGoodsId())) {
GoodsSku oldSku = this.getGoodsSkuByIdFromCache(goodsSku.getId());
if (oldSku != null) {
goodsSku.setCommentNum(oldSku.getCommentNum());
goodsSku.setViewCount(oldSku.getViewCount());
goodsSku.setBuyCount(oldSku.getBuyCount());
goodsSku.setGrade(oldSku.getGrade());
}
}
}
/**
* 渲染sku图片
*
* @param goodsSku
* @param goodsSku sku
*/
void renderImages(GoodsSku goodsSku) {
JSONObject jsonObject = JSONUtil.parseObj(goodsSku.getSpecs());
List<Map<String, String>> images = jsonObject.get("images", List.class);
if (images != null && !images.isEmpty()) {
goodsSku.setThumbnail(goodsGalleryService.getGoodsGallery(images.get(0).get("url")).getThumbnail());
goodsSku.setSmall(goodsGalleryService.getGoodsGallery(images.get(0).get("url")).getSmall());
GoodsGallery goodsGallery = goodsGalleryService.getGoodsGallery(images.get(0).get("url"));
goodsSku.setBig(goodsGallery.getOriginal());
goodsSku.setOriginal(goodsGallery.getOriginal());
goodsSku.setThumbnail(goodsGallery.getThumbnail());
goodsSku.setSmall(goodsGallery.getSmall());
}
}

View File

@ -28,7 +28,7 @@ import java.util.Map;
* @author paulG
**/
@Data
@Document(indexName = "#{@elasticsearchProperties.indexPrefix}_" + EsSuffix.GOODS_INDEX_NAME)
@Document(indexName = "#{@elasticsearchProperties.indexPrefix}_" + EsSuffix.GOODS_INDEX_NAME, createIndex = false)
@ToString
@NoArgsConstructor
@Accessors(chain = true)
@ -215,6 +215,7 @@ public class EsGoodsIndex implements Serializable {
/**
* 销售模式
*
* @see cn.lili.modules.goods.entity.enums.GoodsSalesModeEnum
*/
@Field(type = FieldType.Text)
@ -316,7 +317,7 @@ public class EsGoodsIndex implements Serializable {
/**
* 参数索引增加
*
* @param sku 商品sku信息
* @param sku 商品sku信息
* @param goodsParamDTOS 商品参数信息
*/
public EsGoodsIndex(GoodsSku sku, List<GoodsParamsDTO> goodsParamDTOS) {

View File

@ -66,6 +66,7 @@ import org.springframework.stereotype.Service;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@ -127,7 +128,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
Boolean flag = (Boolean) cache.get(CachePrefix.INIT_INDEX_FLAG.getPrefix());
//为空则默认写入没有任务
if (flag == null) {
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false);
cache.put(CachePrefix.INIT_INDEX_FLAG.getPrefix(), false, 10L, TimeUnit.MINUTES);
}
//有正在初始化的任务则提示异常
if (Boolean.TRUE.equals(flag)) {