mall + trade:调整价格计算的逻辑

This commit is contained in:
YunaiV 2023-05-28 20:09:51 +08:00
parent 55dbff7570
commit 6a9146ff8d
45 changed files with 1465 additions and 657 deletions

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.promotion.api.coupon;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponRespDTO;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponUseReqDTO;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponValidReqDTO;
import javax.validation.Valid;
@ -18,4 +20,12 @@ public interface CouponApi {
*/
void useCoupon(@Valid CouponUseReqDTO useReqDTO);
/**
* 校验优惠劵
*
* @param validReqDTO 校验请求
* @return 优惠劵
*/
CouponRespDTO validateCoupon(@Valid CouponValidReqDTO validReqDTO);
}

View File

@ -0,0 +1,109 @@
package cn.iocoder.yudao.module.promotion.api.coupon.dto;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponStatusEnum;
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
/**
* 优惠劵 Response DTO
*
* @author 芋道源码
*/
@Data
public class CouponRespDTO {
// ========== 基本信息 BEGIN ==========
/**
* 优惠劵编号
*/
private Long id;
/**
* 优惠劵模板编号
*/
private Integer templateId;
/**
* 优惠劵名
*/
private String name;
/**
* 优惠码状态
*
* 枚举 {@link CouponStatusEnum}
*/
private Integer status;
// ========== 基本信息 END ==========
// ========== 领取情况 BEGIN ==========
/**
* 用户编号
*
* 关联 MemberUserDO id 字段
*/
private Long userId;
/**
* 领取类型
*
* 枚举 {@link CouponTakeTypeEnum}
*/
private Integer takeType;
// ========== 领取情况 END ==========
// ========== 使用规则 BEGIN ==========
/**
* 是否设置满多少金额可用单位
*/
private Integer usePrice;
/**
* 生效开始时间
*/
private LocalDateTime validStartTime;
/**
* 生效结束时间
*/
private LocalDateTime validEndTime;
/**
* 商品范围
*/
private Integer productScope;
/**
* 商品 SPU 编号的数组
*/
private List<Long> productSpuIds;
// ========== 使用规则 END ==========
// ========== 使用效果 BEGIN ==========
/**
* 折扣类型
*/
private Integer discountType;
/**
* 折扣百分比
*/
private Integer discountPercent;
/**
* 优惠金额单位
*/
private Integer discountPrice;
/**
* 折扣上限仅在 {@link #discountType} 等于 {@link PromotionDiscountTypeEnum#PERCENT} 时生效
*/
private Integer discountLimitPrice;
// ========== 使用效果 END ==========
// ========== 使用情况 BEGIN ==========
/**
* 使用订单号
*/
private Long useOrderId;
/**
* 使用时间
*/
private LocalDateTime useTime;
// ========== 使用情况 END ==========
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.module.promotion.api.coupon.dto;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 优惠劵使用 Request DTO
*
* @author 芋道源码
*/
@Data
public class CouponValidReqDTO {
/**
* 优惠劵编号
*/
@NotNull(message = "优惠劵编号不能为空")
private Long id;
/**
* 用户编号
*/
@NotNull(message = "用户编号不能为空")
private Long userId;
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.promotion.api.discount;
import cn.iocoder.yudao.module.promotion.api.discount.dto.DiscountProductRespDTO;
import java.util.Collection;
import java.util.List;
/**
* 限时折扣 API 接口
*
* @author 芋道源码
*/
public interface DiscountActivityApi {
/**
* 获得商品匹配的的限时折扣信息
*
* @param skuIds 商品 SKU 编号数组
* @return 限时折扣信息
*/
List<DiscountProductRespDTO> getMatchDiscountProductList(Collection<Long> skuIds);
}

View File

@ -1,25 +1,19 @@
package cn.iocoder.yudao.module.promotion.service.discount.bo;
package cn.iocoder.yudao.module.promotion.api.discount.dto;
import lombok.Data;
/**
* 限时折扣活动商品 BO
* 限时折扣活动商品 Response DTO
*
* @author 芋道源码
*/
@Data
public class DiscountProductDetailBO {
// ========== DiscountProductDO 字段 ==========
public class DiscountProductRespDTO {
/**
* 编号主键自增
*/
private Long id;
/**
* 限时折扣活动的编号
*/
private Long activityId;
/**
* 商品 SPU 编号
*/
@ -41,7 +35,11 @@ public class DiscountProductDetailBO {
*/
private Integer discountPrice;
// ========== DiscountActivityDO 字段 ==========
// ========== 活动字段 ==========
/**
* 限时折扣活动的编号
*/
private Long activityId;
/**
* 活动标题
*/

View File

@ -1,4 +0,0 @@
/**
* 占位
*/
package cn.iocoder.yudao.module.promotion.api;

View File

@ -26,6 +26,11 @@ public class PriceCalculateReqDTO {
*/
private Long couponId;
/**
* 收货地址编号
*/
private Long addressId;
/**
* 商品 SKU 数组
*/

View File

@ -24,6 +24,7 @@ import java.util.List;
* @author 芋道源码
*/
@Data
@Deprecated
public class PriceCalculateRespDTO {
/**
@ -174,6 +175,7 @@ public class PriceCalculateRespDTO {
* 营销明细
*/
@Data
@Deprecated
public static class Promotion {
/**
@ -216,14 +218,14 @@ public class PriceCalculateRespDTO {
/**
* 是否满足优惠条件
*/
private Boolean meet;
private Boolean match;
/**
* 满足条件的提示
*
* 如果 {@link #meet} = true 满足则提示圣诞价: 150.00
* 如果 {@link #meet} = false 不满足则提示购满 85 可减 40
* 如果 {@link #match} = true 满足则提示圣诞价: 150.00
* 如果 {@link #match} = false 不满足则提示购满 85 可减 40
*/
private String meetTip;
private String description;
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.promotion.api.reward;
import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO;
import java.util.Collection;
import java.util.List;
/**
* 满减送活动 API 接口
*
* @author 芋道源码
*/
public interface RewardActivityApi {
/**
* 基于指定的 SPU 编号数组获得它们匹配的满减送活动
*
* @param spuIds SPU 编号数组
* @return 满减送活动列表
*/
List<RewardActivityMatchRespDTO> getMatchRewardActivityList(Collection<Long> spuIds);
}

View File

@ -0,0 +1,77 @@
package cn.iocoder.yudao.module.promotion.api.reward.dto;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
import lombok.Data;
import java.util.List;
/**
* 满减送活动的匹配 Response DTO
*
* @author 芋道源码
*/
@Data
public class RewardActivityMatchRespDTO {
/**
* 活动编号主键自增
*/
private Long id;
/**
* 活动标题
*/
private String name;
/**
* 条件类型
*
* 枚举 {@link PromotionConditionTypeEnum}
*/
private Integer conditionType;
/**
* 优惠规则的数组
*/
private List<Rule> rules;
/**
* 商品 SPU 编号的数组
*/
private List<Long> spuIds;
// TODO 芋艿后面 RewardActivityRespDTO 有了之后Rule 可以放过去
/**
* 优惠规则
*/
@Data
public static class Rule {
/**
* 优惠门槛
*
* 1. N 单位
* 2. N
*/
private Integer limit;
/**
* 优惠价格单位
*/
private Integer discountPrice;
/**
* 是否包邮
*/
private Boolean freeDelivery;
/**
* 赠送的积分
*/
private Integer point;
/**
* 赠送的优惠劵编号的数组
*/
private List<Long> couponIds;
/**
* 赠送的优惠卷数量的数组
*/
private List<Integer> couponCounts;
}
}

View File

@ -1,40 +0,0 @@
package cn.iocoder.yudao.module.promotion.enums.common;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* 营销的级别枚举
*
* 参考有赞<a href="https://img01.yzcdn.cn/upload_files/2021/11/02/FhDjUrNDq-G0wjNdYDtgUX09fdGj.png">营销级别</a>
*
* @author 芋道源码
*/
@Getter
@AllArgsConstructor
public enum PromotionLevelEnum implements IntArrayValuable {
ORDER(1, "订单级"), // 多个商品进行组合后优惠例如说满减送打包一口价第二件半价
SKU(2, "商品级"), // 单个商品直接优惠例如说限时折扣会员折扣
COUPON(3, "优惠劵"), // 多个商品进行组合后优惠例如说优惠劵
;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(PromotionLevelEnum::getLevel).toArray();
/**
* 级别值
*/
private final Integer level;
/**
* 类型名
*/
private final String name;
@Override
public int[] array() {
return ARRAYS;
}
}

View File

@ -18,7 +18,7 @@ public enum PromotionTypeEnum implements IntArrayValuable {
DISCOUNT_ACTIVITY(1, "限时折扣"),
REWARD_ACTIVITY(2, "满减送"),
MEMBER(3, "会员折扣"),
MEMBER(3, "会员折扣"), // TODO 芋艿待实现 StrUtil.format("会员折扣:省 {} 元", formatPrice(orderItem.getPayPrice() - memberPrice)
COUPON(4, "优惠劵")
;
@ -37,4 +37,5 @@ public enum PromotionTypeEnum implements IntArrayValuable {
public int[] array() {
return ARRAYS;
}
}

View File

@ -1,7 +1,11 @@
package cn.iocoder.yudao.module.promotion.api.coupon;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponRespDTO;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponUseReqDTO;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponValidReqDTO;
import cn.iocoder.yudao.module.promotion.convert.coupon.CouponConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO;
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
import org.springframework.stereotype.Service;
@ -24,4 +28,10 @@ public class CouponApiImpl implements CouponApi {
useReqDTO.getOrderId());
}
@Override
public CouponRespDTO validateCoupon(CouponValidReqDTO validReqDTO) {
CouponDO coupon = couponService.validCoupon(validReqDTO.getId(), validReqDTO.getUserId());
return CouponConvert.INSTANCE.convert(coupon);
}
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.promotion.api.discount;
import cn.iocoder.yudao.module.promotion.api.discount.dto.DiscountProductRespDTO;
import cn.iocoder.yudao.module.promotion.convert.discount.DiscountActivityConvert;
import cn.iocoder.yudao.module.promotion.service.discount.DiscountActivityService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
/**
* 限时折扣 API 实现类
*
* @author 芋道源码
*/
@Service
public class DiscountActivityApiImpl implements DiscountActivityApi {
@Resource
private DiscountActivityService discountActivityService;
@Override
public List<DiscountProductRespDTO> getMatchDiscountProductList(Collection<Long> skuIds) {
return DiscountActivityConvert.INSTANCE.convertList02(discountActivityService.getMatchDiscountProductList(skuIds));
}
}

View File

@ -1 +0,0 @@
package cn.iocoder.yudao.module.promotion.api.discount;

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.module.promotion.api.reward;
import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO;
import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
/**
* 满减送活动 API 实现类
*
* @author 芋道源码
*/
@Service
public class RewardActivityApiImpl implements RewardActivityApi {
@Resource
private RewardActivityService rewardActivityService;
@Override
public List<RewardActivityMatchRespDTO> getMatchRewardActivityList(Collection<Long> spuIds) {
return rewardActivityService.getMatchRewardActivityList(spuIds);
}
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.promotion.convert.coupon;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.coupon.CouponPageItemRespVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO;
import org.mapstruct.Mapper;
@ -18,4 +19,6 @@ public interface CouponConvert {
PageResult<CouponPageItemRespVO> convertPage(PageResult<CouponDO> page);
CouponRespDTO convert(CouponDO bean);
}

View File

@ -2,18 +2,15 @@ package cn.iocoder.yudao.module.promotion.convert.discount;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.promotion.api.discount.dto.DiscountProductRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
import java.util.Map;
/**
* 限时折扣活动 Convert
@ -33,20 +30,10 @@ public interface DiscountActivityConvert {
List<DiscountActivityRespVO> convertList(List<DiscountActivityDO> list);
List<DiscountProductRespDTO> convertList02(List<DiscountProductDO> list);
PageResult<DiscountActivityRespVO> convertPage(PageResult<DiscountActivityDO> page);
DiscountProductDetailBO convert(DiscountProductDO product);
default List<DiscountProductDetailBO> convertList(List<DiscountProductDO> products, Map<Long, DiscountActivityDO> activityMap) {
return CollectionUtils.convertList(products, product -> {
DiscountProductDetailBO detail = convert(product);
MapUtils.findAndThen(activityMap, product.getActivityId(), activity -> {
detail.setActivityName(activity.getName());
});
return detail;
});
}
DiscountProductDO convert(DiscountActivityBaseVO.Product bean);
DiscountActivityDetailRespVO convert(DiscountActivityDO activity, List<DiscountProductDO> products);
@ -99,4 +86,5 @@ public interface DiscountActivityConvert {
return true;
}
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.discount;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@ -33,10 +33,13 @@ public class DiscountActivityDO extends BaseDO {
* 活动标题
*/
private String name;
// TODO 芋艿状态调整只有开启和关闭
/**
* 状态
*
* 枚举 {@link PromotionActivityStatusEnum}
* 枚举 {@link CommonStatusEnum}
*
* 活动被关闭后不允许再次开启
*/
private Integer status;
/**

View File

@ -24,12 +24,15 @@ public class DiscountProductDO extends BaseDO {
*/
@TableId
private Long id;
// TODO 芋艿 activity 所有的字段冗余过来
/**
* 限时折扣活动的编号
*
* 关联 {@link DiscountActivityDO#getId()}
*/
private Long activityId;
/**
* 商品 SPU 编号
*

View File

@ -38,6 +38,7 @@ public class RewardActivityDO extends BaseDO {
* 活动标题
*/
private String name;
// TODO @芋艿改成开启禁用两种状态
/**
* 状态
*

View File

@ -6,12 +6,10 @@ import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountAc
import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityUpdateReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO;
import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* 限时折扣 Service 接口
@ -28,7 +26,7 @@ public interface DiscountActivityService {
* @param skuIds SKU 编号数组
* @return 匹配的限时折扣商品
*/
Map<Long, DiscountProductDetailBO> getMatchDiscountProducts(Collection<Long> skuIds);
List<DiscountProductDO> getMatchDiscountProductList(Collection<Long> skuIds);
/**
* 创建限时折扣活动

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.promotion.service.discount;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityPageReqVO;
@ -14,18 +13,17 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProduct
import cn.iocoder.yudao.module.promotion.dal.mysql.discount.DiscountActivityMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.discount.DiscountProductMapper;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum;
import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO;
import cn.iocoder.yudao.module.promotion.util.PromotionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.*;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
import static java.util.Arrays.asList;
/**
* 限时折扣 Service 实现类
@ -42,9 +40,9 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
private DiscountProductMapper discountProductMapper;
@Override
public Map<Long, DiscountProductDetailBO> getMatchDiscountProducts(Collection<Long> skuIds) {
List<DiscountProductDetailBO> discountProducts = getRewardProductListBySkuIds(skuIds, singleton(PromotionActivityStatusEnum.RUN.getStatus()));
return convertMap(discountProducts, DiscountProductDetailBO::getSkuId);
public List<DiscountProductDO> getMatchDiscountProductList(Collection<Long> skuIds) {
// TODO 芋艿开启满足 skuId日期内
return null;
}
@Override
@ -101,6 +99,7 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
}
}
// TODO 芋艿校验逻辑简化只查询时间冲突的活动开启状态的
/**
* 校验商品是否冲突
*
@ -112,9 +111,10 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
return;
}
// 查询商品参加的活动
List<DiscountProductDetailBO> discountActivityProductList = getRewardProductListBySkuIds(
convertSet(products, DiscountActivityBaseVO.Product::getSkuId),
asList(PromotionActivityStatusEnum.WAIT.getStatus(), PromotionActivityStatusEnum.RUN.getStatus()));
List<DiscountProductDO> discountActivityProductList = null;
// getRewardProductListBySkuIds(
// convertSet(products, DiscountActivityBaseVO.Product::getSkuId),
// asList(PromotionActivityStatusEnum.WAIT.getStatus(), PromotionActivityStatusEnum.RUN.getStatus()));
if (id != null) { // 排除自己这个活动
discountActivityProductList.removeIf(product -> id.equals(product.getActivityId()));
}
@ -124,24 +124,6 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
}
}
private List<DiscountProductDetailBO> getRewardProductListBySkuIds(Collection<Long> skuIds,
Collection<Integer> statuses) {
// 查询商品
List<DiscountProductDO> products = discountProductMapper.selectListBySkuId(skuIds);
if (CollUtil.isEmpty(products)) {
return new ArrayList<>(0);
}
// 查询活动
List<DiscountActivityDO> activities = discountActivityMapper.selectBatchIds(skuIds);
activities.removeIf(activity -> !statuses.contains(activity.getStatus())); // 移除不满足 statuses 状态的
Map<Long, DiscountActivityDO> activityMap = CollectionUtils.convertMap(activities, DiscountActivityDO::getId);
// 移除不满足活动的商品
products.removeIf(product -> !activityMap.containsKey(product.getActivityId()));
return DiscountActivityConvert.INSTANCE.convertList(products, activityMap);
}
@Override
public void closeRewardActivity(Long id) {
// 校验存在
@ -153,7 +135,7 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
throw exception(DISCOUNT_ACTIVITY_CLOSE_FAIL_STATUS_END);
}
// 更新
// 更新为关闭
DiscountActivityDO updateObj = new DiscountActivityDO().setId(id).setStatus(PromotionActivityStatusEnum.CLOSE.getStatus());
discountActivityMapper.updateById(updateObj);
}

View File

@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.promotion.service.price;
import cn.iocoder.yudao.module.promotion.api.price.dto.CouponMeetRespDTO;
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateReqDTO;
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateRespDTO;
import java.util.List;
@ -13,14 +12,6 @@ import java.util.List;
*/
public interface PriceService {
/**
* 计算商品的价格
*
* @param calculateReqDTO 价格请求
* @return 价格响应
*/
PriceCalculateRespDTO calculatePrice(PriceCalculateReqDTO calculateReqDTO);
/**
* 获得优惠劵的匹配信息列表
*

View File

@ -1,39 +1,25 @@
package cn.iocoder.yudao.module.promotion.service.price;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.promotion.api.price.dto.CouponMeetRespDTO;
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateReqDTO;
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateRespDTO;
import cn.iocoder.yudao.module.promotion.convert.price.PriceConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
import cn.iocoder.yudao.module.promotion.enums.common.*;
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponStatusEnum;
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
import cn.iocoder.yudao.module.promotion.service.discount.DiscountActivityService;
import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO;
import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService;
import com.google.common.base.Suppliers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Supplier;
import java.util.Collections;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
import static java.util.Collections.singletonList;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_VALID_TIME_NOT_NOW;
/**
* 价格计算 Service 实现类
@ -54,43 +40,14 @@ import static java.util.Collections.singletonList;
@Slf4j
public class PriceServiceImpl implements PriceService {
@Resource
private DiscountActivityService discountService;
@Resource
private RewardActivityService rewardActivityService;
@Resource
private CouponService couponService;
@Resource
private ProductSkuApi productSkuApi;
@Override
public PriceCalculateRespDTO calculatePrice(PriceCalculateReqDTO calculateReqDTO) {
// 获得商品 SKU 数组
List<ProductSkuRespDTO> skuList = checkSkus(calculateReqDTO);
// 初始化 PriceCalculateRespDTO 对象
PriceCalculateRespDTO priceCalculate = PriceConvert.INSTANCE.convert(calculateReqDTO, skuList);
// 计算商品级别的价格
calculatePriceForSkuLevel(calculateReqDTO.getUserId(), priceCalculate);
// 计算订单级别的价格
calculatePriceForOrderLevel(calculateReqDTO.getUserId(), priceCalculate);
// 计算优惠劵级别的价格
calculatePriceForCouponLevel(calculateReqDTO.getUserId(), calculateReqDTO.getCouponId(), priceCalculate);
// 如果最终支付金额小于等于 0则抛出业务异常
if (priceCalculate.getOrder().getPayPrice() <= 0) {
log.error("[calculatePrice][价格计算不正确,请求 calculateReqDTO({}),结果 priceCalculate({})]",
calculateReqDTO, priceCalculate);
throw exception(PRICE_CALCULATE_PAY_PRICE_ILLEGAL);
}
return priceCalculate;
}
@Override
public List<CouponMeetRespDTO> getMeetCouponList(PriceCalculateReqDTO calculateReqDTO) {
// 先计算一轮价格
PriceCalculateRespDTO priceCalculate = calculatePrice(calculateReqDTO);
// PriceCalculateRespDTO priceCalculate = calculatePrice(calculateReqDTO);
PriceCalculateRespDTO priceCalculate = null;
// 获得用户的待使用优惠劵
List<CouponDO> couponList = couponService.getCouponList(calculateReqDTO.getUserId(), CouponStatusEnum.UNUSED.getStatus());
@ -106,7 +63,9 @@ public class PriceServiceImpl implements PriceService {
couponService.validCoupon(coupon);
// 获得匹配的商品 SKU 数组
List<PriceCalculateRespDTO.OrderItem> orderItems = getMatchCouponOrderItems(priceCalculate, coupon);
// TODO 芋艿后续处理
// List<PriceCalculateRespDTO.OrderItem> orderItems = getMatchCouponOrderItems(priceCalculate, coupon);
List<PriceCalculateRespDTO.OrderItem> orderItems = null;
if (CollUtil.isEmpty(orderItems)) {
return couponMeetRespDTO.setMeet(false).setMeetTip("所结算商品没有符合条件的商品");
}
@ -134,413 +93,4 @@ public class PriceServiceImpl implements PriceService {
});
}
private List<ProductSkuRespDTO> checkSkus(PriceCalculateReqDTO calculateReqDTO) {
// 获得商品 SKU 数组
Map<Long, Integer> skuIdCountMap = CollectionUtils.convertMap(calculateReqDTO.getItems(),
PriceCalculateReqDTO.Item::getSkuId, PriceCalculateReqDTO.Item::getCount);
List<ProductSkuRespDTO> skus = productSkuApi.getSkuList(skuIdCountMap.keySet());
// 校验商品 SKU
skus.forEach(sku -> {
Integer count = skuIdCountMap.get(sku.getId());
if (count == null) {
throw exception(SKU_NOT_EXISTS);
}
// 不校验库存不足避免购物车场景商品无货的情况
});
return skus;
}
// ========== 计算商品级别的价格 ==========
/**
* 计算商品级别的价格例如说
* 1. 会员折扣
* 2. 限时折扣 {@link cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO}
*
* 其中会员折扣限时折扣取最低价
*
* @param userId 用户编号
* @param priceCalculate 价格计算的结果
*/
private void calculatePriceForSkuLevel(Long userId, PriceCalculateRespDTO priceCalculate) {
// 获取 SKU 级别的所有优惠信息
Supplier<Double> memberDiscountPercentSupplier = getMemberDiscountPercentSupplier(userId);
Map<Long, DiscountProductDetailBO> discountProducts = discountService.getMatchDiscountProducts(
convertSet(priceCalculate.getOrder().getItems(), PriceCalculateRespDTO.OrderItem::getSkuId));
// 处理每个 SKU 的优惠
priceCalculate.getOrder().getItems().forEach(orderItem -> {
// 获取该 SKU 的优惠信息
Double memberDiscountPercent = memberDiscountPercentSupplier.get();
DiscountProductDetailBO discountProduct = discountProducts.get(orderItem.getSkuId());
if (memberDiscountPercent == null && discountProduct == null) {
return;
}
// 计算价格判断选择哪个折扣
Integer memberPrice = memberDiscountPercent != null ? (int) (orderItem.getPayPrice() * memberDiscountPercent / 100) : null;
Integer promotionPrice = discountProduct != null ? getDiscountProductPrice(discountProduct, orderItem) : null;
if (memberPrice == null) {
calculatePriceByDiscountActivity(priceCalculate, orderItem, discountProduct, promotionPrice);
} else if (promotionPrice == null) {
calculatePriceByMemberDiscount(priceCalculate, orderItem, memberPrice);
} else if (memberPrice < promotionPrice) {
calculatePriceByDiscountActivity(priceCalculate, orderItem, discountProduct, promotionPrice);
} else {
calculatePriceByMemberDiscount(priceCalculate, orderItem, memberPrice);
}
});
}
private Integer getDiscountProductPrice(DiscountProductDetailBO discountProduct,
PriceCalculateRespDTO.OrderItem orderItem) {
Integer price = orderItem.getPayPrice();
if (PromotionDiscountTypeEnum.PRICE.getType().equals(discountProduct.getDiscountType())) { // 减价
price -= discountProduct.getDiscountPrice() * orderItem.getCount();
} else if (PromotionDiscountTypeEnum.PERCENT.getType().equals(discountProduct.getDiscountType())) { // 打折
price = price * discountProduct.getDiscountPercent() / 100;
} else {
throw new IllegalArgumentException(String.format("优惠活动的商品(%s) 的优惠类型不正确", discountProduct));
}
return price;
}
private void calculatePriceByMemberDiscount(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem,
Integer memberPrice) {
// 记录优惠明细
addPromotion(priceCalculate, orderItem, null, PromotionTypeEnum.MEMBER.getName(),
PromotionTypeEnum.MEMBER.getType(), PromotionLevelEnum.SKU.getLevel(), memberPrice,
true, StrUtil.format("会员折扣:省 {} 元", formatPrice(orderItem.getPayPrice() - memberPrice)));
// 修改 SKU 的优惠
modifyOrderItemPayPrice(orderItem, memberPrice, priceCalculate);
}
private void calculatePriceByDiscountActivity(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem,
DiscountProductDetailBO discountProduct, Integer promotionPrice) {
// 记录优惠明细
addPromotion(priceCalculate, orderItem, discountProduct.getActivityId(), discountProduct.getActivityName(),
PromotionTypeEnum.DISCOUNT_ACTIVITY.getType(), PromotionLevelEnum.SKU.getLevel(), promotionPrice,
true, StrUtil.format("限时折扣:省 {} 元", formatPrice(orderItem.getPayPrice() - promotionPrice)));
// 修改 SKU 的优惠
modifyOrderItemPayPrice(orderItem, promotionPrice, priceCalculate);
}
// TODO 芋艿提前实现
private Supplier<Double> getMemberDiscountPercentSupplier(Long userId) {
return Suppliers.memoize(() -> {
if (userId == 1) {
return 90d;
}
if (userId == 2) {
return 80d;
}
return null; // 无优惠
});
}
// ========== 计算商品级别的价格 ==========
/**
* 计算订单级别的价格例如说
* 1. 满减送 {@link cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO}
*
* @param userId 用户编号
* @param priceCalculate 价格计算的结果
*/
@SuppressWarnings("unused")
private void calculatePriceForOrderLevel(Long userId, PriceCalculateRespDTO priceCalculate) {
// 获取 SKU 级别的所有优惠信息
Set<Long> spuIds = convertSet(priceCalculate.getOrder().getItems(), PriceCalculateRespDTO.OrderItem::getSpuId);
Map<RewardActivityDO, Set<Long>> rewardActivities = rewardActivityService.getMatchRewardActivities(spuIds);
// 处理满减送活动
if (CollUtil.isNotEmpty(rewardActivities)) {
rewardActivities.forEach((rewardActivity, activitySpuIds) -> {
List<PriceCalculateRespDTO.OrderItem> orderItems = CollectionUtils.filterList(priceCalculate.getOrder().getItems(),
orderItem -> CollUtil.contains(activitySpuIds, orderItem.getSpuId()));
calculatePriceByRewardActivity(priceCalculate, orderItems, rewardActivity);
});
}
}
private void calculatePriceByRewardActivity(PriceCalculateRespDTO priceCalculate, List<PriceCalculateRespDTO.OrderItem> orderItems,
RewardActivityDO rewardActivity) {
// 获得最大匹配的满减送活动的规则
RewardActivityDO.Rule rule = getLastMatchRewardActivityRule(rewardActivity, orderItems);
if (rule == null) {
// 获取不到的情况下记录不满足的优惠明细
addNotMeetPromotion(priceCalculate, orderItems, rewardActivity.getId(), rewardActivity.getName(),
PromotionTypeEnum.REWARD_ACTIVITY.getType(), PromotionLevelEnum.ORDER.getLevel(),
getRewardActivityNotMeetTip(rewardActivity));
return;
}
// 分摊金额
List<Integer> discountPartPrices = dividePrice(orderItems, rule.getDiscountPrice());
// 记录优惠明细
addPromotion(priceCalculate, orderItems, rewardActivity.getId(), rewardActivity.getName(),
PromotionTypeEnum.REWARD_ACTIVITY.getType(), PromotionLevelEnum.ORDER.getLevel(), discountPartPrices,
true, StrUtil.format("满减送:省 {} 元", formatPrice(rule.getDiscountPrice())));
// 修改 SKU 的分摊
for (int i = 0; i < orderItems.size(); i++) {
modifyOrderItemOrderPartPriceFromDiscountPrice(orderItems.get(i), discountPartPrices.get(i), priceCalculate);
}
}
/**
* 获得最大匹配的满减送活动的规则
*
* @param rewardActivity 满减送活动
* @param orderItems 商品项
* @return 匹配的活动规则
*/
private RewardActivityDO.Rule getLastMatchRewardActivityRule(RewardActivityDO rewardActivity,
List<PriceCalculateRespDTO.OrderItem> orderItems) {
Integer count = getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getCount, Integer::sum);
// price 的计算逻辑使用 orderDividePrice 的原因主要考虑分摊后这个才是该 SKU 当前真实的支付总价
Integer price = getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum);
assert count != null && price != null;
for (int i = rewardActivity.getRules().size() - 1; i >= 0; i--) {
RewardActivityDO.Rule rule = rewardActivity.getRules().get(i);
if (PromotionConditionTypeEnum.PRICE.getType().equals(rewardActivity.getConditionType())
&& price >= rule.getLimit()) {
return rule;
}
if (PromotionConditionTypeEnum.COUNT.getType().equals(rewardActivity.getConditionType())
&& count >= rule.getLimit()) {
return rule;
}
}
return null;
}
/**
* 获得满减送活动部匹配时的提示
*
* @param rewardActivity 满减送活动
* @return 提示
*/
private String getRewardActivityNotMeetTip(RewardActivityDO rewardActivity) {
return "TODO"; // TODO 芋艿后面再想想
}
// ========== 计算优惠劵级别的价格 ==========
private void calculatePriceForCouponLevel(Long userId, Long couponId, PriceCalculateRespDTO priceCalculate) {
// 校验优惠劵
if (couponId == null) {
return;
}
CouponDO coupon = couponService.validCoupon(couponId, userId);
// 获得匹配的商品 SKU 数组
List<PriceCalculateRespDTO.OrderItem> orderItems = getMatchCouponOrderItems(priceCalculate, coupon);
if (CollUtil.isEmpty(orderItems)) {
throw exception(COUPON_NO_MATCH_SPU);
}
// 计算是否满足优惠劵的使用金额
Integer originPrice = getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum);
assert originPrice != null;
if (originPrice < coupon.getUsePrice()) {
throw exception(COUPON_NO_MATCH_MIN_PRICE);
}
// 计算可以优惠的金额
priceCalculate.getOrder().setCouponId(couponId);
Integer couponPrice = getCouponPrice(coupon, originPrice);
// 分摊金额
List<Integer> couponPartPrices = dividePrice(orderItems, couponPrice);
// 记录优惠明细
addPromotion(priceCalculate, orderItems, coupon.getId(), coupon.getName(),
PromotionTypeEnum.COUPON.getType(), PromotionLevelEnum.COUPON.getLevel(), couponPartPrices,
true, StrUtil.format("优惠劵:省 {} 元", formatPrice(couponPrice)));
// 修改 SKU 的分摊
for (int i = 0; i < orderItems.size(); i++) {
modifyOrderItemOrderPartPriceFromCouponPrice(orderItems.get(i), couponPartPrices.get(i), priceCalculate);
}
}
private List<PriceCalculateRespDTO.OrderItem> getMatchCouponOrderItems(PriceCalculateRespDTO priceCalculate,
CouponDO coupon) {
if (PromotionProductScopeEnum.ALL.getScope().equals(coupon.getProductScope())) {
return priceCalculate.getOrder().getItems();
}
return CollectionUtils.filterList(priceCalculate.getOrder().getItems(),
orderItem -> coupon.getProductSpuIds().contains(orderItem.getSpuId()));
}
private Integer getCouponPrice(CouponDO coupon, Integer originPrice) {
if (PromotionDiscountTypeEnum.PRICE.getType().equals(coupon.getDiscountType())) { // 减价
return coupon.getDiscountPrice();
} else if (PromotionDiscountTypeEnum.PERCENT.getType().equals(coupon.getDiscountType())) { // 打折
int couponPrice = originPrice * coupon.getDiscountPercent() / 100;
return coupon.getDiscountLimitPrice() == null ? couponPrice
: Math.min(couponPrice, coupon.getDiscountLimitPrice()); // 优惠上限
}
throw new IllegalArgumentException(String.format("优惠劵(%s) 的优惠类型不正确", coupon));
}
// ========== 其它相对通用的方法 ==========
/**
* 添加单个 OrderItem 的营销明细
*
* @param priceCalculate 价格计算结果
* @param orderItem 单个订单商品 SKU
* @param id 营销编号
* @param name 营销名字
* @param type 营销类型
* @param level 营销级别
* @param newPayPrice 新的单实付金额
* @param meet 是否满足优惠条件
* @param meetTip 满足条件的提示
*/
private void addPromotion(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem,
Long id, String name, Integer type, Integer level,
Integer newPayPrice, Boolean meet, String meetTip) {
// 创建营销明细 Item
// TODO 芋艿orderItem.getPayPrice() 要不要改成 orderDividePrice同时newPayPrice 要不要改成直接传递 discountPrice
PriceCalculateRespDTO.PromotionItem promotionItem = new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId())
.setOriginalPrice(orderItem.getPayPrice()).setDiscountPrice(orderItem.getPayPrice() - newPayPrice);
// 创建营销明细
PriceCalculateRespDTO.Promotion promotion = new PriceCalculateRespDTO.Promotion()
.setId(id).setName(name).setType(type).setLevel(level)
.setTotalPrice(promotionItem.getOriginalPrice()).setDiscountPrice(promotionItem.getDiscountPrice())
.setItems(singletonList(promotionItem)).setMeet(meet).setMeetTip(meetTip);
priceCalculate.getPromotions().add(promotion);
}
/**
* 添加多个 OrderItem 的营销明细
*
* @param priceCalculate 价格计算结果
* @param orderItems 多个订单商品 SKU
* @param id 营销编号
* @param name 营销名字
* @param type 营销类型
* @param level 营销级别
* @param discountPrices 多个订单商品 SKU 的优惠价格 orderItems 一一对应
* @param meet 是否满足优惠条件
* @param meetTip 满足条件的提示
*/
private void addPromotion(PriceCalculateRespDTO priceCalculate, List<PriceCalculateRespDTO.OrderItem> orderItems,
Long id, String name, Integer type, Integer level,
List<Integer> discountPrices, Boolean meet, String meetTip) {
// 创建营销明细 Item
List<PriceCalculateRespDTO.PromotionItem> promotionItems = new ArrayList<>(discountPrices.size());
for (int i = 0; i < orderItems.size(); i++) {
PriceCalculateRespDTO.OrderItem orderItem = orderItems.get(i);
promotionItems.add(new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId())
.setOriginalPrice(orderItem.getPayPrice()).setDiscountPrice(discountPrices.get(i)));
}
// 创建营销明细
PriceCalculateRespDTO.Promotion promotion = new PriceCalculateRespDTO.Promotion()
.setId(id).setName(name).setType(type).setLevel(level)
.setTotalPrice(getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum))
.setDiscountPrice(getSumValue(discountPrices, value -> value, Integer::sum))
.setItems(promotionItems).setMeet(meet).setMeetTip(meetTip);
priceCalculate.getPromotions().add(promotion);
}
private void addNotMeetPromotion(PriceCalculateRespDTO priceCalculate, List<PriceCalculateRespDTO.OrderItem> orderItems,
Long id, String name, Integer type, Integer level, String meetTip) {
// 创建营销明细 Item
List<PriceCalculateRespDTO.PromotionItem> promotionItems = CollectionUtils.convertList(orderItems,
orderItem -> new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId())
.setOriginalPrice(orderItem.getOrderDividePrice()).setDiscountPrice(0));
// 创建营销明细
Integer originalPrice = getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum);
PriceCalculateRespDTO.Promotion promotion = new PriceCalculateRespDTO.Promotion()
.setId(id).setName(name).setType(type).setLevel(level)
.setTotalPrice(originalPrice).setDiscountPrice(0)
.setItems(promotionItems).setMeet(false).setMeetTip(meetTip);
priceCalculate.getPromotions().add(promotion);
}
/**
* 修改 OrderItem payPrice 价格同时会修改 Order payPrice 价格
*
* @param orderItem 订单商品 SKU
* @param newPayPrice 新的 payPrice 价格
* @param priceCalculate 价格计算结果
*/
private void modifyOrderItemPayPrice(PriceCalculateRespDTO.OrderItem orderItem, Integer newPayPrice,
PriceCalculateRespDTO priceCalculate) {
// diffPayPrice 等于额外增加的商品级的优惠
int diffPayPrice = orderItem.getPayPrice() - newPayPrice;
// 设置 OrderItem 价格相关字段
orderItem.setDiscountPrice(orderItem.getDiscountPrice() + diffPayPrice);
orderItem.setPayPrice(newPayPrice);
orderItem.setOrderDividePrice(orderItem.getPayPrice() - orderItem.getOrderPartPrice());
// 设置 Order 相关相关字段
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
order.setPayPrice(order.getPayPrice() - diffPayPrice);
}
/**
* 修改 OrderItem orderPartPrice 价格同时会修改 Order discountPrice 价格
*
* 本质分摊 Order discountPrice 价格到对应的 OrderItem orderPartPrice 价格中
*
* @param orderItem 订单商品 SKU
* @param addOrderPartPrice 新增的 discountPrice 价格
* @param priceCalculate 价格计算结果
*/
private void modifyOrderItemOrderPartPriceFromDiscountPrice(PriceCalculateRespDTO.OrderItem orderItem, Integer addOrderPartPrice,
PriceCalculateRespDTO priceCalculate) {
// 设置 OrderItem 价格相关字段
orderItem.setOrderPartPrice(orderItem.getOrderPartPrice() + addOrderPartPrice);
orderItem.setOrderDividePrice(orderItem.getPayPrice() - orderItem.getOrderPartPrice());
// 设置 Order 相关相关字段
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
order.setDiscountPrice(order.getDiscountPrice() + addOrderPartPrice);
order.setPayPrice(order.getPayPrice() - addOrderPartPrice);
}
/**
* 修改 OrderItem orderPartPrice 价格同时会修改 Order couponPrice 价格
*
* 本质分摊 Order couponPrice 价格到对应的 OrderItem orderPartPrice 价格中
*
* @param orderItem 订单商品 SKU
* @param addOrderPartPrice 新增的 couponPrice 价格
* @param priceCalculate 价格计算结果
*/
private void modifyOrderItemOrderPartPriceFromCouponPrice(PriceCalculateRespDTO.OrderItem orderItem, Integer addOrderPartPrice,
PriceCalculateRespDTO priceCalculate) {
// 设置 OrderItem 价格相关字段
orderItem.setOrderPartPrice(orderItem.getOrderPartPrice() + addOrderPartPrice);
orderItem.setOrderDividePrice(orderItem.getPayPrice() - orderItem.getOrderPartPrice());
// 设置 Order 相关相关字段
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
order.setCouponPrice(order.getCouponPrice() + addOrderPartPrice);
order.setPayPrice(order.getPayPrice() - addOrderPartPrice);
}
private List<Integer> dividePrice(List<PriceCalculateRespDTO.OrderItem> orderItems, Integer price) {
List<Integer> prices = new ArrayList<>(orderItems.size());
Integer total = getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum);
assert total != null;
int remainPrice = price;
// 遍历每一个进行分摊
for (int i = 0; i < orderItems.size(); i++) {
PriceCalculateRespDTO.OrderItem orderItem = orderItems.get(i);
int partPrice;
if (i < orderItems.size() - 1) { // 减一的原因是因为拆分时如果按照比例可能会出现.所以最后一个使用反减
partPrice = (int) (price * (1.0D * orderItem.getOrderDividePrice() / total));
remainPrice -= partPrice;
} else {
partPrice = remainPrice;
}
Assert.isTrue(partPrice > 0, "分摊金额必须大于 0");
prices.add(partPrice);
}
return prices;
}
private String formatPrice(Integer price) {
return String.format("%.2f", price / 100d);
}
}

