修复商品搜索多选问题和定时任务取消过期促销活动时,没有删除es索引中的过期信息问题

This commit is contained in:
paulGao 2021-05-19 22:16:05 +08:00
parent 120189ef99
commit d3fdeef0c3
5 changed files with 72 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import cn.lili.modules.promotion.entity.enums.PromotionStatusEnum;
import cn.lili.modules.promotion.entity.vos.CouponVO;
import cn.lili.modules.promotion.entity.vos.PintuanVO;
import cn.lili.modules.promotion.service.*;
import cn.lili.modules.search.service.EsGoodsIndexService;
import cn.lili.timetask.handler.EveryDayExecute;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@ -34,6 +35,9 @@ public class PromotionEverydayExecute implements EveryDayExecute {
//Mongo
@Autowired
private MongoTemplate mongoTemplate;
//es
@Autowired
private EsGoodsIndexService esGoodsIndexService;
//满额活动
@Autowired
private FullDiscountService fullDiscountService;
@ -74,6 +78,7 @@ public class PromotionEverydayExecute implements EveryDayExecute {
if (vo.getPromotionGoodsList() != null && !vo.getPromotionGoodsList().isEmpty()) {
for (PromotionGoods promotionGoods : vo.getPromotionGoodsList()) {
promotionGoods.setPromotionStatus(PromotionStatusEnum.END.name());
esGoodsIndexService.deleteEsGoodsPromotionByPromotionId(promotionGoods.getSkuId(), vo.getId());
}
}
mongoTemplate.save(vo);
@ -92,6 +97,7 @@ public class PromotionEverydayExecute implements EveryDayExecute {
if (vo.getPromotionGoodsList() != null && !vo.getPromotionGoodsList().isEmpty()) {
for (PromotionGoods promotionGoods : vo.getPromotionGoodsList()) {
promotionGoods.setPromotionStatus(PromotionStatusEnum.END.name());
esGoodsIndexService.deleteEsGoodsPromotionByPromotionId(promotionGoods.getSkuId(), vo.getId());
}
}
mongoTemplate.save(vo);
@ -110,6 +116,7 @@ public class PromotionEverydayExecute implements EveryDayExecute {
if (vo.getPromotionGoodsList() != null && !vo.getPromotionGoodsList().isEmpty()) {
for (PromotionGoods promotionGoods : vo.getPromotionGoodsList()) {
promotionGoods.setPromotionStatus(PromotionStatusEnum.END.name());
esGoodsIndexService.deleteEsGoodsPromotionByPromotionId(promotionGoods.getSkuId(), vo.getId());
}
}
mongoTemplate.save(vo);

View File

@ -438,8 +438,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
goodsIndexService.addIndex(goodsIndex);
} else if (goodsSku.getQuantity() > 0 && esGoodsOld != null) {
goodsIndexService.updateIndex(goodsIndex);
} else if (goodsSku.getQuantity() <= 0 && esGoodsOld != null) {
goodsIndexService.deleteIndexById(goodsSku.getId());
}
//删除sku缓存
cache.remove(GoodsSkuService.getCacheKeys(goodsSku.getId()));

View File

@ -105,6 +105,12 @@ public interface EsGoodsIndexService {
*/
void deleteEsGoodsPromotionIndexByList(List<String> skuIds, PromotionTypeEnum promotionType);
/**
* 删除索引中指定的促销活动id的促销活动
* @param skuId 商品skuId
* @param promotionId 促销活动Id
*/
void deleteEsGoodsPromotionByPromotionId(String skuId, String promotionId);
/**
* 清除所以商品索引的无效促销活动
*/

View File

@ -203,7 +203,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
@Override
public void updateEsGoodsIndexAllByList(BasePromotion promotion, String key) {
List<EsGoodsIndex> goodsIndices;
//如果storeid不为空则表示是店铺活动
//如果storeId不为空则表示是店铺活动
if (promotion.getStoreId() != null) {
EsGoodsSearchDTO searchDTO = new EsGoodsSearchDTO();
searchDTO.setStoreId(promotion.getStoreId());
@ -243,6 +243,41 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
}
}
@Override
public void deleteEsGoodsPromotionByPromotionId(String skuId, String promotionId) {
if (skuId != null) {
EsGoodsIndex goodsIndex = findById(skuId);
//商品索引不为空
if (goodsIndex != null) {
this.removePromotionByPromotionId(goodsIndex, promotionId);
} else {
log.error("更新索引商品促销信息失败skuId 为 【{}】的索引不存在!", skuId);
}
} else {
for (EsGoodsIndex goodsIndex : this.goodsIndexRepository.findAll()) {
this.removePromotionByPromotionId(goodsIndex, promotionId);
}
}
}
/**
* 从索引中删除指定促销活动id的促销活动
*
* @param goodsIndex 索引
* @param promotionId 促销活动id
*/
private void removePromotionByPromotionId(EsGoodsIndex goodsIndex, String promotionId) {
Map<String, Object> promotionMap = goodsIndex.getPromotionMap();
if (promotionMap != null && !promotionMap.isEmpty()) {
// 如果存在同类型促销活动删除
List<String> collect = promotionMap.keySet().stream().filter(i -> i.split("-")[1].equals(promotionId)).collect(Collectors.toList());
collect.forEach(promotionMap::remove);
goodsIndex.setPromotionMap(promotionMap);
updateIndex(goodsIndex);
}
}
/**
* 清除所有商品索引的无效促销活动
*/

View File

@ -22,6 +22,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
@ -336,6 +337,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
String[] props = searchDTO.getProp().split("@");
List<String> nameList = new ArrayList<>();
List<String> valueList = new ArrayList<>();
Map<String, List<String>> valueMap = new HashMap<>();
for (String prop : props) {
String[] propValues = prop.split("_");
String name = propValues[0];
@ -346,14 +348,29 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
if (!valueList.contains(value)) {
valueList.add(value);
}
if (isAggregation) {
filterBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.termQuery(ATTR_NAME, name), ScoreMode.None));
filterBuilder.should(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.termsQuery(ATTR_VALUE, value), ScoreMode.None));
// 将同一规格名下的规格值分组
if (!valueMap.containsKey(name)) {
List<String> values = new ArrayList<>();
values.add(value);
valueMap.put(name, values);
} else {
queryBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.termQuery(ATTR_NAME, name), ScoreMode.None));
queryBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.wildcardQuery(ATTR_VALUE, "*" + value + "*"), ScoreMode.None));
valueMap.get(name).add(value);
}
}
BoolQueryBuilder usedQueryBuilder;
if (isAggregation) {
usedQueryBuilder = filterBuilder;
} else {
usedQueryBuilder = queryBuilder;
}
// 遍历所有的规格
for (Map.Entry<String, List<String>> entry : valueMap.entrySet()) {
usedQueryBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.wildcardQuery(ATTR_NAME, "*" + entry.getKey() + "*"), ScoreMode.None));
BoolQueryBuilder shouldBuilder = QueryBuilders.boolQuery();
for (String s : entry.getValue()) {
shouldBuilder.should(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.wildcardQuery(ATTR_VALUE, "*" + s + "*"), ScoreMode.None));
}
usedQueryBuilder.must(shouldBuilder);
}
searchDTO.getNotShowCol().put(ATTR_NAME_KEY, nameList);
searchDTO.getNotShowCol().put(ATTR_VALUE_KEY, valueList);