Skip to content

Commit

Permalink
!2 修复优惠券价格分摊不对问题
Browse files Browse the repository at this point in the history
Merge pull request !2 from OceansDeep/feature/pg
  • Loading branch information
OceansDeep authored and gitee-org committed May 24, 2021
2 parents e4aa6ab + 43ea8cd commit 7560855
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public boolean addJobId(String jobId, Integer time) {
instance.add(Calendar.SECOND, time);
long delaySeconds = instance.getTimeInMillis() / 1000;
boolean result = redisUtil.zadd(setDelayQueueName(), delaySeconds, jobId);
log.info("redis add delay, key {}, delay time {}", setDelayQueueName(), delaySeconds);
log.info("redis add delay, key {}, delay time {}", setDelayQueueName(), time);
return result;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import cn.lili.modules.order.order.entity.dos.Trade;
import cn.lili.modules.order.order.entity.vo.ReceiptVO;
import cn.lili.modules.promotion.entity.dos.MemberCoupon;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.enums.CouponScopeTypeEnum;
import cn.lili.modules.promotion.entity.enums.MemberCouponStatusEnum;
import cn.lili.modules.promotion.entity.enums.PromotionTypeEnum;
Expand All @@ -43,6 +44,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -137,6 +139,9 @@ public void add(String skuId, Integer num, String cartType) {
cartSkuVOS.add(cartSkuVO);
}
tradeDTO.setCartTypeEnum(cartTypeEnum);
// 如购物车发生更改,则重置优惠券
tradeDTO.setStoreCoupons(null);
tradeDTO.setPlatformCoupon(null);
this.resetTradeDTO(tradeDTO);
} catch (Exception e) {
log.error("购物车渲染异常", e);
Expand Down Expand Up @@ -294,7 +299,7 @@ public void resetTradeDTO(TradeDTO tradeDTO) {

@Override
public TradeDTO getCheckedTradeDTO(CartTypeEnum way) {
return this.readDTO(way);
return tradeBuilder.buildTrade(way);
}

/**
Expand Down Expand Up @@ -474,7 +479,7 @@ public void selectCoupon(String couponId, String way, boolean use) {
}
//使用优惠券 与否
if (use && checkCoupon(memberCoupon, tradeDTO)) {
this.useCoupon(tradeDTO, memberCoupon);
this.useCoupon(tradeDTO, memberCoupon, cartTypeEnum);
} else if (!use) {
if (Boolean.TRUE.equals(memberCoupon.getIsPlatform())) {
tradeDTO.setPlatformCoupon(null);
Expand Down Expand Up @@ -563,10 +568,23 @@ private CartTypeEnum getCartType(String way) {
return cartTypeEnum;
}

private void useCoupon(TradeDTO tradeDTO, MemberCoupon memberCoupon) {
private void useCoupon(TradeDTO tradeDTO, MemberCoupon memberCoupon, CartTypeEnum cartTypeEnum) {
//如果是平台优惠券
if (Boolean.TRUE.equals(memberCoupon.getIsPlatform())) {
if (memberCoupon.getConsumeThreshold() <= tradeDTO.getPriceDetailDTO().getGoodsPrice()) {
// 购物车价格
Double cartPrice = 0d;
for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) {
// 获取商品的促销信息
Optional<PromotionGoods> promotionOptional = cartSkuVO.getPromotions().parallelStream().filter(i -> (i.getPromotionType().equals(PromotionTypeEnum.PINTUAN.name()) && cartTypeEnum.equals(CartTypeEnum.PINTUAN))
|| i.getPromotionType().equals(PromotionTypeEnum.SECKILL.name())).findAny();
if (promotionOptional.isPresent()) {
cartPrice += CurrencyUtil.mul(promotionOptional.get().getPrice(), cartSkuVO.getNum());
} else {
cartPrice += CurrencyUtil.mul(cartSkuVO.getGoodsSku().getPrice(), cartSkuVO.getNum());
}

}
if (memberCoupon.getConsumeThreshold() <= cartPrice) {
tradeDTO.setPlatformCoupon(new MemberCouponDTO(memberCoupon));
tradeDTO.setStoreCoupons(new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ Integer selectInnerOverlapPromotionGoods(@Param("promotionType") String promotio
@Param("skuId") String skuId,
@Param("startTime") Date startTime,
@Param("endTime") Date endTime);

/**
* 查询参加活动促销商品是否同时参加指定类型的活动
*
* @param promotionType 促销类型
* @param skuId skuId
* @param startTime 开始时间
* @param endTime 结束时间
* @return 共参加了几种活动
*/
@Select("select count(0) from li_promotion_goods where promotion_type = #{promotionType} and sku_id = #{skuId} and (" +
"( start_time < #{startTime} && end_time > #{startTime} ) || ( start_time < #{endTime} && end_time > #{endTime} ) || " +
"( start_time < #{startTime} && end_time > #{endTime} ) || ( start_time > #{startTime} && end_time < #{endTime} )" +
" || promotion_status = 'START' ) and promotion_id != #{promotionId}")
Integer selectInnerOverlapPromotionGoodsWithout(@Param("promotionType") String promotionType,
@Param("skuId") String skuId,
@Param("startTime") Date startTime,
@Param("endTime") Date endTime,
@Param("promotionId") String promotionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ static String getPromotionGoodsStockCacheKey(PromotionTypeEnum typeEnum, String
* @param skuId skuId
* @param startTime 开始时间
* @param endTime 结束时间
* @param promotionId 促销活动id(是否排除当前活动,如排除,则填写,没有的话,为null)
* @return 共参加了几种活动
*/
Integer findInnerOverlapPromotionGoods(String promotionType, String skuId, Date startTime, Date endTime);
Integer findInnerOverlapPromotionGoods(String promotionType, String skuId, Date startTime, Date endTime, String promotionId);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void checkCouponLimit(String couponId, String memberId) {
throw new ServiceException("优惠券剩余领取数量不足");
}
if (haveCoupons >= coupon.getCouponLimitNum()) {
throw new ServiceException("此优惠券最多领取" + coupon.getCouponLimitNum()+"张");
throw new ServiceException("此优惠券最多领取" + coupon.getCouponLimitNum() + "张");
}
}

Expand Down Expand Up @@ -96,7 +96,9 @@ public IPage<MemberCoupon> getMemberCoupons(CouponSearchParams param, PageVO pag
@Override
public IPage<MemberCoupon> getMemberCouponsByCanUse(CouponSearchParams param, Double totalPrice, PageVO pageVo) {
LambdaQueryWrapper<MemberCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(MemberCoupon::getStoreId, Arrays.asList(param.getStoreId(), "platform"));
List<String> storeIds = new ArrayList<>(Arrays.asList(param.getStoreId().split(",")));
storeIds.add("platform");
queryWrapper.in(MemberCoupon::getStoreId, storeIds);
queryWrapper.eq(MemberCoupon::getMemberId, param.getMemberId());
queryWrapper.and(
i -> i.like(MemberCoupon::getScopeId, param.getScopeId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ private void updatePintuanPromotionGoods(PintuanVO pintuan) {
throw new ServiceException();
}
// 查询是否在同一时间段参与了拼团活动
Integer count = promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.SECKILL.name(), promotionGood.getSkuId(), pintuan.getStartTime(), pintuan.getEndTime());
Integer count = promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.SECKILL.name(), promotionGood.getSkuId(), pintuan.getStartTime(), pintuan.getEndTime(), pintuan.getId());
// 查询是否在同一时间段参与了限时抢购活动
count += promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.PINTUAN.name(), promotionGood.getSkuId(), pintuan.getStartTime(), pintuan.getEndTime());
count += promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.PINTUAN.name(), promotionGood.getSkuId(), pintuan.getStartTime(), pintuan.getEndTime(), pintuan.getId());
if (count > 0) {
log.error("商品[" + promotionGood.getGoodsName() + "]已经在重叠的时间段参加了限时抢购或拼团活动,不能参加拼团活动");
throw new ServiceException("商品[" + promotionGood.getGoodsName() + "]已经在重叠的时间段参加了限时抢购或拼团活动,不能参加拼团活动");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void updatePromotion(CartSkuVO cartSkuVO) {
if (pointsGoods != null) {
cartSkuVO.setPoint(pointsGoods.getPoints().intValue());
}
DistributionGoods distributionGoods=distributionGoodsService.distributionGoodsVOBySkuId(cartSkuVO.getGoodsSku().getId());
DistributionGoods distributionGoods = distributionGoodsService.distributionGoodsVOBySkuId(cartSkuVO.getGoodsSku().getId());
if (distributionGoods != null) {
cartSkuVO.setDistributionGoods(distributionGoods);
}
Expand Down Expand Up @@ -246,8 +246,12 @@ public IPage<PromotionGoodsDTO> getCurrentPromotionGoods(String promotionType, P


@Override
public Integer findInnerOverlapPromotionGoods(String promotionType, String skuId, Date startTime, Date endTime) {
return this.baseMapper.selectInnerOverlapPromotionGoods(promotionType, skuId, startTime, endTime);
public Integer findInnerOverlapPromotionGoods(String promotionType, String skuId, Date startTime, Date endTime, String promotionId) {
if (promotionId != null) {
return this.baseMapper.selectInnerOverlapPromotionGoodsWithout(promotionType, skuId, startTime, endTime, promotionId);
} else {
return this.baseMapper.selectInnerOverlapPromotionGoods(promotionType, skuId, startTime, endTime);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private List<GoodsSkuPromotionPriceDTO> filterCoupon(MemberCoupon coupon, List<G
private double calculationSingleCoupon(MemberCoupon coupon, List<GoodsSkuPromotionPriceDTO> conformCollect) {
double couponTotalPrice = 0;
// 合计优惠券范围内的所有商品的原价
double totalPrice = conformCollect.parallelStream().mapToDouble(GoodsSkuPromotionPriceDTO::getOriginalPrice).sum();
double totalPrice = conformCollect.parallelStream().mapToDouble(GoodsSkuPromotionPriceDTO::getTotalFinalePrice).sum();

// 根据优惠券优惠类型,判断是否满足条件
if (CouponTypeEnum.PRICE.name().equals(coupon.getCouponType()) && coupon.getConsumeThreshold() <= totalPrice) {
Expand All @@ -314,14 +314,14 @@ private double calculationSingleCoupon(MemberCoupon coupon, List<GoodsSkuPromoti

// 分配到每个商品的优惠券金额
for (GoodsSkuPromotionPriceDTO goodsSkuPromotionPriceDTO : conformCollect) {
double rate = CurrencyUtil.div(goodsSkuPromotionPriceDTO.getFinalePrice(), totalPrice, 5);
double distributeCouponPrice = CurrencyUtil.mul(couponTotalPrice, rate);
double rate = CurrencyUtil.div(goodsSkuPromotionPriceDTO.getTotalFinalePrice(), totalPrice, 5);
double distributeCouponPrice = CurrencyUtil.div(CurrencyUtil.mul(couponTotalPrice, rate), goodsSkuPromotionPriceDTO.getNumber());
if (goodsSkuPromotionPriceDTO.getFinalePrice() != null) {
goodsSkuPromotionPriceDTO.setFinalePrice(CurrencyUtil.sub(goodsSkuPromotionPriceDTO.getFinalePrice(), distributeCouponPrice));
} else {
goodsSkuPromotionPriceDTO.setFinalePrice(CurrencyUtil.sub(goodsSkuPromotionPriceDTO.getOriginalPrice(), distributeCouponPrice));
}
goodsSkuPromotionPriceDTO.setCouponPrice(distributeCouponPrice);
goodsSkuPromotionPriceDTO.setCouponPrice(CurrencyUtil.mul(distributeCouponPrice, goodsSkuPromotionPriceDTO.getNumber()));
goodsSkuPromotionPriceDTO.setTotalFinalePrice(CurrencyUtil.mul(goodsSkuPromotionPriceDTO.getFinalePrice(), goodsSkuPromotionPriceDTO.getNumber()));
BasePromotion basePromotion = new BasePromotion();
basePromotion.setId(coupon.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ public boolean updatePromotionStatus(PromotionMessage promotionMessage) {
pintuanVO.setPromotionStatus(promotionMessage.getPromotionStatus());
result = this.pintuanService.update(promotionMessage.updateWrapper());
this.promotionGoodsService.updateBatchById(pintuanVO.getPromotionGoodsList());
List<PromotionGoods> promotionGoodsList = pintuanVO.getPromotionGoodsList();
// 更新促销商品索引
for (PromotionGoods promotionGoods : promotionGoodsList) {
Pintuan pintuan1 = JSONUtil.toBean(JSONUtil.toJsonStr(pintuanVO), Pintuan.class);
this.goodsIndexService.updateEsGoodsIndex(promotionGoods.getSkuId(), pintuan1, esPromotionKey, promotionGoods.getPrice());
if (pintuanVO.getPromotionGoodsList() != null) {
List<PromotionGoods> promotionGoodsList = pintuanVO.getPromotionGoodsList();
// 更新促销商品索引
for (PromotionGoods promotionGoods : promotionGoodsList) {
Pintuan pintuan1 = JSONUtil.toBean(JSONUtil.toJsonStr(pintuanVO), Pintuan.class);
this.goodsIndexService.updateEsGoodsIndex(promotionGoods.getSkuId(), pintuan1, esPromotionKey, promotionGoods.getPrice());
}
}
this.mongoTemplate.save(pintuanVO);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
Expand Down Expand Up @@ -256,9 +255,9 @@ public void addSeckillApply(String seckillId, String storeId, List<SeckillApplyV
String endTimeStr = DateUtil.toString(seckill.getStartTime(), "yyyy-MM-dd") + " 23:59:59";

// 查询是否在同一时间段参与了拼团活动
Integer count = promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.PINTUAN.name(), goodsSku.getId(), DateUtil.toDate(startTimeStr, DateUtil.STANDARD_FORMAT), DateUtil.toDate(endTimeStr, DateUtil.STANDARD_FORMAT));
Integer count = promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.PINTUAN.name(), goodsSku.getId(), DateUtil.toDate(startTimeStr, DateUtil.STANDARD_FORMAT), DateUtil.toDate(endTimeStr, DateUtil.STANDARD_FORMAT), seckillId);
// 查询是否在同一时间段参与了限时抢购活动
count += promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.SECKILL.name(), goodsSku.getId(), DateUtil.toDate(startTimeStr, DateUtil.STANDARD_FORMAT), DateUtil.toDate(endTimeStr, DateUtil.STANDARD_FORMAT));
count += promotionGoodsService.findInnerOverlapPromotionGoods(PromotionTypeEnum.SECKILL.name(), goodsSku.getId(), DateUtil.toDate(startTimeStr, DateUtil.STANDARD_FORMAT), DateUtil.toDate(endTimeStr, DateUtil.STANDARD_FORMAT), seckillId);
if (count > 0) {
throw new ServiceException("商品[" + goodsSku.getGoodsName() + "]已经在重叠的时间段参加了团购或限时抢购活动,不能参加限时抢购活动");
}
Expand Down

0 comments on commit 7560855

Please sign in to comment.