View File

@ -1,14 +1,15 @@
package cn.iocoder.yudao.module.promotion.service.reward;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityUpdateReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
import javax.validation.Valid;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
import java.util.List;
/**
* 满减送活动 Service 接口
@ -66,8 +67,8 @@ public interface RewardActivityService {
* 基于指定的 SPU 编号数组获得它们匹配的满减送活动
*
* @param spuIds SPU 编号数组
* @return 满减送活动与对应的 SPU 编号的映射value 就是 SPU 编号的集合
* @return 满减送活动列表
*/
Map<RewardActivityDO, Set<Long>> getMatchRewardActivities(Set<Long> spuIds);
List<RewardActivityMatchRespDTO> getMatchRewardActivityList(Collection<Long> spuIds);
}

View File

@ -1,8 +1,8 @@
package cn.iocoder.yudao.module.promotion.service.reward;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityUpdateReqVO;
@ -10,7 +10,6 @@ import cn.iocoder.yudao.module.promotion.convert.reward.RewardActivityConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.reward.RewardActivityMapper;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
import cn.iocoder.yudao.module.promotion.util.PromotionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -18,15 +17,10 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static cn.hutool.core.collection.CollUtil.intersectionDistinct;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
/**
* 满减送活动 Service 实现类
@ -105,6 +99,7 @@ public class RewardActivityServiceImpl implements RewardActivityService {
return activity;
}
// TODO @芋艿逻辑有问题需要优化要分成全场和指定来校验
/**
* 校验商品参加的活动是否冲突
*
@ -151,19 +146,21 @@ public class RewardActivityServiceImpl implements RewardActivityService {
}
@Override
public Map<RewardActivityDO, Set<Long>> getMatchRewardActivities(Set<Long> spuIds) {
// 如果有全局活动则直接选择它
List<RewardActivityDO> allActivities = rewardActivityMapper.selectListByProductScopeAndStatus(
PromotionProductScopeEnum.ALL.getScope(), PromotionActivityStatusEnum.RUN.getStatus());
if (CollUtil.isNotEmpty(allActivities)) {
return MapUtil.builder(allActivities.get(0), spuIds).build();
}
// 查询某个活动参加的活动
List<RewardActivityDO> productActivityList = getRewardActivityListBySpuIds(spuIds,
singleton(PromotionActivityStatusEnum.RUN.getStatus()));
return convertMap(productActivityList, activity -> activity,
rewardActivityDO -> intersectionDistinct(rewardActivityDO.getProductSpuIds(), spuIds)); // 求交集返回
public List<RewardActivityMatchRespDTO> getMatchRewardActivityList(Collection<Long> spuIds) {
// TODO 芋艿待实现先指定然后再全局的
// // 如果有全局活动则直接选择它
// List<RewardActivityDO> allActivities = rewardActivityMapper.selectListByProductScopeAndStatus(
// PromotionProductScopeEnum.ALL.getScope(), PromotionActivityStatusEnum.RUN.getStatus());
// if (CollUtil.isNotEmpty(allActivities)) {
// return MapUtil.builder(allActivities.get(0), spuIds).build();
// }
//
// // 查询某个活动参加的活动
// List<RewardActivityDO> productActivityList = getRewardActivityListBySpuIds(spuIds,
// singleton(PromotionActivityStatusEnum.RUN.getStatus()));
// return convertMap(productActivityList, activity -> activity,
// rewardActivityDO -> intersectionDistinct(rewardActivityDO.getProductSpuIds(), spuIds)); // 求交集返回
return null;
}
}

View File

@ -96,8 +96,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion.getLevel(), PromotionLevelEnum.SKU.getLevel());
assertEquals(promotion.getTotalPrice(), 200);
assertEquals(promotion.getDiscountPrice(), 20);
assertTrue(promotion.getMeet());
assertEquals(promotion.getMeetTip(), "会员折扣:省 0.20 元");
assertTrue(promotion.getMatch());
assertEquals(promotion.getDescription(), "会员折扣:省 0.20 元");
PriceCalculateRespDTO.PromotionItem promotionItem = promotion.getItems().get(0);
assertEquals(promotion.getItems().size(), 1);
assertEquals(promotionItem.getSkuId(), 10L);
@ -122,7 +122,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
DiscountProductDetailBO discountProduct02 = randomPojo(DiscountProductDetailBO.class, o -> o.setActivityId(2000L)
.setActivityName("活动 2000 号").setSkuId(20L)
.setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()).setDiscountPercent(60));
when(discountService.getMatchDiscountProducts(eq(asSet(10L, 20L)))).thenReturn(
when(discountService.getMatchDiscountProductList(eq(asSet(10L, 20L)))).thenReturn(
MapUtil.builder(10L, discountProduct01).put(20L, discountProduct02).map());
// 10L: 100 * 2 - 40 * 2 = 120
@ -167,8 +167,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getLevel(), PromotionLevelEnum.SKU.getLevel());
assertEquals(promotion01.getTotalPrice(), 200);
assertEquals(promotion01.getDiscountPrice(), 80);
assertTrue(promotion01.getMeet());
assertEquals(promotion01.getMeetTip(), "限时折扣:省 0.80 元");
assertTrue(promotion01.getMatch());
assertEquals(promotion01.getDescription(), "限时折扣:省 0.80 元");
PriceCalculateRespDTO.PromotionItem promotionItem01 = promotion01.getItems().get(0);
assertEquals(promotion01.getItems().size(), 1);
assertEquals(promotionItem01.getSkuId(), 10L);
@ -181,8 +181,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion02.getLevel(), PromotionLevelEnum.SKU.getLevel());
assertEquals(promotion02.getTotalPrice(), 150);
assertEquals(promotion02.getDiscountPrice(), 60);
assertTrue(promotion02.getMeet());
assertEquals(promotion02.getMeetTip(), "限时折扣:省 0.60 元");
assertTrue(promotion02.getMatch());
assertEquals(promotion02.getDescription(), "限时折扣:省 0.60 元");
PriceCalculateRespDTO.PromotionItem promotionItem02 = promotion02.getItems().get(0);
assertEquals(promotion02.getItems().size(), 1);
assertEquals(promotionItem02.getSkuId(), 20L);
@ -267,8 +267,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getLevel(), PromotionLevelEnum.ORDER.getLevel());
assertEquals(promotion01.getTotalPrice(), 350);
assertEquals(promotion01.getDiscountPrice(), 70);
assertTrue(promotion01.getMeet());
assertEquals(promotion01.getMeetTip(), "满减送:省 0.70 元");
assertTrue(promotion01.getMatch());
assertEquals(promotion01.getDescription(), "满减送:省 0.70 元");
assertEquals(promotion01.getItems().size(), 2);
PriceCalculateRespDTO.PromotionItem promotionItem011 = promotion01.getItems().get(0);
assertEquals(promotionItem011.getSkuId(), 10L);
@ -286,8 +286,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion02.getLevel(), PromotionLevelEnum.ORDER.getLevel());
assertEquals(promotion02.getTotalPrice(), 120);
assertEquals(promotion02.getDiscountPrice(), 60);
assertTrue(promotion02.getMeet());
assertEquals(promotion02.getMeetTip(), "满减送:省 0.60 元");
assertTrue(promotion02.getMatch());
assertEquals(promotion02.getDescription(), "满减送:省 0.60 元");
PriceCalculateRespDTO.PromotionItem promotionItem02 = promotion02.getItems().get(0);
assertEquals(promotion02.getItems().size(), 1);
assertEquals(promotionItem02.getSkuId(), 30L);
@ -355,8 +355,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getLevel(), PromotionLevelEnum.ORDER.getLevel());
assertEquals(promotion01.getTotalPrice(), 350);
assertEquals(promotion01.getDiscountPrice(), 0);
assertFalse(promotion01.getMeet());
assertEquals(promotion01.getMeetTip(), "TODO"); // TODO 芋艿后面再想想
assertFalse(promotion01.getMatch());
assertEquals(promotion01.getDescription(), "TODO"); // TODO 芋艿后面再想想
assertEquals(promotion01.getItems().size(), 2);
PriceCalculateRespDTO.PromotionItem promotionItem011 = promotion01.getItems().get(0);
assertEquals(promotionItem011.getSkuId(), 10L);
@ -437,8 +437,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getLevel(), PromotionLevelEnum.COUPON.getLevel());
assertEquals(promotion01.getTotalPrice(), 350);
assertEquals(promotion01.getDiscountPrice(), 70);
assertTrue(promotion01.getMeet());
assertEquals(promotion01.getMeetTip(), "优惠劵:省 0.70 元");
assertTrue(promotion01.getMatch());
assertEquals(promotion01.getDescription(), "优惠劵:省 0.70 元");
assertEquals(promotion01.getItems().size(), 2);
PriceCalculateRespDTO.PromotionItem promotionItem011 = promotion01.getItems().get(0);
assertEquals(promotionItem011.getSkuId(), 10L);

View File

@ -50,4 +50,8 @@ public interface ErrorCodeConstants {
ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011003001, "已经存在该编码的快递公司");
ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011003002, "运费模板不存在");
ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003002, "已经存在该运费模板名");
// ========== Price 相关 1011004000 ============
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0");
}

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.trade.service.price;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
/**
* 价格计算 Service 接口
*
* @author 芋道源码
*/
public interface TradePriceService {
/**
* 价格计算
*
* @param calculateReqDTO 计算信息
* @return 计算结果
*/
TradePriceCalculateRespBO calculatePrice(TradePriceCalculateReqBO calculateReqDTO);
}

