修复更新商品索引促销时,更新的促销信息不正确问题

This commit is contained in:
paulGao 2022-03-30 17:17:09 +08:00
parent ee4f87d12d
commit c093261369
4 changed files with 19 additions and 46 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

@ -97,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;
@ -375,6 +375,10 @@ public class EsGoodsIndex implements Serializable {
} }
} }
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

@ -451,27 +451,6 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
this.updateEsGoodsIndexPromotions(skuIds, promotion, key); 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 {
log.error("更新索引商品促销信息失败skuId 为 【{}】的索引不存在!", skuId);
}
}
}
@Override @Override
public void deleteEsGoodsPromotionByPromotionKey(List<String> skuIds, String promotionsKey) { public void deleteEsGoodsPromotionByPromotionKey(List<String> skuIds, String promotionsKey) {
BulkRequest bulkRequest = new BulkRequest(); BulkRequest bulkRequest = new BulkRequest();
@ -520,13 +499,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 +515,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 +551,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 +616,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 +647,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));