trade:价格计算时,接入优惠劵逻辑

This commit is contained in:
YunaiV 2023-10-04 00:30:03 +08:00
parent a1e9bedf34
commit 20f5834d9a
18 changed files with 191 additions and 102 deletions

View File

@ -1,9 +1,6 @@
package cn.iocoder.yudao.module.promotion.api.seckill; package cn.iocoder.yudao.module.promotion.api.seckill;
import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillActivityProductRespDTO; import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO;
import java.util.Collection;
import java.util.List;
/** /**
* 秒杀活动 API 接口 * 秒杀活动 API 接口
@ -22,12 +19,14 @@ public interface SeckillActivityApi {
void updateSeckillStock(Long id, Long skuId, Integer count); void updateSeckillStock(Long id, Long skuId, Integer count);
/** /**
* 获取秒杀活动商品信息 * 校验是否参与秒杀商品
* *
* @param id 活动编号 * 如果校验失败则抛出业务异常
* @param skuIds sku 编号 *
* @return 秒杀活动商品信息列表 * @param activityId 活动编号
* @param skuId SKU 编号
* @param count 数量
*/ */
List<SeckillActivityProductRespDTO> getSeckillActivityProductList(Long id, Collection<Long> skuIds); SeckillValidateJoinRespDTO validateJoinSeckill(Long activityId, Long skuId, Integer count);
} }

View File

@ -1,65 +0,0 @@
package cn.iocoder.yudao.module.promotion.api.seckill.dto;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
/**
* 秒杀活动商品 Response DTO
*
* @author HUIHUI
*/
@Data
public class SeckillActivityProductRespDTO {
/**
* 秒杀参与商品编号
*/
private Long id;
/**
* 秒杀活动 id
*
* 关联 SeckillActivityDO#getId()
*/
private Long activityId;
/**
* 秒杀时段 id
*
* 关联 SeckillConfigDO#getId()
*/
private List<Long> configIds;
/**
* 商品 SPU 编号
*/
private Long spuId;
/**
* 商品 SKU 编号
*/
private Long skuId;
/**
* 秒杀金额单位
*/
private Integer seckillPrice;
/**
* 秒杀库存
*/
private Integer stock;
/**
* 秒杀商品状态
*
* 枚举 {@link CommonStatusEnum 对应的类}
*/
private Integer activityStatus;
/**
* 活动开始时间点
*/
private LocalDateTime activityStartTime;
/**
* 活动结束时间点
*/
private LocalDateTime activityEndTime;
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.module.promotion.api.seckill.dto;
import lombok.Data;
/**
* 校验参与秒杀 Response DTO
*/
@Data
public class SeckillValidateJoinRespDTO {
/**
* 秒杀活动名称
*/
private String name;
/**
* 总限购数量
*
* 目的目前只有 trade 有具体下单的数据需要交给 trade 价格计算使用
*/
private Integer totalLimitCount;
/**
* 秒杀金额
*/
private Integer seckillPrice;
}

View File

@ -56,6 +56,10 @@ public interface ErrorCodeConstants {
ErrorCode SECKILL_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED_OR_END = new ErrorCode(1_013_008_004, "秒杀活动未关闭或未结束,不能删除"); ErrorCode SECKILL_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED_OR_END = new ErrorCode(1_013_008_004, "秒杀活动未关闭或未结束,不能删除");
ErrorCode SECKILL_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_008_005, "秒杀活动已关闭,不能重复关闭"); ErrorCode SECKILL_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_008_005, "秒杀活动已关闭,不能重复关闭");
ErrorCode SECKILL_ACTIVITY_UPDATE_STOCK_FAIL = new ErrorCode(1_013_008_006, "秒杀失败,原因秒杀库存不足"); ErrorCode SECKILL_ACTIVITY_UPDATE_STOCK_FAIL = new ErrorCode(1_013_008_006, "秒杀失败,原因秒杀库存不足");
ErrorCode SECKILL_JOIN_ACTIVITY_TIME_ERROR = new ErrorCode(1_013_008_007, "秒杀失败,原因:不在活动时间范围内");
ErrorCode SECKILL_JOIN_ACTIVITY_STATUS_CLOSED = new ErrorCode(1_013_008_008, "秒杀失败,原因:秒杀活动已关闭");
ErrorCode SECKILL_JOIN_ACTIVITY_SINGLE_LIMIT_COUNT_EXCEED = new ErrorCode(1_013_008_009, "秒杀失败,原因:单次限购超出");
ErrorCode SECKILL_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS = new ErrorCode(1_013_008_010, "秒杀失败,原因:商品不存在");
// ========== 秒杀时段 1-013-009-000 ========== // ========== 秒杀时段 1-013-009-000 ==========
ErrorCode SECKILL_CONFIG_NOT_EXISTS = new ErrorCode(1_013_009_000, "秒杀时段不存在"); ErrorCode SECKILL_CONFIG_NOT_EXISTS = new ErrorCode(1_013_009_000, "秒杀时段不存在");