View File

@ -0,0 +1,73 @@
package cn.iocoder.yudao.module.trade.service.price;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculator;
import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_STOCK_NOT_ENOUGH;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.PRICE_CALCULATE_PAY_PRICE_ILLEGAL;
/**
* 价格计算 Service 实现类
*
* @author 芋道源码
*/
@Service
@Slf4j
public class TradePriceServiceImpl implements TradePriceService {
@Resource
private ProductSkuApi productSkuApi;
@Resource
private List<TradePriceCalculator> priceCalculators;
@Override
public TradePriceCalculateRespBO calculatePrice(TradePriceCalculateReqBO calculateReqBO) {
// 1. 获得商品 SKU 数组
List<ProductSkuRespDTO> skuList = checkSkus(calculateReqBO);
// 2.1 计算价格
TradePriceCalculateRespBO calculateRespBO = TradePriceCalculatorHelper
.buildCalculateResp(calculateReqBO, skuList);
priceCalculators.forEach(calculator -> calculator.calculate(calculateReqBO, calculateRespBO));
// 2.2 如果最终支付金额小于等于 0则抛出业务异常
if (calculateRespBO.getPrice().getPayPrice() <= 0) {
log.error("[calculatePrice][价格计算不正确,请求 calculateReqDTO({}),结果 priceCalculate({})]",
calculateReqBO, calculateRespBO);
throw exception(PRICE_CALCULATE_PAY_PRICE_ILLEGAL);
}
return calculateRespBO;
}
private List<ProductSkuRespDTO> checkSkus(TradePriceCalculateReqBO reqBO) {
// 获得商品 SKU 数组
Map<Long, Integer> skuIdCountMap = convertMap(reqBO.getItems(),
TradePriceCalculateReqBO.Item::getSkuId, TradePriceCalculateReqBO.Item::getCount);
List<ProductSkuRespDTO> skus = productSkuApi.getSkuList(skuIdCountMap.keySet());
// 校验商品 SKU
skus.forEach(sku -> {
Integer count = skuIdCountMap.get(sku.getId());
if (count == null) {
throw exception(SKU_NOT_EXISTS);
}
if (count > sku.getStock()) {
throw exception(SKU_STOCK_NOT_ENOUGH);
}
});
return skus;
}
}

