Merge branch 'master' of gitee.com:beijing_hongye_huicheng/lilishop

This commit is contained in:
Chopper 2022-03-31 09:21:23 +08:00
commit 82546eed06
5 changed files with 52 additions and 84 deletions

View File

@ -394,7 +394,7 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
} }
} }
if (goodsIndex.getPromotionMap() == null || goodsIndex.getPromotionMap().isEmpty()) { if (goodsIndex.getOriginPromotionMap() == null || goodsIndex.getOriginPromotionMap().isEmpty()) {
Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsSkuPromotionMap(goodsIndex.getStoreId(), goodsIndex.getId()); Map<String, Object> goodsCurrentPromotionMap = promotionService.getGoodsSkuPromotionMap(goodsIndex.getStoreId(), goodsIndex.getId());
goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(goodsCurrentPromotionMap)); goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(goodsCurrentPromotionMap));
} }

View File

@ -165,14 +165,8 @@ public abstract class BaseElasticsearchService {
" \"type\": \"long\"\n" + " \"type\": \"long\"\n" +
" },\n" + " },\n" +
" \"releaseTime\": {\n" + " \"releaseTime\": {\n" +
" \"type\": \"text\",\n" + " \"type\": \"date\",\n" +
" \"fielddata\": true, \n" + " \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" +
" \"fields\": {\n" +
" \"keyword\": {\n" +
" \"type\": \"keyword\",\n" +
" \"ignore_above\": 256\n" +
" }\n" +
" }\n" +
" },\n" + " },\n" +
" \"categoryPath\": {\n" + " \"categoryPath\": {\n" +
" \"type\": \"text\",\n" + " \"type\": \"text\",\n" +

View File

@ -6,14 +6,12 @@ import cn.lili.elasticsearch.EsSuffix;
import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO; import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.promotion.tools.PromotionTools; import cn.lili.modules.promotion.tools.PromotionTools;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType; import org.springframework.data.elasticsearch.annotations.FieldType;
@ -99,42 +97,42 @@ public class EsGoodsIndex implements Serializable {
/** /**
* 品牌id * 品牌id
*/ */
@Field(type = FieldType.Integer, fielddata = true) @Field(type = FieldType.Text, fielddata = true)
@ApiModelProperty("品牌id") @ApiModelProperty("品牌id")
private String brandId; private String brandId;
/** /**
* 品牌名称 * 品牌名称
*/ */
@Field(type = FieldType.Keyword, fielddata = true) @Field(type = FieldType.Text, fielddata = true)
@ApiModelProperty("品牌名称") @ApiModelProperty("品牌名称")
private String brandName; private String brandName;
/** /**
* 品牌图片地址 * 品牌图片地址
*/ */
@Field(type = FieldType.Keyword, fielddata = true) @Field(type = FieldType.Text, fielddata = true)
@ApiModelProperty("品牌图片地址") @ApiModelProperty("品牌图片地址")
private String brandUrl; private String brandUrl;
/** /**
* 分类path * 分类path
*/ */
@Field(type = FieldType.Keyword) @Field(type = FieldType.Text, fielddata = true)
@ApiModelProperty("分类path") @ApiModelProperty("分类path")
private String categoryPath; private String categoryPath;
/** /**
* 分类名称path * 分类名称path
*/ */
@Field(type = FieldType.Keyword) @Field(type = FieldType.Text, fielddata = true)
@ApiModelProperty("分类名称path") @ApiModelProperty("分类名称path")
private String categoryNamePath; private String categoryNamePath;
/** /**
* 店铺分类id * 店铺分类id
*/ */
@Field(type = FieldType.Keyword) @Field(type = FieldType.Text, fielddata = true)
@ApiModelProperty("店铺分类id") @ApiModelProperty("店铺分类id")
private String storeCategoryPath; private String storeCategoryPath;
@ -251,9 +249,8 @@ public class EsGoodsIndex implements Serializable {
private String goodsVideo; private String goodsVideo;
@ApiModelProperty("商品发布时间") @ApiModelProperty("商品发布时间")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @Field(type = FieldType.Date)
@Field(type = FieldType.Date, format = DateFormat.basic_date_time) private Long releaseTime;
private Date releaseTime;
/** /**
* @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum * @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
@ -309,7 +306,7 @@ public class EsGoodsIndex implements Serializable {
this.grade = sku.getGrade(); this.grade = sku.getGrade();
this.recommend = sku.getRecommend(); this.recommend = sku.getRecommend();
this.goodsType = sku.getGoodsType(); this.goodsType = sku.getGoodsType();
this.releaseTime = new Date(); this.releaseTime = new Date().getTime();
} }
} }
@ -374,10 +371,14 @@ public class EsGoodsIndex implements Serializable {
this.authFlag = sku.getAuthFlag(); this.authFlag = sku.getAuthFlag();
this.intro = sku.getIntro(); this.intro = sku.getIntro();
this.grade = sku.getGrade(); this.grade = sku.getGrade();
this.releaseTime = new Date(); this.releaseTime = new Date().getTime();
} }
} }
public Map<String, Object> getOriginPromotionMap() {
return JSONUtil.parseObj(this.promotionMapJson);
}
public Map<String, Object> getPromotionMap() { public Map<String, Object> getPromotionMap() {
return PromotionTools.filterInvalidPromotionsMap(JSONUtil.parseObj(this.promotionMapJson)); return PromotionTools.filterInvalidPromotionsMap(JSONUtil.parseObj(this.promotionMapJson));
} }

View File

@ -131,14 +131,6 @@ public interface EsGoodsIndexService {
*/ */
void updateEsGoodsIndexAllByList(BasePromotions promotion, String key); void updateEsGoodsIndexAllByList(BasePromotions promotion, String key);
/**
* 删除指定商品的促销信息
*
* @param skuIds skuId列表
* @param promotionType 促销类型
*/
void deleteEsGoodsPromotionIndexByList(List<String> skuIds, PromotionTypeEnum promotionType);
/** /**
* 删除索引中指定的促销活动id的促销活动 * 删除索引中指定的促销活动id的促销活动
* *

View File

@ -277,7 +277,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
UpdateRequest updateRequest = new UpdateRequest(indexName, goodsIndex.getId()); UpdateRequest updateRequest = new UpdateRequest(indexName, goodsIndex.getId());
JSONObject jsonObject = JSONUtil.parseObj(goodsIndex); JSONObject jsonObject = JSONUtil.parseObj(goodsIndex);
jsonObject.set("releaseTime", goodsIndex.getReleaseTime().getTime()); jsonObject.set("releaseTime", goodsIndex.getReleaseTime());
updateRequest.doc(jsonObject); updateRequest.doc(jsonObject);
request.add(updateRequest); request.add(updateRequest);
} }
@ -431,45 +431,26 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/ */
@Override @Override
public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) { public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) {
List<EsGoodsIndex> goodsIndices = new ArrayList<>(); ThreadUtil.execAsync(() -> {
//如果storeId不为空则表示是店铺活动 List<EsGoodsIndex> goodsIndices = new ArrayList<>();
if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) { //如果storeId不为空则表示是店铺活动
EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO(); if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) {
searchDTO.setStoreId(promotion.getStoreId()); EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO();
//查询出店铺商品 searchDTO.setStoreId(promotion.getStoreId());
SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(searchDTO, null); //查询出店铺商品
for (SearchHit<EsGoodsIndex> searchHit : esGoodsIndices.getContent()) { SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(searchDTO, null);
goodsIndices.add(searchHit.getContent()); for (SearchHit<EsGoodsIndex> searchHit : esGoodsIndices.getContent()) {
} goodsIndices.add(searchHit.getContent());
} else {
//否则是平台活动
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll();
//查询出全部商品
goodsIndices = new ArrayList<>(IterableUtil.toCollection(all));
}
List<String> skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
}
@Override
public void deleteEsGoodsPromotionIndexByList(List<String> skuIds, PromotionTypeEnum promotionType) {
//批量删除活动索引
for (String skuId : skuIds) {
EsGoodsIndex goodsIndex = findById(skuId);
//商品索引不为空
if (goodsIndex != null) {
Map<String, Object> promotionMap = goodsIndex.getPromotionMap();
if (promotionMap != null && !promotionMap.isEmpty()) {
//如果存在同类型促销活动删除
List<String> collect = promotionMap.keySet().stream().filter(i -> i.contains(promotionType.name())).collect(Collectors.toList());
collect.forEach(promotionMap::remove);
goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(promotionMap));
updateIndex(goodsIndex);
} }
} else { } else {
log.error("更新索引商品促销信息失败skuId 为 【{}】的索引不存在!", skuId); //否则是平台活动
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll();
//查询出全部商品
goodsIndices = new ArrayList<>(IterableUtil.toCollection(all));
} }
} List<String> skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
});
} }
@Override @Override
@ -503,14 +484,16 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/ */
@Override @Override
public void deleteEsGoodsPromotionByPromotionKey(String promotionsKey) { public void deleteEsGoodsPromotionByPromotionKey(String promotionsKey) {
BulkRequest bulkRequest = new BulkRequest(); ThreadUtil.execAsync(() -> {
for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) { BulkRequest bulkRequest = new BulkRequest();
UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey); for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) {
if (updateRequest != null) { UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey);
bulkRequest.add(updateRequest); if (updateRequest != null) {
bulkRequest.add(updateRequest);
}
} }
} this.executeBulkUpdateRequest(bulkRequest);
this.executeBulkUpdateRequest(bulkRequest); });
} }
/** /**
@ -520,13 +503,11 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
* @param promotionsKey 促销活动key * @param promotionsKey 促销活动key
*/ */
private UpdateRequest removePromotionByPromotionKey(EsGoodsIndex goodsIndex, String promotionsKey) { private UpdateRequest removePromotionByPromotionKey(EsGoodsIndex goodsIndex, String promotionsKey) {
Map<String, Object> promotionMap = goodsIndex.getPromotionMap(); Map<String, Object> promotionMap = goodsIndex.getOriginPromotionMap();
if (promotionMap != null && !promotionMap.isEmpty()) { if (promotionMap != null && !promotionMap.isEmpty()) {
//如果存在同促销ID的活动删除 //如果存在同促销ID的活动删除
List<String> collect = promotionMap.keySet().stream().filter(i -> i.equals(promotionsKey)).collect(Collectors.toList()); Map<String, Object> filterPromotionMap = promotionMap.entrySet().stream().filter(i -> !i.getKey().equals(promotionsKey)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
collect.forEach(promotionMap::remove); return this.getGoodsIndexPromotionUpdateRequest(goodsIndex.getId(), filterPromotionMap);
goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(promotionMap));
return this.getGoodsIndexPromotionUpdateRequest(goodsIndex.getId(), promotionMap);
} }
return null; return null;
} }
@ -538,7 +519,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
public void cleanInvalidPromotion() { public void cleanInvalidPromotion() {
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll(); Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll();
for (EsGoodsIndex goodsIndex : all) { for (EsGoodsIndex goodsIndex : all) {
Map<String, Object> promotionMap = goodsIndex.getPromotionMap(); Map<String, Object> promotionMap = goodsIndex.getOriginPromotionMap();
//获取商品索引 //获取商品索引
if (promotionMap != null && !promotionMap.isEmpty()) { if (promotionMap != null && !promotionMap.isEmpty()) {
//促销不为空则进行清洗 //促销不为空则进行清洗
@ -574,7 +555,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
//如果商品索引不为空返回促销信息否则返回空 //如果商品索引不为空返回促销信息否则返回空
if (goodsIndex != null) { if (goodsIndex != null) {
Map<String, Object> promotionMap = goodsIndex.getPromotionMap(); Map<String, Object> promotionMap = goodsIndex.getOriginPromotionMap();
if (promotionMap == null || promotionMap.isEmpty()) { if (promotionMap == null || promotionMap.isEmpty()) {
return new HashMap<>(16); return new HashMap<>(16);
} }
@ -639,10 +620,10 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
private UpdateRequest updateGoodsIndexPromotion(EsGoodsIndex goodsIndex, String key, BasePromotions promotion) { private UpdateRequest updateGoodsIndexPromotion(EsGoodsIndex goodsIndex, String key, BasePromotions promotion) {
Map<String, Object> promotionMap; Map<String, Object> promotionMap;
//数据非空处理如果空给一个新的信息 //数据非空处理如果空给一个新的信息
if (goodsIndex.getPromotionMap() == null || goodsIndex.getPromotionMap().isEmpty()) { if (goodsIndex.getOriginPromotionMap() == null || goodsIndex.getOriginPromotionMap().isEmpty()) {
promotionMap = new HashMap<>(1); promotionMap = new HashMap<>(1);
} else { } else {
promotionMap = goodsIndex.getPromotionMap(); promotionMap = goodsIndex.getOriginPromotionMap();
} }
log.info("ES修改商品活动索引-活动信息:{}", promotion); log.info("ES修改商品活动索引-活动信息:{}", promotion);
@ -670,7 +651,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
UpdateRequest updateRequest = new UpdateRequest(); UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(getIndexName()); updateRequest.index(getIndexName());
updateRequest.id(id); updateRequest.id(id);
// updateRequest.retryOnConflict(5); updateRequest.retryOnConflict(3);
// updateRequest.version(promotionMap.size()); // updateRequest.version(promotionMap.size());
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("promotionMap", JSONUtil.toJsonStr(promotionMap)); params.put("promotionMap", JSONUtil.toJsonStr(promotionMap));