fix: 优化促销相关流程,修复部分促销相关bug。

This commit is contained in:
misworga831 2023-04-10 14:43:04 +08:00
parent 1cb72a5bdd
commit e18b62da6e
14 changed files with 156 additions and 101 deletions

View File

@ -82,6 +82,7 @@ public enum ResultCode {
HAVE_INVALID_SALES_MODEL(11023, "批发规则存在小于等于0的无效数据"),
MUST_HAVE_GOODS_SKU_VALUE(11024, "规格值不能为空!"),
DO_NOT_MATCH_WHOLESALE(11025, "批发商品购买数量不能低于起拍量!"),
GOODS_NOT_ERROR(11026, "商品不存在"),
GOODS_PARAMS_ERROR(11013, "商品参数错误,刷新后重试"),
PHYSICAL_GOODS_NEED_TEMP(11014, "实物商品需选择配送模板"),

View File

@ -128,4 +128,11 @@ public interface AbstractPromotionsService<T extends BasePromotions> extends ISe
*/
PromotionTypeEnum getPromotionType();
/**
* 是否允许同一时间内存在相同的促销
*
* @return 是否允许同一时间内存在相同的促销
*/
boolean allowExistSame();
}

View File

@ -165,4 +165,9 @@ public interface MemberCouponService extends IService<MemberCoupon> {
*/
Page<MemberCouponVO> getMemberCouponsPage(Page<MemberCoupon> page, MemberCouponSearchParams param);
/**
* 获取会员领取过的优惠券数量
*/
long getMemberCouponNum(String memberId, String couponId);
}

View File