View File

@ -0,0 +1,86 @@
package cn.iocoder.yudao.module.trade.service.price.bo;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
import lombok.Data;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* 价格计算 Request BO
*
* @author yudao源码
*/
@Data
public class TradePriceCalculateReqBO {
/**
* 订单类型
*
* 枚举 {@link TradeOrderTypeEnum}
*/
private Integer orderType;
/**
* 用户编号
*
* 对应 MemberUserDO id 编号
*/
private Long userId;
/**
* 优惠劵编号
*
* 对应 CouponDO id 编号
*/
private Long couponId;
/**
* 收货地址编号
*
* 对应 MemberAddressDO id 编号
*/
private Long addressId;
/**
* 商品 SKU 数组
*/
@NotNull(message = "商品数组不能为空")
private List<Item> items;
/**
* 商品 SKU
*/
@Data
@Valid
public static class Item {
/**
* SKU 编号
*/
@NotNull(message = "商品 SKU 编号不能为空")
private Long skuId;
/**
* SKU 数量
*/
@NotNull(message = "商品 SKU 数量不能为空")
@Min(value = 0L, message = "商品 SKU 数量必须大于等于 0")
private Integer count;
/**
* 购物车项的编号
*/
private Long cartId;
/**
* 是否选中
*/
@NotNull(message = "是否选中不能为空")
private Boolean selected;
}
}

