增加结算时,检查使用的优惠券是否存在与用户持有的优惠券中
This commit is contained in:
parent
76c253041a
commit
099708390d
@ -1,5 +1,6 @@
|
||||
package cn.lili.modules.order.cart.render.impl;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.lili.common.enums.PromotionTypeEnum;
|
||||
import cn.lili.common.utils.CurrencyUtil;
|
||||
import cn.lili.common.utils.StringUtils;
|
||||
@ -18,6 +19,8 @@ import cn.lili.modules.promotion.service.MemberCouponService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@ -31,22 +34,21 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class CouponRender implements CartRenderStep {
|
||||
|
||||
@Autowired
|
||||
private PromotionPriceUtil promotionPriceUtil;
|
||||
@Autowired
|
||||
private MemberCouponService memberCouponService;
|
||||
|
||||
@Override
|
||||
public RenderStepEnums step() {
|
||||
return RenderStepEnums.COUPON;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private PromotionPriceUtil promotionPriceUtil;
|
||||
|
||||
@Autowired
|
||||
private MemberCouponService memberCouponService;
|
||||
|
||||
@Override
|
||||
public void render(TradeDTO tradeDTO) {
|
||||
|
||||
//优惠券列表
|
||||
renderCouponRule(tradeDTO);
|
||||
this.renderCouponRule(tradeDTO);
|
||||
//主要渲染各个优惠的价格
|
||||
this.renderCoupon(tradeDTO);
|
||||
}
|
||||
@ -54,27 +56,55 @@ public class CouponRender implements CartRenderStep {
|
||||
|
||||
/**
|
||||
* 渲染优惠券规则
|
||||
*
|
||||
* @param tradeDTO 交易dto
|
||||
*/
|
||||
private void renderCouponRule(TradeDTO tradeDTO) {
|
||||
List<MemberCoupon> memberCouponList = memberCouponService.getMemberCoupons();
|
||||
|
||||
memberCouponList.forEach(memberCoupon -> {
|
||||
available(tradeDTO, memberCoupon);
|
||||
});
|
||||
if (!memberCouponList.isEmpty()) {
|
||||
this.checkMemberExistCoupon(tradeDTO, memberCouponList);
|
||||
} else {
|
||||
tradeDTO.setPlatformCoupon(null);
|
||||
tradeDTO.setStoreCoupons(new HashMap<>());
|
||||
}
|
||||
memberCouponList.forEach(memberCoupon -> available(tradeDTO, memberCoupon));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查使用中的优惠券是否存在与用户的优惠券中
|
||||
*
|
||||
* @param tradeDTO 交易dto
|
||||
* @param memberCouponList 会员优惠券列表
|
||||
*/
|
||||
private void checkMemberExistCoupon(TradeDTO tradeDTO, List<MemberCoupon> memberCouponList) {
|
||||
if (tradeDTO.getPlatformCoupon() != null && tradeDTO.getPlatformCoupon().getMemberCoupon() != null) {
|
||||
boolean b = memberCouponList.parallelStream().anyMatch(i -> i.getId().equals(tradeDTO.getPlatformCoupon().getMemberCoupon().getId()));
|
||||
if (!b) {
|
||||
tradeDTO.setPlatformCoupon(null);
|
||||
}
|
||||
}
|
||||
if (!tradeDTO.getStoreCoupons().isEmpty()) {
|
||||
for (Map.Entry<String, MemberCouponDTO> entry : tradeDTO.getStoreCoupons().entrySet()) {
|
||||
if (entry.getValue().getMemberCoupon() != null && memberCouponList.parallelStream().noneMatch(i -> i.getId().equals(entry.getValue().getMemberCoupon().getId()))) {
|
||||
tradeDTO.getStoreCoupons().remove(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定优惠券是否可用
|
||||
*
|
||||
* @param tradeDTO
|
||||
* @param memberCoupon
|
||||
* @param tradeDTO 交易dto
|
||||
* @param memberCoupon 会员优惠券
|
||||
*/
|
||||
private void available(TradeDTO tradeDTO, MemberCoupon memberCoupon) {
|
||||
if (memberCoupon == null) {
|
||||
return;
|
||||
}
|
||||
List<CartSkuVO> filterSku = filterSkuVo(tradeDTO.getSkuList(), memberCoupon);
|
||||
if (filterSku == null || filterSku.size() == 0) {
|
||||
if (filterSku == null || filterSku.isEmpty()) {
|
||||
tradeDTO.getCantUseCoupons().add(new MemberCouponVO(memberCoupon,
|
||||
"购物车中没有满足优惠券使用范围的优惠券"));
|
||||
return;
|
||||
@ -100,23 +130,21 @@ public class CouponRender implements CartRenderStep {
|
||||
/**
|
||||
* 过滤购物车商品信息,按照优惠券的适用范围过滤
|
||||
*
|
||||
* @param cartSkuVOS
|
||||
* @param memberCoupon
|
||||
* @return
|
||||
* @param cartSkuVOS 购物车中的产品列表
|
||||
* @param memberCoupon 会员优惠券
|
||||
* @return 按照优惠券的适用范围过滤的购物车商品信息
|
||||
*/
|
||||
private List<CartSkuVO> filterSkuVo(List<CartSkuVO> cartSkuVOS, MemberCoupon memberCoupon) {
|
||||
|
||||
List<CartSkuVO> filterSku;
|
||||
//平台店铺过滤
|
||||
if (memberCoupon.getIsPlatform()) {
|
||||
if (Boolean.TRUE.equals(memberCoupon.getIsPlatform())) {
|
||||
filterSku = cartSkuVOS;
|
||||
} else {
|
||||
filterSku = cartSkuVOS.stream().filter(cartSkuVO -> {
|
||||
return cartSkuVO.getStoreId().equals(memberCoupon.getStoreId());
|
||||
}).collect(Collectors.toList());
|
||||
filterSku = cartSkuVOS.stream().filter(cartSkuVO -> cartSkuVO.getStoreId().equals(memberCoupon.getStoreId())).collect(Collectors.toList());
|
||||
}
|
||||
if (filterSku == null || filterSku.size() == 0) {
|
||||
return null;
|
||||
if (filterSku == null || filterSku.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
//优惠券类型判定
|
||||
switch (CouponScopeTypeEnum.valueOf(memberCoupon.getScopeType())) {
|
||||
@ -124,28 +152,12 @@ public class CouponRender implements CartRenderStep {
|
||||
return filterSku;
|
||||
case PORTION_GOODS:
|
||||
//按照商品过滤
|
||||
filterSku = filterSku.stream().filter(cartSkuVO -> {
|
||||
return memberCoupon.getScopeId().indexOf(cartSkuVO.getGoodsSku().getId()) > 0;
|
||||
}).collect(Collectors.toList());
|
||||
filterSku = filterSku.stream().filter(cartSkuVO -> memberCoupon.getScopeId().contains(cartSkuVO.getGoodsSku().getId())).collect(Collectors.toList());
|
||||
break;
|
||||
|
||||
case PORTION_SHOP_CATEGORY:
|
||||
//按照店铺分类过滤
|
||||
filterSku = filterSku.stream().filter(cartSkuVO -> {
|
||||
if (StringUtils.isNotEmpty(cartSkuVO.getGoodsSku().getStoreCategoryPath())) {
|
||||
//获取店铺分类
|
||||
String[] storeCategoryPath = cartSkuVO.getGoodsSku().getStoreCategoryPath().split(",");
|
||||
for (String category : storeCategoryPath) {
|
||||
//店铺分类只要有一项吻合,即可返回true
|
||||
if (memberCoupon.getScopeId().indexOf(category) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
filterSku = this.filterPromotionShopCategory(filterSku, memberCoupon);
|
||||
break;
|
||||
|
||||
case PORTION_GOODS_CATEGORY:
|
||||
@ -156,18 +168,37 @@ public class CouponRender implements CartRenderStep {
|
||||
String[] categoryPath = cartSkuVO.getGoodsSku().getCategoryPath().split(",");
|
||||
//平台三级分类
|
||||
String categoryId = categoryPath[categoryPath.length - 1];
|
||||
if (memberCoupon.getScopeId().indexOf(categoryId) > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return memberCoupon.getScopeId().contains(categoryId);
|
||||
}).collect(Collectors.toList());
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return filterSku;
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券按照店铺分类过滤
|
||||
*
|
||||
* @param filterSku 过滤的购物车商品信息
|
||||
* @param memberCoupon 会员优惠
|
||||
* @return 优惠券按照店铺分类过滤的购物车商品信息
|
||||
*/
|
||||
private List<CartSkuVO> filterPromotionShopCategory(List<CartSkuVO> filterSku, MemberCoupon memberCoupon) {
|
||||
return filterSku.stream().filter(cartSkuVO -> {
|
||||
if (CharSequenceUtil.isNotEmpty(cartSkuVO.getGoodsSku().getStoreCategoryPath())) {
|
||||
//获取店铺分类
|
||||
String[] storeCategoryPath = cartSkuVO.getGoodsSku().getStoreCategoryPath().split(",");
|
||||
for (String category : storeCategoryPath) {
|
||||
//店铺分类只要有一项吻合,即可返回true
|
||||
if (memberCoupon.getScopeId().contains(category)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染优惠券
|
||||
@ -212,55 +243,78 @@ public class CouponRender implements CartRenderStep {
|
||||
coupon.setPrice(countPrice);
|
||||
}
|
||||
|
||||
//减免现金,则按照商品价格计算 需要通过工具类进行优惠金额的分发,分发给每个商品
|
||||
if (coupon.getCouponType().equals(CouponTypeEnum.PRICE.name())) {
|
||||
//分发优惠券
|
||||
promotionPriceUtil.recountPrice(tradeDTO, memberCouponDTO.getSkuDetail(), memberCouponDTO.getMemberCoupon().getPrice(),
|
||||
coupon.getIsPlatform() ?
|
||||
PromotionTypeEnum.PLATFORM_COUPON : PromotionTypeEnum.COUPON);
|
||||
//如果是平台券 则需要计算商家承担比例
|
||||
if (coupon.getIsPlatform() && coupon.getStoreCommission() > 0) {
|
||||
|
||||
//循环所有优惠券
|
||||
for (String skuId : couponMap.keySet()) {
|
||||
|
||||
for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) {
|
||||
//写入平台优惠券承担比例
|
||||
if (cartSkuVO.getGoodsSku().getId().equals(skuId)) {
|
||||
//写入店铺承担比例
|
||||
cartSkuVO.getPriceDetailDTO().setSiteCouponPoint(coupon.getStoreCommission());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//减免现金,则按照商品价格计算 需要通过工具类进行优惠金额的分发,分发给每个商品
|
||||
this.renderCouponPrice(couponMap, tradeDTO, coupon, memberCouponDTO);
|
||||
} else {
|
||||
//打折券 直接计算
|
||||
this.renderCouponDiscount(couponMap, tradeDTO, coupon);
|
||||
}
|
||||
//打折券 直接计算
|
||||
else {
|
||||
}
|
||||
|
||||
/**
|
||||
* 减免现金,则按照商品价格计算 需要通过工具类进行优惠金额的分发,分发给每个商品
|
||||
*
|
||||
* @param couponMap 优惠券结算信息
|
||||
* @param tradeDTO 交易dto
|
||||
* @param coupon 优惠券信息
|
||||
* @param memberCouponDTO 用于计算优惠券结算详情
|
||||
*/
|
||||
private void renderCouponPrice(Map<String, Double> couponMap, TradeDTO tradeDTO, MemberCoupon coupon, MemberCouponDTO memberCouponDTO) {
|
||||
//分发优惠券
|
||||
promotionPriceUtil.recountPrice(tradeDTO, memberCouponDTO.getSkuDetail(), memberCouponDTO.getMemberCoupon().getPrice(),
|
||||
Boolean.TRUE.equals(coupon.getIsPlatform()) ?
|
||||
PromotionTypeEnum.PLATFORM_COUPON : PromotionTypeEnum.COUPON);
|
||||
//如果是平台券 则需要计算商家承担比例
|
||||
if (Boolean.TRUE.equals(coupon.getIsPlatform()) && coupon.getStoreCommission() > 0) {
|
||||
|
||||
//循环所有优惠券
|
||||
for (String skuId : couponMap.keySet()) {
|
||||
|
||||
// 循环购物车商品
|
||||
for (CartSkuVO item : tradeDTO.getSkuList()) {
|
||||
//如果id相等,则渲染商品价格信息
|
||||
if (item.getGoodsSku().getId().equals(skuId)) {
|
||||
|
||||
PriceDetailDTO priceDetailDTO = item.getPriceDetailDTO();
|
||||
|
||||
// 打折金额=商品金额*折扣/10
|
||||
Double discountCouponPrice = CurrencyUtil.mul(priceDetailDTO.getGoodsPrice(),
|
||||
CurrencyUtil.sub(1, CurrencyUtil.div(coupon.getDiscount(), 10, 3)));
|
||||
|
||||
//平台券则写入店铺承担优惠券比例
|
||||
if (coupon.getIsPlatform()) {
|
||||
priceDetailDTO.setSiteCouponPrice(discountCouponPrice);
|
||||
priceDetailDTO.setSiteCouponPoint(coupon.getStoreCommission());
|
||||
}
|
||||
priceDetailDTO.setCouponPrice(CurrencyUtil.add(priceDetailDTO.getCouponPrice(), discountCouponPrice));
|
||||
|
||||
for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) {
|
||||
//写入平台优惠券承担比例
|
||||
if (cartSkuVO.getGoodsSku().getId().equals(skuId)) {
|
||||
//写入店铺承担比例
|
||||
cartSkuVO.getPriceDetailDTO().setSiteCouponPoint(coupon.getStoreCommission());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 打折券计算
|
||||
*
|
||||
* @param couponMap 优惠券结算信息
|
||||
* @param tradeDTO 交易dto
|
||||
* @param coupon 优惠券信息
|
||||
*/
|
||||
private void renderCouponDiscount(Map<String, Double> couponMap, TradeDTO tradeDTO, MemberCoupon coupon) {
|
||||
//循环所有优惠券
|
||||
for (String skuId : couponMap.keySet()) {
|
||||
|
||||
// 循环购物车商品
|
||||
for (CartSkuVO item : tradeDTO.getSkuList()) {
|
||||
//如果id相等,则渲染商品价格信息
|
||||
if (item.getGoodsSku().getId().equals(skuId)) {
|
||||
|
||||
PriceDetailDTO priceDetailDTO = item.getPriceDetailDTO();
|
||||
|
||||
// 打折金额=商品金额*折扣/10
|
||||
Double discountCouponPrice = CurrencyUtil.mul(priceDetailDTO.getGoodsPrice(),
|
||||
CurrencyUtil.sub(1, CurrencyUtil.div(coupon.getDiscount(), 10, 3)));
|
||||
|
||||
//平台券则写入店铺承担优惠券比例
|
||||
if (Boolean.TRUE.equals(coupon.getIsPlatform())) {
|
||||
priceDetailDTO.setSiteCouponPrice(discountCouponPrice);
|
||||
priceDetailDTO.setSiteCouponPoint(coupon.getStoreCommission());
|
||||
}
|
||||
priceDetailDTO.setCouponPrice(CurrencyUtil.add(priceDetailDTO.getCouponPrice(), discountCouponPrice));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ public class CouponActivityServiceImpl extends ServiceImpl<CouponActivityMapper,
|
||||
//检测优惠券活动是否可以添加
|
||||
this.checkParam(couponActivityDTO);
|
||||
//如果有会员,则写入会员信息
|
||||
if (couponActivityDTO.getMemberDTOS() != null && couponActivityDTO.getMemberDTOS().size() != 0) {
|
||||
if (couponActivityDTO.getMemberDTOS() != null && !couponActivityDTO.getMemberDTOS().isEmpty()) {
|
||||
couponActivityDTO.setActivityScopeInfo(JSONUtil.toJsonStr(couponActivityDTO.getMemberDTOS()));
|
||||
}
|
||||
//添加优惠券活动
|
||||
@ -106,8 +106,7 @@ public class CouponActivityServiceImpl extends ServiceImpl<CouponActivityMapper,
|
||||
@Override
|
||||
public CouponActivityVO getCouponActivityVO(String couponActivityId) {
|
||||
CouponActivity couponActivity = this.getById(couponActivityId);
|
||||
CouponActivityVO couponActivityVO = new CouponActivityVO(couponActivity, couponActivityItemService.getCouponActivityItemListVO(couponActivityId));
|
||||
return couponActivityVO;
|
||||
return new CouponActivityVO(couponActivity, couponActivityItemService.getCouponActivityItemListVO(couponActivityId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -217,13 +216,11 @@ public class CouponActivityServiceImpl extends ServiceImpl<CouponActivityMapper,
|
||||
//活动时间需超过当前时间
|
||||
PromotionTools.checkPromotionTime(couponActivity.getStartTime().getTime(), couponActivity.getEndTime().getTime());
|
||||
//指定会员判定
|
||||
if (couponActivity.getActivityScope().equals(CouponActivitySendTypeEnum.DESIGNATED.name())) {
|
||||
if (couponActivity.getMemberDTOS().size() == 0) {
|
||||
throw new ServiceException(ResultCode.COUPON_ACTIVITY_MEMBER_ERROR);
|
||||
}
|
||||
if (couponActivity.getActivityScope().equals(CouponActivitySendTypeEnum.DESIGNATED.name()) && couponActivity.getMemberDTOS().isEmpty()) {
|
||||
throw new ServiceException(ResultCode.COUPON_ACTIVITY_MEMBER_ERROR);
|
||||
}
|
||||
//优惠券数量判定
|
||||
if (couponActivity.getCouponActivityItems().size() == 0) {
|
||||
if (couponActivity.getCouponActivityItems().isEmpty()) {
|
||||
throw new ServiceException(ResultCode.COUPON_ACTIVITY_ITEM_ERROR);
|
||||
} else if (couponActivity.getCouponActivityItems().size() > 10) {
|
||||
throw new ServiceException(ResultCode.COUPON_ACTIVITY_ITEM_MUST_NUM_ERROR);
|
||||
|
Loading…
x
Reference in New Issue
Block a user