@ -192,6 +192,9 @@ public abstract class AbstractPromotionsServiceImpl<M extends BaseMapper<T>, T e
@Override
public void checkPromotions(T promotions) {
PromotionTools.checkPromotionTime(promotions.getStartTime(), promotions.getEndTime());
if (!this.allowExistSame()) {
this.checkSamePromotions(promotions);
}
}
/**
@ -264,4 +267,21 @@ public abstract class AbstractPromotionsServiceImpl<M extends BaseMapper<T>, T e
}
}
@Override
public boolean allowExistSame() {
return false;
}
public void checkSamePromotions(T promotions) {
if (promotions.getStartTime() == null || promotions.getEndTime() == null) {
return;
}
QueryWrapper<T> queryWrapper = PromotionTools.checkActiveTime(promotions.getStartTime(), promotions.getEndTime(), this.getPromotionType(), promotions.getStoreId(), promotions.getId());
long sameNum = this.count(queryWrapper);
//当前时间段是否存在同类活动
if (sameNum > 0) {
throw new ServiceException(ResultCode.PROMOTION_SAME_ACTIVE_EXIST);
}
}
}

View File

@ -310,11 +310,11 @@ public class CouponServiceImpl extends AbstractPromotionsServiceImpl<CouponMappe
*/
private void checkCouponPortionGoods(CouponVO coupon) {
String[] split = coupon.getScopeId().split(",");
if (split.length <= 0) {
if (split.length == 0) {
throw new ServiceException(ResultCode.COUPON_SCOPE_ERROR);
}
for (String id : split) {
GoodsSku goodsSku = goodsSkuService.getCanPromotionGoodsSkuByIdFromCache(id);
GoodsSku goodsSku = goodsSkuService.getGoodsSkuByIdFromCache(id);
if (goodsSku == null) {
throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
}

View File

@ -23,7 +23,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
@ -95,8 +94,6 @@ public class FullDiscountServiceImpl extends AbstractPromotionsServiceImpl<FullD
PromotionTools.checkPromotionTime(fullDiscountVO.getStartTime(), fullDiscountVO.getEndTime());
}
//当前时间段是否存在同类活动
this.checkSameActiveExist(promotions.getStartTime(), promotions.getEndTime(), promotions.getStoreId(), promotions.getId());
//检查满减参数
this.checkFullDiscount(promotions);
@ -112,11 +109,11 @@ public class FullDiscountServiceImpl extends AbstractPromotionsServiceImpl<FullD
@Transactional(rollbackFor = {Exception.class})
public boolean updatePromotionsGoods(FullDiscount promotions) {
boolean result = super.updatePromotionsGoods(promotions);
if (!PromotionsStatusEnum.CLOSE.name().equals(promotions.getPromotionStatus())
&& PromotionsScopeTypeEnum.PORTION_GOODS.name().equals(promotions.getScopeType())
&& promotions instanceof FullDiscountVO) {
if (!PromotionsStatusEnum.CLOSE.name().equals(promotions.getPromotionStatus()) && PromotionsScopeTypeEnum.PORTION_GOODS.name().equals(promotions.getScopeType()) && promotions instanceof FullDiscountVO) {
FullDiscountVO fullDiscountVO = (FullDiscountVO) promotions;
List<PromotionGoods> promotionGoodsList = PromotionTools.promotionGoodsInit(fullDiscountVO.getPromotionGoodsList(), fullDiscountVO, PromotionTypeEnum.FULL_DISCOUNT);
List<PromotionGoods> promotionGoodsList =
PromotionTools.promotionGoodsInit(fullDiscountVO.getPromotionGoodsList(), fullDiscountVO,
PromotionTypeEnum.FULL_DISCOUNT);
this.promotionGoodsService.deletePromotionGoods(Collections.singletonList(promotions.getId()));
//促销活动商品更新
result = this.promotionGoodsService.saveBatch(promotionGoodsList);
@ -166,8 +163,7 @@ public class FullDiscountServiceImpl extends AbstractPromotionsServiceImpl<FullD
* @param fullDiscount 满减参数信息
*/
private void checkFullDiscount(FullDiscount fullDiscount) {
if (fullDiscount.getFullMinusFlag() == null && fullDiscount.getCouponFlag() == null && fullDiscount.getGiftFlag() == null
&& fullDiscount.getPointFlag() == null && fullDiscount.getFullRateFlag() == null) {
if (fullDiscount.getFullMinusFlag() == null && fullDiscount.getCouponFlag() == null && fullDiscount.getGiftFlag() == null && fullDiscount.getPointFlag() == null && fullDiscount.getFullRateFlag() == null) {
throw new ServiceException(ResultCode.FULL_DISCOUNT_WAY_ERROR);
}
//如果优惠方式是满减
@ -199,23 +195,6 @@ public class FullDiscountServiceImpl extends AbstractPromotionsServiceImpl<FullD
}
/**
* 检查同一时间段内不能存在相同的活动数量
*
* @param statTime 开始时间
* @param endTime 结束时间
* @param storeId 店铺id
* @param id 满优惠活动ID
*/
private void checkSameActiveExist(Date statTime, Date endTime, String storeId, String id) {
//同一时间段内相同的活动
QueryWrapper<FullDiscount> queryWrapper = PromotionTools.checkActiveTime(statTime, endTime, PromotionTypeEnum.FULL_DISCOUNT, storeId, id);
long sameNum = this.count(queryWrapper);
if (sameNum > 0) {
throw new ServiceException(ResultCode.PROMOTION_SAME_ACTIVE_EXIST);
}
}
/**
* 检查优惠券信息
*

View File

@ -327,6 +327,14 @@ public class MemberCouponServiceImpl extends ServiceImpl<MemberCouponMapper, Mem
return this.baseMapper.getMemberCoupons(page, queryWrapper);
}
@Override
public long getMemberCouponNum(String memberId, String couponId) {
LambdaQueryWrapper<MemberCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(MemberCoupon::getMemberId, memberId);
queryWrapper.eq(MemberCoupon::getCouponId, couponId);
return this.count(queryWrapper);
}
/**
* 清除无效的会员优惠券
*

View File

@ -31,12 +31,10 @@ import cn.lili.trigger.interfaces.TimeTrigger;
import cn.lili.trigger.model.TimeExecuteConstant;
import cn.lili.trigger.model.TimeTriggerMsg;
import cn.lili.trigger.util.DelayQueueTools;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@ -156,48 +154,6 @@ public class PintuanServiceImpl extends AbstractPromotionsServiceImpl<PintuanMap
return pintuanShareVO;
}
/**
* 更新促销状态
* 如果要更新促销状态为关闭startTime和endTime置为空即可
*
* @param ids 促销id集合
* @param startTime 开始时间
* @param endTime 结束时间
* @return 是否更新成功
*/
@Override
public boolean updateStatus(List<String> ids, Long startTime, Long endTime) {
if (startTime != null && endTime != null) {
for (String id : ids) {
Pintuan pintuan = this.getById(id);
QueryWrapper<Pintuan> queryWrapper = PromotionTools.checkActiveTime(new Date(startTime), new Date(endTime), PromotionTypeEnum.PINTUAN, pintuan.getStoreId(), id);
long sameNum = this.count(queryWrapper);
//当前时间段是否存在同类活动
if (sameNum > 0) {
throw new ServiceException(ResultCode.PROMOTION_SAME_ACTIVE_EXIST);
}
}
}
return super.updateStatus(ids, startTime, endTime);
}
/**
* 检查促销参数
*
* @param promotions 促销实体
*/
@Override
public void checkPromotions(Pintuan promotions) {
QueryWrapper<Pintuan> queryWrapper = PromotionTools.checkActiveTime(promotions.getStartTime(), promotions.getEndTime(), PromotionTypeEnum.PINTUAN, promotions.getStoreId(), promotions.getId());
long sameNum = this.count(queryWrapper);
//当前时间段是否存在同类活动
if (sameNum > 0) {
throw new ServiceException(ResultCode.PROMOTION_SAME_ACTIVE_EXIST);
}
super.checkPromotions(promotions);
}
/**
* 更新促销商品信息
*

View File

@ -74,7 +74,8 @@ public class PointsGoodsServiceImpl extends AbstractPromotionsServiceImpl<Points
if (this.checkSkuDuplicate(pointsGoods.getSkuId(), null) == null) {
pointsGoods.setPromotionName("积分商品活动");
} else {
throw new ServiceException("商品id为" + pointsGoods.getSkuId() + "的商品已参加积分商品活动!");
throw new ServiceException(ResultCode.PROMOTION_LOG_EXIST, "商品id为" + pointsGoods.getSkuId() +
"的商品已参加积分商品活动!");
}
GoodsSku goodsSku = this.checkSkuExist(pointsGoods.getSkuId());
pointsGoods.setStoreId(goodsSku.getStoreId());

View File

@ -262,17 +262,6 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
promotions.setStartTime(DateUtil.parse(startTimeStr, DatePattern.NORM_DATETIME_MINUTE_PATTERN));
promotions.setEndTime(DateUtil.endOfDay(promotions.getStartTime()));
}
if (promotions.getStartTime() != null && promotions.getEndTime() != null) {
//同一时间段内相同的活动
QueryWrapper<Seckill> queryWrapper = PromotionTools.checkActiveTime(promotions.getStartTime(), promotions.getEndTime(), PromotionTypeEnum.SECKILL, null, promotions.getId());
long sameNum = this.count(queryWrapper);
//当前时间段是否存在同类活动
if (sameNum > 0) {
throw new ServiceException(ResultCode.PROMOTION_SAME_ACTIVE_EXIST);
}
}
}
/**

View File

@ -34,6 +34,10 @@ public class PromotionTools {
public static final String PLATFORM_ID = "0";
public static final String PLATFORM_NAME = "platform";
private PromotionTools() {
throw new IllegalStateException("Utility class");
}
/**
* 参数验证
* 1活动起始时间必须大于当前时间
@ -87,13 +91,43 @@ public class PromotionTools {
} else {
queryWrapper.ge(START_TIME_COLUMN, DateUtil.beginOfDay(startTime)).le(END_TIME_COLUMN, DateUtil.endOfDay(endTime));
}
queryWrapper.eq(CharSequenceUtil.isNotEmpty(storeId), "store_id", storeId);
queryWrapper.ne(CharSequenceUtil.isNotEmpty(activityId), "id", activityId);
if (storeId != null) {
queryWrapper.eq("store_id", storeId);
}
if (activityId != null) {
queryWrapper.ne("id", activityId);
}
queryWrapper.and(i -> i.or(queryPromotionStatus(PromotionsStatusEnum.NEW)).or(queryPromotionStatus(PromotionsStatusEnum.START)));
queryWrapper.eq("delete_flag", false);
return queryWrapper;
}
/**
* 检查商品是否重复参加同类型活动
*
* @param exceptType 排除的促销活动类型可同时参加的活动类型
* @param skuIds 商品skuId
* @param activityId 当前活动id
* @return mybatis plus query wrapper对象
*/
public static QueryWrapper<PromotionGoods> checkSkuDuplicate(List<PromotionTypeEnum> exceptType, List<String> skuIds, String activityId) {
QueryWrapper<PromotionGoods> queryWrapper = new QueryWrapper<>();
if (skuIds != null && !skuIds.isEmpty()) {
queryWrapper.in("sku_id", skuIds);
}
if (CharSequenceUtil.isNotEmpty(activityId)) {
queryWrapper.ne("id", activityId);
}
queryWrapper.and(i -> i.or(PromotionTools.queryPromotionStatus(PromotionsStatusEnum.START)).or(PromotionTools.queryPromotionStatus(PromotionsStatusEnum.NEW)));
if (exceptType != null) {
queryWrapper.notIn(!exceptType.isEmpty(), "promotion_type", exceptType.stream().map(PromotionTypeEnum::name).collect(Collectors.toList()));
}
queryWrapper.eq("delete_flag", false);
return queryWrapper;
}
public static <T> Consumer<QueryWrapper<T>> queryPromotionStatus(PromotionsStatusEnum promotionsStatusEnum) {
switch (promotionsStatusEnum) {
@ -158,6 +192,12 @@ public class PromotionTools {
return nextHour;
}
/**
* 过滤无效促销活动
*
* @param map 促销活动map
* @return 过滤后的促销活动map
*/
public static Map<String, Object> filterInvalidPromotionsMap(Map<String, Object> map) {
if (CollUtil.isEmpty(map)) {
return new HashMap<>();
@ -173,9 +213,22 @@ public class PromotionTools {
return i.getValue() != null;
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> newValue));
} catch (Exception e) {
log.error("过滤无效促销活动出现异常。异常促销信息:{},异常信息{} ", map, e);
log.error("过滤无效促销活动出现异常。异常促销信息:{},异常信息 ", map, e);
return new HashMap<>();
}
}
/**
* 是否为需要检查的促销活动类型(用于判定部分类型的商品不能参与活动的条件)
* 内容为不需要检查的促销活动类型
*
* @param key 促销key
* @return 当前促销key是否存在
*/
public static boolean isPromotionsTypeNeedsToChecked(String key) {
return !CharSequenceUtil.containsAny(key,
PromotionTypeEnum.COUPON.name(),
PromotionTypeEnum.FULL_DISCOUNT.name());
}
}

View File

@ -58,6 +58,24 @@ public class EsGoodsSearchDTO {
@ApiModelProperty(value = "促销活动id")
private String promotionsId;
/**
* @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
*/
@ApiModelProperty(value = "商品类型")
private String goodsType;
@ApiModelProperty("销售模式")
private String salesModel;
/**
* @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
*/
@ApiModelProperty(value = "除了当前商品类型之外")
private String neGoodsType;
@ApiModelProperty("除了销售模式当前销售模式之外")
private String neSalesModel;
//过滤搜索关键字
public String getKeyword() {
if (CharSequenceUtil.isNotEmpty(keyword)) {

View File

@ -22,6 +22,7 @@ import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsSalesModeEnum;
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
import cn.lili.modules.goods.service.BrandService;
import cn.lili.modules.goods.service.CategoryService;
@ -542,6 +543,10 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
public UpdateRequest updateEsGoodsIndexPromotions(String id, BasePromotions promotion, String key) {
EsGoodsIndex goodsIndex = findById(id);
if (goodsIndex != null) {
// 批发商品不参与促销除优惠券和满减
if (PromotionTools.isPromotionsTypeNeedsToChecked(key) && GoodsSalesModeEnum.WHOLESALE.name().equals(goodsIndex.getSalesModel())) {
return null;
}
//更新索引
return this.updateGoodsIndexPromotion(goodsIndex, key, promotion);
} else {
@ -610,24 +615,23 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements
private void executeUpdateEsGoodsIndexAll(BasePromotions promotion, String key) {
for (int i = 0; ; 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();
if (PromotionTools.isPromotionsTypeNeedsToChecked(key)) {
searchDTO.setSalesModel(GoodsSalesModeEnum.RETAIL.name());
}
//如果storeId不为空则表示是店铺活动
if (promotion.getStoreId() != null && !promotion.getStoreId().equals(PromotionTools.PLATFORM_ID)) {
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());
}
skuIds = esGoodsIndices.isEmpty() ? new ArrayList<>() :
esGoodsIndices.getContent().stream().map(SearchHit::getId).collect(Collectors.toList());
if (skuIds.isEmpty()) {
break;
}

View File

@ -466,6 +466,20 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService {
if (searchDTO.getRecommend() != null) {
filterBuilder.filter(QueryBuilders.termQuery("recommend", searchDTO.getRecommend()));
}
// 商品类型判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getGoodsType())) {
filterBuilder.filter(QueryBuilders.termQuery("goodsType", searchDTO.getGoodsType()));
}
if (CharSequenceUtil.isNotEmpty(searchDTO.getNeGoodsType())) {
filterBuilder.mustNot(QueryBuilders.termQuery("goodsType", searchDTO.getNeGoodsType()));
}
// 销售类型判定
if (CharSequenceUtil.isNotEmpty(searchDTO.getSalesModel())) {
filterBuilder.filter(QueryBuilders.termQuery("salesModel", searchDTO.getSalesModel()));
}
if (CharSequenceUtil.isNotEmpty(searchDTO.getNeSalesModel())) {
filterBuilder.mustNot(QueryBuilders.termQuery("salesModel", searchDTO.getNeSalesModel()));
}
//规格项判定
if (searchDTO.getNameIds() != null && !searchDTO.getNameIds().isEmpty()) {
filterBuilder.must(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.termsQuery("attrList.nameId", searchDTO.getNameIds()), ScoreMode.None));