View File

@ -1,13 +1,10 @@
package cn.iocoder.yudao.module.promotion.api.seckill; package cn.iocoder.yudao.module.promotion.api.seckill;
import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillActivityProductRespDTO; import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.convert.seckill.seckillactivity.SeckillActivityConvert;
import cn.iocoder.yudao.module.promotion.service.seckill.SeckillActivityService; import cn.iocoder.yudao.module.promotion.service.seckill.SeckillActivityService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
/** /**
* 秒杀活动接口 Api 接口实现类 * 秒杀活动接口 Api 接口实现类
@ -26,8 +23,8 @@ public class SeckillActivityApiImpl implements SeckillActivityApi {
} }
@Override @Override
public List<SeckillActivityProductRespDTO> getSeckillActivityProductList(Long id, Collection<Long> skuIds) { public SeckillValidateJoinRespDTO validateJoinSeckill(Long activityId, Long skuId, Integer count) {
return SeckillActivityConvert.INSTANCE.convertList4(activityService.getSeckillActivityProductList(id, skuIds)); return activityService.validateJoinSeckill(activityId, skuId, count);
} }
} }

View File

@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils; import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.product.enums.DictTypeConstants; import cn.iocoder.yudao.module.product.enums.DictTypeConstants;
import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillActivityProductRespDTO; import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityDetailRespVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityDetailRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityRespVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityRespVO;
@ -140,6 +140,6 @@ public interface SeckillActivityConvert {
.setStartTime(startTime).setEndTime(endTime); .setStartTime(startTime).setEndTime(endTime);
} }
List<SeckillActivityProductRespDTO> convertList4(List<SeckillProductDO> seckillActivityProductList); SeckillValidateJoinRespDTO convert02(SeckillActivityDO activity, SeckillProductDO product);
} }

View File

@ -46,7 +46,7 @@ public interface SeckillActivityMapper extends BaseMapperX<SeckillActivityDO> {
.eq(SeckillActivityDO::getId, id) .eq(SeckillActivityDO::getId, id)
.gt(SeckillActivityDO::getTotalStock, 0) .gt(SeckillActivityDO::getTotalStock, 0)
.setSql("stock = stock + " + count) .setSql("stock = stock + " + count)
.setSql("totalStock = totalStock - " + count)); .setSql("total_stock = total_stock - " + count));
} }
default PageResult<SeckillActivityDO> selectPage(AppSeckillActivityPageReqVO pageReqVO, Integer status) { default PageResult<SeckillActivityDO> selectPage(AppSeckillActivityPageReqVO pageReqVO, Integer status) {

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.promotion.service.seckill; package cn.iocoder.yudao.module.promotion.service.seckill;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityPageReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityUpdateReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityUpdateReqVO;
@ -115,4 +116,15 @@ public interface SeckillActivityService {
*/ */
List<SeckillProductDO> getSeckillActivityProductList(Long id, Collection<Long> skuIds); List<SeckillProductDO> getSeckillActivityProductList(Long id, Collection<Long> skuIds);
/**
* 校验是否参与秒杀商品
*
* 如果校验失败则抛出业务异常
*
* @param activityId 活动编号
* @param skuId SKU 编号
* @param count 数量
*/
SeckillValidateJoinRespDTO validateJoinSeckill(Long activityId, Long skuId, Integer count);
} }

View File