View File

@ -0,0 +1,249 @@
package cn.iocoder.yudao.module.trade.service.price.bo;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionLevelEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
import lombok.Data;
import java.util.List;
/**
* 价格计算 Response BO
*
* 整体设计参考 taobao 的技术文档
* 1. <a href="https://developer.alibaba.com/docs/doc.htm?treeId=1&articleId=1029&docType=1">订单管理</a>
* 2. <a href="https://open.taobao.com/docV3.htm?docId=108471&docType=1">常用订单金额说明</a>
*
* @author 芋道源码
*/
@Data
public class TradePriceCalculateRespBO {
/**
* 订单类型
*
* 枚举 {@link TradeOrderTypeEnum}
*/
private Integer orderType;
/**
* 订单价格
*/
private Price price;
/**
* 订单项数组
*/
private List<OrderItem> items;
/**
* 营销活动数组
*
* 只对应 {@link Price#items} 商品匹配的活动
*/
private List<Promotion> promotions;
/**
* 优惠劵编号
*/
private Long couponId;
/**
* 订单价格
*/
@Data
public static class Price {
/**
* 商品原价单位
*
* 基于 {@link OrderItem#getPrice()} * {@link OrderItem#getCount()} 求和
*
* 对应 taobao trade.total_fee 字段
*/
private Integer totalPrice;
/**
* 订单优惠单位
*
* 对应 taobao order.discount_fee 字段
*/
private Integer discountPrice;
/**
* 运费金额单位
*/
private Integer deliveryPrice;
/**
* 优惠劵减免金额单位
*
* 对应 taobao trade.coupon_fee 字段
*/
private Integer couponPrice;
/**
* 积分抵扣的金额单位
*
* 对应 taobao trade.point_fee 字段
*/
private Integer pointPrice;
/**
* 最终购买金额单位
*
* = {@link #totalPrice}
* - {@link #couponPrice}
* - {@link #pointPrice}
* - {@link #discountPrice}
* + {@link #deliveryPrice}
*/
private Integer payPrice;
}
/**
* 订单商品 SKU
*/
@Data
public static class OrderItem {
/**
* SPU 编号
*/
private Long spuId;
/**
* SKU 编号
*/
private Long skuId;
/**
* 购买数量
*/
private Integer count;
/**
* 购物车项的编号
*/
private Long cartId;
/**
* 是否选中
*/
private Boolean selected;
/**
* 商品原价单位
*
* 对应 ProductSkuDO price 字段
* 对应 taobao order.price 字段
*/
private Integer price;
/**
* 优惠金额单位
*
* 对应 taobao order.discount_fee 字段
*/
private Integer discountPrice;
/**
* 运费金额单位
*/
private Integer deliveryPrice;
/**
* 优惠劵减免金额单位
*
* 对应 taobao trade.coupon_fee 字段
*/
private Integer couponPrice;
/**
* 积分抵扣的金额单位
*
* 对应 taobao trade.point_fee 字段
*/
private Integer pointPrice;
/**
* 应付金额单位
*
* = {@link #price} * {@link #count}
* - {@link #couponPrice}
* - {@link #pointPrice}
* - {@link #discountPrice}
* + {@link #deliveryPrice}
*/
private Integer payPrice;
// TODO 芋艿这里补充下基本信息简单一点
}
/**
* 营销明细
*/
@Data
public static class Promotion {
/**
* 营销编号
*
* 例如说营销活动的编号优惠劵的编号
*/
private Long id;
/**
* 营销名字
*/
private String name;
/**
* 营销类型
*
* 枚举 {@link PromotionTypeEnum}
*/
private Integer type;
/**
* 营销级别
*
* 枚举 {@link PromotionLevelEnum}
*/
private Integer level;
/**
* 计算时的原价单位
*/
private Integer totalPrice;
/**
* 计算时的优惠单位
*/
private Integer discountPrice;
/**
* 匹配的商品 SKU 数组
*/
private List<PromotionItem> items;
// ========== 匹配情况 ==========
/**
* 是否满足优惠条件
*/
private Boolean match;
/**
* 满足条件的提示
*
* 如果 {@link #match} = true 满足则提示圣诞价: 150.00
* 如果 {@link #match} = false 不满足则提示购满 85 可减 40
*/
private String description;
}
/**
* 营销匹配的商品 SKU
*/
@Data
public static class PromotionItem {
/**
* 商品 SKU 编号
*/
private Long skuId;
/**
* 计算时的原价单位
*/
private Integer totalPrice;
/**
* 计算时的优惠单位
*/
private Integer discountPrice;
}
}

