Merge remote-tracking branch 'yudao/feature/mall_product' into feature/mall_product

This commit is contained in:
puhui999 2023-05-30 09:46:31 +08:00
commit fcaa92d406
92 changed files with 1858 additions and 1107 deletions

View File

@ -330,9 +330,9 @@ CREATE TABLE `product_favorite` (
-- ----------------------------
DROP TABLE IF EXISTS `trade_delivery_express_template`;
CREATE TABLE `trade_delivery_express_template` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号自增',
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` varchar(64) NOT NULL COMMENT '模板名称',
`charge_mode` tinyint NOT NULL DEFAULT 1 COMMENT '配送计费方式 1:按件 2:按重量 3:按体积',
`charge_mode` tinyint NOT NULL COMMENT '配送计费方式',
`sort` int NOT NULL DEFAULT 0 COMMENT '排序',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
@ -348,11 +348,11 @@ CREATE TABLE `trade_delivery_express_template` (
-- ----------------------------
DROP TABLE IF EXISTS `trade_delivery_express_template_free`;
CREATE TABLE `trade_delivery_express_template_free` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号自增',
`template_id` bigint NOT NULL COMMENT '配送模板编号, 对应delivery_template表id',
`area_id` int NOT NULL COMMENT '包邮区域id',
`free_price` int NOT NULL COMMENT '包邮金额(单位分) 订单总金额>包邮金额才免运费',
`free_count` int NOT NULL DEFAULT 0 COMMENT '包邮件数,订单总件数>包邮件数才免运费',
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`template_id` bigint NOT NULL COMMENT '快递运费模板编号',
`area_ids` varchar(100) NOT NULL COMMENT '包邮区域 ids',
`free_price` int NOT NULL COMMENT '包邮金额单位',
`free_count` int NOT NULL DEFAULT 0 COMMENT '包邮件数,',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
@ -368,13 +368,13 @@ CREATE TABLE `trade_delivery_express_template_free` (
DROP TABLE IF EXISTS `trade_delivery_express_template_charge`;
CREATE TABLE `trade_delivery_express_template_charge` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号自增',
`template_id` bigint NOT NULL COMMENT '配送模板编号, 对应delivery_template表id',
`area_id` int NOT NULL COMMENT '配送区域id 1:适用于全国',
`charge_mode` tinyint NOT NULL COMMENT '配送计费方式 1:按件 2:按重量 3:按体积',
`start_count` double NOT NULL COMMENT '首件数量(件数,重量或体积)',
`start_price` int NOT NULL COMMENT '起步价(单位分)',
`extra_count` double NOT NULL COMMENT '续件数量(,重量或体积)',
`extra_price` int NOT NULL COMMENT '额外价(单位分)',
`template_id` bigint NOT NULL COMMENT '快递运费模板编号',
`area_ids` varchar(100) NOT NULL COMMENT '配送区域 ids',
`charge_mode` tinyint NOT NULL COMMENT '配送计费方式',
`start_count` double NOT NULL COMMENT '首件数量',
`start_price` int NOT NULL COMMENT '起步价单位',
`extra_count` double NOT NULL COMMENT '续件数量',
`extra_price` int NOT NULL COMMENT '额外价单位',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
@ -417,7 +417,7 @@ DROP TABLE IF EXISTS `trade_delivery_pick_up_store_staff`;
CREATE TABLE `trade_delivery_pick_up_store_staff` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号自增',
store_id bigint NOT NULL COMMENT '自提门店编号',
`status` tinyint NOT NULL DEFAULT 0 COMMENT '状态0正常 1停用',
`status` tinyint NOT NULL DEFAULT 0 COMMENT '状态',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
@ -451,12 +451,12 @@ CREATE TABLE `trade_delivery_pick_up_store_staff` (
-- ----------------------------
DROP TABLE IF EXISTS `trade_delivery_express`;
CREATE TABLE `trade_delivery_express` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号自增',
`code` varchar(64) NOT NULL COMMENT '快递公司编',
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`code` varchar(64) NOT NULL COMMENT '快递公司编',
`name` varchar(64) NOT NULL COMMENT '快递公司名称',
`logo` varchar(256) COMMENT '快递公司logo',
`logo` varchar(256) COMMENT '快递公司 logo',
`sort` int NOT NULL DEFAULT 0 COMMENT '排序',
`status` tinyint NOT NULL DEFAULT 0 COMMENT '状态0正常 1停用',
`status` tinyint NOT NULL DEFAULT 0 COMMENT '状态',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
@ -501,7 +501,7 @@ INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `status`, `visible`, `keep_alive`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2029, 'Banner删除', 'market:banner:delete', 3, 4, 2025, '', '', '', 0, b'1', b'1', '', '2022-08-01 14:56:14', '', '2022-08-01 14:56:14', b'0');
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2164, '配送管理', '', 1, 0, 2072, 'delivery', '', '', '', 0, b'1', b'1', b'1', '1', '2023-05-18 09:18:02', '1', '2023-05-18 09:48:48', b'0');
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2165, '快递发货', '', 1, 0, 2164, 'express', '', '', '', 0, b'1', b'1', b'1', '1', '2023-05-18 09:22:06', '1', '2023-05-18 09:22:06', b'0');
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2166, '门店自提', '', 1, 1, 2164, 'pick-up-store', '', '', '', 0, b'1', b'1', b'1', '1', '2023-05-18 09:23:14', '1', '2023-05-18 09:23:14', b'0');
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2166, '门店自提', '', 1, 1, 2164, 'pick-up', '', '', '', 0, b'1', b'1', b'1', '1', '2023-05-18 09:23:14', '1', '2023-05-18 09:23:14', b'0');
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2167, '快递公司', '', 2, 0, 2165, 'express', '', 'mall/trade/delivery/express/index', 'Express', 0, b'1', b'1', b'1', '1', '2023-05-18 09:27:21', '1', '2023-05-18 22:11:14', b'0');
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2168, '快递公司查询', 'trade:delivery:express:query', 3, 1, 2167, '', '', '', NULL, 0, b'1', b'1', b'1', '', '2023-05-18 09:37:53', '', '2023-05-18 09:37:53', b'0');
INSERT INTO `ruoyi-vue-pro`.`system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (2169, '快递公司创建', 'trade:delivery:express:create', 3, 2, 2167, '', '', '', NULL, 0, b'1', b'1', b'1', '', '2023-05-18 09:37:53', '', '2023-05-18 09:37:53', b'0');

View File

@ -45,4 +45,9 @@ public class StrUtils {
return Arrays.stream(longs).boxed().collect(Collectors.toList());
}
public static List<Integer> splitToInteger(String value, CharSequence separator) {
int[] integers = StrUtil.splitToInt(value, separator);
return Arrays.stream(integers).boxed().collect(Collectors.toList());
}
}

View File