@ -4,10 +4,12 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi; import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityPageReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityUpdateReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityUpdateReqVO;
@ -15,6 +17,7 @@ import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.Sec
import cn.iocoder.yudao.module.promotion.controller.app.seckill.vo.activity.AppSeckillActivityPageReqVO; import cn.iocoder.yudao.module.promotion.controller.app.seckill.vo.activity.AppSeckillActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.convert.seckill.seckillactivity.SeckillActivityConvert; import cn.iocoder.yudao.module.promotion.convert.seckill.seckillactivity.SeckillActivityConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillActivityDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillConfigDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillProductDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillProductDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillActivityMapper; import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillActivityMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillProductMapper; import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillProductMapper;
@ -277,8 +280,7 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
@Override @Override
public List<SeckillProductDO> getSeckillActivityProductList(Long id, Collection<Long> skuIds) { public List<SeckillProductDO> getSeckillActivityProductList(Long id, Collection<Long> skuIds) {
// 1校验秒杀活动是否存在
validateSeckillActivityExists(id);
// 2校验活动商品是否存在 // 2校验活动商品是否存在
List<SeckillProductDO> productList = filterList(seckillProductMapper.selectListByActivityId(id), List<SeckillProductDO> productList = filterList(seckillProductMapper.selectListByActivityId(id),
item -> skuIds.contains(item.getSkuId())); item -> skuIds.contains(item.getSkuId()));
@ -288,4 +290,36 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
return productList; return productList;
} }
@Override
public SeckillValidateJoinRespDTO validateJoinSeckill(Long activityId, Long skuId, Integer count) {
// 1.1 校验秒杀活动是否存在
SeckillActivityDO activity = validateSeckillActivityExists(activityId);
if (ObjectUtil.notEqual(activity.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
throw exception(SECKILL_JOIN_ACTIVITY_STATUS_CLOSED);
}
// 1.2 是否在活动时间范围内
if (!LocalDateTimeUtils.isBetween(activity.getStartTime(), activity.getEndTime())) {
throw exception(SECKILL_JOIN_ACTIVITY_TIME_ERROR);
}
SeckillConfigDO config = seckillConfigService.getCurrentSeckillConfig();
if (config == null || !CollectionUtil.contains(activity.getConfigIds(), config.getId())) {
throw exception(SECKILL_JOIN_ACTIVITY_TIME_ERROR);
}
// 1.3 超过单次购买限制
if (count > activity.getSingleLimitCount()) {
throw exception(SECKILL_JOIN_ACTIVITY_SINGLE_LIMIT_COUNT_EXCEED);
}
// 2.1 校验秒杀商品是否存在
SeckillProductDO product = seckillProductMapper.selectByActivityIdAndSkuId(activityId, skuId);
if (product == null) {
throw exception(SECKILL_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS);
}
// 2.2 校验库存是否充足
if (count > product.getStock()) {
throw exception(SECKILL_ACTIVITY_UPDATE_STOCK_FAIL);
}
return SeckillActivityConvert.INSTANCE.convert02(activity, product);
}
} }

View File

@ -55,6 +55,7 @@ public interface ErrorCodeConstants {
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1_011_003_000, "支付价格计算异常,原因:价格小于等于 0"); ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1_011_003_000, "支付价格计算异常,原因:价格小于等于 0");
ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND = new ErrorCode(1_011_003_002, "计算快递运费异常,找不到对应的运费模板"); ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND = new ErrorCode(1_011_003_002, "计算快递运费异常,找不到对应的运费模板");
ErrorCode PRICE_CALCULATE_COUPON_NOT_MATCH_NORMAL_ORDER = new ErrorCode(1_011_003_004, "参与秒杀、拼团、砍价的营销商品,无法使用优惠劵"); ErrorCode PRICE_CALCULATE_COUPON_NOT_MATCH_NORMAL_ORDER = new ErrorCode(1_011_003_004, "参与秒杀、拼团、砍价的营销商品,无法使用优惠劵");
ErrorCode PRICE_CALCULATE_SECKILL_TOTAL_LIMIT_COUNT = new ErrorCode(1_011_003_005, "参与秒杀的商品,超过了秒杀总限购数量");
// ========== 物流 Express 模块 1-011-004-000 ========== // ========== 物流 Express 模块 1-011-004-000 ==========
ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1_011_004_000, "快递公司不存在"); ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1_011_004_000, "快递公司不存在");

View File

