Skip to content

Commit

Permalink
!109 优化生成索引
Browse files Browse the repository at this point in the history
Merge pull request !109 from OceansDeep/feature/pg
  • Loading branch information
OceansDeep authored and gitee-org committed Jan 7, 2022
2 parents 3fa3991 + ad1f086 commit 5d96d43
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ private void updateGoodsIndex(Goods goods) {
GoodsSearchParams searchParams = new GoodsSearchParams();
searchParams.setGoodsId(goods.getId());
List<GoodsSku> goodsSkuList = this.goodsSkuService.getGoodsSkuByList(searchParams);
log.info("goods:{}", goods);
log.info("goodsSkuList:{}", goodsSkuList);
if (goods.getAuthFlag().equals(GoodsAuthEnum.PASS.name())
&& goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name())
&& Boolean.FALSE.equals(goods.getDeleteFlag())) {
Expand Down Expand Up @@ -340,6 +342,8 @@ private void generatorGoodsIndex(Goods goods, List<GoodsSku> goodsSkuList) {
EsGoodsIndex esGoodsOld = goodsIndexService.findById(goodsSku.getId());
EsGoodsIndex goodsIndex = this.settingUpGoodsIndexData(goods, goodsSku);
goodsIndex.setSkuSource(skuSource--);
log.info("goodsSku:{}", goodsSku);
log.info("esGoodsOld:{}", esGoodsOld);
//如果商品库存不为0,并且es中有数据
if (goodsSku.getQuantity() > 0 && esGoodsOld == null) {
log.info("生成商品索引 {}", goodsIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
Expand Down Expand Up @@ -123,7 +122,9 @@ public void add(List<Map<String, Object>> skuList, Goods goods) {
}

this.updateStock(newSkuList);
generateEs(goods);
if (!newSkuList.isEmpty()) {
generateEs(goods);
}
}

@Override
Expand All @@ -140,9 +141,9 @@ public void update(List<Map<String, Object>> skuList, Goods goods, Boolean regen
//删除旧索引
for (GoodsSkuVO goodsSkuVO : goodsListByGoodsId) {
oldSkuIds.add(goodsSkuVO.getId());
goodsIndexService.deleteIndexById(goodsSkuVO.getId());
cache.remove(GoodsSkuService.getCacheKeys(goodsSkuVO.getId()));
}
goodsIndexService.deleteIndexByIds(oldSkuIds);
this.removeByIds(oldSkuIds);
//删除sku相册
goodsGalleryService.removeByIds(oldSkuIds);
Expand Down Expand Up @@ -170,7 +171,9 @@ public void update(List<Map<String, Object>> skuList, Goods goods, Boolean regen
this.updateBatchById(newSkuList);
}
this.updateStock(newSkuList);
generateEs(goods);
if (!newSkuList.isEmpty()) {
generateEs(goods);
}
}

/**
Expand Down Expand Up @@ -329,7 +332,9 @@ public void updateGoodsSkuStatus(Goods goods) {
cache.remove(GoodsSkuService.getCacheKeys(sku.getId()));
cache.put(GoodsSkuService.getCacheKeys(sku.getId()), sku);
}
generateEs(goods);
if (!goodsSkus.isEmpty()) {
generateEs(goods);
}
}
}

Expand Down Expand Up @@ -564,18 +569,19 @@ public void generateEs(Goods goods) {
if (!GoodsStatusEnum.UPPER.name().equals(goods.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag())) {
return;
}
ThreadUtil.execAsync(() -> {
try {
// 延时执行,防止商品未保存完成就去生成商品索引导致生成索引时找不到商品问题
Thread.sleep(2000);
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, goods.getId(), RocketmqSendCallbackBuilder.commonCallback());
} catch (InterruptedException e) {
log.error("发送商品索引信息失败!", e);
Thread.currentThread().interrupt();
}
});
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GENERATOR_GOODS_INDEX.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, goods.getId(), RocketmqSendCallbackBuilder.commonCallback());
// ThreadUtil.execAsync(() -> {
// try {
// // 延时执行,防止商品未保存完成就去生成商品索引导致生成索引时找不到商品问题
// Thread.sleep(2000);
//
// } catch (InterruptedException e) {
// log.error("发送商品索引信息失败!", e);
// Thread.currentThread().interrupt();
// }
// });
}

/**
Expand Down

0 comments on commit 5d96d43

Please sign in to comment.