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

Merge pull request !1 from OceansDeep/feature/pg
This commit is contained in:
chopper711 2021-05-21 09:15:48 +08:00 committed by Gitee
commit 3c64d21bfd
13 changed files with 114 additions and 41 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

@ -35,7 +35,9 @@ public abstract class AbstractDelayQueueMachineFactory {
Calendar instance = Calendar.getInstance();
instance.add(Calendar.SECOND, time);
long delaySeconds = instance.getTimeInMillis() / 1000;
return redisUtil.zadd(setDelayQueueName(), delaySeconds, jobId);
boolean result = redisUtil.zadd(setDelayQueueName(), delaySeconds, jobId);
log.info("redis add delay, key {}, delay time {}", setDelayQueueName(), delaySeconds);
return result;
}

View File

@ -37,7 +37,7 @@ public class RocketmqTimerTrigger implements TimeTrigger {
TimeTriggerMsg timeTriggerMsg = new TimeTriggerMsg(executorName, triggerTime, param, uniqueKey, topic);
Message<TimeTriggerMsg> message = MessageBuilder.withPayload(timeTriggerMsg).build();
log.info("延时任务发送信息:{}", message);
this.rocketMQTemplate.asyncSend(topic, message, RocketmqSendCallbackBuilder.commonCallback());
}

View File

@ -4,6 +4,7 @@ import cn.hutool.json.JSONUtil;
import cn.lili.common.delayqueue.AbstractDelayQueueMachineFactory;
import cn.lili.common.trigger.interfaces.TimeTrigger;
import cn.lili.common.trigger.model.TimeTriggerMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
@ -18,6 +19,7 @@ import org.springframework.stereotype.Component;
@Component
public class PromotionDelayQueue extends AbstractDelayQueueMachineFactory {
@Autowired
private TimeTrigger timeTrigger;
@Override

View File

@ -444,8 +444,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

@ -19,7 +19,6 @@ import cn.lili.modules.promotion.entity.dto.PromotionPriceParamDTO;
import cn.lili.modules.promotion.entity.dto.StorePromotionPriceDTO;
import cn.lili.modules.promotion.service.PromotionGoodsService;
import cn.lili.modules.promotion.service.PromotionPriceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
@ -27,6 +26,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@ -151,29 +151,26 @@ public class SkuPromotionRender implements CartRenderStep {
for (CartVO cartVO : tradeDTO.getCartList()) {
// 根据店铺分配店铺价格计算结果
List<StorePromotionPriceDTO> collect = promotionPrice.getStorePromotionPriceList().parallelStream().map(i -> i.getStoreId().equals(cartVO.getStoreId()) ? i : null).collect(Collectors.toList());
if (!collect.isEmpty() && collect.get(0) != null) {
StorePromotionPriceDTO storePromotionPriceDTO = collect.get(0);
Optional<StorePromotionPriceDTO> storePromotionPriceDTOOptional = promotionPrice.getStorePromotionPriceList().parallelStream().filter(i -> i.getStoreId().equals(cartVO.getStoreId())).findAny();
if (storePromotionPriceDTOOptional.isPresent()) {
StorePromotionPriceDTO storePromotionPriceDTO = storePromotionPriceDTOOptional.get();
// 根据商品分配商品结果计算结果
this.distributionSkuPromotionPrice(cartVO.getSkuList(), storePromotionPriceDTO);
if (storePromotionPriceDTO != null) {
PriceDetailDTO sSpd = new PriceDetailDTO();
PriceDetailVO sPd = new PriceDetailVO();
sSpd.setGoodsPrice(storePromotionPriceDTO.getTotalOriginPrice());
sSpd.setDiscountPrice(storePromotionPriceDTO.getTotalDiscountPrice());
sSpd.setCouponPrice(storePromotionPriceDTO.getTotalCouponPrice());
sSpd.setPayPoint(storePromotionPriceDTO.getTotalPoints().intValue());
sPd.setOriginalPrice(storePromotionPriceDTO.getTotalOriginPrice());
sPd.setFinalePrice(storePromotionPriceDTO.getTotalFinalePrice());
sPd.setDiscountPrice(storePromotionPriceDTO.getTotalDiscountPrice());
sPd.setPayPoint(storePromotionPriceDTO.getTotalPoints().intValue());
cartVO.setPriceDetailDTO(sSpd);
cartVO.setPriceDetailVO(sPd);
cartVO.setWeight(storePromotionPriceDTO.getTotalWeight());
}
PriceDetailDTO sSpd = new PriceDetailDTO();
PriceDetailVO sPd = new PriceDetailVO();
sSpd.setGoodsPrice(storePromotionPriceDTO.getTotalOriginPrice());
sSpd.setDiscountPrice(storePromotionPriceDTO.getTotalDiscountPrice());
sSpd.setCouponPrice(storePromotionPriceDTO.getTotalCouponPrice());
sSpd.setPayPoint(storePromotionPriceDTO.getTotalPoints().intValue());
sPd.setOriginalPrice(storePromotionPriceDTO.getTotalOriginPrice());
sPd.setFinalePrice(storePromotionPriceDTO.getTotalFinalePrice());
sPd.setDiscountPrice(storePromotionPriceDTO.getTotalDiscountPrice());
sPd.setPayPoint(storePromotionPriceDTO.getTotalPoints().intValue());
cartVO.setPriceDetailDTO(sSpd);
cartVO.setPriceDetailVO(sPd);
cartVO.setWeight(storePromotionPriceDTO.getTotalWeight());
}
}

View File

@ -269,6 +269,9 @@ public class CartServiceImpl implements CartService {
}
}
cartSkuVOS.removeAll(deleteVos);
// 清除选择的优惠券
tradeDTO.setPlatformCoupon(null);
tradeDTO.setStoreCoupons(null);
// 清除添加过的备注
tradeDTO.setStoreRemark(null);
cache.put(this.getOriginKey(tradeDTO.getCartTypeEnum()), tradeDTO);

View File

@ -118,7 +118,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
promotionMessage,
coupon.getStartTime().getTime(), couponVO.getStartTime().getTime(),
DelayQueueTools.wrapperUniqueKey(DelayQueueType.PROMOTION, (promotionMessage.getPromotionType() + promotionMessage.getPromotionId())),
DateUtil.getDelayTime(coupon.getStartTime().getTime()),
DateUtil.getDelayTime(couponVO.getStartTime().getTime()),
rocketmqCustomProperties.getPromotionTopic());
return couponVO;
}