@ -14,7 +14,7 @@ import java.util.List;
public class AppTradeOrderSettlementRespVO { public class AppTradeOrderSettlementRespVO {
@Schema(description = "交易类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") // 对应 TradeOrderTypeEnum 枚举 @Schema(description = "交易类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") // 对应 TradeOrderTypeEnum 枚举
private Integer type = 1; // TODO 芋艿改成计算 private Integer type;
@Schema(description = "购物项数组", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "购物项数组", requiredMode = Schema.RequiredMode.REQUIRED)
private List<Item> items; private List<Item> items;
@ -75,6 +75,9 @@ public class AppTradeOrderSettlementRespVO {
@Schema(description = "商品原价(总),单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "500") @Schema(description = "商品原价(总),单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "500")
private Integer totalPrice; private Integer totalPrice;
@Schema(description = "订单优惠(总),单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "66")
private Integer discountPrice;
@Schema(description = "运费金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "50") @Schema(description = "运费金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "50")
private Integer deliveryPrice; private Integer deliveryPrice;

View File

@ -211,7 +211,8 @@ public interface TradeOrderConvert {
.setCouponId(settlementReqVO.getCouponId()).setPointStatus(settlementReqVO.getPointStatus()) .setCouponId(settlementReqVO.getCouponId()).setPointStatus(settlementReqVO.getPointStatus())
.setDeliveryType(settlementReqVO.getDeliveryType()).setAddressId(settlementReqVO.getAddressId()) .setDeliveryType(settlementReqVO.getDeliveryType()).setAddressId(settlementReqVO.getAddressId())
.setPickUpStoreId(settlementReqVO.getPickUpStoreId()) .setPickUpStoreId(settlementReqVO.getPickUpStoreId())
.setItems(new ArrayList<>(settlementReqVO.getItems().size())); .setItems(new ArrayList<>(settlementReqVO.getItems().size()))
.setSeckillActivityId(settlementReqVO.getSeckillActivityId());
// 商品项的构建 // 商品项的构建
Map<Long, CartDO> cartMap = convertMap(cartList, CartDO::getId); Map<Long, CartDO> cartMap = convertMap(cartList, CartDO::getId);
for (AppTradeOrderSettlementReqVO.Item item : settlementReqVO.getItems()) { for (AppTradeOrderSettlementReqVO.Item item : settlementReqVO.getItems()) {

View File

@ -288,4 +288,11 @@ public class TradeOrderDO extends BaseDO {
*/ */
private Integer vipPrice; private Integer vipPrice;
/**
* 秒杀活动编号
*
* 关联 SeckillActivityDO id 字段
*/
private Long seckillActivityId;
} }

View File

@ -1,13 +1,19 @@
package cn.iocoder.yudao.module.trade.dal.mysql.order; package cn.iocoder.yudao.module.trade.dal.mysql.order;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
@Mapper @Mapper
public interface TradeOrderItemMapper extends BaseMapperX<TradeOrderItemDO> { public interface TradeOrderItemMapper extends BaseMapperX<TradeOrderItemDO> {
@ -38,4 +44,13 @@ public interface TradeOrderItemMapper extends BaseMapperX<TradeOrderItemDO> {
.eq(TradeOrderItemDO::getCommentStatus, commentStatus)); .eq(TradeOrderItemDO::getCommentStatus, commentStatus));
} }
default int selectProductSumByOrderId(@Param("orderIds") Set<Long> orderIds) {
// SQL sum 查询
List<Map<String, Object>> result = selectMaps(new QueryWrapper<TradeOrderItemDO>()
.select("SUM(count) AS sumCount")
.in("order_id", orderIds)); // 只计算选中的
// 获得数量
return CollUtil.getFirst(result) != null ? MapUtil.getInt(result.get(0), "sumCount") : 0;
}
} }

View File

@ -82,4 +82,10 @@ public interface TradeOrderMapper extends BaseMapperX<TradeOrderDO> {
.eq(TradeOrderDO::getCommentStatus, commentStatus)); .eq(TradeOrderDO::getCommentStatus, commentStatus));
} }
default List<TradeOrderDO> selectListByUserIdAndSeckillActivityId(Long userId, Long seckillActivityId) {
return selectList(new LambdaUpdateWrapper<>(TradeOrderDO.class)
.eq(TradeOrderDO::getUserId, userId)
.eq(TradeOrderDO::getSeckillActivityId, seckillActivityId));
}
} }

View File

