diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApi.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApi.java index 6f01d57ba..30b756610 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApi.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApi.java @@ -1,9 +1,6 @@ package cn.iocoder.yudao.module.promotion.api.seckill; -import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillActivityProductRespDTO; - -import java.util.Collection; -import java.util.List; +import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO; /** * 秒杀活动 API 接口 @@ -22,12 +19,14 @@ public interface SeckillActivityApi { void updateSeckillStock(Long id, Long skuId, Integer count); /** - * 获取秒杀活动商品信息 + * 校验是否参与秒杀商品 * - * @param id 活动编号 - * @param skuIds sku 编号 - * @return 秒杀活动商品信息列表 + * 如果校验失败,则抛出业务异常 + * + * @param activityId 活动编号 + * @param skuId SKU 编号 + * @param count 数量 */ - List getSeckillActivityProductList(Long id, Collection skuIds); + SeckillValidateJoinRespDTO validateJoinSeckill(Long activityId, Long skuId, Integer count); } diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/dto/SeckillActivityProductRespDTO.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/dto/SeckillActivityProductRespDTO.java deleted file mode 100644 index 5fb0554d3..000000000 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/dto/SeckillActivityProductRespDTO.java +++ /dev/null @@ -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 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; - -} diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/dto/SeckillValidateJoinRespDTO.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/dto/SeckillValidateJoinRespDTO.java new file mode 100644 index 000000000..aae89a415 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/dto/SeckillValidateJoinRespDTO.java @@ -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; + +} diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java index cb0f74f5b..4102c6160 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java @@ -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_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_008_005, "秒杀活动已关闭,不能重复关闭"); 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 ========== ErrorCode SECKILL_CONFIG_NOT_EXISTS = new ErrorCode(1_013_009_000, "秒杀时段不存在"); diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApiImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApiImpl.java index b837dac7e..239cdf4ba 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApiImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/seckill/SeckillActivityApiImpl.java @@ -1,13 +1,10 @@ package cn.iocoder.yudao.module.promotion.api.seckill; -import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillActivityProductRespDTO; -import cn.iocoder.yudao.module.promotion.convert.seckill.seckillactivity.SeckillActivityConvert; +import cn.iocoder.yudao.module.promotion.api.seckill.dto.SeckillValidateJoinRespDTO; import cn.iocoder.yudao.module.promotion.service.seckill.SeckillActivityService; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.Collection; -import java.util.List; /** * 秒杀活动接口 Api 接口实现类 @@ -26,8 +23,8 @@ public class SeckillActivityApiImpl implements SeckillActivityApi { } @Override - public List getSeckillActivityProductList(Long id, Collection skuIds) { - return SeckillActivityConvert.INSTANCE.convertList4(activityService.getSeckillActivityProductList(id, skuIds)); + public SeckillValidateJoinRespDTO validateJoinSeckill(Long activityId, Long skuId, Integer count) { + return activityService.validateJoinSeckill(activityId, skuId, count); } } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/seckill/seckillactivity/SeckillActivityConvert.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/seckill/seckillactivity/SeckillActivityConvert.java index b15ad53c5..271a0340f 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/seckill/seckillactivity/SeckillActivityConvert.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/seckill/seckillactivity/SeckillActivityConvert.java @@ -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.module.product.api.spu.dto.ProductSpuRespDTO; 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.SeckillActivityDetailRespVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityRespVO; @@ -140,6 +140,6 @@ public interface SeckillActivityConvert { .setStartTime(startTime).setEndTime(endTime); } - List convertList4(List seckillActivityProductList); + SeckillValidateJoinRespDTO convert02(SeckillActivityDO activity, SeckillProductDO product); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java index 668a9e1cf..28f764dae 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java @@ -46,7 +46,7 @@ public interface SeckillActivityMapper extends BaseMapperX { .eq(SeckillActivityDO::getId, id) .gt(SeckillActivityDO::getTotalStock, 0) .setSql("stock = stock + " + count) - .setSql("totalStock = totalStock - " + count)); + .setSql("total_stock = total_stock - " + count)); } default PageResult selectPage(AppSeckillActivityPageReqVO pageReqVO, Integer status) { diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityService.java index fe238a7fb..f5663640f 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityService.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.promotion.service.seckill; 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.SeckillActivityPageReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityUpdateReqVO; @@ -115,4 +116,15 @@ public interface SeckillActivityService { */ List getSeckillActivityProductList(Long id, Collection skuIds); + /** + * 校验是否参与秒杀商品 + * + * 如果校验失败,则抛出业务异常 + * + * @param activityId 活动编号 + * @param skuId SKU 编号 + * @param count 数量 + */ + SeckillValidateJoinRespDTO validateJoinSeckill(Long activityId, Long skuId, Integer count); + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java index 69f1a3134..671ae4466 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java @@ -4,10 +4,12 @@ import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; 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.dto.ProductSkuRespDTO; import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi; 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.SeckillActivityPageReqVO; 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.convert.seckill.seckillactivity.SeckillActivityConvert; 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.mysql.seckill.seckillactivity.SeckillActivityMapper; import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillProductMapper; @@ -277,8 +280,7 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { @Override public List getSeckillActivityProductList(Long id, Collection skuIds) { - // 1、校验秒杀活动是否存在 - validateSeckillActivityExists(id); + // 2、校验活动商品是否存在 List productList = filterList(seckillProductMapper.selectListByActivityId(id), item -> skuIds.contains(item.getSkuId())); @@ -288,4 +290,36 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { 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); + } + } diff --git a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java index 7539aa22e..d50ff34dc 100644 --- a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java @@ -55,6 +55,7 @@ public interface ErrorCodeConstants { 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_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 ========== ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1_011_004_000, "快递公司不存在"); diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeOrderSettlementRespVO.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeOrderSettlementRespVO.java index 2ac2f5245..0c851bf34 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeOrderSettlementRespVO.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeOrderSettlementRespVO.java @@ -14,7 +14,7 @@ import java.util.List; public class AppTradeOrderSettlementRespVO { @Schema(description = "交易类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") // 对应 TradeOrderTypeEnum 枚举 - private Integer type = 1; // TODO 芋艿:改成计算 + private Integer type; @Schema(description = "购物项数组", requiredMode = Schema.RequiredMode.REQUIRED) private List items; @@ -75,6 +75,9 @@ public class AppTradeOrderSettlementRespVO { @Schema(description = "商品原价(总),单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "500") private Integer totalPrice; + @Schema(description = "订单优惠(总),单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "66") + private Integer discountPrice; + @Schema(description = "运费金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "50") private Integer deliveryPrice; diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java index b4cdca58c..8f9afbd74 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java @@ -211,7 +211,8 @@ public interface TradeOrderConvert { .setCouponId(settlementReqVO.getCouponId()).setPointStatus(settlementReqVO.getPointStatus()) .setDeliveryType(settlementReqVO.getDeliveryType()).setAddressId(settlementReqVO.getAddressId()) .setPickUpStoreId(settlementReqVO.getPickUpStoreId()) - .setItems(new ArrayList<>(settlementReqVO.getItems().size())); + .setItems(new ArrayList<>(settlementReqVO.getItems().size())) + .setSeckillActivityId(settlementReqVO.getSeckillActivityId()); // 商品项的构建 Map cartMap = convertMap(cartList, CartDO::getId); for (AppTradeOrderSettlementReqVO.Item item : settlementReqVO.getItems()) { diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java index 2909eb432..903f7be40 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java @@ -288,4 +288,11 @@ public class TradeOrderDO extends BaseDO { */ private Integer vipPrice; + /** + * 秒杀活动编号 + * + * 关联 SeckillActivityDO 的 id 字段 + */ + private Long seckillActivityId; + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderItemMapper.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderItemMapper.java index 480fe7ddd..b701b7f87 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderItemMapper.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderItemMapper.java @@ -1,13 +1,19 @@ 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.query.LambdaQueryWrapperX; 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 org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.Set; @Mapper public interface TradeOrderItemMapper extends BaseMapperX { @@ -38,4 +44,13 @@ public interface TradeOrderItemMapper extends BaseMapperX { .eq(TradeOrderItemDO::getCommentStatus, commentStatus)); } + default int selectProductSumByOrderId(@Param("orderIds") Set orderIds) { + // SQL sum 查询 + List> result = selectMaps(new QueryWrapper() + .select("SUM(count) AS sumCount") + .in("order_id", orderIds)); // 只计算选中的 + // 获得数量 + return CollUtil.getFirst(result) != null ? MapUtil.getInt(result.get(0), "sumCount") : 0; + } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderMapper.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderMapper.java index 743f31baa..22265c9f7 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderMapper.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/order/TradeOrderMapper.java @@ -82,4 +82,10 @@ public interface TradeOrderMapper extends BaseMapperX { .eq(TradeOrderDO::getCommentStatus, commentStatus)); } + default List selectListByUserIdAndSeckillActivityId(Long userId, Long seckillActivityId) { + return selectList(new LambdaUpdateWrapper<>(TradeOrderDO.class) + .eq(TradeOrderDO::getUserId, userId) + .eq(TradeOrderDO::getSeckillActivityId, seckillActivityId)); + } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryService.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryService.java index 442f83d3b..70e7a723b 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryService.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryService.java @@ -82,6 +82,15 @@ public interface TradeOrderQueryService { */ List getExpressTrackList(Long id); + /** + * 【会员】在指定秒杀活动下,用户购买的商品数量 + * + * @param userId 用户编号 + * @param activityId 活动编号 + * @return 秒杀商品数量 + */ + int getSeckillProductCount(Long userId, Long activityId); + // =================== Order Item =================== /** diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java index c0dfbfd8c..8d24c0a3a 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderQueryServiceImpl.java @@ -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.mysql.order.TradeOrderItemMapper; 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.dto.ExpressTrackQueryReqDTO; import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO; @@ -120,6 +121,18 @@ public class TradeOrderQueryServiceImpl implements TradeOrderQueryService { return getExpressTrackList(order); } + @Override + public int getSeckillProductCount(Long userId, Long activityId) { + // 获得订单列表 + List 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 分钟;主要考虑及时性要求不高,但是每次调用需要钱; /** * 获得订单的物流轨迹 diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeSeckillActivityPriceCalculator.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeSeckillActivityPriceCalculator.java index 95996ab9f..457419443 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeSeckillActivityPriceCalculator.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeSeckillActivityPriceCalculator.java @@ -1,19 +1,22 @@ 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.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.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.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.PRICE_CALCULATE_SECKILL_TOTAL_LIMIT_COUNT; +// TODO huihui:单测需要补充 /** * 秒杀活动的 {@link TradePriceCalculator} 实现类 * @@ -24,22 +27,45 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. public class TradeSeckillActivityPriceCalculator implements TradePriceCalculator { @Resource - private SeckillActivityApi activityApi; + private SeckillActivityApi seckillActivityApi; + + @Resource + private TradeOrderQueryService tradeOrderQueryService; @Override public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) { - // 1、判断订单类型和是否具有秒杀活动编号 + // 1. 判断订单类型和是否具有秒杀活动编号 if (param.getSeckillActivityId() == null) { return; } - // 2、获取秒杀活动商品信息 - List productList = activityApi.getSeckillActivityProductList(param.getSeckillActivityId(), convertSet(param.getItems(), - TradePriceCalculateReqBO.Item::getSkuId)); - Map productMap = convertMap(productList, SeckillActivityProductRespDTO::getSkuId); - result.getItems().forEach(item -> { - SeckillActivityProductRespDTO product = productMap.get(item.getSkuId()); - item.setActivityPrice(product.getSeckillPrice()); // 设置活动金额 - }); + Assert.isTrue(param.getItems().size() == 1, "秒杀时,只允许选择一个商品"); + // 2. 校验是否可以参与秒杀 + TradePriceCalculateRespBO.OrderItem orderItem = result.getItems().get(0); + SeckillValidateJoinRespDTO seckillActivity = validateJoinSeckill( + param.getUserId(), param.getSeckillActivityId(), + orderItem.getSkuId(), orderItem.getCount()); + + // 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; } }