From 941782fb1057c2d0ba8324af6dc2cebfad9099ea Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 6 Nov 2022 12:38:13 +0800 Subject: [PATCH] =?UTF-8?q?promotion=EF=BC=9A=E5=AE=8C=E5=96=84=E9=99=90?= =?UTF-8?q?=E6=97=B6=E6=8A=98=E6=89=A3=E7=9A=84=E4=BF=AE=E6=94=B9=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../discount/DiscountActivityController.java | 16 ++-- .../vo/DiscountActivityDetailRespVO.java | 21 +++++ .../discount/DiscountActivityConvert.java | 57 +++++++++++++- .../discount/DiscountActivityService.java | 9 +++ .../discount/DiscountActivityServiceImpl.java | 11 ++- .../DiscountActivityServiceImplTest.java | 21 +++-- .../src/test/resources/sql/create_tables.sql | 4 +- .../mall/promotion/discountActivity/index.vue | 77 +++++++++++-------- 8 files changed, 165 insertions(+), 51 deletions(-) create mode 100755 yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/vo/DiscountActivityDetailRespVO.java diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/DiscountActivityController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/DiscountActivityController.java index 5010a3124..332a1b822 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/DiscountActivityController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/DiscountActivityController.java @@ -2,12 +2,10 @@ package cn.iocoder.yudao.module.promotion.controller.admin.discount; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityCreateReqVO; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityPageReqVO; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityRespVO; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityUpdateReqVO; +import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.*; import cn.iocoder.yudao.module.promotion.convert.discount.DiscountActivityConvert; import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO; +import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO; import cn.iocoder.yudao.module.promotion.service.discount.DiscountActivityService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -18,6 +16,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; +import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -67,9 +66,14 @@ public class DiscountActivityController { @ApiOperation("获得限时折扣活动") @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) @PreAuthorize("@ss.hasPermission('promotion:discount-activity:query')") - public CommonResult getDiscountActivity(@RequestParam("id") Long id) { + public CommonResult getDiscountActivity(@RequestParam("id") Long id) { DiscountActivityDO discountActivity = discountActivityService.getDiscountActivity(id); - return success(DiscountActivityConvert.INSTANCE.convert(discountActivity)); + if (discountActivity == null) { + return success(null); + } + // 拼接结果 + List discountProducts = discountActivityService.getDiscountProductsByActivityId(id); + return success(DiscountActivityConvert.INSTANCE.convert(discountActivity, discountProducts)); } @GetMapping("/page") diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/vo/DiscountActivityDetailRespVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/vo/DiscountActivityDetailRespVO.java new file mode 100755 index 000000000..1dfdeec18 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/discount/vo/DiscountActivityDetailRespVO.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.module.promotion.controller.admin.discount.vo; + +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +import java.util.List; + +@ApiModel("管理后台 - 限时折扣活动的详细 Response VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class DiscountActivityDetailRespVO extends DiscountActivityRespVO { + + /** + * 商品列表 + */ + private List products; + +} diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/discount/DiscountActivityConvert.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/discount/DiscountActivityConvert.java index 6708199e0..07d2e03ab 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/discount/DiscountActivityConvert.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/discount/DiscountActivityConvert.java @@ -1,14 +1,13 @@ package cn.iocoder.yudao.module.promotion.convert.discount; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.MapUtils; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityBaseVO; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityCreateReqVO; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityRespVO; -import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityUpdateReqVO; +import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.*; import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO; +import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum; import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -50,4 +49,54 @@ public interface DiscountActivityConvert { DiscountProductDO convert(DiscountActivityBaseVO.Product bean); + DiscountActivityDetailRespVO convert(DiscountActivityDO activity, List products); + + // =========== 比较是否相等 ========== + /** + * 比较两个限时折扣商品是否相等 + * + * @param productDO 数据库中的商品 + * @param productVO 前端传入的商品 + * @return 是否匹配 + */ + @SuppressWarnings("DuplicatedCode") + default boolean isEquals(DiscountProductDO productDO, DiscountActivityBaseVO.Product productVO) { + if (ObjectUtil.notEqual(productDO.getSpuId(), productVO.getSpuId()) + || ObjectUtil.notEqual(productDO.getSkuId(), productVO.getSkuId()) + || ObjectUtil.notEqual(productDO.getDiscountType(), productVO.getDiscountType())) { + return false; + } + if (productDO.getDiscountType().equals(PromotionDiscountTypeEnum.PRICE.getType())) { + return ObjectUtil.equal(productDO.getDiscountPrice(), productVO.getDiscountPrice()); + } + if (productDO.getDiscountType().equals(PromotionDiscountTypeEnum.PERCENT.getType())) { + return ObjectUtil.equal(productDO.getDiscountPercent(), productVO.getDiscountPercent()); + } + return true; + } + + /** + * 比较两个限时折扣商品是否相等 + * 注意,比较时忽略 id 编号 + * + * @param productDO 商品 1 + * @param productVO 商品 2 + * @return 是否匹配 + */ + @SuppressWarnings("DuplicatedCode") + default boolean isEquals(DiscountProductDO productDO, DiscountProductDO productVO) { + if (ObjectUtil.notEqual(productDO.getSpuId(), productVO.getSpuId()) + || ObjectUtil.notEqual(productDO.getSkuId(), productVO.getSkuId()) + || ObjectUtil.notEqual(productDO.getDiscountType(), productVO.getDiscountType())) { + return false; + } + if (productDO.getDiscountType().equals(PromotionDiscountTypeEnum.PRICE.getType())) { + return ObjectUtil.equal(productDO.getDiscountPrice(), productVO.getDiscountPrice()); + } + if (productDO.getDiscountType().equals(PromotionDiscountTypeEnum.PERCENT.getType())) { + return ObjectUtil.equal(productDO.getDiscountPercent(), productVO.getDiscountPercent()); + } + return true; + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityService.java index ac8d9ac17..ff90943dd 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityService.java @@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProduct import javax.validation.Valid; import java.util.Collection; +import java.util.List; import java.util.Map; /** @@ -73,4 +74,12 @@ public interface DiscountActivityService { */ PageResult getDiscountActivityPage(DiscountActivityPageReqVO pageReqVO); + /** + * 获得活动编号,对应对应的商品列表 + * + * @param activityId 活动编号 + * @return 活动的商品列表 + */ + List getDiscountProductsByActivityId(Long activityId); + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImpl.java index 5504ac25d..cba0381a3 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImpl.java @@ -91,8 +91,7 @@ public class DiscountActivityServiceImpl implements DiscountActivityService { // 计算要删除的记录 List deleteIds = convertList(dbDiscountProducts, DiscountProductDO::getId, discountProductDO -> updateReqVO.getProducts().stream() - .noneMatch(product -> product.getSkuId().equals(discountProductDO.getSkuId()) - && product.getDiscountPrice().equals(discountProductDO.getDiscountPrice()))); + .noneMatch(product -> DiscountActivityConvert.INSTANCE.isEquals(discountProductDO, product))); if (CollUtil.isNotEmpty(deleteIds)) { discountProductMapper.deleteBatchIds(deleteIds); } @@ -100,8 +99,7 @@ public class DiscountActivityServiceImpl implements DiscountActivityService { List newDiscountProducts = convertList(updateReqVO.getProducts(), product -> DiscountActivityConvert.INSTANCE.convert(product).setActivityId(updateReqVO.getId())); newDiscountProducts.removeIf(product -> dbDiscountProducts.stream().anyMatch( - dbProduct -> dbProduct.getSkuId().equals(product.getSkuId()) - && dbProduct.getDiscountPrice().equals(product.getDiscountPrice()))); // 如果匹配到,说明是更新的 + dbProduct -> DiscountActivityConvert.INSTANCE.isEquals(dbProduct, product))); // 如果匹配到,说明是更新的 if (CollectionUtil.isNotEmpty(newDiscountProducts)) { discountProductMapper.insertBatch(newDiscountProducts); } @@ -194,4 +192,9 @@ public class DiscountActivityServiceImpl implements DiscountActivityService { return discountActivityMapper.selectPage(pageReqVO); } + @Override + public List getDiscountProductsByActivityId(Long activityId) { + return discountProductMapper.selectListByActivityId(activityId); + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImplTest.java b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImplTest.java index 6d482bde0..c21e622c2 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImplTest.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/discount/DiscountActivityServiceImplTest.java @@ -11,6 +11,7 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProduct import cn.iocoder.yudao.module.promotion.dal.mysql.discount.DiscountActivityMapper; import cn.iocoder.yudao.module.promotion.dal.mysql.discount.DiscountProductMapper; import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum; +import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.Import; @@ -53,8 +54,10 @@ public class DiscountActivityServiceImplTest extends BaseDbUnitTest { // 用于触发进行中的状态 o.setStartTime(addTime(Duration.ofDays(1))).setEndTime(addTime(Duration.ofDays(2))); // 设置商品 - o.setProducts(asList(new DiscountActivityBaseVO.Product().setSpuId(1L).setSkuId(2L).setDiscountPrice(3), - new DiscountActivityBaseVO.Product().setSpuId(10L).setSkuId(20L).setDiscountPrice(30))); + o.setProducts(asList(new DiscountActivityBaseVO.Product().setSpuId(1L).setSkuId(2L) + .setDiscountType(PromotionDiscountTypeEnum.PRICE.getType()).setDiscountPrice(3), + new DiscountActivityBaseVO.Product().setSpuId(10L).setSkuId(20L) + .setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()).setDiscountPercent(30))); }); // 调用 @@ -74,7 +77,9 @@ public class DiscountActivityServiceImplTest extends BaseDbUnitTest { assertEquals(discountProduct.getActivityId(), discountActivity.getId()); assertEquals(discountProduct.getSpuId(), product.getSpuId()); assertEquals(discountProduct.getSkuId(), product.getSkuId()); + assertEquals(discountProduct.getDiscountType(), product.getDiscountType()); assertEquals(discountProduct.getDiscountPrice(), product.getDiscountPrice()); + assertEquals(discountProduct.getDiscountPercent(), product.getDiscountPercent()); } } @@ -85,9 +90,9 @@ public class DiscountActivityServiceImplTest extends BaseDbUnitTest { discountActivityMapper.insert(dbDiscountActivity);// @Sql: 先插入出一条存在的数据 // mock 数据(活动) DiscountProductDO dbDiscountProduct01 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(dbDiscountActivity.getId()) - .setSpuId(1L).setSkuId(2L)); + .setSpuId(1L).setSkuId(2L).setDiscountType(PromotionDiscountTypeEnum.PRICE.getType()).setDiscountPrice(3).setDiscountPercent(null)); DiscountProductDO dbDiscountProduct02 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(dbDiscountActivity.getId()) - .setSpuId(10L).setSkuId(20L)); + .setSpuId(10L).setSkuId(20L).setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()).setDiscountPercent(30).setDiscountPrice(null)); discountProductMapper.insert(dbDiscountProduct01); discountProductMapper.insert(dbDiscountProduct02); // 准备参数 @@ -96,8 +101,10 @@ public class DiscountActivityServiceImplTest extends BaseDbUnitTest { // 用于触发进行中的状态 o.setStartTime(addTime(Duration.ofDays(1))).setEndTime(addTime(Duration.ofDays(2))); // 设置商品 - o.setProducts(asList(new DiscountActivityBaseVO.Product().setSpuId(1L).setSkuId(2L).setDiscountPrice(3), - new DiscountActivityBaseVO.Product().setSpuId(100L).setSkuId(200L).setDiscountPrice(30))); + o.setProducts(asList(new DiscountActivityBaseVO.Product().setSpuId(1L).setSkuId(2L) + .setDiscountType(PromotionDiscountTypeEnum.PRICE.getType()).setDiscountPrice(3).setDiscountPercent(null), + new DiscountActivityBaseVO.Product().setSpuId(100L).setSkuId(200L) + .setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()).setDiscountPercent(30).setDiscountPrice(null))); }); // 调用 @@ -115,7 +122,9 @@ public class DiscountActivityServiceImplTest extends BaseDbUnitTest { assertEquals(discountProduct.getActivityId(), discountActivity.getId()); assertEquals(discountProduct.getSpuId(), product.getSpuId()); assertEquals(discountProduct.getSkuId(), product.getSkuId()); + assertEquals(discountProduct.getDiscountType(), product.getDiscountType()); assertEquals(discountProduct.getDiscountPrice(), product.getDiscountPrice()); + assertEquals(discountProduct.getDiscountPercent(), product.getDiscountPercent()); } } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql b/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql index f7a9db229..7ff1a7239 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql +++ b/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql @@ -112,7 +112,9 @@ CREATE TABLE IF NOT EXISTS "promotion_discount_product" ( "activity_id" bigint NOT NULL, "spu_id" bigint NOT NULL, "sku_id" bigint NOT NULL, - "discount_price" int NOT NULL, + "discount_type" int NOT NULL, + "discount_percent" int, + "discount_price" int, "creator" varchar DEFAULT '', "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, "updater" varchar DEFAULT '', diff --git a/yudao-ui-admin/src/views/mall/promotion/discountActivity/index.vue b/yudao-ui-admin/src/views/mall/promotion/discountActivity/index.vue index b0998ae9a..267dbf093 100755 --- a/yudao-ui-admin/src/views/mall/promotion/discountActivity/index.vue +++ b/yudao-ui-admin/src/views/mall/promotion/discountActivity/index.vue @@ -84,8 +84,8 @@ - + multiple filterable style="width: 880px" @change="changeFormSku"> + {{ item.spuName }}   {{ item.name}} ¥{{ (item.price / 100.0).toFixed(2) }} @@ -102,7 +102,7 @@ - +