View File

@ -0,0 +1,109 @@
package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.promotion.api.coupon.CouponApi;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponRespDTO;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponValidReqDTO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.function.Predicate;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_NO_MATCH_MIN_PRICE;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_NO_MATCH_SPU;
/**
* 优惠劵的 {@link TradePriceCalculator} 实现类
*
* @author 芋道源码
*/
@Component
@Order(TradePriceCalculator.ORDER_COUPON)
public class TradeCouponPriceCalculator implements TradePriceCalculator {
@Resource
private CouponApi couponApi;
@Override
public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
// 1.1 校验优惠劵
if (param.getCouponId() == null) {
return;
}
CouponRespDTO coupon = couponApi.validateCoupon(new CouponValidReqDTO()
.setId(param.getCouponId()).setUserId(param.getUserId()));
Assert.notNull(coupon, "校验通过的优惠劵({}),不能为空", param.getCouponId());
// 2.1 获得匹配的商品 SKU 数组
List<TradePriceCalculateRespBO.OrderItem> orderItems = filterMatchCouponOrderItems(result, coupon);
if (CollUtil.isEmpty(orderItems)) {
throw exception(COUPON_NO_MATCH_SPU);
}
// 2.2 计算是否满足优惠劵的使用金额
Integer totalPayPrice = TradePriceCalculatorHelper.calculateTotalPayPrice(orderItems);
if (totalPayPrice < coupon.getUsePrice()) {
throw exception(COUPON_NO_MATCH_MIN_PRICE);
}
// 3.1 计算可以优惠的金额
Integer couponPrice = getCouponPrice(coupon, totalPayPrice);
Assert.isTrue(couponPrice < totalPayPrice,
"优惠劵({}) 的优惠金额({}),不能大于订单总金额({})", coupon.getId(), couponPrice, totalPayPrice);
// 3.2 计算分摊的优惠金额
List<Integer> divideCouponPrices = TradePriceCalculatorHelper.dividePrice(orderItems, couponPrice);
// 4.1 记录使用的优惠劵
result.setCouponId(param.getCouponId());
// 4.2 记录优惠明细
TradePriceCalculatorHelper.addPromotion(result, orderItems,
param.getCouponId(), coupon.getName(), PromotionTypeEnum.COUPON.getType(),
StrUtil.format("优惠劵:省 {} 元", TradePriceCalculatorHelper.formatPrice(couponPrice)),
divideCouponPrices);
// 4.3 更新 SKU 优惠金额
for (int i = 0; i < orderItems.size(); i++) {
TradePriceCalculateRespBO.OrderItem orderItem = orderItems.get(i);
orderItem.setCouponPrice(divideCouponPrices.get(i));
TradePriceCalculatorHelper.recountPayPrice(orderItem);
}
TradePriceCalculatorHelper.recountAllPrice(result);
}
private Integer getCouponPrice(CouponRespDTO coupon, Integer totalPayPrice) {
if (PromotionDiscountTypeEnum.PRICE.getType().equals(coupon.getDiscountType())) { // 减价
return coupon.getDiscountPrice();
} else if (PromotionDiscountTypeEnum.PERCENT.getType().equals(coupon.getDiscountType())) { // 打折
int couponPrice = totalPayPrice * coupon.getDiscountPercent() / 100;
return coupon.getDiscountLimitPrice() == null ? couponPrice
: Math.min(couponPrice, coupon.getDiscountLimitPrice()); // 优惠上限
}
throw new IllegalArgumentException(String.format("优惠劵(%s) 的优惠类型不正确", coupon));
}
/**
* 获得优惠劵可使用的订单项商品列表
*
* @param result 计算结果
* @param coupon 优惠劵
* @return 订单项商品列表
*/
private List<TradePriceCalculateRespBO.OrderItem> filterMatchCouponOrderItems(TradePriceCalculateRespBO result,
CouponRespDTO coupon) {
Predicate<TradePriceCalculateRespBO.OrderItem> matchPredicate = TradePriceCalculateRespBO.OrderItem::getSelected;
if (PromotionProductScopeEnum.SPU.getScope().equals(coupon.getProductScope())) {
matchPredicate = orderItem -> coupon.getProductSpuIds().contains(orderItem.getSpuId());
}
return filterList(result.getItems(), matchPredicate);
}
}

View File

@ -0,0 +1,80 @@
package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.promotion.api.discount.DiscountActivityApi;
import cn.iocoder.yudao.module.promotion.api.discount.dto.DiscountProductRespDTO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper.formatPrice;
/**
* 限时折扣的 {@link TradePriceCalculator} 实现类
*
* @author 芋道源码
*/
@Component
@Order(TradePriceCalculator.ORDER_DISCOUNT_ACTIVITY)
public class TradeDiscountActivityPriceCalculator implements TradePriceCalculator {
@Resource
private DiscountActivityApi discountActivityApi;
@Override
public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
// 获得 SKU 对应的限时折扣活动
List<DiscountProductRespDTO> discountProducts = discountActivityApi.getMatchDiscountProductList(
convertSet(result.getItems(), TradePriceCalculateRespBO.OrderItem::getSkuId));
if (CollUtil.isEmpty(discountProducts)) {
return;
}
Map<Long, DiscountProductRespDTO> discountProductMap = convertMap(discountProducts, DiscountProductRespDTO::getSkuId);
// 处理每个 SKU 的限时折扣
result.getItems().forEach(orderItem -> {
// 1. 获取该 SKU 的优惠信息
DiscountProductRespDTO discountProduct = discountProductMap.get(orderItem.getSkuId());
if (discountProduct == null) {
return;
}
// 2. 计算优惠金额
Integer newPayPrice = calculatePayPrice(discountProduct, orderItem);
Integer newDiscountPrice = orderItem.getPayPrice() - newPayPrice;
// 3.1 记录优惠明细
TradePriceCalculatorHelper.addPromotion(result, orderItem,
discountProduct.getActivityId(), discountProduct.getActivityName(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType(),
StrUtil.format("限时折扣:省 {} 元", formatPrice(newDiscountPrice)),
newDiscountPrice);
// 3.2 更新 SKU 优惠金额
orderItem.setDiscountPrice(orderItem.getDiscountPrice() + newDiscountPrice);
TradePriceCalculatorHelper.recountPayPrice(orderItem);
});
TradePriceCalculatorHelper.recountAllPrice(result);
}
private Integer calculatePayPrice(DiscountProductRespDTO discountProduct,
TradePriceCalculateRespBO.OrderItem orderItem) {
Integer price = orderItem.getPayPrice();
if (PromotionDiscountTypeEnum.PRICE.getType().equals(discountProduct.getDiscountType())) { // 减价
price -= discountProduct.getDiscountPrice() * orderItem.getCount();
} else if (PromotionDiscountTypeEnum.PERCENT.getType().equals(discountProduct.getDiscountType())) { // 打折
price = price * discountProduct.getDiscountPercent() / 100;
} else {
throw new IllegalArgumentException(String.format("优惠活动的商品(%s) 的优惠类型不正确", discountProduct));
}
return price;
}
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
/**
* 价格计算的计算器接口
*
* @author 芋道源码
*/
public interface TradePriceCalculator {
int ORDER_DISCOUNT_ACTIVITY = 10;
int ORDER_REWARD_ACTIVITY = 20;
int ORDER_COUPON = 30;
void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result);
}

View File