@ -113,10 +113,20 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> {
Db.saveBatch(entities, size);
}
// @芋艿 是不是叫 updateByDo 或者 updateByEntity 更合适回复因为是使用实体作为条件去批量更新所以没加 ByEntity保持和 mybatis plus 风格一致
default void updateBatch(T update) {
update(update, new QueryWrapper<>());
}
/**
* 根据ID 批量更新适合大量数据更新
*
* @param entities 实体们
*/
default void updateBatch(Collection<T> entities) {
Db.updateBatchById(entities);
}
default void updateBatch(Collection<T> entities, int size) {
Db.updateBatchById(entities, size);
}

View File

@ -0,0 +1,56 @@
package cn.iocoder.yudao.framework.mybatis.core.type;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* List<Integer> 的类型转换器实现类对应数据库的 varchar 类型
*
* @author jason
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class IntegerListTypeHandler implements TypeHandler<List<Integer>> {
private static final String COMMA = ",";
@Override
public void setParameter(PreparedStatement ps, int i, List<Integer> strings, JdbcType jdbcType) throws SQLException {
ps.setString(i, CollUtil.join(strings, COMMA));
}
@Override
public List<Integer> getResult(ResultSet rs, String columnName) throws SQLException {
String value = rs.getString(columnName);
return getResult(value);
}
@Override
public List<Integer> getResult(ResultSet rs, int columnIndex) throws SQLException {
String value = rs.getString(columnIndex);
return getResult(value);
}
@Override
public List<Integer> getResult(CallableStatement cs, int columnIndex) throws SQLException {
String value = cs.getString(columnIndex);
return getResult(value);
}
private List<Integer> getResult(String value) {
if (value == null) {
return null;
}
return StrUtils.splitToInteger(value, COMMA);
}
}

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 {
/**
@ -38,6 +39,7 @@ public class PriceCalculateRespDTO {
*/
private List<Promotion> promotions;
// TODO @芋艿需要改造下主要是价格字段
/**
* 订单
*/
@ -51,14 +53,7 @@ public class PriceCalculateRespDTO {
*
* 对应 taobao trade.total_fee 字段
*/
private Integer originalPrice;
/**
* 订单原价单位
*
* 基于 {@link OrderItem#getPayPrice()} 求和
* {@link #originalPrice} 的差异去除商品级优惠
*/
private Integer orderPrice;
private Integer totalPrice;
/**
* 订单优惠单位
*
@ -180,6 +175,7 @@ public class PriceCalculateRespDTO {
* 营销明细
*/
@Data
@Deprecated
public static class Promotion {
/**
@ -207,7 +203,7 @@ public class PriceCalculateRespDTO {
/**
* 计算时的原价单位
*/
private Integer originalPrice;
private Integer totalPrice;
/**
* 计算时的优惠单位
*/
@ -222,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

@ -22,7 +22,7 @@ public interface PriceConvert {
// 创建 PriceCalculateRespDTO 对象
PriceCalculateRespDTO priceCalculate = new PriceCalculateRespDTO();
// 创建它的 Order 属性
PriceCalculateRespDTO.Order order = new PriceCalculateRespDTO.Order().setOriginalPrice(0).setDiscountPrice(0)
PriceCalculateRespDTO.Order order = new PriceCalculateRespDTO.Order().setTotalPrice(0).setDiscountPrice(0)
.setCouponPrice(0).setPointPrice(0).setDeliveryPrice(0).setPayPrice(0)
.setItems(new ArrayList<>()).setCouponId(calculateReqDTO.getCouponId());
priceCalculate.setOrder(order).setPromotions(new ArrayList<>());
@ -38,8 +38,8 @@ public interface PriceConvert {
orderItem.setPayPrice(orderItem.getOriginalPrice()).setOrderDividePrice(orderItem.getOriginalPrice());
priceCalculate.getOrder().getItems().add(orderItem);
// 补充价格信息到 Order
order.setOriginalPrice(order.getOriginalPrice() + orderItem.getOriginalPrice())
.setOrderPrice(order.getOriginalPrice()).setPayPrice(order.getOriginalPrice());
order.setTotalPrice(order.getTotalPrice() + orderItem.getOriginalPrice())
.setPayPrice(order.getTotalPrice());
});
return priceCalculate;
}

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,414 +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)
.setOriginalPrice(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)
.setOriginalPrice(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)
.setOriginalPrice(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);
order.setOrderPrice(order.getOrderPrice() - 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

@ -70,8 +70,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
// 断言 Order 部分
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
assertEquals(order.getOriginalPrice(), 200);
assertEquals(order.getOrderPrice(), 180);
assertEquals(order.getTotalPrice(), 200);
assertEquals(order.getDiscountPrice(), 0);
assertEquals(order.getPointPrice(), 0);
assertEquals(order.getDeliveryPrice(), 0);
@ -95,10 +94,10 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion.getName(), "会员折扣");
assertEquals(promotion.getType(), PromotionTypeEnum.MEMBER.getType());
assertEquals(promotion.getLevel(), PromotionLevelEnum.SKU.getLevel());
assertEquals(promotion.getOriginalPrice(), 200);
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);
@ -123,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
@ -133,8 +132,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
// 断言 Order 部分
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
assertEquals(order.getOriginalPrice(), 350);
assertEquals(order.getOrderPrice(), 210);
assertEquals(order.getTotalPrice(), 350);
assertEquals(order.getDiscountPrice(), 0);
assertEquals(order.getPointPrice(), 0);
assertEquals(order.getDeliveryPrice(), 0);
@ -167,10 +165,10 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getName(), "活动 1000 号");
assertEquals(promotion01.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType());
assertEquals(promotion01.getLevel(), PromotionLevelEnum.SKU.getLevel());
assertEquals(promotion01.getOriginalPrice(), 200);
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,10 +179,10 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion02.getName(), "活动 2000 号");
assertEquals(promotion02.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType());
assertEquals(promotion02.getLevel(), PromotionLevelEnum.SKU.getLevel());
assertEquals(promotion02.getOriginalPrice(), 150);
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);
@ -225,8 +223,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
// 断言 Order 部分
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
assertEquals(order.getOriginalPrice(), 470);
assertEquals(order.getOrderPrice(), 470);
assertEquals(order.getTotalPrice(), 470);
assertEquals(order.getDiscountPrice(), 130);
assertEquals(order.getPointPrice(), 0);
assertEquals(order.getDeliveryPrice(), 0);
@ -268,10 +265,10 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getName(), "活动 1000 号");
assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
assertEquals(promotion01.getLevel(), PromotionLevelEnum.ORDER.getLevel());
assertEquals(promotion01.getOriginalPrice(), 350);
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);
@ -287,10 +284,10 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion02.getName(), "活动 2000 号");
assertEquals(promotion02.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
assertEquals(promotion02.getLevel(), PromotionLevelEnum.ORDER.getLevel());
assertEquals(promotion02.getOriginalPrice(), 120);
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);
@ -323,8 +320,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
// 断言 Order 部分
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
assertEquals(order.getOriginalPrice(), 350);
assertEquals(order.getOrderPrice(), 350);
assertEquals(order.getTotalPrice(), 350);
assertEquals(order.getDiscountPrice(), 0);
assertEquals(order.getPointPrice(), 0);
assertEquals(order.getDeliveryPrice(), 0);
@ -357,10 +353,10 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getName(), "活动 1000 号");
assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType());
assertEquals(promotion01.getLevel(), PromotionLevelEnum.ORDER.getLevel());
assertEquals(promotion01.getOriginalPrice(), 350);
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);
@ -396,8 +392,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
// 断言 Order 部分
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
assertEquals(order.getOriginalPrice(), 470);
assertEquals(order.getOrderPrice(), 470);
assertEquals(order.getTotalPrice(), 470);
assertEquals(order.getDiscountPrice(), 0);
assertEquals(order.getPointPrice(), 0);
assertEquals(order.getDeliveryPrice(), 0);
@ -440,10 +435,10 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
assertEquals(promotion01.getName(), "程序员节");
assertEquals(promotion01.getType(), PromotionTypeEnum.COUPON.getType());
assertEquals(promotion01.getLevel(), PromotionLevelEnum.COUPON.getLevel());
assertEquals(promotion01.getOriginalPrice(), 350);
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

