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

Merge pull request !222 from OceansDeep/feature/pg
This commit is contained in:
OceansDeep 2022-08-11 04:01:38 +00:00 committed by Gitee
commit 1cf0496cd1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 55 additions and 43 deletions

View File

@ -1,5 +1,6 @@
package cn.lili.modules.promotion.serviceimpl; package cn.lili.modules.promotion.serviceimpl;
import cn.hutool.core.convert.Convert;
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;
@ -249,7 +250,7 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
public void checkStatus(Seckill promotions) { public void checkStatus(Seckill promotions) {
super.checkStatus(promotions); super.checkStatus(promotions);
if (promotions.getStartTime() != null && CharSequenceUtil.isNotEmpty(promotions.getHours())) { if (promotions.getStartTime() != null && CharSequenceUtil.isNotEmpty(promotions.getHours())) {
String[] split = promotions.getHours().split(","); Integer[] split = Convert.toIntArray(promotions.getHours().split(","));
Arrays.sort(split); Arrays.sort(split);
String startTimeStr = DateUtil.format(promotions.getStartTime(), DatePattern.NORM_DATE_PATTERN) + " " + split[0] + ":00"; String startTimeStr = DateUtil.format(promotions.getStartTime(), DatePattern.NORM_DATE_PATTERN) + " " + split[0] + ":00";
promotions.setStartTime(DateUtil.parse(startTimeStr, DatePattern.NORM_DATETIME_MINUTE_PATTERN)); promotions.setStartTime(DateUtil.parse(startTimeStr, DatePattern.NORM_DATETIME_MINUTE_PATTERN));

View File

@ -316,7 +316,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
* @return * @return
*/ */
public static void removeDuplicate(List<String> list) { public static void removeDuplicate(List<String> list) {
HashSet<String> h = new HashSet(list); HashSet<String> h = new HashSet<>(list);
list.clear(); list.clear();
list.addAll(h); list.addAll(h);
} }
@ -553,36 +553,37 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/ */
@Override @Override
public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) { public void updateEsGoodsIndexAllByList(BasePromotions promotion, String key) {
ThreadUtil.execAsync(() -> { ThreadUtil.execAsync(() -> this.executeUpdateEsGoodsIndexAll(promotion, 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));
//查询出全部商品 private void executeUpdateEsGoodsIndexAll(BasePromotions promotion, String key) {
skuIds = all.isEmpty() ? new ArrayList<>() : all.toList().stream().map(EsGoodsIndex::getId).collect(Collectors.toList()); for (int i = 1; ; i++) {
} List<String> skuIds;
if (skuIds.isEmpty()) { //如果storeId不为空则表示是店铺活动
break; if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) {
} PageVO pageVO = new PageVO();
this.deleteEsGoodsPromotionByPromotionKey(skuIds, key); pageVO.setPageNumber(i);
this.updateEsGoodsIndexPromotions(skuIds, promotion, key); 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 @Override
@ -649,22 +650,32 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
*/ */
@Override @Override
public void cleanInvalidPromotion() { public void cleanInvalidPromotion() {
Iterable<EsGoodsIndex> all = goodsIndexRepository.findAll(); ThreadUtil.execAsync(this::executeCleanInvalidPromotions);
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();
});
}
}
goodsIndexRepository.saveAll(all);
} }
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);
}
}
@Override @Override
public EsGoodsIndex findById(String id) { public EsGoodsIndex findById(String id) {
Optional<EsGoodsIndex> goodsIndex = goodsIndexRepository.findById(id); Optional<EsGoodsIndex> goodsIndex = goodsIndexRepository.findById(id);