@ -0,0 +1,221 @@
package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue;
import static java.util.Collections.singletonList;
/**
* {@link TradePriceCalculator} 的工具类
*
* 主要实现对 {@link TradePriceCalculateRespBO} 计算结果的操作
*
* @author 芋道源码
*/
public class TradePriceCalculatorHelper {
public static TradePriceCalculateRespBO buildCalculateResp(TradePriceCalculateReqBO param,
List<ProductSkuRespDTO> skuList) {
// 创建 PriceCalculateRespDTO 对象
TradePriceCalculateRespBO result = new TradePriceCalculateRespBO();
result.setOrderType(param.getOrderType());
// 创建它的 OrderItem 属性
Map<Long, TradePriceCalculateReqBO.Item> skuItemMap = convertMap(param.getItems(),
TradePriceCalculateReqBO.Item::getSkuId);
result.setItems(new ArrayList<>(skuItemMap.size()));
skuList.forEach(sku -> {
TradePriceCalculateReqBO.Item skuItem = skuItemMap.get(sku.getId());
TradePriceCalculateRespBO.OrderItem orderItem = new TradePriceCalculateRespBO.OrderItem()
// SKU 字段
.setSpuId(sku.getSpuId()).setSkuId(sku.getId())
.setCount(skuItem.getCount()).setCartId(skuItem.getCartId()).setSelected(skuItem.getSelected())
// 价格字段
.setPrice(sku.getPrice()).setPayPrice(sku.getPrice() * skuItem.getCount())
.setDiscountPrice(0).setDeliveryPrice(0).setCouponPrice(0).setPointPrice(0);
result.getItems().add(orderItem);
});
// 创建它的 Price 属性
result.setPrice(new TradePriceCalculateRespBO.Price());
recountAllPrice(result);
return result;
}
/**
* 基于订单项重新计算 price 总价
*
* @param result 计算结果
*/
public static void recountAllPrice(TradePriceCalculateRespBO result) {
// 先重置
TradePriceCalculateRespBO.Price price = result.getPrice();
price.setTotalPrice(0).setDiscountPrice(0).setDeliveryPrice(0)
.setCouponPrice(0).setPointPrice(0).setPayPrice(0);
// 再合计 item
result.getItems().forEach(item -> {
if (!item.getSelected()) {
return;
}
price.setTotalPrice(price.getTotalPrice() + item.getPrice() * item.getCount());
price.setDiscountPrice(price.getDiscountPrice() + item.getDiscountPrice());
price.setDeliveryPrice(price.getDeliveryPrice() + item.getDeliveryPrice());
price.setCouponPrice(price.getCouponPrice() + item.getCouponPrice());
price.setPointPrice(price.getPointPrice() + item.getPointPrice());
price.setPayPrice(price.getPayPrice() + item.getPayPrice());
});
}
/**
* 重新计算单个订单项的支付金额
*
* @param orderItem 订单项
*/
public static void recountPayPrice(TradePriceCalculateRespBO.OrderItem orderItem) {
orderItem.setPayPrice(orderItem.getPrice()* orderItem.getCount()
- orderItem.getDiscountPrice()
+ orderItem.getDeliveryPrice()
- orderItem.getCouponPrice()
- orderItem.getPointPrice());
}
/**
* 计算已选中的订单项总支付金额
*
* @param orderItems 订单项数组
* @return 总支付金额
*/
public static Integer calculateTotalPayPrice(List<TradePriceCalculateRespBO.OrderItem> orderItems) {
return getSumValue(orderItems,
orderItem -> orderItem.getSelected() ? orderItem.getPayPrice() : 0, // 未选中的情况下不计算支付金额
Integer::sum);
}
/**
* 计算已选中的订单项总商品数
*
* @param orderItems 订单项数组
* @return 总商品数
*/
public static Integer calculateTotalCount(List<TradePriceCalculateRespBO.OrderItem> orderItems) {
return getSumValue(orderItems,
orderItem -> orderItem.getSelected() ? orderItem.getCount() : 0, // 未选中的情况下不计算数量
Integer::sum);
}
/**
* 按照支付金额返回每个订单项的分摊金额数组
*
* @param orderItems 订单项数组
* @param price 金额
* @return 分摊金额数组和传入的 orderItems 一一对应
*/
public static List<Integer> dividePrice(List<TradePriceCalculateRespBO.OrderItem> orderItems, Integer price) {
Integer total = calculateTotalPayPrice(orderItems);
assert total != null;
// 遍历每一个进行分摊
List<Integer> prices = new ArrayList<>(orderItems.size());
int remainPrice = price;
for (int i = 0; i < orderItems.size(); i++) {
TradePriceCalculateRespBO.OrderItem orderItem = orderItems.get(i);
// 1. 如果是未选中则分摊为 0
if (!orderItem.getSelected()) {
prices.add(0);
continue;
}
// 2. 如果选中则按照百分比进行分摊
int partPrice;
if (i < orderItems.size() - 1) { // 减一的原因是因为拆分时如果按照比例可能会出现.所以最后一个使用反减
partPrice = (int) (price * (1.0D * orderItem.getPayPrice() / total));
remainPrice -= partPrice;
} else {
partPrice = remainPrice;
}
Assert.isTrue(partPrice >= 0, "分摊金额必须大于等于 0");
prices.add(partPrice);
}
return prices;
}
/**
* 添加匹配单个 OrderItem 的营销明细
*
* @param result 价格计算结果
* @param orderItem 单个订单商品 SKU
* @param id 营销编号
* @param name 营销名字
* @param description 满足条件的提示
* @param type 营销类型
* @param discountPrice 单个订单商品 SKU 的优惠价格
*/
public static void addPromotion(TradePriceCalculateRespBO result, TradePriceCalculateRespBO.OrderItem orderItem,
Long id, String name, Integer type, String description, Integer discountPrice) {
addPromotion(result, singletonList(orderItem), id, name, type, description, singletonList(discountPrice));
}
/**
* 添加匹配多个 OrderItem 的营销明细
*
* @param result 价格计算结果
* @param orderItems 多个订单商品 SKU
* @param id 营销编号
* @param name 营销名字
* @param description 满足条件的提示
* @param type 营销类型
* @param discountPrices 多个订单商品 SKU 的优惠价格 orderItems 一一对应
*/
public static void addPromotion(TradePriceCalculateRespBO result, List<TradePriceCalculateRespBO.OrderItem> orderItems,
Long id, String name, Integer type, String description, List<Integer> discountPrices) {
// 创建营销明细 Item
List<TradePriceCalculateRespBO.PromotionItem> promotionItems = new ArrayList<>(discountPrices.size());
for (int i = 0; i < orderItems.size(); i++) {
TradePriceCalculateRespBO.OrderItem orderItem = orderItems.get(i);
promotionItems.add(new TradePriceCalculateRespBO.PromotionItem().setSkuId(orderItem.getSkuId())
.setTotalPrice(orderItem.getPayPrice()).setDiscountPrice(discountPrices.get(i)));
}
// 创建营销明细
TradePriceCalculateRespBO.Promotion promotion = new TradePriceCalculateRespBO.Promotion()
.setId(id).setName(name).setType(type)
.setTotalPrice(calculateTotalPayPrice(orderItems))
.setDiscountPrice(getSumValue(discountPrices, value -> value, Integer::sum))
.setItems(promotionItems).setMatch(true).setDescription(description);
result.getPromotions().add(promotion);
}
/**
* 添加不匹配多个 OrderItem 的营销明细
*
* @param result 价格计算结果
* @param orderItems 多个订单商品 SKU
* @param id 营销编号
* @param name 营销名字
* @param description 满足条件的提示
* @param type 营销类型
*/
public static void addNotMatchPromotion(TradePriceCalculateRespBO result, List<TradePriceCalculateRespBO.OrderItem> orderItems,
Long id, String name, Integer type, String description) {
// 创建营销明细 Item
List<TradePriceCalculateRespBO.PromotionItem> promotionItems = CollectionUtils.convertList(orderItems,
orderItem -> new TradePriceCalculateRespBO.PromotionItem().setSkuId(orderItem.getSkuId())
.setTotalPrice(orderItem.getPayPrice()).setDiscountPrice(0));
// 创建营销明细
TradePriceCalculateRespBO.Promotion promotion = new TradePriceCalculateRespBO.Promotion()
.setId(id).setName(name).setType(type)
.setTotalPrice(calculateTotalPayPrice(orderItems))
.setDiscountPrice(0)
.setItems(promotionItems).setMatch(false).setDescription(description);
result.getPromotions().add(promotion);
}
public static String formatPrice(Integer price) {
return String.format("%.2f", price / 100d);
}
}

View File

