From 6fdd4da0b3f04d6a096b6a131cc14956679cef77 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 12 Aug 2023 16:05:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A7=92=E6=9D=80=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E7=9A=84=E7=AE=A1=E7=90=86=E5=90=8E=E5=8F=B0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/product/api/spu/ProductSpuApi.java | 7 + .../product/api/spu/ProductSpuApiImpl.java | 16 ++- .../convert/spu/ProductSpuConvert.java | 2 + .../promotion/enums/ErrorCodeConstants.java | 9 +- .../seckill/SeckillActivityController.java | 26 ++-- .../seckill/SeckillConfigController.java | 1 + .../vo/activity/SeckillActivityBaseVO.java | 2 +- .../activity/SeckillActivityCreateReqVO.java | 4 +- .../vo/activity/SeckillActivityRespVO.java | 12 +- .../activity/SeckillActivityUpdateReqVO.java | 4 +- .../vo/product/SeckillProductBaseVO.java | 2 +- .../vo/product/SeckillProductCreateReqVO.java | 13 -- .../vo/product/SeckillProductUpdateReqVO.java | 14 -- .../SeckillActivityConvert.java | 51 ++++---- .../seckillactivity/SeckillProductMapper.java | 13 -- .../SeckillActivityService.java | 16 +-- .../SeckillActivityServiceImpl.java | 122 ++++++++++-------- .../seckillconfig/SeckillConfigService.java | 6 +- .../SeckillConfigServiceImpl.java | 66 ++++------ .../SeckillConfigServiceImplTest.java | 6 +- 20 files changed, 183 insertions(+), 209 deletions(-) delete mode 100644 yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductCreateReqVO.java delete mode 100644 yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductUpdateReqVO.java diff --git a/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApi.java b/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApi.java index 8ba0fba7d..78c1154c1 100644 --- a/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApi.java +++ b/yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApi.java @@ -21,4 +21,11 @@ public interface ProductSpuApi { */ List getSpuList(Collection ids); + /** + * 获得 SPU + * + * @return SPU + */ + ProductSpuRespDTO getSpu(Long id); + } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApiImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApiImpl.java index 6823e0a57..5803c097a 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApiImpl.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApiImpl.java @@ -5,6 +5,8 @@ import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; import cn.iocoder.yudao.module.product.convert.spu.ProductSpuConvert; import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO; import cn.iocoder.yudao.module.product.dal.mysql.spu.ProductSpuMapper; +import cn.iocoder.yudao.module.product.service.sku.ProductSkuService; +import cn.iocoder.yudao.module.product.service.spu.ProductSpuService; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -24,15 +26,19 @@ import java.util.List; public class ProductSpuApiImpl implements ProductSpuApi { @Resource - private ProductSpuMapper productSpuMapper; + private ProductSpuService spuService; @Override - public List getSpuList(Collection spuIds) { - if (CollectionUtil.isEmpty(spuIds)) { + public List getSpuList(Collection ids) { + if (CollectionUtil.isEmpty(ids)) { return Collections.emptyList(); } - List productSpuDOList = productSpuMapper.selectBatchIds(spuIds); - return ProductSpuConvert.INSTANCE.convertList2(productSpuDOList); + return ProductSpuConvert.INSTANCE.convertList2(spuService.getSpuList(ids)); + } + + @Override + public ProductSpuRespDTO getSpu(Long id) { + return ProductSpuConvert.INSTANCE.convert02(spuService.getSpu(id)); } } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/spu/ProductSpuConvert.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/spu/ProductSpuConvert.java index 874b5673e..6d52e5cad 100755 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/spu/ProductSpuConvert.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/spu/ProductSpuConvert.java @@ -64,6 +64,8 @@ public interface ProductSpuConvert { ProductSpuDetailRespVO convert03(ProductSpuDO spu); + ProductSpuRespDTO convert02(ProductSpuDO bean); + // ========== 用户 App 相关 ========== PageResult convertPageForGetSpuPage(PageResult page); 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 e2f2bb98c..dfeb9b8e8 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 @@ -50,14 +50,11 @@ public interface ErrorCodeConstants { ErrorCode SECKILL_ACTIVITY_UPDATE_FAIL_STATUS_CLOSED = new ErrorCode(1013008003, "秒杀活动已关闭,不能修改"); ErrorCode SECKILL_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED_OR_END = new ErrorCode(1013008004, "秒杀活动未关闭或未结束,不能删除"); ErrorCode SECKILL_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1013008005, "秒杀活动已关闭,不能重复关闭"); - ErrorCode SECKILL_ACTIVITY_CLOSE_FAIL_STATUS_END = new ErrorCode(1013008006, "秒杀活动已结束,不能关闭"); // ========== 秒杀时段 1013009000 ========== - ErrorCode SECKILL_TIME_NOT_EXISTS = new ErrorCode(1013009000, "秒杀时段不存在"); - ErrorCode SECKILL_TIME_CONFLICTS = new ErrorCode(1013009001, "秒杀时段冲突"); - ErrorCode SECKILL_TIME_EQUAL = new ErrorCode(1013009002, "秒杀时段开始时间和结束时间不能相等"); - ErrorCode SECKILL_START_TIME_BEFORE_END_TIME = new ErrorCode(1013009003, "秒杀时段开始时间不能在结束时间之后"); - ErrorCode SECKILL_TIME_DISABLE = new ErrorCode(1013009004, "秒杀时段已关闭"); + ErrorCode SECKILL_CONFIG_NOT_EXISTS = new ErrorCode(1013009000, "秒杀时段不存在"); + ErrorCode SECKILL_CONFIG_TIME_CONFLICTS = new ErrorCode(1013009001, "秒杀时段冲突"); + ErrorCode SECKILL_CONFIG_DISABLE = new ErrorCode(1013009004, "秒杀时段已关闭"); // ========== 拼团活动 1013010000 ========== ErrorCode COMBINATION_ACTIVITY_NOT_EXISTS = new ErrorCode(1013010000, "拼团活动不存在"); diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillActivityController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillActivityController.java index 1bc015bf5..702e247c8 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillActivityController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillActivityController.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.promotion.controller.admin.seckill; +import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi; @@ -19,7 +20,6 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.util.List; -import java.util.Set; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; @@ -33,7 +33,7 @@ public class SeckillActivityController { @Resource private SeckillActivityService seckillActivityService; @Resource - private ProductSpuApi spuApi; + private ProductSpuApi productSpuApi; @PostMapping("/create") @Operation(summary = "创建秒杀活动") @@ -73,21 +73,27 @@ public class SeckillActivityController { @Parameter(name = "id", description = "编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('promotion:seckill-activity:query')") public CommonResult getSeckillActivity(@RequestParam("id") Long id) { - SeckillActivityDO seckillActivity = seckillActivityService.getSeckillActivity(id); - List seckillProducts = seckillActivityService.getSeckillProductListByActivityId(id); - return success(SeckillActivityConvert.INSTANCE.convert(seckillActivity, seckillProducts)); + SeckillActivityDO activity = seckillActivityService.getSeckillActivity(id); + List products = seckillActivityService.getSeckillProductListByActivityId(id); + return success(SeckillActivityConvert.INSTANCE.convert(activity, products)); } @GetMapping("/page") @Operation(summary = "获得秒杀活动分页") @PreAuthorize("@ss.hasPermission('promotion:seckill-activity:query')") public CommonResult> getSeckillActivityPage(@Valid SeckillActivityPageReqVO pageVO) { + // 查询活动列表 PageResult pageResult = seckillActivityService.getSeckillActivityPage(pageVO); - Set aIds = convertSet(pageResult.getList(), SeckillActivityDO::getId); - List seckillProducts = seckillActivityService.getSeckillProductListByActivityId(aIds); - Set spuIds = convertSet(pageResult.getList(), SeckillActivityDO::getSpuId); - List spuList = spuApi.getSpuList(spuIds); - return success(SeckillActivityConvert.INSTANCE.convertPage(pageResult, seckillProducts, spuList)); + if (CollUtil.isEmpty(pageResult.getList())) { + return success(PageResult.empty(pageResult.getTotal())); + } + + // 拼接数据 + List products = seckillActivityService.getSeckillProductListByActivityId( + convertSet(pageResult.getList(), SeckillActivityDO::getId)); + List spuList = productSpuApi.getSpuList( + convertSet(pageResult.getList(), SeckillActivityDO::getSpuId)); + return success(SeckillActivityConvert.INSTANCE.convertPage(pageResult, products, spuList)); } } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillConfigController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillConfigController.java index c6e3c5693..89fc54b81 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillConfigController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/SeckillConfigController.java @@ -92,4 +92,5 @@ public class SeckillConfigController { PageResult pageResult = seckillConfigService.getSeckillConfigPage(pageVO); return success(SeckillConfigConvert.INSTANCE.convertPage(pageResult)); } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityBaseVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityBaseVO.java index 504d4a057..8aada5a1f 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityBaseVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityBaseVO.java @@ -20,7 +20,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ @Data public class SeckillActivityBaseVO { - @Schema(description = "秒杀活动商品id", requiredMode = Schema.RequiredMode.REQUIRED, example = "[121,1212]") + @Schema(description = "秒杀活动商品 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "[121,1212]") @NotNull(message = "秒杀活动商品不能为空") private Long spuId; diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityCreateReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityCreateReqVO.java index 46a58b565..9b6e7291a 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityCreateReqVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityCreateReqVO.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity; -import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.SeckillProductCreateReqVO; +import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.SeckillProductBaseVO; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; @@ -16,6 +16,6 @@ import java.util.List; public class SeckillActivityCreateReqVO extends SeckillActivityBaseVO { @Schema(description = "秒杀商品", requiredMode = Schema.RequiredMode.REQUIRED) - private List products; + private List products; } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityRespVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityRespVO.java index ad78a9464..84cb92db3 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityRespVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityRespVO.java @@ -21,28 +21,28 @@ public class SeckillActivityRespVO extends SeckillActivityBaseVO { @Schema(description = "商品主图", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/xx.png") private String picUrl; - @Schema(description = "秒杀活动id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + @Schema(description = "秒杀活动 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") private Long id; @Schema(description = "秒杀商品", requiredMode = Schema.RequiredMode.REQUIRED) private List products; - @Schema(description = "活动状态 开启:0 禁用:1", requiredMode = Schema.RequiredMode.REQUIRED, example = "0") + @Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0") private Integer status; @Schema(description = "订单实付金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "22354") private Integer totalPrice; - @Schema(description = "秒杀库存", example = "10") + @Schema(description = "秒杀库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") private Integer stock; - @Schema(description = "秒杀总库存", example = "20") + @Schema(description = "秒杀总库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "20") private Integer totalStock; - @Schema(description = "新增订单数", example = "20") + @Schema(description = "新增订单数", requiredMode = Schema.RequiredMode.REQUIRED, example = "20") private Integer orderCount; - @Schema(description = "付款人数", example = "20") + @Schema(description = "付款人数", requiredMode = Schema.RequiredMode.REQUIRED, example = "20") private Integer userCount; @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityUpdateReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityUpdateReqVO.java index 3fdf4ed6e..bf2ca35bb 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityUpdateReqVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/activity/SeckillActivityUpdateReqVO.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity; -import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.SeckillProductUpdateReqVO; +import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.SeckillProductBaseVO; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; @@ -18,6 +18,6 @@ public class SeckillActivityUpdateReqVO extends SeckillActivityBaseVO { private Long id; @Schema(description = "秒杀商品", requiredMode = Schema.RequiredMode.REQUIRED) - private List products; + private List products; } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductBaseVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductBaseVO.java index a11d78930..6584b79cb 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductBaseVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductBaseVO.java @@ -22,7 +22,7 @@ public class SeckillProductBaseVO { @NotNull(message = "秒杀金额,单位:分不能为空") private Integer seckillPrice; - @Schema(description = "秒杀库存", requiredMode = Schema.RequiredMode.REQUIRED) + @Schema(description = "秒杀库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "100") @NotNull(message = "秒杀库存不能为空") private Integer stock; diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductCreateReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductCreateReqVO.java deleted file mode 100644 index 8ee4884ea..000000000 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductCreateReqVO.java +++ /dev/null @@ -1,13 +0,0 @@ -package cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; -@Schema(description = "管理后台 - 秒杀参与商品创建 Request VO") -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class SeckillProductCreateReqVO extends SeckillProductBaseVO { - -} diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductUpdateReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductUpdateReqVO.java deleted file mode 100644 index 5fdccf8d0..000000000 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/seckill/vo/product/SeckillProductUpdateReqVO.java +++ /dev/null @@ -1,14 +0,0 @@ -package cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -@Schema(description = "管理后台 - 秒杀参与商品更新 Request VO") -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class SeckillProductUpdateReqVO extends SeckillProductBaseVO { - -} 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 65f3b9b0c..0f4d21979 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 @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.promotion.convert.seckill.seckillactivity; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; +import cn.iocoder.yudao.framework.common.util.collection.MapUtils; import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityDetailRespVO; @@ -19,6 +20,8 @@ import org.mapstruct.factory.Mappers; import java.util.List; import java.util.Map; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; + /** * 秒杀活动 Convert * @@ -37,43 +40,43 @@ public interface SeckillActivityConvert { List convertList(List list); - PageResult convertPage(PageResult page); - - default PageResult convertPage(PageResult page, List seckillProducts, List spuList) { - Map spuMap = CollectionUtils.convertMap(spuList, ProductSpuRespDTO::getId, c -> c); + default PageResult convertPage(PageResult page, + List seckillProducts, + List spuList) { PageResult pageResult = convertPage(page); + // 拼接商品 + Map spuMap = convertMap(spuList, ProductSpuRespDTO::getId); pageResult.getList().forEach(item -> { - item.setSpuName(spuMap.get(item.getSpuId()).getName()); - item.setPicUrl(spuMap.get(item.getSpuId()).getPicUrl()); item.setProducts(convertList2(seckillProducts)); + MapUtils.findAndThen(spuMap, item.getSpuId(), + spu -> item.setSpuName(spu.getName()).setPicUrl(spu.getPicUrl())); }); return pageResult; } + PageResult convertPage(PageResult page); SeckillActivityDetailRespVO convert1(SeckillActivityDO seckillActivity); - default SeckillActivityDetailRespVO convert(SeckillActivityDO seckillActivity, List seckillProducts) { - return convert1(seckillActivity).setProducts(convertList2(seckillProducts)); + default SeckillActivityDetailRespVO convert(SeckillActivityDO activity, + List products) { + return convert1(activity).setProducts(convertList2(products)); } - - // TODO @puhui999:参数改成 activity、product 会不会干净一点哈 - @Mappings({ - @Mapping(target = "id", ignore = true), - @Mapping(target = "activityId", source = "activityDO.id"), - @Mapping(target = "configIds", source = "activityDO.configIds"), - @Mapping(target = "spuId", source = "activityDO.spuId"), - @Mapping(target = "skuId", source = "vo.skuId"), - @Mapping(target = "seckillPrice", source = "vo.seckillPrice"), - @Mapping(target = "stock", source = "vo.stock"), - @Mapping(target = "activityStartTime", source = "activityDO.startTime"), - @Mapping(target = "activityEndTime", source = "activityDO.endTime") - }) - SeckillProductDO convert(SeckillActivityDO activityDO, SeckillProductBaseVO vo); + List convertList2(List productDOs); default List convertList(List products, SeckillActivityDO activityDO) { return CollectionUtils.convertList(products, item -> convert(activityDO, item).setActivityStatus(activityDO.getStatus())); } - - List convertList2(List productDOs); + @Mappings({ + @Mapping(target = "id", ignore = true), + @Mapping(target = "activityId", source = "activity.id"), + @Mapping(target = "configIds", source = "activity.configIds"), + @Mapping(target = "spuId", source = "activity.spuId"), + @Mapping(target = "skuId", source = "product.skuId"), + @Mapping(target = "seckillPrice", source = "product.seckillPrice"), + @Mapping(target = "stock", source = "product.stock"), + @Mapping(target = "activityStartTime", source = "activity.startTime"), + @Mapping(target = "activityEndTime", source = "activity.endTime") + }) + SeckillProductDO convert(SeckillActivityDO activity, SeckillProductBaseVO product); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java index 625769901..f11bd1aba 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillProductMapper.java @@ -1,9 +1,7 @@ package cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity; -import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckillactivity.SeckillProductDO; -import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import org.apache.ibatis.annotations.Mapper; import java.util.Collection; @@ -25,15 +23,4 @@ public interface SeckillProductMapper extends BaseMapperX { return selectList(SeckillProductDO::getActivityId, ids); } - default List selectListBySkuIds(Collection skuIds) { - return selectList(SeckillProductDO::getSkuId, skuIds); - } - - default void updateTimeIdsByActivityId(Long id, List timeIds) { - new LambdaUpdateChainWrapper<>(this) - .set(SeckillProductDO::getConfigIds, CollUtil.join(timeIds, ",")) - .eq(SeckillProductDO::getActivityId, id) - .update(); - } - } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityService.java index 4096f0087..5ba873577 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityService.java @@ -55,14 +55,6 @@ public interface SeckillActivityService { */ SeckillActivityDO getSeckillActivity(Long id); - /** - * 获得秒杀活动列表 - * - * @param ids 编号 - * @return 秒杀活动列表 - */ - List getSeckillActivityList(Collection ids); - /** * 获得秒杀活动分页 * @@ -74,17 +66,17 @@ public interface SeckillActivityService { /** * 通过活动编号获取活动商品 * - * @param id 活动编号 + * @param activityId 活动编号 * @return 活动商品列表 */ - List getSeckillProductListByActivityId(Long id); + List getSeckillProductListByActivityId(Long activityId); /** * 通过活动编号获取活动商品 * - * @param ids 活动编号 + * @param activityIds 活动编号 * @return 活动商品列表 */ - List getSeckillProductListByActivityId(Collection ids); + List getSeckillProductListByActivityId(Collection activityIds); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityServiceImpl.java index 72f985656..7c700b9a7 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityServiceImpl.java @@ -1,6 +1,5 @@ package cn.iocoder.yudao.module.promotion.service.seckill.seckillactivity; -import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; @@ -11,8 +10,7 @@ import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; 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; -import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.SeckillProductCreateReqVO; -import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.SeckillProductUpdateReqVO; +import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.product.SeckillProductBaseVO; import cn.iocoder.yudao.module.promotion.convert.seckill.seckillactivity.SeckillActivityConvert; import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckillactivity.SeckillActivityDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckillactivity.SeckillProductDO; @@ -27,7 +25,7 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.Collection; import java.util.List; -import java.util.Set; +import java.util.Map; import static cn.hutool.core.collection.CollUtil.isNotEmpty; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -35,7 +33,7 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS; import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SPU_NOT_EXISTS; import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; -import static cn.iocoder.yudao.module.promotion.util.PromotionUtils.validateProductSkuAllExists; +import static java.util.Collections.singletonList; /** * 秒杀活动 Service 实现类 @@ -60,19 +58,15 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { @Override @Transactional(rollbackFor = Exception.class) public Long createSeckillActivity(SeckillActivityCreateReqVO createReqVO) { - // 校验商品秒秒杀时段是否冲突 - validateProductSpuSeckillConflict(createReqVO.getConfigIds(), createReqVO.getSpuId(), null); - // 获取所选 spu 下的所有 sku - List skus = productSkuApi.getSkuListBySpuId(CollUtil.newArrayList(createReqVO.getSpuId())); - // 校验商品 sku 是否存在 - if (skus.size() != createReqVO.getProducts().size()) { - throw exception(SKU_NOT_EXISTS); - } + // 校验商品秒杀时段是否冲突 + validateProductConflict(createReqVO.getConfigIds(), createReqVO.getSpuId(), null); + // 校验商品是否存在 + validateProductExists(createReqVO.getSpuId(), createReqVO.getProducts()); // 插入秒杀活动 SeckillActivityDO activity = SeckillActivityConvert.INSTANCE.convert(createReqVO) .setStatus(PromotionUtils.calculateActivityStatus(createReqVO.getEndTime())) - .setTotalStock(getSumValue(createReqVO.getProducts(), SeckillProductCreateReqVO::getStock, Integer::sum)); + .setTotalStock(getSumValue(createReqVO.getProducts(), SeckillProductBaseVO::getStock, Integer::sum)); seckillActivityMapper.insert(activity); // 插入商品 List products = SeckillActivityConvert.INSTANCE.convertList(createReqVO.getProducts(), activity); @@ -80,35 +74,62 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { return activity.getId(); } - private void validateProductSpuSeckillConflict(List configIds, Long spuId, Long activityId) { - // 校验秒杀时段是否存在 + /** + * 校验秒杀商品参与的活动是否存在冲突 + * + * 1. 校验秒杀时段是否存在 + * 2. 校验商品 spu 是否存在 + * 3. 秒杀商品是否参加其它活动 + * + * @param configIds 秒杀时段数组 + * @param spuId 商品 SPU 编号 + * @param activityId 秒杀活动编号 + */ + private void validateProductConflict(List configIds, Long spuId, Long activityId) { + // 1. 校验秒杀时段是否存在 seckillConfigService.validateSeckillConfigExists(configIds); - // 校验商品 spu 是否存在 - List spuList = productSpuApi.getSpuList(CollUtil.newArrayList(spuId)); - if (CollUtil.isEmpty(spuList)) { - throw exception(SPU_NOT_EXISTS); + + // 2.1 查询所有开启的秒杀活动 + List activityList = seckillActivityMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus()); + if (activityId != null) { // 排除自己 + activityList.removeIf(item -> ObjectUtil.equal(item.getId(), activityId)); } - // 查询所有开启的秒杀活动 - List activityDOs = seckillActivityMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus()); - if (activityId != null) { - // 更新时移除本活动 - activityDOs.removeIf(item -> ObjectUtil.equal(item.getId(), activityId)); - } - // 过滤出所有 spuId 有交集的活动 - List activityDOs1 = convertList(activityDOs, c -> c, s -> ObjectUtil.equal(s.getSpuId(), spuId)); + // TODO @puhui999:一个 spu,参与两个活动应该没关系,关键是活动时间不充能重叠; + // 2.2 过滤出所有 spuId 有交集的活动,判断是否存在重叠 + List activityDOs1 = filterList(activityList, s -> ObjectUtil.equal(s.getSpuId(), spuId)); if (isNotEmpty(activityDOs1)) { throw exception(SECKILL_ACTIVITY_SPU_CONFLICTS); } - List activityDOs2 = convertList(activityDOs, c -> c, s -> { - // 判断秒杀时段是否有交集 - return containsAny(s.getConfigIds(), configIds); - }); - + // 2.3 过滤出所有 configIds 有交集的活动,判断是否存在重叠 + List activityDOs2 = filterList(activityList, s -> containsAny(s.getConfigIds(), configIds)); if (isNotEmpty(activityDOs2)) { - throw exception(SECKILL_TIME_CONFLICTS); + throw exception(SECKILL_ACTIVITY_SPU_CONFLICTS); } } + /** + * 校验秒杀商品是否都存在 + * + * @param spuId 商品 SPU 编号 + * @param products 秒杀商品 + */ + private void validateProductExists(Long spuId, List products) { + // 1. 校验商品 spu 是否存在 + ProductSpuRespDTO spu = productSpuApi.getSpu(spuId); + if (spu == null) { + throw exception(SPU_NOT_EXISTS); + } + + // 2. 校验商品 sku 都存在 + Map skuMap = convertMap(productSkuApi.getSkuListBySpuId(singletonList(spuId)), + ProductSkuRespDTO::getId); + products.forEach(product -> { + if (!skuMap.containsKey(product.getSkuId())) { + throw exception(SKU_NOT_EXISTS); + } + }); + } + @Override @Transactional(rollbackFor = Exception.class) public void updateSeckillActivity(SeckillActivityUpdateReqVO updateReqVO) { @@ -118,29 +139,26 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { throw exception(SECKILL_ACTIVITY_UPDATE_FAIL_STATUS_CLOSED); } // 校验商品是否冲突 - validateProductSpuSeckillConflict(updateReqVO.getConfigIds(), updateReqVO.getSpuId(), updateReqVO.getId()); - // 获取所选 spu下的所有 sku - List skus = productSkuApi.getSkuListBySpuId(CollUtil.newArrayList(updateReqVO.getSpuId())); - // 校验商品 sku 是否存在 - validateProductSkuAllExists(skus, updateReqVO.getProducts(), SeckillProductUpdateReqVO::getSkuId); + validateProductConflict(updateReqVO.getConfigIds(), updateReqVO.getSpuId(), updateReqVO.getId()); + // 校验商品是否存在 + validateProductExists(updateReqVO.getSpuId(), updateReqVO.getProducts()); // 更新活动 SeckillActivityDO updateObj = SeckillActivityConvert.INSTANCE.convert(updateReqVO) .setStatus(PromotionUtils.calculateActivityStatus(updateReqVO.getEndTime())) - .setTotalStock(getSumValue(updateReqVO.getProducts(), SeckillProductUpdateReqVO::getStock, Integer::sum)); + .setTotalStock(getSumValue(updateReqVO.getProducts(), SeckillProductBaseVO::getStock, Integer::sum)); seckillActivityMapper.updateById(updateObj); // 更新商品 updateSeckillProduct(updateObj, updateReqVO.getProducts()); } - /** * 更新秒杀商品 * * @param activity 秒杀活动 * @param products 该活动的最新商品配置 */ - private void updateSeckillProduct(SeckillActivityDO activity, List products) { + private void updateSeckillProduct(SeckillActivityDO activity, List products) { // 第一步,对比新老数据,获得添加、修改、删除的列表 List newList = SeckillActivityConvert.INSTANCE.convertList(products, activity); List oldList = seckillProductMapper.selectListByActivityId(activity.getId()); @@ -159,7 +177,6 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { if (isNotEmpty(diffList.get(1))) { seckillProductMapper.updateBatch(diffList.get(1)); } - // delete if (isNotEmpty(diffList.get(2))) { seckillProductMapper.deleteBatchIds(convertList(diffList.get(2), SeckillProductDO::getId)); } @@ -167,7 +184,6 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { @Override public void closeSeckillActivity(Long id) { - // TODO 待验证没使用过 // 校验存在 SeckillActivityDO activity = validateSeckillActivityExists(id); if (CommonStatusEnum.DISABLE.getStatus().equals(activity.getStatus())) { @@ -191,9 +207,8 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { // 删除活动 seckillActivityMapper.deleteById(id); // 删除活动商品 - List productDOs = seckillProductMapper.selectListByActivityId(id); - Set convertSet = convertSet(productDOs, SeckillProductDO::getSkuId); - seckillProductMapper.deleteBatchIds(convertSet); + List products = seckillProductMapper.selectListByActivityId(id); + seckillProductMapper.deleteBatchIds(convertSet(products, SeckillProductDO::getId)); } private SeckillActivityDO validateSeckillActivityExists(Long id) { @@ -209,24 +224,19 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { return validateSeckillActivityExists(id); } - @Override - public List getSeckillActivityList(Collection ids) { - return seckillActivityMapper.selectBatchIds(ids); - } - @Override public PageResult getSeckillActivityPage(SeckillActivityPageReqVO pageReqVO) { return seckillActivityMapper.selectPage(pageReqVO); } @Override - public List getSeckillProductListByActivityId(Long id) { - return seckillProductMapper.selectListByActivityId(id); + public List getSeckillProductListByActivityId(Long activityId) { + return seckillProductMapper.selectListByActivityId(activityId); } @Override - public List getSeckillProductListByActivityId(Collection ids) { - return seckillProductMapper.selectListByActivityId(ids); + public List getSeckillProductListByActivityId(Collection activityIds) { + return seckillProductMapper.selectListByActivityId(activityIds); } } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigService.java index e8cbce199..eb45d8a44 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigService.java @@ -57,10 +57,9 @@ public interface SeckillConfigService { /** * 校验秒杀时段是否存在 * - * @param timeIds 秒杀时段id集合 + * @param ids 秒杀时段 id 集合 */ - void validateSeckillConfigExists(Collection timeIds); - + void validateSeckillConfigExists(Collection ids); /** * 获得秒杀时间段配置分页数据 @@ -85,4 +84,5 @@ public interface SeckillConfigService { * @param status 状态 */ void updateSeckillConfigStatus(Long id, Integer status); + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigServiceImpl.java index 537cb1e27..af0d4fd16 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillconfig/SeckillConfigServiceImpl.java @@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil; 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.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.config.SeckillConfigCreateReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.config.SeckillConfigPageReqVO; @@ -13,7 +12,6 @@ import cn.iocoder.yudao.module.promotion.convert.seckill.seckillconfig.SeckillCo import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckillconfig.SeckillConfigDO; import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillconfig.SeckillConfigMapper; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; @@ -37,7 +35,6 @@ public class SeckillConfigServiceImpl implements SeckillConfigService { private SeckillConfigMapper seckillConfigMapper; @Override - @Transactional(rollbackFor = Exception.class) public Long createSeckillConfig(SeckillConfigCreateReqVO createReqVO) { // 校验时间段是否冲突 validateSeckillConfigConflict(createReqVO.getStartTime(), createReqVO.getEndTime(), null); @@ -50,7 +47,6 @@ public class SeckillConfigServiceImpl implements SeckillConfigService { } @Override - @Transactional(rollbackFor = Exception.class) public void updateSeckillConfig(SeckillConfigUpdateReqVO updateReqVO) { // 校验存在 validateSeckillConfigExists(updateReqVO.getId()); @@ -62,7 +58,6 @@ public class SeckillConfigServiceImpl implements SeckillConfigService { seckillConfigMapper.updateById(updateObj); } - // TODO @puhui999: 这个要不合并到更新操作里? 不单独有个操作咧; fix: 更新状态不用那么多必须的参数,更新的时候需要校验时间段 @Override public void updateSeckillConfigStatus(Long id, Integer status) { // 校验秒杀时段是否存在 @@ -73,7 +68,6 @@ public class SeckillConfigServiceImpl implements SeckillConfigService { } @Override - @Transactional(rollbackFor = Exception.class) public void deleteSeckillConfig(Long id) { // 校验存在 validateSeckillConfigExists(id); @@ -84,35 +78,31 @@ public class SeckillConfigServiceImpl implements SeckillConfigService { private void validateSeckillConfigExists(Long id) { if (seckillConfigMapper.selectById(id) == null) { - throw exception(SECKILL_TIME_NOT_EXISTS); + throw exception(SECKILL_CONFIG_NOT_EXISTS); } } /** * 校验时间是否存在冲突 * - * @param startTime 开始时间 - * @param endTime 结束时间 + * @param startTimeStr 开始时间 + * @param endTimeStr 结束时间 */ - private void validateSeckillConfigConflict(String startTime, String endTime, Long seckillConfigId) { - LocalTime startTime1 = LocalTime.parse(startTime); - LocalTime endTime1 = LocalTime.parse(endTime); - // 查询出所有的时段配置 - List configDOs = seckillConfigMapper.selectList(); + private void validateSeckillConfigConflict(String startTimeStr, String endTimeStr, Long id) { + // 1. 查询出所有的时段配置 + LocalTime startTime = LocalTime.parse(startTimeStr); + LocalTime endTime = LocalTime.parse(endTimeStr); + List configs = seckillConfigMapper.selectList(); // 更新时排除自己 - if (seckillConfigId != null) { - configDOs.removeIf(item -> ObjectUtil.equal(item.getId(), seckillConfigId)); + if (id != null) { + configs.removeIf(item -> ObjectUtil.equal(item.getId(), id)); } - // 过滤出重叠的时段 ids - boolean hasConflict = configDOs.stream().anyMatch(config -> { - LocalTime startTime2 = LocalTime.parse(config.getStartTime()); - LocalTime endTime2 = LocalTime.parse(config.getEndTime()); - // 判断时间是否重叠 - return LocalDateTimeUtils.isOverlap(startTime1, endTime1, startTime2, endTime2); - }); + // 2. 判断是否有重叠的时间 + boolean hasConflict = configs.stream().anyMatch(config -> LocalDateTimeUtils.isOverlap(startTime, endTime, + LocalTime.parse(config.getStartTime()), LocalTime.parse(config.getEndTime()))); if (hasConflict) { - throw exception(SECKILL_TIME_CONFLICTS); + throw exception(SECKILL_CONFIG_TIME_CONFLICTS); } } @@ -128,22 +118,22 @@ public class SeckillConfigServiceImpl implements SeckillConfigService { } @Override - public void validateSeckillConfigExists(Collection configIds) { - if (CollUtil.isEmpty(configIds)) { - throw exception(SECKILL_TIME_NOT_EXISTS); + public void validateSeckillConfigExists(Collection ids) { + if (CollUtil.isEmpty(ids)) { + return; } - List configDOs = seckillConfigMapper.selectBatchIds(configIds); - if (CollUtil.isEmpty(configDOs)) { - throw exception(SECKILL_TIME_NOT_EXISTS); - } - // 过滤出关闭的时段 - List filterList = CollectionUtils.filterList(configDOs, item -> ObjectUtil.equal(item.getStatus(), CommonStatusEnum.DISABLE.getStatus())); - if (CollUtil.isNotEmpty(filterList)) { - throw exception(SECKILL_TIME_DISABLE); - } - if (configDOs.size() != configIds.size()) { - throw exception(SECKILL_TIME_NOT_EXISTS); + // 1. 如果有数量不匹配,说明有不存在的,则抛出 SECKILL_CONFIG_NOT_EXISTS 业务异常 + List configs = seckillConfigMapper.selectBatchIds(ids); + if (configs.size() != ids.size()) { + throw exception(SECKILL_CONFIG_NOT_EXISTS); } + + // 2. 如果存在关闭,则抛出 SECKILL_CONFIG_DISABLE 业务异常 + configs.forEach(config -> { + if (ObjectUtil.equal(config.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { + throw exception(SECKILL_CONFIG_DISABLE); + } + }); } @Override diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/seckillconfig/SeckillConfigServiceImplTest.java b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/seckillconfig/SeckillConfigServiceImplTest.java index d6d5e90df..85e7098e6 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/seckillconfig/SeckillConfigServiceImplTest.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/seckillconfig/SeckillConfigServiceImplTest.java @@ -19,7 +19,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; -import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.SECKILL_TIME_NOT_EXISTS; +import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.SECKILL_CONFIG_NOT_EXISTS; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -94,7 +94,7 @@ public class SeckillConfigServiceImplTest extends BaseDbUnitTest { SeckillConfigUpdateReqVO reqVO = randomPojo(SeckillConfigUpdateReqVO.class); // 调用, 并断言异常 - assertServiceException(() -> SeckillConfigService.updateSeckillConfig(reqVO), SECKILL_TIME_NOT_EXISTS); + assertServiceException(() -> SeckillConfigService.updateSeckillConfig(reqVO), SECKILL_CONFIG_NOT_EXISTS); } @Test @@ -117,7 +117,7 @@ public class SeckillConfigServiceImplTest extends BaseDbUnitTest { Long id = randomLongId(); // 调用, 并断言异常 - assertServiceException(() -> SeckillConfigService.deleteSeckillConfig(id), SECKILL_TIME_NOT_EXISTS); + assertServiceException(() -> SeckillConfigService.deleteSeckillConfig(id), SECKILL_CONFIG_NOT_EXISTS); } @Test