@ -1,31 +1,29 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.*;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.*;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressUpdateReqVO;
import cn.iocoder.yudao.module.trade.convert.delivery.DeliveryExpressConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
@Tag(name = "管理后台 - 快递公司")
@RestController
@ -77,6 +75,7 @@ public class DeliveryExpressController {
return success(DeliveryExpressConvert.INSTANCE.convertPage(pageResult));
}
// TODO @jason运费模版@芋艿 你的意思是运费模板导出没有必要吧已经去掉了这个是快递公司导出
@GetMapping("/export-excel")
@Operation(summary = "导出快递公司 Excel")
@PreAuthorize("@ss.hasPermission('trade:delivery:express:export')")

View File

@ -1,31 +1,25 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.*;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.*;
import cn.iocoder.yudao.module.trade.convert.delivery.DeliveryExpressTemplateConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressTemplateService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
@Tag(name = "管理后台 - 快递运费模板")
@RestController
@ -85,16 +79,4 @@ public class DeliveryExpressTemplateController {
return success(DeliveryExpressTemplateConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@Operation(summary = "导出快递运费模板 Excel")
@PreAuthorize("@ss.hasPermission('trade:delivery:express-template:export')")
@OperateLog(type = EXPORT)
public void exportDeliveryExpressTemplateExcel(@Valid DeliveryExpressTemplateExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<DeliveryExpressTemplateDO> list = deliveryExpressTemplateService.getDeliveryExpressTemplateList(exportReqVO);
// 导出 Excel
List<DeliveryExpressTemplateExcelVO> datas = DeliveryExpressTemplateConvert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "快递运费模板.xls", "数据", DeliveryExpressTemplateExcelVO.class, datas);
}
}

View File

@ -1,31 +0,0 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 快递运费模板 Excel VO
*
* @author jason
*/
@Data
public class DeliveryExpressTemplateExcelVO {
@ExcelProperty("编号,自增")
private Long id;
@ExcelProperty("模板名称")
private String name;
@ExcelProperty("配送计费方式 1:按件 2:按重量 3:按体积")
private Integer chargeMode;
@ExcelProperty("排序")
private Integer sort;
@ExcelProperty("创建时间")
private LocalDateTime createTime;
}

View File

@ -1,26 +0,0 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 快递运费模板 Excel 导出 Request VO参数和 DeliveryExpressTemplatePageReqVO 是一致的")
@Data
public class DeliveryExpressTemplateExportReqVO {
@Schema(description = "模板名称", example = "王五")
private String name;
@Schema(description = "配送计费方式 1:按件 2:按重量 3:按体积")
private Integer chargeMode;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}

View File