@ -0,0 +1,133 @@
package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.promotion.api.reward.RewardActivityApi;
import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper.formatPrice;
/**
* 满减送活动的 {@link TradePriceCalculator} 实现类
*
* @author 芋道源码
*/
@Component
@Order(TradePriceCalculator.ORDER_REWARD_ACTIVITY)
public class TradeRewardActivityPriceCalculator implements TradePriceCalculator {
@Resource
private RewardActivityApi rewardActivityApi;
@Override
public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
// 获得 SKU 对应的满减送活动
List<RewardActivityMatchRespDTO> rewardActivities = rewardActivityApi.getMatchRewardActivityList(
convertSet(result.getItems(), TradePriceCalculateRespBO.OrderItem::getSpuId));
if (CollUtil.isEmpty(rewardActivities)) {
return;
}
// 处理每个满减送活动
rewardActivities.forEach(rewardActivity -> calculate(param, result, rewardActivity));
}
private void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result,
RewardActivityMatchRespDTO rewardActivity) {
// 1.1 获得满减送的订单项商品列表
List<TradePriceCalculateRespBO.OrderItem> orderItems = filterMatchCouponOrderItems(result, rewardActivity);
if (CollUtil.isEmpty(orderItems)) {
return;
}
// 1.2 获得最大匹配的满减送活动的规则
RewardActivityMatchRespDTO.Rule rule = getMaxMatchRewardActivityRule(rewardActivity, orderItems);
if (rule == null) {
return;
}
// 2.1 计算可以优惠的金额
Integer newDiscountPrice = rule.getDiscountPrice();
// 2.2 计算分摊的优惠金额
List<Integer> divideDiscountPrices = TradePriceCalculatorHelper.dividePrice(orderItems, newDiscountPrice);
// 3.1 记录使用的优惠劵
result.setCouponId(param.getCouponId());
// 3.2 记录优惠明细
TradePriceCalculatorHelper.addPromotion(result, orderItems,
rewardActivity.getId(), rewardActivity.getName(), PromotionTypeEnum.REWARD_ACTIVITY.getType(),
StrUtil.format("满减送:省 {} 元", formatPrice(rule.getDiscountPrice())),
divideDiscountPrices);
// 3.3 更新 SKU 优惠金额
for (int i = 0; i < orderItems.size(); i++) {
TradePriceCalculateRespBO.OrderItem orderItem = orderItems.get(i);
orderItem.setDiscountPrice(orderItem.getDiscountPrice() + divideDiscountPrices.get(i));
TradePriceCalculatorHelper.recountPayPrice(orderItem);
}
TradePriceCalculatorHelper.recountAllPrice(result);
}
/**
* 获得满减送的订单项商品列表
*
* @param result 计算结果
* @param rewardActivity 满减送活动
* @return 订单项商品列表
*/
private List<TradePriceCalculateRespBO.OrderItem> filterMatchCouponOrderItems(TradePriceCalculateRespBO result,
RewardActivityMatchRespDTO rewardActivity) {
return filterList(result.getItems(),
orderItem -> CollUtil.contains(rewardActivity.getSpuIds(), orderItem.getSpuId()));
}
/**
* 获得最大匹配的满减送活动的规则
*
* @param rewardActivity 满减送活动
* @param orderItems 商品项
* @return 匹配的活动规则
*/
private RewardActivityMatchRespDTO.Rule getMaxMatchRewardActivityRule(RewardActivityMatchRespDTO rewardActivity,
List<TradePriceCalculateRespBO.OrderItem> orderItems) {
// 1. 计算数量和价格
Integer count = TradePriceCalculatorHelper.calculateTotalCount(orderItems);
Integer price = TradePriceCalculatorHelper.calculateTotalPayPrice(orderItems);
assert count != null && price != null;
// 2. 倒序找一个最大优惠的规则
for (int i = rewardActivity.getRules().size() - 1; i >= 0; i--) {
RewardActivityMatchRespDTO.Rule rule = rewardActivity.getRules().get(i);
if (PromotionConditionTypeEnum.PRICE.getType().equals(rewardActivity.getConditionType())
&& price >= rule.getLimit()) {
return rule;
}
if (PromotionConditionTypeEnum.COUNT.getType().equals(rewardActivity.getConditionType())
&& count >= rule.getLimit()) {
return rule;
}
}
return null;
}
/**
* 获得满减送活动部匹配时的提示
*
* @param rewardActivity 满减送活动
* @return 提示
*/
private String getRewardActivityNotMeetTip(RewardActivityMatchRespDTO rewardActivity) {
// TODO 芋艿后面再想想应该找第一个规则算下还差多少即可
return "TODO";
}
}

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreate
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressRespVO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.address.AddressConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.MemberAddressDO;
import cn.iocoder.yudao.module.member.service.address.AddressService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
@ -54,21 +54,21 @@ public class AppAddressController {
@Operation(summary = "获得用户收件地址")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
public CommonResult<AppAddressRespVO> getAddress(@RequestParam("id") Long id) {
AddressDO address = addressService.getAddress(getLoginUserId(), id);
MemberAddressDO address = addressService.getAddress(getLoginUserId(), id);
return success(AddressConvert.INSTANCE.convert(address));
}
@GetMapping("/get-default")
@Operation(summary = "获得默认的用户收件地址")
public CommonResult<AppAddressRespVO> getDefaultUserAddress() {
AddressDO address = addressService.getDefaultUserAddress(getLoginUserId());
MemberAddressDO address = addressService.getDefaultUserAddress(getLoginUserId());
return success(AddressConvert.INSTANCE.convert(address));
}
@GetMapping("/list")
@Operation(summary = "获得用户收件地址列表")
public CommonResult<List<AppAddressRespVO>> getAddressList() {
List<AddressDO> list = addressService.getAddressList(getLoginUserId());
List<MemberAddressDO> list = addressService.getAddressList(getLoginUserId());
return success(AddressConvert.INSTANCE.convertList(list));
}

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressRespVO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.MemberAddressDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@ -21,16 +21,16 @@ public interface AddressConvert {
AddressConvert INSTANCE = Mappers.getMapper(AddressConvert.class);
AddressDO convert(AppAddressCreateReqVO bean);
MemberAddressDO convert(AppAddressCreateReqVO bean);
AddressDO convert(AppAddressUpdateReqVO bean);
MemberAddressDO convert(AppAddressUpdateReqVO bean);
AppAddressRespVO convert(AddressDO bean);
AppAddressRespVO convert(MemberAddressDO bean);
List<AppAddressRespVO> convertList(List<AddressDO> list);
List<AppAddressRespVO> convertList(List<MemberAddressDO> list);
PageResult<AppAddressRespVO> convertPage(PageResult<AddressDO> page);
PageResult<AppAddressRespVO> convertPage(PageResult<MemberAddressDO> page);
AddressRespDTO convert02(AddressDO bean);
AddressRespDTO convert02(MemberAddressDO bean);
}

View File

@ -17,7 +17,7 @@ import lombok.*;
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AddressDO extends BaseDO {
public class MemberAddressDO extends BaseDO {
/**
* 编号

View File

@ -2,21 +2,21 @@ package cn.iocoder.yudao.module.member.dal.mysql.address;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.MemberAddressDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface AddressMapper extends BaseMapperX<AddressDO> {
public interface AddressMapper extends BaseMapperX<MemberAddressDO> {
default AddressDO selectByIdAndUserId(Long id, Long userId) {
return selectOne(AddressDO::getId, id, AddressDO::getUserId, userId);
default MemberAddressDO selectByIdAndUserId(Long id, Long userId) {
return selectOne(MemberAddressDO::getId, id, MemberAddressDO::getUserId, userId);
}
default List<AddressDO> selectListByUserIdAndDefaulted(Long userId, Boolean defaulted) {
return selectList(new LambdaQueryWrapperX<AddressDO>().eq(AddressDO::getUserId, userId)
.eqIfPresent(AddressDO::getDefaulted, defaulted));
default List<MemberAddressDO> selectListByUserIdAndDefaulted(Long userId, Boolean defaulted) {
return selectList(new LambdaQueryWrapperX<MemberAddressDO>().eq(MemberAddressDO::getUserId, userId)
.eqIfPresent(MemberAddressDO::getDefaulted, defaulted));
}
}

View File

@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.member.service.address;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.MemberAddressDO;
import javax.validation.Valid;
import java.util.List;
@ -46,7 +46,7 @@ public interface AddressService {
* @param id 编号
* @return 用户收件地址
*/
AddressDO getAddress(Long userId, Long id);
MemberAddressDO getAddress(Long userId, Long id);
/**
* 获得用户收件地址列表
@ -54,7 +54,7 @@ public interface AddressService {
* @param userId 用户编号
* @return 用户收件地址列表
*/
List<AddressDO> getAddressList(Long userId);
List<MemberAddressDO> getAddressList(Long userId);
/**
* 获得用户默认的收件地址
@ -62,6 +62,6 @@ public interface AddressService {
* @param userId 用户编号
* @return 用户收件地址
*/
AddressDO getDefaultUserAddress(Long userId);
MemberAddressDO getDefaultUserAddress(Long userId);
}

View File

@ -4,7 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.address.AddressConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.MemberAddressDO;
import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -33,12 +33,12 @@ public class AddressServiceImpl implements AddressService {
public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) {
// 如果添加的是默认收件地址则将原默认地址修改为非默认
if (Boolean.TRUE.equals(createReqVO.getDefaulted())) {
List<AddressDO> addresses = addressMapper.selectListByUserIdAndDefaulted(userId, true);
addresses.forEach(address -> addressMapper.updateById(new AddressDO().setId(address.getId()).setDefaulted(false)));
List<MemberAddressDO> addresses = addressMapper.selectListByUserIdAndDefaulted(userId, true);
addresses.forEach(address -> addressMapper.updateById(new MemberAddressDO().setId(address.getId()).setDefaulted(false)));
}
// 插入
AddressDO address = AddressConvert.INSTANCE.convert(createReqVO);
MemberAddressDO address = AddressConvert.INSTANCE.convert(createReqVO);
address.setUserId(userId);
addressMapper.insert(address);
// 返回
@ -53,13 +53,13 @@ public class AddressServiceImpl implements AddressService {
// 如果修改的是默认收件地址则将原默认地址修改为非默认
if (Boolean.TRUE.equals(updateReqVO.getDefaulted())) {
List<AddressDO> addresses = addressMapper.selectListByUserIdAndDefaulted(userId, true);
List<MemberAddressDO> addresses = addressMapper.selectListByUserIdAndDefaulted(userId, true);
addresses.stream().filter(u -> !u.getId().equals(updateReqVO.getId())) // 排除自己
.forEach(address -> addressMapper.updateById(new AddressDO().setId(address.getId()).setDefaulted(false)));
.forEach(address -> addressMapper.updateById(new MemberAddressDO().setId(address.getId()).setDefaulted(false)));
}
// 更新
AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO);
MemberAddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO);
addressMapper.updateById(updateObj);
}
@ -72,25 +72,25 @@ public class AddressServiceImpl implements AddressService {
}
private void validAddressExists(Long userId, Long id) {
AddressDO addressDO = getAddress(userId, id);
MemberAddressDO addressDO = getAddress(userId, id);
if (addressDO == null) {
throw exception(ADDRESS_NOT_EXISTS);
}
}
@Override
public AddressDO getAddress(Long userId, Long id) {
public MemberAddressDO getAddress(Long userId, Long id) {
return addressMapper.selectByIdAndUserId(id, userId);
}
@Override
public List<AddressDO> getAddressList(Long userId) {
public List<MemberAddressDO> getAddressList(Long userId) {
return addressMapper.selectListByUserIdAndDefaulted(userId, null);
}
@Override
public AddressDO getDefaultUserAddress(Long userId) {
List<AddressDO> addresses = addressMapper.selectListByUserIdAndDefaulted(userId, true);
public MemberAddressDO getDefaultUserAddress(Long userId) {
List<MemberAddressDO> addresses = addressMapper.selectListByUserIdAndDefaulted(userId, true);
return CollUtil.getFirst(addresses);
}

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.member.service.address;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.MemberAddressDO;
import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
@ -42,14 +42,14 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
// 断言
assertNotNull(addressId);
// 校验记录的属性是否正确
AddressDO address = addressMapper.selectById(addressId);
MemberAddressDO address = addressMapper.selectById(addressId);
assertPojoEquals(reqVO, address);
}
@Test
public void testUpdateAddress_success() {
// mock 数据
AddressDO dbAddress = randomPojo(AddressDO.class);
MemberAddressDO dbAddress = randomPojo(MemberAddressDO.class);
addressMapper.insert(dbAddress);// @Sql: 先插入出一条存在的数据
// 准备参数
AppAddressUpdateReqVO reqVO = randomPojo(AppAddressUpdateReqVO.class, o -> {
@ -59,7 +59,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
// 调用
addressService.updateAddress(dbAddress.getUserId(), reqVO);
// 校验是否更新正确
AddressDO address = addressMapper.selectById(reqVO.getId()); // 获取最新的
MemberAddressDO address = addressMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, address);
}
@ -75,7 +75,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
@Test
public void testDeleteAddress_success() {
// mock 数据
AddressDO dbAddress = randomPojo(AddressDO.class);
MemberAddressDO dbAddress = randomPojo(MemberAddressDO.class);
addressMapper.insert(dbAddress);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbAddress.getId();