优化更新es商品促销流程

This commit is contained in:
paulGao 2022-03-09 18:39:04 +08:00
parent 72e7941230
commit 9d0d318d66
5 changed files with 63 additions and 39 deletions

View File

@ -144,11 +144,15 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
this.updateGoodsIndexPromotions(new String(messageExt.getBody())); this.updateGoodsIndexPromotions(new String(messageExt.getBody()));
break; break;
case DELETE_GOODS_INDEX_PROMOTIONS: case DELETE_GOODS_INDEX_PROMOTIONS:
BasePromotions promotions = JSONUtil.toBean(new String(messageExt.getBody()), BasePromotions.class); JSONObject jsonObject = JSONUtil.parseObj(new String(messageExt.getBody()));
if (CharSequenceUtil.isNotEmpty(promotions.getScopeId())) { String promotionKey = jsonObject.getStr("promotionKey");
this.goodsIndexService.deleteEsGoodsPromotionByPromotionId(Arrays.asList(promotions.getScopeId().split(",")), promotions.getId()); if (CharSequenceUtil.isEmpty(promotionKey)) {
break;
}
if (CharSequenceUtil.isNotEmpty(jsonObject.getStr("scopeId"))) {
this.goodsIndexService.deleteEsGoodsPromotionByPromotionKey(Arrays.asList(jsonObject.getStr("scopeId").split(",")), promotionKey);
} else { } else {
this.goodsIndexService.deleteEsGoodsPromotionByPromotionId(null, promotions.getId()); this.goodsIndexService.deleteEsGoodsPromotionByPromotionKey(promotionKey);
} }
break; break;
case UPDATE_GOODS_INDEX: case UPDATE_GOODS_INDEX:
@ -252,16 +256,17 @@ public class GoodsMessageListener implements RocketMQListener<MessageExt> {
searchParams.setPromotionId(promotions.getId()); searchParams.setPromotionId(promotions.getId());
List<PromotionGoods> promotionGoodsList = this.promotionGoodsService.listFindAll(searchParams); List<PromotionGoods> promotionGoodsList = this.promotionGoodsService.listFindAll(searchParams);
List<String> skuIds = promotionGoodsList.stream().map(PromotionGoods::getSkuId).collect(Collectors.toList()); List<String> skuIds = promotionGoodsList.stream().map(PromotionGoods::getSkuId).collect(Collectors.toList());
this.goodsIndexService.deleteEsGoodsPromotionByPromotionId(skuIds, promotions.getId()); this.goodsIndexService.deleteEsGoodsPromotionByPromotionKey(skuIds, esPromotionKey);
this.goodsIndexService.updateEsGoodsIndexByList(promotionGoodsList, promotions, esPromotionKey); this.goodsIndexService.updateEsGoodsIndexByList(promotionGoodsList, promotions, esPromotionKey);
} else if (PromotionsScopeTypeEnum.PORTION_GOODS_CATEGORY.name().equals(promotions.getScopeType())) { } else if (PromotionsScopeTypeEnum.PORTION_GOODS_CATEGORY.name().equals(promotions.getScopeType())) {
GoodsSearchParams searchParams = new GoodsSearchParams(); GoodsSearchParams searchParams = new GoodsSearchParams();
searchParams.setCategoryPath(promotions.getScopeId()); searchParams.setCategoryPath(promotions.getScopeId());
List<GoodsSku> goodsSkuByList = this.goodsSkuService.getGoodsSkuByList(searchParams); List<GoodsSku> goodsSkuByList = this.goodsSkuService.getGoodsSkuByList(searchParams);
List<String> skuIds = goodsSkuByList.stream().map(GoodsSku::getId).collect(Collectors.toList()); List<String> skuIds = goodsSkuByList.stream().map(GoodsSku::getId).collect(Collectors.toList());
this.goodsIndexService.deleteEsGoodsPromotionByPromotionId(skuIds, promotions.getId()); this.goodsIndexService.deleteEsGoodsPromotionByPromotionKey(skuIds, esPromotionKey);
this.goodsIndexService.updateEsGoodsIndexPromotions(skuIds, promotions, esPromotionKey); this.goodsIndexService.updateEsGoodsIndexPromotions(skuIds, promotions, esPromotionKey);
} else if (PromotionsScopeTypeEnum.ALL.name().equals(promotions.getScopeType())) { } else if (PromotionsScopeTypeEnum.ALL.name().equals(promotions.getScopeType())) {
this.goodsIndexService.deleteEsGoodsPromotionByPromotionKey(esPromotionKey);
this.goodsIndexService.updateEsGoodsIndexAllByList(promotions, esPromotionKey); this.goodsIndexService.updateEsGoodsIndexAllByList(promotions, esPromotionKey);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -1,5 +1,6 @@
package cn.lili.modules.promotion.serviceimpl; package cn.lili.modules.promotion.serviceimpl;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.ResultCode; import cn.lili.common.enums.ResultCode;
@ -254,10 +255,11 @@ public abstract class AbstractPromotionsServiceImpl<M extends BaseMapper<T>, T e
@Override @Override
public void updateEsGoodsIndex(T promotions) { public void updateEsGoodsIndex(T promotions) {
if (promotions.getStartTime() == null && promotions.getEndTime() == null) { if (promotions.getStartTime() == null && promotions.getEndTime() == null) {
Map<Object, Object> build = MapBuilder.create().put("promotionKey", this.getPromotionType() + "-" + promotions.getId()).put("scopeId", promotions.getScopeId()).build();
//删除商品促销消息 //删除商品促销消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.DELETE_GOODS_INDEX_PROMOTIONS.name(); String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.DELETE_GOODS_INDEX_PROMOTIONS.name();
//发送mq消息 //发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(promotions), RocketmqSendCallbackBuilder.commonCallback()); rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(build), RocketmqSendCallbackBuilder.commonCallback());
} else { } else {
String esPromotionKey = this.getPromotionType().name() + "-" + promotions.getId(); String esPromotionKey = this.getPromotionType().name() + "-" + promotions.getId();

View File

@ -3,6 +3,7 @@ package cn.lili.modules.promotion.serviceimpl;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
@ -14,7 +15,6 @@ import cn.lili.modules.promotion.entity.dos.Seckill;
import cn.lili.modules.promotion.entity.dos.SeckillApply; import cn.lili.modules.promotion.entity.dos.SeckillApply;
import cn.lili.modules.promotion.entity.dto.search.SeckillSearchParams; import cn.lili.modules.promotion.entity.dto.search.SeckillSearchParams;
import cn.lili.modules.promotion.entity.enums.PromotionsApplyStatusEnum; import cn.lili.modules.promotion.entity.enums.PromotionsApplyStatusEnum;
import cn.lili.modules.promotion.entity.enums.PromotionsScopeTypeEnum;
import cn.lili.modules.promotion.entity.vos.SeckillVO; import cn.lili.modules.promotion.entity.vos.SeckillVO;
import cn.lili.modules.promotion.mapper.SeckillMapper; import cn.lili.modules.promotion.mapper.SeckillMapper;
import cn.lili.modules.promotion.service.SeckillApplyService; import cn.lili.modules.promotion.service.SeckillApplyService;
@ -40,6 +40,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 秒杀活动业务层实现 * 秒杀活动业务层实现
@ -188,12 +189,11 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
*/ */
@Override @Override
public void deleteEsGoodsSeckill(Seckill seckill, List<String> skuIds) { public void deleteEsGoodsSeckill(Seckill seckill, List<String> skuIds) {
seckill.setScopeType(PromotionsScopeTypeEnum.PORTION_GOODS.name()); Map<Object, Object> build = MapBuilder.create().put("promotionKey", this.getPromotionType() + "-" + seckill.getId()).put("scopeId", ArrayUtil.join(skuIds.toArray(), ",")).build();
seckill.setScopeId(ArrayUtil.join(skuIds.toArray(), ","));
//删除商品促销消息 //删除商品促销消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.DELETE_GOODS_INDEX_PROMOTIONS.name(); String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.DELETE_GOODS_INDEX_PROMOTIONS.name();
//发送mq消息 //发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(seckill), RocketmqSendCallbackBuilder.commonCallback()); rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(build), RocketmqSendCallbackBuilder.commonCallback());
} }
@Override @Override

View File

@ -143,9 +143,17 @@ public interface EsGoodsIndexService {
* 删除索引中指定的促销活动id的促销活动 * 删除索引中指定的促销活动id的促销活动
* *
* @param skuIds 商品skuId * @param skuIds 商品skuId
* @param promotionId 促销活动Id * @param promotionsKey 促销活动Key
*/ */
void deleteEsGoodsPromotionByPromotionId(List<String> skuIds, String promotionId); void deleteEsGoodsPromotionByPromotionKey(List<String> skuIds, String promotionsKey);
/**
* 删除索引中指定的促销活动id的促销活动
*
* @param promotionsKey 促销活动Key
*/
void deleteEsGoodsPromotionByPromotionKey(String promotionsKey);
/** /**
* 清除所以商品索引的无效促销活动 * 清除所以商品索引的无效促销活动

View File

@ -389,9 +389,8 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
@Override @Override
public void updateEsGoodsIndexPromotions(List<String> ids, BasePromotions promotion, String key) { public void updateEsGoodsIndexPromotions(List<String> ids, BasePromotions promotion, String key) {
BulkRequest bulkRequest = new BulkRequest(); BulkRequest bulkRequest = new BulkRequest();
log.info("修改商品活动索引"); log.info("更新商品索引的促销信息----------");
log.info("商品ids: {}", ids); log.info("商品ids: {}", ids);
log.info("活动关键字: {}", key);
log.info("活动: {}", promotion); log.info("活动: {}", promotion);
for (String id : ids) { for (String id : ids) {
UpdateRequest updateRequest = this.updateEsGoodsIndexPromotions(id, promotion, key); UpdateRequest updateRequest = this.updateEsGoodsIndexPromotions(id, promotion, key);
@ -449,7 +448,6 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
goodsIndices = new ArrayList<>(IterableUtil.toCollection(all)); goodsIndices = new ArrayList<>(IterableUtil.toCollection(all));
} }
List<String> skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList()); List<String> skuIds = goodsIndices.stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
this.deleteEsGoodsPromotionByPromotionId(skuIds, promotion.getId());
this.updateEsGoodsIndexPromotions(skuIds, promotion, key); this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
} }
@ -475,17 +473,19 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
} }
@Override @Override
public void deleteEsGoodsPromotionByPromotionId(List<String> skuIds, String promotionId) { public void deleteEsGoodsPromotionByPromotionKey(List<String> skuIds, String promotionsKey) {
BulkRequest bulkRequest = new BulkRequest(); BulkRequest bulkRequest = new BulkRequest();
log.info("删除商品活动索引"); log.info("删除商品活动索引");
log.info("商品skuIds: {}", skuIds); log.info("商品skuIds: {}", skuIds);
log.info("活动Id: {}", promotionId); log.info("活动Key: {}", promotionsKey);
if (skuIds != null && !skuIds.isEmpty()) { if (skuIds == null || skuIds.isEmpty()) {
return;
}
for (String skuId : skuIds) { for (String skuId : skuIds) {
EsGoodsIndex goodsIndex = findById(skuId); EsGoodsIndex goodsIndex = findById(skuId);
//商品索引不为空 //商品索引不为空
if (goodsIndex != null) { if (goodsIndex != null) {
UpdateRequest updateRequest = this.removePromotionByPromotionId(goodsIndex, promotionId); UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey);
if (updateRequest != null) { if (updateRequest != null) {
bulkRequest.add(updateRequest); bulkRequest.add(updateRequest);
} }
@ -493,29 +493,37 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
log.error("更新索引商品促销信息失败skuId 为 【{}】的索引不存在!", skuId); log.error("更新索引商品促销信息失败skuId 为 【{}】的索引不存在!", skuId);
} }
} }
} else { this.executeBulkUpdateRequest(bulkRequest);
}
/**
* 删除索引中指定的促销活动id的促销活动
*
* @param promotionsKey 促销活动Key
*/
@Override
public void deleteEsGoodsPromotionByPromotionKey(String promotionsKey) {
BulkRequest bulkRequest = new BulkRequest();
for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) { for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) {
UpdateRequest updateRequest = this.removePromotionByPromotionId(goodsIndex, promotionId); UpdateRequest updateRequest = this.removePromotionByPromotionKey(goodsIndex, promotionsKey);
if (updateRequest != null) { if (updateRequest != null) {
bulkRequest.add(updateRequest); bulkRequest.add(updateRequest);
} }
} }
}
this.executeBulkUpdateRequest(bulkRequest); this.executeBulkUpdateRequest(bulkRequest);
} }
/** /**
* 从索引中删除指定促销活动id的促销活动 * 从索引中删除指定促销活动id的促销活动
* *
* @param goodsIndex 索引 * @param goodsIndex 索引
* @param promotionId 促销活动id * @param promotionsKey 促销活动key
*/ */
private UpdateRequest removePromotionByPromotionId(EsGoodsIndex goodsIndex, String promotionId) { private UpdateRequest removePromotionByPromotionKey(EsGoodsIndex goodsIndex, String promotionsKey) {
Map<String, Object> promotionMap = goodsIndex.getPromotionMap(); Map<String, Object> promotionMap = goodsIndex.getPromotionMap();
if (promotionMap != null && !promotionMap.isEmpty()) { if (promotionMap != null && !promotionMap.isEmpty()) {
//如果存在同促销ID的活动删除 //如果存在同促销ID的活动删除
List<String> collect = promotionMap.keySet().stream().filter(i -> i.split("-")[1].equals(promotionId)).collect(Collectors.toList()); List<String> collect = promotionMap.keySet().stream().filter(i -> i.equals(promotionsKey)).collect(Collectors.toList());
collect.forEach(promotionMap::remove); collect.forEach(promotionMap::remove);
goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(promotionMap)); goodsIndex.setPromotionMapJson(JSONUtil.toJsonStr(promotionMap));
return this.getGoodsIndexPromotionUpdateRequest(goodsIndex.getId(), promotionMap); return this.getGoodsIndexPromotionUpdateRequest(goodsIndex.getId(), promotionMap);
@ -662,7 +670,8 @@ 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(5);
// 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));
Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.promotionMapJson=params.promotionMap;", params); Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.promotionMapJson=params.promotionMap;", params);
@ -684,7 +693,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
if (responses.hasFailures()) { if (responses.hasFailures()) {
log.info("批量更新商品索引的促销信息中出现部分异常:{}", responses.buildFailureMessage()); log.info("批量更新商品索引的促销信息中出现部分异常:{}", responses.buildFailureMessage());
} else { } else {
log.info("批量更新商品索引的促销信息结果:{}", responses); log.info("批量更新商品索引的促销信息结果:{}", responses.status());
} }
} catch (IOException e) { } catch (IOException e) {
log.error("批量更新商品索引的促销信息出现异常!", e); log.error("批量更新商品索引的促销信息出现异常!", e);