@ -82,6 +82,15 @@ public interface TradeOrderQueryService {
*/ */
List<ExpressTrackRespDTO> getExpressTrackList(Long id); List<ExpressTrackRespDTO> getExpressTrackList(Long id);
/**
* 会员在指定秒杀活动下用户购买的商品数量
*
* @param userId 用户编号
* @param activityId 活动编号
* @return 秒杀商品数量
*/
int getSeckillProductCount(Long userId, Long activityId);
// =================== Order Item =================== // =================== Order Item ===================
/** /**

View File

@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderItemMapper; import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderItemMapper;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderMapper; import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderMapper;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory; import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO; import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO; import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
@ -120,6 +121,18 @@ public class TradeOrderQueryServiceImpl implements TradeOrderQueryService {
return getExpressTrackList(order); return getExpressTrackList(order);
} }
@Override
public int getSeckillProductCount(Long userId, Long activityId) {
// 获得订单列表
List<TradeOrderDO> orders = tradeOrderMapper.selectListByUserIdAndSeckillActivityId(userId, activityId);
orders.removeIf(order -> TradeOrderStatusEnum.isCanceled(order.getStatus())); // 过滤掉已取消的订单
if (CollUtil.isEmpty(orders)) {
return 0;
}
// 获得订单项列表
return tradeOrderItemMapper.selectProductSumByOrderId(convertSet(orders, TradeOrderDO::getId));
}
// TODO @puhui999可以加个 spring 缓存30 分钟主要考虑及时性要求不高但是每次调用需要钱 // TODO @puhui999可以加个 spring 缓存30 分钟主要考虑及时性要求不高但是每次调用需要钱
/** /**
* 获得订单的物流轨迹 * 获得订单的物流轨迹

View File

@ -1,19 +1,22 @@
package cn.iocoder.yudao.module.trade.service.price.calculator; package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.promotion.api.seckill.SeckillActivityApi; import cn.iocoder.yudao.module.promotion.api.seckill.SeckillActivityApi;
import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillActivityProductRespDTO; import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; 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.bo.TradePriceCalculateRespBO;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; 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.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.PRICE_CALCULATE_SECKILL_TOTAL_LIMIT_COUNT;
// TODO huihui单测需要补充
/** /**
* 秒杀活动的 {@link TradePriceCalculator} 实现类 * 秒杀活动的 {@link TradePriceCalculator} 实现类
* *
@ -24,22 +27,45 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
public class TradeSeckillActivityPriceCalculator implements TradePriceCalculator { public class TradeSeckillActivityPriceCalculator implements TradePriceCalculator {
@Resource @Resource
private SeckillActivityApi activityApi; private SeckillActivityApi seckillActivityApi;
@Resource
private TradeOrderQueryService tradeOrderQueryService;
@Override @Override
public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) { public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
// 1判断订单类型和是否具有秒杀活动编号 // 1. 判断订单类型和是否具有秒杀活动编号
if (param.getSeckillActivityId() == null) { if (param.getSeckillActivityId() == null) {
return; return;
} }
// 2获取秒杀活动商品信息 Assert.isTrue(param.getItems().size() == 1, "秒杀时,只允许选择一个商品");
List<SeckillActivityProductRespDTO> productList = activityApi.getSeckillActivityProductList(param.getSeckillActivityId(), convertSet(param.getItems(), // 2. 校验是否可以参与秒杀
TradePriceCalculateReqBO.Item::getSkuId)); TradePriceCalculateRespBO.OrderItem orderItem = result.getItems().get(0);
Map<Long, SeckillActivityProductRespDTO> productMap = convertMap(productList, SeckillActivityProductRespDTO::getSkuId); SeckillValidateJoinRespDTO seckillActivity = validateJoinSeckill(
result.getItems().forEach(item -> { param.getUserId(), param.getSeckillActivityId(),
SeckillActivityProductRespDTO product = productMap.get(item.getSkuId()); orderItem.getSkuId(), orderItem.getCount());
item.setActivityPrice(product.getSeckillPrice()); // 设置活动金额
}); // 3.1 记录优惠明细
Integer discountPrice = orderItem.getPayPrice() - seckillActivity.getSeckillPrice();
TradePriceCalculatorHelper.addPromotion(result, orderItem,
param.getSeckillActivityId(), seckillActivity.getName(), PromotionTypeEnum.SECKILL_ACTIVITY.getType(),
StrUtil.format("秒杀活动:省 {} 元", TradePriceCalculatorHelper.formatPrice(discountPrice)),
discountPrice);
// 3.2 更新 SKU 优惠金额
orderItem.setDiscountPrice(orderItem.getDiscountPrice() + discountPrice);
TradePriceCalculatorHelper.recountPayPrice(orderItem);
TradePriceCalculatorHelper.recountAllPrice(result);
}
private SeckillValidateJoinRespDTO validateJoinSeckill(Long userId, Long activityId, Long skuId, Integer count) {
// 1. 校验是否可以参与秒杀
SeckillValidateJoinRespDTO seckillActivity = seckillActivityApi.validateJoinSeckill(activityId, skuId, count);
// 2. 校验总限购数量目前只有 trade 有具体下单的数据需要交给 trade 价格计算使用
int seckillProductCount = tradeOrderQueryService.getSeckillProductCount(userId, activityId);
if (seckillProductCount + count > seckillActivity.getTotalLimitCount()) {
throw exception(PRICE_CALCULATE_SECKILL_TOTAL_LIMIT_COUNT);
}
return seckillActivity;
} }
} }