Skip to content

Commit

Permalink
优化清除无效促销活动任务,改为批量清除
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed Aug 11, 2022
1 parent 93dd2e0 commit 050b026
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.lili.modules.promotion.serviceimpl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
Expand Down Expand Up @@ -249,7 +250,7 @@ public void initPromotion(Seckill promotions) {
public void checkStatus(Seckill promotions) {
super.checkStatus(promotions);
if (promotions.getStartTime() != null && CharSequenceUtil.isNotEmpty(promotions.getHours())) {
String[] split = promotions.getHours().split(",");
Integer[] split = Convert.toIntArray(promotions.getHours().split(","));
Arrays.sort(split);
String startTimeStr = DateUtil.format(promotions.getStartTime(), DatePattern.NORM_DATE_PATTERN) + " " + split[0] + ":00";
promotions.setStartTime(DateUtil.parse(startTimeStr, DatePattern.NORM_DATETIME_MINUTE_PATTERN));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private void analyzeAndSaveWords(EsGoodsIndex goods) {
* @return
*/
public static void removeDuplicate(List<String> list) {
HashSet<String> h = new HashSet(list);
HashSet<String> h = new HashSet<>(list);
list.clear();
list.addAll(h);
}
Expand Down Expand Up @@ -553,36 +553,37 @@ public void updateEsGoodsIndexByList(List<PromotionGoods> promotionGoodsList, Ba
*/
@Override
public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) {
ThreadUtil.execAsync(() -> {
for (int i = 1; ; i++) {
List<String> skuIds;
//如果storeId不为空,则表示是店铺活动
if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) {
PageVO pageVO = new PageVO();
pageVO.setPageNumber(i);
pageVO.setPageSize(1000);
EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO();
searchDTO.setStoreId(promotion.getStoreId());
//查询出店铺商品
SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(searchDTO, pageVO);

skuIds = esGoodsIndices.isEmpty() ? new ArrayList<>() : esGoodsIndices.getContent().stream().map(SearchHit::getId).collect(Collectors.toList());
} else {
//否则是平台活动
org.springframework.data.domain.Page<EsGoodsIndex> all = goodsIndexRepository.findAll(PageRequest.of(i, 1000));

//查询出全部商品
skuIds = all.isEmpty() ? new ArrayList<>() : all.toList().stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
}
if (skuIds.isEmpty()) {
break;
}
this.deleteEsGoodsPromotionByPromotionKey(skuIds, key);
this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
}
ThreadUtil.execAsync(() -> this.executeUpdateEsGoodsIndexAll(promotion, key));

});
}

private void executeUpdateEsGoodsIndexAll(BasePromotions promotion, String key) {
for (int i = 1; ; i++) {
List<String> skuIds;
//如果storeId不为空,则表示是店铺活动
if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) {
PageVO pageVO = new PageVO();
pageVO.setPageNumber(i);
pageVO.setPageSize(1000);
EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO();
searchDTO.setStoreId(promotion.getStoreId());
//查询出店铺商品
SearchPage<EsGoodsIndex> esGoodsIndices = goodsSearchService.searchGoods(searchDTO, pageVO);

skuIds = esGoodsIndices.isEmpty() ? new ArrayList<>() : esGoodsIndices.getContent().stream().map(SearchHit::getId).collect(Collectors.toList());
} else {
//否则是平台活动
org.springframework.data.domain.Page<EsGoodsIndex> all = goodsIndexRepository.findAll(PageRequest.of(i, 1000));

//查询出全部商品
skuIds = all.isEmpty() ? new ArrayList<>() : all.toList().stream().map(EsGoodsIndex::getId).collect(Collectors.toList());
}
if (skuIds.isEmpty()) {
break;
}
this.deleteEsGoodsPromotionByPromotionKey(skuIds, key);
this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
}
}

@Override
Expand Down Expand Up @@ -649,22 +650,32 @@ private UpdateRequest removePromotionByPromotionKey(EsGoodsIndex goodsIndex, Str
*/
@Override
public void cleanInvalidPromotion() {
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll();
for (EsGoodsIndex goodsIndex : all) {
Map<String, Object> promotionMap = goodsIndex.getOriginPromotionMap();
//获取商品索引
if (promotionMap != null && !promotionMap.isEmpty()) {
//促销不为空则进行清洗
promotionMap.entrySet().removeIf(i -> {
JSONObject promotionJson = JSONUtil.parseObj(i.getValue());
BasePromotions promotion = promotionJson.toBean(BasePromotions.class);
return promotion.getEndTime() != null && promotion.getEndTime().getTime() < DateUtil.date().getTime();
});
ThreadUtil.execAsync(this::executeCleanInvalidPromotions);
}

private void executeCleanInvalidPromotions() {
for (int i = 1; ; i++) {
org.springframework.data.domain.Page<EsGoodsIndex> all = goodsIndexRepository.findAll(PageRequest.of(i, 1000));
if (all.isEmpty()) {
break;
}
for (EsGoodsIndex goodsIndex : all.toList()) {
Map<String, Object> promotionMap = goodsIndex.getOriginPromotionMap();
//获取商品索引
if (promotionMap != null && !promotionMap.isEmpty()) {
//促销不为空则进行清洗
promotionMap.entrySet().removeIf(j -> {
JSONObject promotionJson = JSONUtil.parseObj(j.getValue());
BasePromotions promotion = promotionJson.toBean(BasePromotions.class);
return promotion.getEndTime() != null && promotion.getEndTime().getTime() < DateUtil.date().getTime();
});
}
}
goodsIndexRepository.saveAll(all);
}
goodsIndexRepository.saveAll(all);
}


@Override
public EsGoodsIndex findById(String id) {
Optional<EsGoodsIndex> goodsIndex = goodsIndexRepository.findById(id);
Expand Down

0 comments on commit 050b026

Please sign in to comment.