mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-08-14 02:01:53 +08:00
mall + trade:review 配置管理列表
This commit is contained in:
@@ -4,7 +4,8 @@ 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.*;
|
||||
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;
|
||||
|
@@ -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
|
||||
@@ -84,4 +78,5 @@ public class DeliveryExpressTemplateController {
|
||||
PageResult<DeliveryExpressTemplateDO> pageResult = deliveryExpressTemplateService.getDeliveryExpressTemplatePage(pageVO);
|
||||
return success(DeliveryExpressTemplateConvert.INSTANCE.convertPage(pageResult));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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;
|
@@ -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
|
@@ -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 cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
|
||||
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
|
@@ -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;
|
@@ -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.*;
|
@@ -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.*;
|
@@ -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
|
@@ -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;
|
@@ -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();
|
@@ -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;
|
@@ -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;
|
@@ -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 @jason:simplae 是不是不用继承 DeliveryExpressTemplateBaseVO,直接 id name 属性就够了。
|
||||
@Schema(description = "管理后台 - 快递运费模板 精简 Response VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
@@ -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 @jason:pojo 不给默认值哈
|
||||
|
||||
@Schema(description = "区域运费列表")
|
||||
@Valid
|
||||
private List<ExpressTemplateChargeUpdateVO> templateCharge = Collections.emptyList();
|
@@ -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;
|
@@ -1,18 +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;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
}
|
@@ -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;
|
||||
@@ -24,4 +24,5 @@ public class ExpressTemplateFreeBaseVO {
|
||||
@Schema(description = "包邮件数", requiredMode = Schema.RequiredMode.REQUIRED, example = "5")
|
||||
@NotNull(message = "包邮件数不能为空")
|
||||
private Integer freeCount;
|
||||
|
||||
}
|
@@ -1,8 +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.Data;
|
||||
|
||||
// TODO @jason:这个 vo 可以内嵌到 DeliveryExpressTemplateUpdateReqVO,避免 vo 过多,不好分辨
|
||||
// TODO @jason:swagger 缺失
|
||||
/**
|
||||
* 快递运费模板包邮 更新 VO
|
||||
*/
|
@@ -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;
|
||||
|
@@ -1,17 +1,14 @@
|
||||
package cn.iocoder.yudao.module.trade.convert.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;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@@ -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;
|
||||
|
@@ -4,7 +4,7 @@ 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.DeliveryExpressTemplatePageReqVO;
|
||||
import cn.iocoder.yudao.module.trade.controller.admin.delivery.vo.expresstemplate.DeliveryExpressTemplatePageReqVO;
|
||||
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
/**
|
||||
|
@@ -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;
|
||||
|
@@ -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 接口
|
||||
*
|
||||
|
@@ -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 实现类
|
||||
@@ -42,6 +43,7 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
|
||||
public Long createDeliveryExpressTemplate(DeliveryExpressTemplateCreateReqVO createReqVO) {
|
||||
// 校验模板名是否唯一
|
||||
validateTemplateNameUnique(createReqVO.getName(), null);
|
||||
|
||||
// 插入
|
||||
DeliveryExpressTemplateDO deliveryExpressTemplate = INSTANCE.convert(createReqVO);
|
||||
expressTemplateMapper.insert(deliveryExpressTemplate);
|
||||
@@ -67,6 +69,7 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
|
||||
validateDeliveryExpressTemplateExists(updateReqVO.getId());
|
||||
// 校验模板名是否唯一
|
||||
validateTemplateNameUnique(updateReqVO.getName(), updateReqVO.getId());
|
||||
|
||||
// 更新运费从表
|
||||
updateExpressTemplateCharge(updateReqVO);
|
||||
// 更新包邮从表
|
||||
@@ -77,12 +80,11 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
|
||||
}
|
||||
|
||||
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));
|
||||
@@ -91,30 +93,29 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
|
||||
addFreeList.add(INSTANCE.convertTemplateFree(item));
|
||||
}
|
||||
}
|
||||
// 新增
|
||||
// 1.2 新增
|
||||
if (CollUtil.isNotEmpty(addFreeList)) {
|
||||
expressTemplateFreeMapper.insertBatch(addFreeList);
|
||||
}
|
||||
// 修改
|
||||
// 1.3 修改
|
||||
if (CollUtil.isNotEmpty(updateFreeList)) {
|
||||
expressTemplateFreeMapper.updateBatch(updateFreeList);
|
||||
}
|
||||
// 得到删除ids
|
||||
Set<Long> deleteFreeIds = CollectionUtils.convertSet(oldFreeList, DeliveryExpressTemplateFreeDO::getId);
|
||||
deleteFreeIds.removeAll(CollectionUtils.convertSet(updateFreeList, DeliveryExpressTemplateFreeDO::getId));
|
||||
//删除
|
||||
|
||||
// 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 (item.getId() != null) {
|
||||
// 计费模式以主表为准
|
||||
@@ -126,18 +127,18 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
|
||||
addList.add(INSTANCE.convertTemplateCharge(item));
|
||||
}
|
||||
}
|
||||
// 新增
|
||||
// 1.2 新增
|
||||
if (CollUtil.isNotEmpty(addList)) {
|
||||
expressTemplateChargeMapper.insertBatch(addList);
|
||||
}
|
||||
// 修改
|
||||
// 1.3 修改
|
||||
if (CollUtil.isNotEmpty(updateList)) {
|
||||
expressTemplateChargeMapper.updateBatch(updateList);
|
||||
}
|
||||
// 得到删除的ids
|
||||
Set<Long> deleteChargeIds = CollectionUtils.convertSet(oldChargeList, DeliveryExpressTemplateChargeDO::getId);
|
||||
deleteChargeIds.removeAll(CollectionUtils.convertSet(updateList, DeliveryExpressTemplateChargeDO::getId));
|
||||
// 删除
|
||||
|
||||
// 2. 删除
|
||||
Set<Long> deleteChargeIds = convertSet(oldChargeList, DeliveryExpressTemplateChargeDO::getId);
|
||||
deleteChargeIds.removeAll(convertSet(updateList, DeliveryExpressTemplateChargeDO::getId));
|
||||
if (CollUtil.isNotEmpty(deleteChargeIds)) {
|
||||
expressTemplateChargeMapper.deleteBatchIds(deleteChargeIds);
|
||||
}
|
||||
@@ -148,6 +149,7 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
|
||||
public void deleteDeliveryExpressTemplate(Long id) {
|
||||
// 校验存在
|
||||
validateDeliveryExpressTemplateExists(id);
|
||||
|
||||
// 删除主表
|
||||
expressTemplateMapper.deleteById(id);
|
||||
// 删除运费从表
|
||||
@@ -199,4 +201,5 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
|
||||
public PageResult<DeliveryExpressTemplateDO> getDeliveryExpressTemplatePage(DeliveryExpressTemplatePageReqVO pageReqVO) {
|
||||
return expressTemplateMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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);
|
||||
|
||||
}
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
@@ -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);
|
||||
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
@@ -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";
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user