@ -1,11 +1,9 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDateTime;
import javax.validation.constraints.*;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 快递公司 Base VO提供给添加修改详细的子 VO 使用
@ -14,8 +12,8 @@ import javax.validation.constraints.*;
@Data
public class DeliveryExpressBaseVO {
@Schema(description = "快递公司编", required = true)
@NotNull(message = "快递公司编不能为空")
@Schema(description = "快递公司编", required = true)
@NotNull(message = "快递公司编不能为空")
private String code;
@Schema(description = "快递公司名称", required = true, example = "李四")

View File

@ -1,9 +1,7 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.*;
@Schema(description = "管理后台 - 快递公司创建 Request VO")
@Data

View File

@ -1,5 +1,8 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
import cn.iocoder.yudao.module.system.enums.DictTypeConstants;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@ -7,8 +10,6 @@ import java.time.LocalDateTime;
/**
* 快递公司 Excel VO
*
* @author jason
*/
@Data
public class DeliveryExpressExcelVO {
@ -16,20 +17,21 @@ public class DeliveryExpressExcelVO {
@ExcelProperty("编号")
private Long id;
@ExcelProperty("快递公司编")
@ExcelProperty("快递公司编")
private String code;
@ExcelProperty("快递公司名称")
private String name;
@ExcelProperty("快递公司logo")
@ExcelProperty("快递公司 logo")
private String logo;
@ExcelProperty("排序")
private Integer sort;
@ExcelProperty("状态0正常 1停用")
private Byte status;
@ExcelProperty(value = "状态", converter = DictConvert.class)
@DictFormat(DictTypeConstants.COMMON_STATUS)
private Integer status;
@ExcelProperty("创建时间")
private LocalDateTime createTime;

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@ -12,7 +12,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
@Data
public class DeliveryExpressExportReqVO {
@Schema(description = "快递公司编")
@Schema(description = "快递公司编")
private String code;
@Schema(description = "快递公司名称", example = "李四")

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express;
import lombok.*;
import java.util.*;
@ -15,7 +15,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
@ToString(callSuper = true)
public class DeliveryExpressPageReqVO extends PageParam {
@Schema(description = "快递公司编")
@Schema(description = "快递公司编")
private String code;
@Schema(description = "快递公司名称", example = "李四")

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

View File

@ -1,9 +1,11 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import javax.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotNull;
@Schema(description = "管理后台 - 快递公司更新 Request VO")
@Data

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

View File

@ -1,10 +1,13 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
@Schema(description = "管理后台 - 快递运费模板创建 Request VO")
@Data
@ -12,6 +15,8 @@ import javax.validation.Valid;
@ToString(callSuper = true)
public class DeliveryExpressTemplateCreateReqVO extends DeliveryExpressTemplateBaseVO {
// TODO @jason不用给默认值哈
@Schema(description = "区域运费列表")
@Valid
private List<ExpressTemplateChargeBaseVO> templateCharge = Collections.emptyList();

View File

@ -1,10 +1,12 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

View File

@ -1,9 +1,10 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
// TODO @jasonsimplae 是不是不用继承 DeliveryExpressTemplateBaseVO直接 id name 属性就够了
@Schema(description = "管理后台 - 快递运费模板 精简 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)

View File

@ -1,10 +1,14 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.Valid;
import javax.validation.constraints.*;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
@Schema(description = "管理后台 - 快递运费模板更新 Request VO")
@Data
@ -16,6 +20,8 @@ public class DeliveryExpressTemplateUpdateReqVO extends DeliveryExpressTemplateB
@NotNull(message = "编号不能为空")
private Long id;
// TODO @jasonpojo 不给默认值哈
@Schema(description = "区域运费列表")
@Valid
private List<ExpressTemplateChargeUpdateVO> templateCharge = Collections.emptyList();

View File

@ -1,21 +1,21 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* 快递运费模板运费设置 Base VO提供给添加运费模板使用
*
* @author jason
*/
@Data
public class ExpressTemplateChargeBaseVO {
@Schema(description = "区域编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "区域编号不能为空")
private Integer areaId;
@Schema(description = "区域编号列表", requiredMode = Schema.RequiredMode.REQUIRED, example = "[1,120000]")
@NotEmpty(message = "区域编号列表不能为空")
private List<Integer> areaIds;
@Schema(description = "首件数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "5")
@NotNull(message = "首件数量不能为空")

View File

@ -1,20 +1,22 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* @author jason
*/
// TODO @jason这个 vo 可以内嵌到 DeliveryExpressTemplateUpdateReqVO避免 vo 过多不好分辨
@Schema(description = "管理后台 - 快递公司创建 Request VO")
@Data
public class ExpressTemplateChargeUpdateVO extends ExpressTemplateChargeBaseVO {
@Schema(description = "编号", example = "6592")
private Long id;
// TODO @jason这几个字段应该不通过前端传递而是后端查询后去赋值的
@Schema(description = "配送模板编号", example = "1")
private Long templateId;
@Schema(description = "配送计费方式", example = "1")
private Integer chargeMode;
}

View File

@ -1,21 +1,21 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* 快递运费模板包邮 Base VO提供给添加运费模板使用
*
* @author jason
*/
@Data
public class ExpressTemplateFreeBaseVO {
@Schema(description = "区域编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "区域编号不能为空")
private Integer areaId;
@Schema(description = "区域编号列表", requiredMode = Schema.RequiredMode.REQUIRED, example = "[1,120000]")
@NotEmpty(message = "区域编号列表不能为空")
private List<Integer> areaIds;
@Schema(description = "包邮金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "5000")
@NotNull(message = "包邮金额不能为空")
@ -24,4 +24,5 @@ public class ExpressTemplateFreeBaseVO {
@Schema(description = "包邮件数", requiredMode = Schema.RequiredMode.REQUIRED, example = "5")
@NotNull(message = "包邮件数不能为空")
private Integer freeCount;
}

View File

@ -1,12 +1,12 @@
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo;
package cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
// TODO @jason这个 vo 可以内嵌到 DeliveryExpressTemplateUpdateReqVO避免 vo 过多不好分辨
// TODO @jasonswagger 缺失
/**
* 快递运费模板包邮 更新 VO
*
* @author jason
*/
@Data
public class ExpressTemplateFreeUpdateVO extends ExpressTemplateFreeBaseVO {

View File

@ -73,9 +73,6 @@ public class TradeOrderBaseVO {
@Schema(description = "商品原价(总)", required = true, example = "1000")
private Integer originalPrice;
@Schema(description = "订单原价(总)", required = true, example = "1000")
private Integer orderPrice;
@Schema(description = "订单优惠(总)", required = true, example = "100")
private Integer discountPrice;

View File

@ -59,10 +59,7 @@ public class AppTradeOrderDetailRespVO {
private String payChannelCode;
@Schema(description = "商品原价(总)", required = true, example = "1000")
private Integer originalPrice;
@Schema(description = "订单原价(总)", required = true, example = "1000")
private Integer orderPrice;
private Integer totalPrice;
@Schema(description = "订单优惠(总)", required = true, example = "100")
private Integer discountPrice;

View File

@ -4,10 +4,10 @@ import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressExcelVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressUpdateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressExcelVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressUpdateReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@ -16,7 +16,7 @@ import org.mapstruct.factory.Mappers;
/**
* 快递公司 Convert
*
* @author 芋道源码
* @author jason
*/
@Mapper
public interface DeliveryExpressConvert {

View File

@ -1,27 +1,28 @@
package cn.iocoder.yudao.module.trade.convert.delivery;
import java.util.*;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.*;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateChargeDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateFreeDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 快递运费模板 Convert
*
* @author 芋道源码
* @author jason
*/
@Mapper
public interface DeliveryExpressTemplateConvert {
DeliveryExpressTemplateConvert INSTANCE = Mappers.getMapper(DeliveryExpressTemplateConvert.class);
// ========== Template ==========
DeliveryExpressTemplateDO convert(DeliveryExpressTemplateCreateReqVO bean);
DeliveryExpressTemplateDO convert(DeliveryExpressTemplateUpdateReqVO bean);
@ -34,12 +35,27 @@ public interface DeliveryExpressTemplateConvert {
PageResult<DeliveryExpressTemplateSimpleRespVO> convertPage(PageResult<DeliveryExpressTemplateDO> page);
List<DeliveryExpressTemplateExcelVO> convertList02(List<DeliveryExpressTemplateDO> list);
default DeliveryExpressTemplateRespVO convert(DeliveryExpressTemplateDO bean,
List<DeliveryExpressTemplateChargeDO> chargeList,
List<DeliveryExpressTemplateFreeDO> freeList){
DeliveryExpressTemplateRespVO respVO = convert2(bean);
respVO.setTemplateCharge(convertTemplateChargeList(chargeList));
respVO.setTemplateFree(convertTemplateFreeList(freeList));
return respVO;
}
// ========== Template Charge ==========
DeliveryExpressTemplateChargeDO convertTemplateCharge(Long templateId, Integer chargeMode, ExpressTemplateChargeBaseVO vo);
DeliveryExpressTemplateChargeDO convertTemplateCharge(ExpressTemplateChargeUpdateVO vo);
default List<DeliveryExpressTemplateChargeDO> convertTemplateChargeList(Long templateId, Integer chargeMode, List<ExpressTemplateChargeBaseVO> list) {
return CollectionUtils.convertList(list, vo -> convertTemplateCharge(templateId, chargeMode, vo));
}
// ========== Template Free ==========
DeliveryExpressTemplateFreeDO convertTemplateFree(Long templateId, ExpressTemplateFreeBaseVO vo);
DeliveryExpressTemplateFreeDO convertTemplateFree(ExpressTemplateFreeUpdateVO vo);
@ -48,36 +64,8 @@ public interface DeliveryExpressTemplateConvert {
List<ExpressTemplateFreeBaseVO> convertTemplateFreeList(List<DeliveryExpressTemplateFreeDO> list);
default List<DeliveryExpressTemplateChargeDO> convertTemplateChargeList(Long templateId, Integer chargeMode, List<ExpressTemplateChargeBaseVO> list){
if(CollUtil.isEmpty(list)){
return Collections.emptyList();
}
List<DeliveryExpressTemplateChargeDO> templateChargeList = new ArrayList<>( list.size() );
for (ExpressTemplateChargeBaseVO item : list) {
templateChargeList.add(convertTemplateCharge(templateId, chargeMode, item));
}
return templateChargeList;
}
default List<DeliveryExpressTemplateFreeDO> convertTemplateFreeList(Long templateId, List<ExpressTemplateFreeBaseVO> list) {
if (CollUtil.isEmpty(list)) {
return Collections.emptyList();
}
List<DeliveryExpressTemplateFreeDO> templateFreeList = new ArrayList<>(list.size());
for (ExpressTemplateFreeBaseVO item : list) {
templateFreeList.add(convertTemplateFree(templateId, item));
}
return templateFreeList;
return CollectionUtils.convertList(list, vo -> convertTemplateFree(templateId, vo));
}
default DeliveryExpressTemplateRespVO convert(DeliveryExpressTemplateDO bean,
List<DeliveryExpressTemplateChargeDO> chargeList,
List<DeliveryExpressTemplateFreeDO> freeList){
DeliveryExpressTemplateRespVO respVO = convert2(bean);
respVO.setTemplateCharge(convertTemplateChargeList(chargeList));
respVO.setTemplateFree(convertTemplateFreeList(freeList));
return respVO;
}
}

View File

@ -1,17 +1,21 @@
package cn.iocoder.yudao.module.trade.dal.dataobject.delivery;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.type.IntegerListTypeHandler;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.List;
/**
* 快递运费模板计费配置 DO
*
* @author jason
*/
@TableName(value ="trade_delivery_express_template_charge")
@TableName(value ="trade_delivery_express_template_charge", autoResultMap = true)
@KeySequence("trade_delivery_express_template_charge_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
public class DeliveryExpressTemplateChargeDO extends BaseDO {
@ -30,9 +34,10 @@ public class DeliveryExpressTemplateChargeDO extends BaseDO {
private Long templateId;
/**
* 配送区域
* 配送区域编号列表
*/
private Integer areaId;
@TableField(typeHandler = IntegerListTypeHandler.class)
private List<Integer> areaIds;
/**
* 配送计费方式

View File

@ -1,17 +1,21 @@
package cn.iocoder.yudao.module.trade.dal.dataobject.delivery;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.type.IntegerListTypeHandler;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.List;
/**
* 快递运费模板包邮配置 DO
*
* @author jason
*/
@TableName(value ="trade_delivery_express_template_free")
@TableName(value ="trade_delivery_express_template_free", autoResultMap = true)
@KeySequence("trade_delivery_express_template_free_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
public class DeliveryExpressTemplateFreeDO extends BaseDO {
@ -29,10 +33,12 @@ public class DeliveryExpressTemplateFreeDO extends BaseDO {
*/
private Long templateId;
/**
* 包邮区域id
* 配送区域编号列表
*/
private Integer areaId;
@TableField(typeHandler = IntegerListTypeHandler.class)
private List<Integer> areaIds;
/**
* 包邮金额单位

View File

@ -2,13 +2,8 @@ package cn.iocoder.yudao.module.trade.dal.dataobject.order;
import cn.iocoder.yudao.framework.common.enums.TerminalEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateRespDTO.OrderItem;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderCancelTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderRefundStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderDeliveryStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.*;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
@ -134,25 +129,15 @@ public class TradeOrderDO extends BaseDO {
private String payChannelCode;
/**
* 商品原价单位
* 商品原价单位
*
* totalPrice = {@link TradeOrderItemDO#getPrice()} * {@link TradeOrderItemDO#getCount()} 求和
*
* 对应 taobao trade.total_fee 字段
*/
private Integer totalPrice;
// TODO 芋艿是不是要删除这个字段
/**
* 订单原价单位
*
* 1. orderPrice = {@link OrderItem#getPayPrice()} 求和
* 2. orderPrice = {@link #totalPrice} - 商品级优惠
*/
private Integer orderPrice;
/**
* 订单优惠单位
*
* 订单级优惠对主订单的优惠常见如订单满 200 元减 10 订单满 80 包邮
* 优惠金额单位
*
* 对应 taobao order.discount_fee 字段
*/
@ -162,7 +147,7 @@ public class TradeOrderDO extends BaseDO {
*/
private Integer deliveryPrice;
/**
* 订单调价单位
* 订单调价单位
*
* 正数加价负数减价
*/
@ -170,11 +155,11 @@ public class TradeOrderDO extends BaseDO {
/**
* 应付金额单位
*
* = {@link OrderItem#getPayPrice()} 求和
* = {@link #totalPrice}
* - {@link #couponPrice}
* - {@link #pointPrice}
* + {@link #deliveryPrice}
* - {@link #discountPrice}
* + {@link #deliveryPrice}
* + {@link #adjustPrice}
*/
private Integer payPrice;

View File

@ -102,44 +102,47 @@ public class TradeOrderItemDO extends BaseDO {
*/
private Integer price;
/**
* 商品优惠单位
*
* 商品级优惠对单个商品的常见如商品原价的 8 商品原价的减 50
* 优惠金额单位
*
* 对应 taobao order.discount_fee 字段
*/
private Integer discountPrice;
/**
* 子订单实付金额不算主订单分摊金额单位
* 运费金额单位
*/
private Integer deliveryPrice;
/**
* 订单调价单位
*
* 正数加价负数减价
*/
private Integer adjustPrice;
/**
* 应付金额单位
*
* = {@link #price} * {@link #count}
* - {@link #couponPrice}
* - {@link #pointPrice}
* - {@link #discountPrice}
*
* 对应 taobao order.payment 字段
* + {@link #deliveryPrice}
* + {@link #adjustPrice}
*/
private Integer payPrice;
/**
* 子订单分摊金额单位
* 需要分摊 {@link TradeOrderDO#getDiscountPrice()}{@link TradeOrderDO#getCouponPrice()}{@link TradeOrderDO#getPointPrice()}
*
* 对应 taobao order.part_mjz_discount 字段
* 淘宝说明子订单分摊优惠基础逻辑一般正常优惠券和满减优惠按照子订单的金额进行分摊特殊情况如果优惠券是指定商品使用的只会分摊到对应商品子订单上不分摊
*/
private Integer orderPartPrice;
/**
* 分摊后子订单实付金额单位
*
* = {@link #payPrice}
* - {@link #orderPartPrice}
*
* 对应 taobao divide_order_fee 字段
*/
private Integer orderDividePrice;
// ========== 营销基本信息 ==========
// TODO 芋艿在捉摸一下
/**
* 优惠劵减免金额单位
*
* 对应 taobao trade.coupon_fee 字段
*/
private Integer couponPrice;
/**
* 积分抵扣的金额单位
*
* 对应 taobao trade.point_fee 字段
*/
private Integer pointPrice;
// ========== 售后基本信息 ==========
/**

View File

@ -3,8 +3,8 @@ package cn.iocoder.yudao.module.trade.dal.mysql.delivery;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressExportReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressExportReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;

View File

@ -14,10 +14,6 @@ import java.util.List;
@Mapper
public interface DeliveryExpressTemplateChargeMapper extends BaseMapperX<DeliveryExpressTemplateChargeDO> {
@Repository
class BatchInsertMapper extends ServiceImpl<DeliveryExpressTemplateChargeMapper, DeliveryExpressTemplateChargeDO> {
}
default List<DeliveryExpressTemplateChargeDO> selectListByTemplateId(Long templateId){
return selectList(new LambdaQueryWrapper<DeliveryExpressTemplateChargeDO>()
.eq(DeliveryExpressTemplateChargeDO::getTemplateId, templateId));

View File

@ -14,10 +14,6 @@ import java.util.List;
@Mapper
public interface DeliveryExpressTemplateFreeMapper extends BaseMapperX<DeliveryExpressTemplateFreeDO> {
@Repository
class BatchInsertMapper extends ServiceImpl<DeliveryExpressTemplateFreeMapper, DeliveryExpressTemplateFreeDO> {
}
default List<DeliveryExpressTemplateFreeDO> selectListByTemplateId(Long templateId) {
return selectList(new LambdaQueryWrapper<DeliveryExpressTemplateFreeDO>()
.eq(DeliveryExpressTemplateFreeDO::getTemplateId, templateId));

View File

@ -3,17 +3,11 @@ package cn.iocoder.yudao.module.trade.dal.mysql.delivery;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressTemplateExportReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressTemplatePageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplatePageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeliveryExpressTemplateMapper extends BaseMapperX<DeliveryExpressTemplateDO> {
@ -25,16 +19,8 @@ public interface DeliveryExpressTemplateMapper extends BaseMapperX<DeliveryExpre
.orderByAsc(DeliveryExpressTemplateDO::getSort));
}
default List<DeliveryExpressTemplateDO> selectList(DeliveryExpressTemplateExportReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<DeliveryExpressTemplateDO>()
.likeIfPresent(DeliveryExpressTemplateDO::getName, reqVO.getName())
.eqIfPresent(DeliveryExpressTemplateDO::getChargeMode, reqVO.getChargeMode())
.betweenIfPresent(DeliveryExpressTemplateDO::getCreateTime, reqVO.getCreateTime())
.orderByAsc(DeliveryExpressTemplateDO::getSort));
default DeliveryExpressTemplateDO selectByName(String name) {
return selectOne(DeliveryExpressTemplateDO::getName,name);
}
default DeliveryExpressTemplateDO selectByName(String name) {
return selectOne(new LambdaQueryWrapper<DeliveryExpressTemplateDO>()
.eq(DeliveryExpressTemplateDO::getName, name));
}
}

View File

@ -97,7 +97,7 @@ public class TradeAfterSaleServiceImpl implements TradeAfterSaleService {
}
// 申请的退款金额不能超过商品的价格
if (createReqVO.getRefundPrice() > orderItem.getOrderDividePrice()) {
if (createReqVO.getRefundPrice() > orderItem.getPayPrice()) {
throw exception(AFTER_SALE_CREATE_FAIL_REFUND_PRICE_ERROR);
}

View File

@ -4,10 +4,10 @@ import java.util.*;
import javax.validation.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressExportReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressUpdateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressExportReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressUpdateReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
/**

View File

@ -1,10 +1,10 @@
package cn.iocoder.yudao.module.trade.service.delivery;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressExportReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.DeliveryExpressUpdateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressExportReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.express.DeliveryExpressUpdateReqVO;
import cn.iocoder.yudao.module.trade.convert.delivery.DeliveryExpressConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.iocoder.yudao.module.trade.dal.mysql.delivery.DeliveryExpressMapper;

View File

@ -1,11 +1,16 @@
package cn.iocoder.yudao.module.trade.service.delivery;
import java.util.*;
import javax.validation.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.*;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplateCreateReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplatePageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplateRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplateUpdateReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
/**
* 快递运费模板 Service 接口
*
@ -58,13 +63,4 @@ public interface DeliveryExpressTemplateService {
* @return 快递运费模板分页
*/
PageResult<DeliveryExpressTemplateDO> getDeliveryExpressTemplatePage(DeliveryExpressTemplatePageReqVO pageReqVO);
/**
* 获得快递运费模板列表, 用于 Excel 导出
*
* @param exportReqVO 查询条件
* @return 快递运费模板列表
*/
List<DeliveryExpressTemplateDO> getDeliveryExpressTemplateList(DeliveryExpressTemplateExportReqVO exportReqVO);
}

View File

@ -2,8 +2,7 @@ package cn.iocoder.yudao.module.trade.service.delivery;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.*;
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateChargeDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateFreeDO;
@ -18,8 +17,10 @@ import javax.annotation.Resource;
import java.util.*;
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.module.trade.convert.delivery.DeliveryExpressTemplateConvert.INSTANCE;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_TEMPLATE_NAME_DUPLICATE;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_TEMPLATE_NOT_EXISTS;
/**
* 快递运费模板 Service 实现类
@ -36,28 +37,25 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
private DeliveryExpressTemplateChargeMapper expressTemplateChargeMapper;
@Resource
private DeliveryExpressTemplateFreeMapper expressTemplateFreeMapper;
@Resource
private DeliveryExpressTemplateChargeMapper.BatchInsertMapper expressTemplateChargeBatchMapper;
@Resource
private DeliveryExpressTemplateFreeMapper.BatchInsertMapper expressTemplateFreeBatchMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public Long createDeliveryExpressTemplate(DeliveryExpressTemplateCreateReqVO createReqVO) {
//校验模板名是否唯一
// 校验模板名是否唯一
validateTemplateNameUnique(createReqVO.getName(), null);
// 插入
DeliveryExpressTemplateDO deliveryExpressTemplate = INSTANCE.convert(createReqVO);
expressTemplateMapper.insert(deliveryExpressTemplate);
//插入运费模板计费表
if(CollUtil.isNotEmpty(createReqVO.getTemplateCharge())) {
expressTemplateChargeBatchMapper.saveBatch(
// 插入运费模板计费表
if (CollUtil.isNotEmpty(createReqVO.getTemplateCharge())) {
expressTemplateChargeMapper.insertBatch(
INSTANCE.convertTemplateChargeList(deliveryExpressTemplate.getId(), createReqVO.getChargeMode(), createReqVO.getTemplateCharge())
);
}
//插入运费模板包邮表
if(CollUtil.isNotEmpty(createReqVO.getTemplateFree())) {
expressTemplateFreeBatchMapper.saveBatch(
// 插入运费模板包邮表
if (CollUtil.isNotEmpty(createReqVO.getTemplateFree())) {
expressTemplateFreeMapper.insertBatch(
INSTANCE.convertTemplateFreeList(deliveryExpressTemplate.getId(), createReqVO.getTemplateFree())
);
}
@ -69,80 +67,78 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
public void updateDeliveryExpressTemplate(DeliveryExpressTemplateUpdateReqVO updateReqVO) {
// 校验存在
validateDeliveryExpressTemplateExists(updateReqVO.getId());
//校验模板名是否唯一
// 校验模板名是否唯一
validateTemplateNameUnique(updateReqVO.getName(), updateReqVO.getId());
//更新运费从表
// 更新运费从表
updateExpressTemplateCharge(updateReqVO);
//更新包邮从表
// 更新包邮从表
updateExpressTemplateFree(updateReqVO);
//更新模板主表
// 更新模板主表
DeliveryExpressTemplateDO updateObj = INSTANCE.convert(updateReqVO);
expressTemplateMapper.updateById(updateObj);
}
private void updateExpressTemplateFree(DeliveryExpressTemplateUpdateReqVO updateReqVO) {
// 1.1 获得新增/修改的区域列表
List<DeliveryExpressTemplateFreeDO> oldFreeList = expressTemplateFreeMapper.selectListByTemplateId(updateReqVO.getId());
List<ExpressTemplateFreeUpdateVO> newFreeList = updateReqVO.getTemplateFree();
//新增包邮区域列表
List<DeliveryExpressTemplateFreeDO> addFreeList = new ArrayList<>(newFreeList.size());
//更新包邮区域列表
List<DeliveryExpressTemplateFreeDO> updateFreeList = new ArrayList<>(newFreeList.size());
List<DeliveryExpressTemplateFreeDO> addFreeList = new ArrayList<>(newFreeList.size()); // 新增包邮区域列表
List<DeliveryExpressTemplateFreeDO> updateFreeList = new ArrayList<>(newFreeList.size()); // 更新包邮区域列表
for (ExpressTemplateFreeUpdateVO item : newFreeList) {
if (Objects.nonNull(item.getId())) {
updateFreeList.add(INSTANCE.convertTemplateFree(item));
}else{
} else {
item.setTemplateId(updateReqVO.getId());
addFreeList.add(INSTANCE.convertTemplateFree(item));
}
}
//删除的包邮区域id
Set<Long> deleteFreeIds = CollectionUtils.convertSet(oldFreeList, DeliveryExpressTemplateFreeDO::getId);
deleteFreeIds.removeAll(CollectionUtils.convertSet(updateFreeList, DeliveryExpressTemplateFreeDO::getId));
//新增
// 1.2 新增
if (CollUtil.isNotEmpty(addFreeList)) {
expressTemplateFreeBatchMapper.saveBatch(addFreeList);
expressTemplateFreeMapper.insertBatch(addFreeList);
}
//修改
// 1.3 修改
if (CollUtil.isNotEmpty(updateFreeList)) {
expressTemplateFreeBatchMapper.saveOrUpdateBatch(updateFreeList);
expressTemplateFreeMapper.updateBatch(updateFreeList);
}
//删除
// 2. 删除
Set<Long> deleteFreeIds = convertSet(oldFreeList, DeliveryExpressTemplateFreeDO::getId);
deleteFreeIds.removeAll(convertSet(updateFreeList, DeliveryExpressTemplateFreeDO::getId));
if (CollUtil.isNotEmpty(deleteFreeIds)) {
expressTemplateFreeMapper.deleteBatchIds(deleteFreeIds);
}
}
private void updateExpressTemplateCharge(DeliveryExpressTemplateUpdateReqVO updateReqVO) {
// 1.1 获得新增/修改的区域列表
List<DeliveryExpressTemplateChargeDO> oldChargeList = expressTemplateChargeMapper.selectListByTemplateId(updateReqVO.getId());
List<ExpressTemplateChargeUpdateVO> newChargeList = updateReqVO.getTemplateCharge();
//新增运费区域列表
List<DeliveryExpressTemplateChargeDO> addList = new ArrayList<>(newChargeList.size());
//更新运费区域列表
List<DeliveryExpressTemplateChargeDO> updateList = new ArrayList<>(newChargeList.size());
List<DeliveryExpressTemplateChargeDO> addList = new ArrayList<>(newChargeList.size()); // 新增运费区域列表
List<DeliveryExpressTemplateChargeDO> updateList = new ArrayList<>(newChargeList.size()); // 更新运费区域列表
for (ExpressTemplateChargeUpdateVO item : newChargeList) {
if (Objects.nonNull(item.getId())) {
//计费模式以主表为准
if (item.getId() != null) {
// 计费模式以主表为准
item.setChargeMode(updateReqVO.getChargeMode());
updateList.add(INSTANCE.convertTemplateCharge(item));
}else{
} else {
item.setTemplateId(updateReqVO.getId());
item.setChargeMode(updateReqVO.getChargeMode());
addList.add(INSTANCE.convertTemplateCharge(item));
}
}
//删除的运费区域id
Set<Long> deleteChargeIds = CollectionUtils.convertSet(oldChargeList, DeliveryExpressTemplateChargeDO::getId);
deleteChargeIds.removeAll(CollectionUtils.convertSet(updateList, DeliveryExpressTemplateChargeDO::getId));
//新增
// 1.2 新增
if (CollUtil.isNotEmpty(addList)) {
expressTemplateChargeBatchMapper.saveBatch(addList);
expressTemplateChargeMapper.insertBatch(addList);
}
//修改
// 1.3 修改
if (CollUtil.isNotEmpty(updateList)) {
expressTemplateChargeBatchMapper.saveOrUpdateBatch(updateList);
expressTemplateChargeMapper.updateBatch(updateList);
}
//删除
// 2. 删除
Set<Long> deleteChargeIds = convertSet(oldChargeList, DeliveryExpressTemplateChargeDO::getId);
deleteChargeIds.removeAll(convertSet(updateList, DeliveryExpressTemplateChargeDO::getId));
if (CollUtil.isNotEmpty(deleteChargeIds)) {
expressTemplateChargeMapper.deleteBatchIds(deleteChargeIds);
}
@ -153,6 +149,7 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
public void deleteDeliveryExpressTemplate(Long id) {
// 校验存在
validateDeliveryExpressTemplateExists(id);
// 删除主表
expressTemplateMapper.deleteById(id);
// 删除运费从表
@ -163,8 +160,9 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
/**
* 校验运费模板名是否唯一
*
* @param name 模板名称
* @param id 运费模板编号, 可以为null
* @param id 运费模板编号,可以为 null
*/
private void validateTemplateNameUnique(String name, Long id) {
DeliveryExpressTemplateDO template = expressTemplateMapper.selectByName(name);
@ -191,7 +189,7 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
List<DeliveryExpressTemplateChargeDO> chargeList = expressTemplateChargeMapper.selectListByTemplateId(id);
List<DeliveryExpressTemplateFreeDO> freeList = expressTemplateFreeMapper.selectListByTemplateId(id);
DeliveryExpressTemplateDO template = expressTemplateMapper.selectById(id);
return INSTANCE.convert(template, chargeList,freeList);
return INSTANCE.convert(template, chargeList, freeList);
}
@Override
@ -204,9 +202,4 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
return expressTemplateMapper.selectPage(pageReqVO);
}
@Override
public List<DeliveryExpressTemplateDO> getDeliveryExpressTemplateList(DeliveryExpressTemplateExportReqVO exportReqVO) {
return expressTemplateMapper.selectList(exportReqVO);
}
}

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

@ -124,7 +124,7 @@ public class TradeOrderServiceTest extends BaseDbUnitTest {
.setSpuId(21L).setSkuId(2L).setCount(4).setOriginalPrice(80).setOriginalUnitPrice(20)
.setDiscountPrice(40).setPayPrice(40).setOrderPartPrice(15).setOrderDividePrice(25);
PriceCalculateRespDTO.Order priceOrder = new PriceCalculateRespDTO.Order()
.setOriginalPrice(230).setOrderPrice(100).setDiscountPrice(0).setCouponPrice(30)
.setTotalPrice(230).setDiscountPrice(0).setCouponPrice(30)
.setPointPrice(10).setDeliveryPrice(20).setPayPrice(80).setCouponId(101L).setCouponPrice(30)
.setItems(Arrays.asList(priceOrderItem01, priceOrderItem02));
when(priceApi.calculatePrice(argThat(priceCalculateReqDTO -> {
@ -170,7 +170,6 @@ public class TradeOrderServiceTest extends BaseDbUnitTest {
assertFalse(tradeOrderDO.getPayed());
assertNull(tradeOrderDO.getPayTime());
assertEquals(tradeOrderDO.getTotalPrice(), 230);
assertEquals(tradeOrderDO.getOrderPrice(), 100);
assertEquals(tradeOrderDO.getDiscountPrice(), 0);
assertEquals(tradeOrderDO.getAdjustPrice(), 0);
assertEquals(tradeOrderDO.getPayPrice(), 80);
@ -208,8 +207,6 @@ public class TradeOrderServiceTest extends BaseDbUnitTest {
assertEquals(tradeOrderItemDO01.getPrice(), 50);
assertEquals(tradeOrderItemDO01.getDiscountPrice(), 20);
assertEquals(tradeOrderItemDO01.getPayPrice(), 130);
assertEquals(tradeOrderItemDO01.getOrderPartPrice(), 7);
assertEquals(tradeOrderItemDO01.getOrderDividePrice(), 35);
assertEquals(tradeOrderItemDO01.getAfterSaleStatus(), TradeOrderItemAfterSaleStatusEnum.NONE.getStatus());
// 断言 TradeOrderItemDO 订单 2
TradeOrderItemDO tradeOrderItemDO02 = tradeOrderItemDOs.get(1);
@ -228,8 +225,6 @@ public class TradeOrderServiceTest extends BaseDbUnitTest {
assertEquals(tradeOrderItemDO02.getPrice(), 20);
assertEquals(tradeOrderItemDO02.getDiscountPrice(), 40);
assertEquals(tradeOrderItemDO02.getPayPrice(), 40);
assertEquals(tradeOrderItemDO02.getOrderPartPrice(), 15);
assertEquals(tradeOrderItemDO02.getOrderDividePrice(), 25);
assertEquals(tradeOrderItemDO02.getAfterSaleStatus(), TradeOrderItemAfterSaleStatusEnum.NONE.getStatus());
// 校验调用
verify(productSkuApi).updateSkuStock(argThat(updateStockReqDTO -> {

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();

View File

@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.ip.core.utils.IPUtils;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.LazyAreaNodeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeSimpleRespVO;
import cn.iocoder.yudao.module.system.convert.ip.AreaConvert;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@ -15,7 +15,6 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
@ -35,19 +34,23 @@ public class AreaController {
return success(AreaConvert.INSTANCE.convertList(area.getChildren()));
}
@GetMapping("/getChildrenArea")
@GetMapping("/get-children")
@Operation(summary = "获得地区的下级区域")
public CommonResult<List<LazyAreaNodeRespVO>> getChildrenArea(Integer id) {
@Parameter(name = "id", description = "区域编号", required = true, example = "150000")
public CommonResult<List<AreaNodeSimpleRespVO>> getChildren(@RequestParam("id") Integer id) {
Area area = AreaUtils.getArea(id);
Assert.notNull(area, String.format("获取不到 id : %d的区域", id));
Assert.notNull(area, String.format("获取不到 id : %d 的区域", id));
return success(AreaConvert.INSTANCE.convertList2(area.getChildren()));
}
@PostMapping("/list")
@Operation(summary = "通过区域ids获得地区列表")
public CommonResult<List<LazyAreaNodeRespVO>> list(@RequestBody Set<Integer> areaIds) {
List<Area> areaList = new ArrayList<>(areaIds.size());
for (Integer areaId : areaIds) {
// 4)方法改成 getAreaChildrenList 获得子节点们5url 可以已改成 children-list
//@芋艿 是不是叫 getAreaListByIds 更合适 因为不一定是子节点 用于前端树选择获取缓存数据 <el-tree-select :cache-data="areaCache">
@GetMapping("/get-by-ids")
@Operation(summary = "通过区域 ids 获得地区列表")
@Parameter(name = "ids", description = "区域编号 ids", required = true, example = "1,150000")
public CommonResult<List<AreaNodeSimpleRespVO>> getAreaListByIds(@RequestParam("ids") Set<Integer> ids) {
List<Area> areaList = new ArrayList<>(ids.size());
for (Integer areaId : ids) {
areaList.add(AreaUtils.getArea(areaId));
}
return success(AreaConvert.INSTANCE.convertList2(areaList));

View File

@ -3,12 +3,9 @@ package cn.iocoder.yudao.module.system.controller.admin.ip.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* @author jason
*/
@Schema(description = "管理后台 - 懒加载地区节点 Response VO")
@Schema(description = "管理后台 - 简洁的地区节点 Response VO")
@Data
public class LazyAreaNodeRespVO {
public class AreaNodeSimpleRespVO {
@Schema(description = "编号", required = true, example = "110000")
private Integer id;
@ -16,6 +13,7 @@ public class LazyAreaNodeRespVO {
@Schema(description = "名字", required = true, example = "北京")
private String name;
@Schema(description = "是否叶子节点", required = true, example = "false")
private Boolean leaf = Boolean.FALSE;
@Schema(description = "是否叶子节点", required = false, example = "false")
private Boolean leaf;
}

View File

@ -3,12 +3,10 @@ package cn.iocoder.yudao.module.system.convert.ip;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.LazyAreaNodeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.ip.vo.AreaNodeSimpleRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import org.springframework.context.annotation.Lazy;
import java.util.List;
import java.util.Objects;
@ -20,12 +18,13 @@ public interface AreaConvert {
List<AreaNodeRespVO> convertList(List<Area> list);
List<LazyAreaNodeRespVO> convertList2(List<Area> list);
List<AreaNodeSimpleRespVO> convertList2(List<Area> list);
@Mapping(source = "type", target = "leaf")
LazyAreaNodeRespVO convert(Area area);
AreaNodeSimpleRespVO convert(Area area);
default Boolean convertAreaType(Integer type){
return Objects.equals(AreaTypeEnum.DISTRICT.getType(),type);
default Boolean convertAreaType(Integer type) {
return Objects.equals(AreaTypeEnum.DISTRICT.getType(), type);
}
}