From 60e684c4556c7796032d14845d6db8b5971fcd34 Mon Sep 17 00:00:00 2001 From: puhui999 Date: Sun, 24 Dec 2023 00:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=90=A5=E9=94=80=E6=B4=BB=E5=8A=A8=EF=BC=9A?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=95=86=E5=93=81=E8=BF=91=E6=9C=9F=E5=8F=82?= =?UTF-8?q?=E4=B8=8E=E7=9A=84=E6=AF=8F=E4=B8=AA=E6=B4=BB=E5=8A=A8=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=BB=A1=E5=87=8F=E9=80=81=E5=92=8C=E9=99=90=E6=97=B6?= =?UTF-8?q?=E6=8A=98=E6=89=A3=E6=B4=BB=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/activity/AppActivityController.java | 147 +++++++++++++----- .../discount/DiscountActivityMapper.java | 18 ++- .../mysql/discount/DiscountProductMapper.java | 18 +++ .../mysql/reward/RewardActivityMapper.java | 31 ++++ .../discount/DiscountActivityService.java | 13 +- .../discount/DiscountActivityServiceImpl.java | 22 ++- .../service/reward/RewardActivityService.java | 13 +- .../reward/RewardActivityServiceImpl.java | 22 ++- 8 files changed, 238 insertions(+), 46 deletions(-) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/activity/AppActivityController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/activity/AppActivityController.java index 16233bef3..6c5cec14a 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/activity/AppActivityController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/activity/AppActivityController.java @@ -7,26 +7,33 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.module.promotion.controller.app.activity.vo.AppActivityRespVO; import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO; +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.dal.dataobject.reward.RewardActivityDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillActivityDO; +import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum; import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; import cn.iocoder.yudao.module.promotion.service.bargain.BargainActivityService; import cn.iocoder.yudao.module.promotion.service.combination.CombinationActivityService; +import cn.iocoder.yudao.module.promotion.service.discount.DiscountActivityService; +import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService; import cn.iocoder.yudao.module.promotion.service.seckill.SeckillActivityService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import jakarta.annotation.Resource; import java.time.LocalDateTime; import java.util.*; +import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; @Tag(name = "用户 APP - 营销活动") // 用于提供跨多个活动的 HTTP 接口 @RestController @@ -40,6 +47,10 @@ public class AppActivityController { private SeckillActivityService seckillActivityService; @Resource private BargainActivityService bargainActivityService; + @Resource + private DiscountActivityService discountActivityService; + @Resource + private RewardActivityService rewardActivityService; @GetMapping("/list-by-spu-id") @Operation(summary = "获得单个商品,近期参与的每个活动") @@ -64,45 +75,105 @@ public class AppActivityController { if (CollUtil.isEmpty(spuIds)) { return new ArrayList<>(); } + LocalDateTime now = LocalDateTime.now(); + // 获取开启的且开始的且没有结束的活动 List activityList = new ArrayList<>(); - - // 1. 拼团活动 - 获取开启的且开始的且没有结束的活动 - List combinationActivities = combinationActivityService.getCombinationActivityBySpuIdsAndStatusAndDateTimeLt( - spuIds, CommonStatusEnum.ENABLE.getStatus(), now); - if (CollUtil.isNotEmpty(combinationActivities)) { - combinationActivities.forEach(item -> { - activityList.add(new AppActivityRespVO().setId(item.getId()) - .setType(PromotionTypeEnum.COMBINATION_ACTIVITY.getType()).setName(item.getName()) - .setSpuId(item.getSpuId()).setStartTime(item.getStartTime()).setEndTime(item.getEndTime())); - }); - } - - // 2. 秒杀活动 - 获取开启的且开始的且没有结束的活动 - List seckillActivities = seckillActivityService.getSeckillActivityBySpuIdsAndStatusAndDateTimeLt( - spuIds, CommonStatusEnum.ENABLE.getStatus(), now); - if (CollUtil.isNotEmpty(seckillActivities)) { - seckillActivities.forEach(item -> { - activityList.add(new AppActivityRespVO().setId(item.getId()) - .setType(PromotionTypeEnum.SECKILL_ACTIVITY.getType()).setName(item.getName()) - .setSpuId(item.getSpuId()).setStartTime(item.getStartTime()).setEndTime(item.getEndTime())); - }); - } - - // 3. 砍价活动 - 获取开启的且开始的且没有结束的活动 - List bargainActivities = bargainActivityService.getBargainActivityBySpuIdsAndStatusAndDateTimeLt( - spuIds, CommonStatusEnum.ENABLE.getStatus(), now); - if (CollUtil.isNotEmpty(bargainActivities)) { - bargainActivities.forEach(item -> { - activityList.add(new AppActivityRespVO().setId(item.getId()) - .setType(PromotionTypeEnum.BARGAIN_ACTIVITY.getType()).setName(item.getName()) - .setSpuId(item.getSpuId()).setStartTime(item.getStartTime()).setEndTime(item.getEndTime())); - }); - } - - // TODO 芋艿:满减送活动 - // TODO 芋艿:限时折扣活动 + // 1. 拼团活动 + getCombinationActivities(spuIds, now, activityList); + // 2. 秒杀活动 + getSeckillActivities(spuIds, now, activityList); + // 3. 砍价活动 + getBargainActivities(spuIds, now, activityList); + // 4. 限时折扣活动 + getDiscountActivities(spuIds, now, activityList); + // 5. 满减送活动 + getRewardActivities(spuIds, now, activityList); return activityList; } + private void getCombinationActivities(Collection spuIds, LocalDateTime now, List activityList) { + List combinationActivities = combinationActivityService.getCombinationActivityBySpuIdsAndStatusAndDateTimeLt( + spuIds, CommonStatusEnum.ENABLE.getStatus(), now); + if (CollUtil.isEmpty(combinationActivities)) { + return; + } + + combinationActivities.forEach(item -> { + activityList.add(new AppActivityRespVO().setId(item.getId()) + .setType(PromotionTypeEnum.COMBINATION_ACTIVITY.getType()).setName(item.getName()) + .setSpuId(item.getSpuId()).setStartTime(item.getStartTime()).setEndTime(item.getEndTime())); + }); + } + + private void getSeckillActivities(Collection spuIds, LocalDateTime now, List activityList) { + List seckillActivities = seckillActivityService.getSeckillActivityBySpuIdsAndStatusAndDateTimeLt( + spuIds, CommonStatusEnum.ENABLE.getStatus(), now); + if (CollUtil.isEmpty(seckillActivities)) { + return; + } + + seckillActivities.forEach(item -> { + activityList.add(new AppActivityRespVO().setId(item.getId()) + .setType(PromotionTypeEnum.SECKILL_ACTIVITY.getType()).setName(item.getName()) + .setSpuId(item.getSpuId()).setStartTime(item.getStartTime()).setEndTime(item.getEndTime())); + }); + } + + private void getBargainActivities(Collection spuIds, LocalDateTime now, List activityList) { + List bargainActivities = bargainActivityService.getBargainActivityBySpuIdsAndStatusAndDateTimeLt( + spuIds, CommonStatusEnum.ENABLE.getStatus(), now); + if (CollUtil.isNotEmpty(bargainActivities)) { + return; + } + + bargainActivities.forEach(item -> { + activityList.add(new AppActivityRespVO().setId(item.getId()) + .setType(PromotionTypeEnum.BARGAIN_ACTIVITY.getType()).setName(item.getName()) + .setSpuId(item.getSpuId()).setStartTime(item.getStartTime()).setEndTime(item.getEndTime())); + }); + } + + private void getDiscountActivities(Collection spuIds, LocalDateTime now, List activityList) { + List discountActivities = discountActivityService.getDiscountActivityBySpuIdsAndStatusAndDateTimeLt( + spuIds, CommonStatusEnum.ENABLE.getStatus(), now); + if (CollUtil.isEmpty(discountActivities)) { + return; + } + + List products = discountActivityService.getDiscountProductsByActivityId( + convertSet(discountActivities, DiscountActivityDO::getId)); + Map productMap = convertMap(products, DiscountProductDO::getActivityId, DiscountProductDO::getSpuId); + discountActivities.forEach(item -> { + activityList.add(new AppActivityRespVO().setId(item.getId()) + .setType(PromotionTypeEnum.DISCOUNT_ACTIVITY.getType()).setName(item.getName()) + .setSpuId(productMap.get(item.getId())).setStartTime(item.getStartTime()).setEndTime(item.getEndTime())); + }); + } + + private void getRewardActivities(Collection spuIds, LocalDateTime now, List activityList) { + List rewardActivityList = rewardActivityService.getRewardActivityBySpuIdsAndStatusAndDateTimeLt( + spuIds, PromotionActivityStatusEnum.RUN.getStatus(), now); + if (CollUtil.isEmpty(rewardActivityList)) { + return; + } + + Map> spuIdAndActivityMap = spuIds.stream() + .collect(Collectors.toMap( + spuId -> spuId, + spuId -> rewardActivityList.stream() + .filter(activity -> activity.getProductSpuIds().contains(spuId)) + .max(Comparator.comparing(RewardActivityDO::getCreateTime)))); + for (Long supId : spuIdAndActivityMap.keySet()) { + if (spuIdAndActivityMap.get(supId).isEmpty()) { + continue; + } + + RewardActivityDO rewardActivityDO = spuIdAndActivityMap.get(supId).get(); + activityList.add(new AppActivityRespVO().setId(rewardActivityDO.getId()) + .setType(PromotionTypeEnum.REWARD_ACTIVITY.getType()).setName(rewardActivityDO.getName()) + .setSpuId(supId).setStartTime(rewardActivityDO.getStartTime()).setEndTime(rewardActivityDO.getEndTime())); + } + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountActivityMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountActivityMapper.java index 534ce627a..efd4e4d25 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountActivityMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountActivityMapper.java @@ -7,9 +7,9 @@ import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountAc import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO; import org.apache.ibatis.annotations.Mapper; +import java.time.LocalDateTime; import java.util.Collection; import java.util.List; -import java.util.Set; /** * 限时折扣活动 Mapper @@ -27,4 +27,20 @@ public interface DiscountActivityMapper extends BaseMapperX .orderByDesc(DiscountActivityDO::getId)); } + /** + * 获取指定活动编号的活动列表且 + * 开始时间和结束时间小于给定时间 dateTime 的活动列表 + * + * @param ids 活动编号 + * @param dateTime 指定日期 + * @return 活动列表 + */ + default List selectListByIdsAndDateTimeLt(Collection ids, LocalDateTime dateTime) { + return selectList(new LambdaQueryWrapperX() + .in(DiscountActivityDO::getId, ids) + .lt(DiscountActivityDO::getStartTime, dateTime) + .gt(DiscountActivityDO::getEndTime, dateTime)// 开始时间 < 指定时间 < 结束时间,也就是说获取指定时间段的活动 + .orderByDesc(DiscountActivityDO::getCreateTime)); + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountProductMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountProductMapper.java index 10df2ce3a..5257b836d 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountProductMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/discount/DiscountProductMapper.java @@ -2,11 +2,13 @@ package cn.iocoder.yudao.module.promotion.dal.mysql.discount; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.Collection; import java.util.List; +import java.util.Map; /** * 限时折扣商城 Mapper @@ -30,4 +32,20 @@ public interface DiscountProductMapper extends BaseMapperX { // TODO @zhangshuai:逻辑里,尽量避免写 join 语句哈,你可以看看这个查询,有什么办法优化?目前的一个思路,是分 2 次查询,性能也是 ok 的 List getMatchDiscountProductList(@Param("skuIds") Collection skuIds); + + /** + * 查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 + * + * @param spuIds spu 编号 + * @param status 状态 + * @return 包含 spuId 和 activityId 的 map 对象列表 + */ + default List> selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(Collection spuIds, Integer status) { + return selectMaps(new QueryWrapper() + .select("spu_id AS spuId, MAX(DISTINCT(activity_id)) AS activityId") + .in("spu_id", spuIds) + .eq("activity_status", status) + .groupBy("spu_id")); + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/reward/RewardActivityMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/reward/RewardActivityMapper.java index 2ee879823..ca9e9668f 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/reward/RewardActivityMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/reward/RewardActivityMapper.java @@ -1,14 +1,19 @@ package cn.iocoder.yudao.module.promotion.dal.mysql.reward; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO; import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.apache.ibatis.annotations.Mapper; +import java.time.LocalDateTime; import java.util.Collection; import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; /** * 满减送活动 Mapper @@ -35,4 +40,30 @@ public interface RewardActivityMapper extends BaseMapperX { .eq(RewardActivityDO::getStatus, status)); } + default List selectListBySpuIdsAndStatus(Collection spuIds, Integer status) { + Function, String> productScopeValuesFindInSetFunc = ids -> ids.stream() + .map(id -> StrUtil.format("FIND_IN_SET({}, product_spu_ids) ", id)) + .collect(Collectors.joining(" OR ")); + return selectList(new QueryWrapper() + .eq("status", status) + .apply(productScopeValuesFindInSetFunc.apply(spuIds))); + } + + /** + * 获取指定活动编号的活动列表且 + * 开始时间和结束时间小于给定时间 dateTime 的活动列表 + * + * @param ids 活动编号 + * @param dateTime 指定日期 + * @return 活动列表 + */ + default List selectListByIdsAndDateTimeLt(Collection ids, LocalDateTime dateTime) { + return selectList(new LambdaQueryWrapperX() + .in(RewardActivityDO::getId, ids) + .lt(RewardActivityDO::getStartTime, dateTime) + .gt(RewardActivityDO::getEndTime, dateTime)// 开始时间 < 指定时间 < 结束时间,也就是说获取指定时间段的活动 + .orderByDesc(RewardActivityDO::getCreateTime) + ); + } + } 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 05ad13eed..d754d0d9f 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 @@ -6,8 +6,9 @@ import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountAc import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityUpdateReqVO; import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO; - import jakarta.validation.Valid; + +import java.time.LocalDateTime; import java.util.Collection; import java.util.List; @@ -89,4 +90,14 @@ public interface DiscountActivityService { */ List getDiscountProductsByActivityId(Collection activityIds); + /** + * 获取指定 spu 编号最近参加的活动,每个 spuId 只返回一条记录 + * + * @param spuIds spu 编号 + * @param status 状态 + * @param dateTime 当前日期时间 + * @return 折扣活动列表 + */ + List getDiscountActivityBySpuIdsAndStatusAndDateTimeLt(Collection spuIds, Integer status, LocalDateTime dateTime); + } 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 b8b496fdb..f103cf553 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 @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.promotion.service.discount; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.map.MapUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityBaseVO; @@ -15,16 +16,20 @@ import cn.iocoder.yudao.module.promotion.dal.mysql.discount.DiscountActivityMapp 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.util.PromotionUtils; +import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; -import jakarta.annotation.Resource; +import java.time.LocalDateTime; import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; /** @@ -109,7 +114,7 @@ public class DiscountActivityServiceImpl implements DiscountActivityService { /** * 校验商品是否冲突 * - * @param id 编号 + * @param id 编号 * @param products 商品列表 */ private void validateDiscountActivityProductConflicts(Long id, List products) { @@ -184,4 +189,17 @@ public class DiscountActivityServiceImpl implements DiscountActivityService { return discountProductMapper.selectList("activity_id", activityIds); } + @Override + public List getDiscountActivityBySpuIdsAndStatusAndDateTimeLt(Collection spuIds, Integer status, LocalDateTime dateTime) { + // 1. 查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 + List> spuIdAndActivityIdMaps = discountProductMapper.selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(spuIds, status); + if (CollUtil.isEmpty(spuIdAndActivityIdMaps)) { + return Collections.emptyList(); + } + + // 2. 查询活动详情 + return discountActivityMapper.selectListByIdsAndDateTimeLt( + convertSet(spuIdAndActivityIdMaps, map -> MapUtil.getLong(map, "activityId")), dateTime); + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityService.java index 7176e980b..e2e225608 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityService.java @@ -6,8 +6,9 @@ import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivi import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO; import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityUpdateReqVO; import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO; - import jakarta.validation.Valid; + +import java.time.LocalDateTime; import java.util.Collection; import java.util.List; @@ -71,4 +72,14 @@ public interface RewardActivityService { */ List getMatchRewardActivityList(Collection spuIds); + /** + * 获取指定 spu 编号最近参加的活动,每个 spuId 只返回一条记录 + * + * @param spuIds spu 编号 + * @param status 状态 + * @param dateTime 当前日期时间 + * @return 满减送活动列表 + */ + List getRewardActivityBySpuIdsAndStatusAndDateTimeLt(Collection spuIds, Integer status, LocalDateTime dateTime); + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityServiceImpl.java index 723dda933..e896eab92 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityServiceImpl.java @@ -11,14 +11,17 @@ import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO; import cn.iocoder.yudao.module.promotion.dal.mysql.reward.RewardActivityMapper; import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum; import cn.iocoder.yudao.module.promotion.util.PromotionUtils; +import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; -import jakarta.annotation.Resource; +import java.time.LocalDateTime; import java.util.Collection; +import java.util.Collections; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; import static java.util.Arrays.asList; @@ -100,10 +103,11 @@ public class RewardActivityServiceImpl implements RewardActivityService { } // TODO @芋艿:逻辑有问题,需要优化;要分成全场、和指定来校验; + /** * 校验商品参加的活动是否冲突 * - * @param id 活动编号 + * @param id 活动编号 * @param spuIds 商品 SPU 编号数组 */ private void validateRewardActivitySpuConflicts(Long id, Collection spuIds) { @@ -125,7 +129,7 @@ public class RewardActivityServiceImpl implements RewardActivityService { /** * 获得商品参加的满减送活动的数组 * - * @param spuIds 商品 SPU 编号数组 + * @param spuIds 商品 SPU 编号数组 * @param statuses 活动状态数组 * @return 商品参加的满减送活动的数组 */ @@ -163,4 +167,16 @@ public class RewardActivityServiceImpl implements RewardActivityService { return null; } + @Override + public List getRewardActivityBySpuIdsAndStatusAndDateTimeLt(Collection spuIds, Integer status, LocalDateTime dateTime) { + // 1. 查询出指定 spuId 的 spu 参加的活动 + List rewardActivityList = rewardActivityMapper.selectListBySpuIdsAndStatus(spuIds, status); + if (CollUtil.isEmpty(rewardActivityList)) { + return Collections.emptyList(); + } + + // 2. 查询活动详情 + return rewardActivityMapper.selectListByIdsAndDateTimeLt(convertSet(rewardActivityList, RewardActivityDO::getId), dateTime); + } + }