优化清除无效促销活动任务,改为批量清除

This commit is contained in:
paulGao 2022-08-11 11:57:58 +08:00
parent 93dd2e06d8
commit 050b026166
2 changed files with 55 additions and 43 deletions

View File

@ -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;
@ -249,7 +250,7 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
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));

View File

@ -316,7 +316,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
* @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);
}
@ -553,7 +553,11 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/
@Override
public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) {
ThreadUtil.execAsync(() -> {
ThreadUtil.execAsync(() -> this.executeUpdateEsGoodsIndexAll(promotion, key));
}
private void executeUpdateEsGoodsIndexAll(BasePromotions promotion, String key) {
for (int i = 1; ; i++) {
List<String> skuIds;
//如果storeId不为空则表示是店铺活动
@ -580,9 +584,6 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
this.deleteEsGoodsPromotionByPromotionKey(skuIds, key);
this.updateEsGoodsIndexPromotions(skuIds, promotion, key);
}
});
}
@Override
@ -649,14 +650,22 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/
@Override
public void cleanInvalidPromotion() {
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll();
for (EsGoodsIndex goodsIndex : all) {
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(i -> {
JSONObject promotionJson = JSONUtil.parseObj(i.getValue());
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();
});
@ -664,6 +673,8 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
}
goodsIndexRepository.saveAll(all);
}
}
@Override
public EsGoodsIndex findById(String id) {