View File

@ -223,7 +223,7 @@ public class PintuanServiceImpl extends ServiceImpl<PintuanMapper, Pintuan> impl
pintuanVO.getStartTime().getTime(),
pintuan.getStartTime().getTime(),
DelayQueueTools.wrapperUniqueKey(DelayQueueType.PROMOTION, (promotionMessage.getPromotionType() + promotionMessage.getPromotionId())),
DateUtil.getDelayTime(pintuan.getStartTime().getTime()),
DateUtil.getDelayTime(pintuanVO.getStartTime().getTime()),
rocketmqCustomProperties.getPromotionTopic());
}

View File

@ -126,6 +126,10 @@ public class PromotionPriceServiceImpl implements PromotionPriceService {
if (platformCoupons != null && !platformCoupons.isEmpty()) {
// 计算平台优惠券活动
couponTotalPrice = CurrencyUtil.add(couponTotalPrice, this.calculationCoupon(platformCoupons, priceDTOList));
for (StorePromotionPriceDTO storePromotionPriceDTO : storePromotionPriceList) {
Double couponPrice = storePromotionPriceDTO.getGoodsSkuPromotionPriceList().parallelStream().mapToDouble(GoodsSkuPromotionPriceDTO::getCouponPrice).sum();
storePromotionPriceDTO.setTotalCouponPrice(couponPrice);
}
}
promotionPrice.setStorePromotionPriceList(storePromotionPriceList);
promotionPrice.setTotalCouponPrice(couponTotalPrice);
@ -290,17 +294,14 @@ public class PromotionPriceServiceImpl implements PromotionPriceService {
// 合计优惠券范围内的所有商品的原价
double totalPrice = conformCollect.parallelStream().mapToDouble(GoodsSkuPromotionPriceDTO::getOriginalPrice).sum();
double discountPrice = 0;
// 根据优惠券优惠类型判断是否满足条件
if (CouponTypeEnum.PRICE.name().equals(coupon.getCouponType()) && coupon.getConsumeThreshold() <= totalPrice) {
couponTotalPrice = CurrencyUtil.add(couponTotalPrice, coupon.getPrice());
discountPrice = coupon.getPrice();
} else if (CouponTypeEnum.DISCOUNT.name().equals(coupon.getCouponType())) {
double fullRate = coupon.getDiscount() >= 10 ? coupon.getDiscount() / 100 : coupon.getDiscount() / 10;
double discountRatePrice = CurrencyUtil.sub(totalPrice, CurrencyUtil.mul(totalPrice, fullRate));
couponTotalPrice = CurrencyUtil.add(couponTotalPrice, discountRatePrice);
discountPrice = discountRatePrice;
// 消费限额判断
// if (coupon.getConsumeThreshold() >= discountRatePrice) {
// couponTotalPrice = CurrencyUtil.add(couponTotalPrice, discountRatePrice);
@ -312,14 +313,15 @@ public class PromotionPriceServiceImpl implements PromotionPriceService {
}
// 分配到每个商品的优惠券金额
double distributePrice = CurrencyUtil.div(discountPrice, conformCollect.size());
for (GoodsSkuPromotionPriceDTO goodsSkuPromotionPriceDTO : conformCollect) {
double rate = CurrencyUtil.div(goodsSkuPromotionPriceDTO.getFinalePrice(), totalPrice, 5);
double distributeCouponPrice = CurrencyUtil.mul(couponTotalPrice, rate);
if (goodsSkuPromotionPriceDTO.getFinalePrice() != null) {
goodsSkuPromotionPriceDTO.setFinalePrice(CurrencyUtil.sub(goodsSkuPromotionPriceDTO.getFinalePrice(), distributePrice));
goodsSkuPromotionPriceDTO.setFinalePrice(CurrencyUtil.sub(goodsSkuPromotionPriceDTO.getFinalePrice(), distributeCouponPrice));
} else {
goodsSkuPromotionPriceDTO.setFinalePrice(CurrencyUtil.sub(goodsSkuPromotionPriceDTO.getOriginalPrice(), distributePrice));
goodsSkuPromotionPriceDTO.setFinalePrice(CurrencyUtil.sub(goodsSkuPromotionPriceDTO.getOriginalPrice(), distributeCouponPrice));
}
goodsSkuPromotionPriceDTO.setCouponPrice(distributePrice);
goodsSkuPromotionPriceDTO.setCouponPrice(distributeCouponPrice);
goodsSkuPromotionPriceDTO.setTotalFinalePrice(CurrencyUtil.mul(goodsSkuPromotionPriceDTO.getFinalePrice(), goodsSkuPromotionPriceDTO.getNumber()));
BasePromotion basePromotion = new BasePromotion();
basePromotion.setId(coupon.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;
@ -256,7 +257,11 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
private NativeSearchQueryBuilder createSearchQueryBuilder(EsGoodsSearchDTO searchDTO, PageVO pageVo, boolean isAggregation) {
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
if (pageVo != null) {
Pageable pageable = PageRequest.of(pageVo.getPageNumber(), pageVo.getPageSize());
Integer pageNumber = pageVo.getPageNumber() - 1;
if (pageNumber < 0) {
pageNumber = 0;
}
Pageable pageable = PageRequest.of(pageNumber, pageVo.getPageSize());
//分页
nativeSearchQueryBuilder.withPageable(pageable);
}
@ -336,6 +341,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 +352,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);