Commit 831983e31430b9807ca4a7f7a12d12bf9edda1ba
1 parent
e845872a
营销活动:重构 getCombinationActivityBySpuIdsAndStatus 方法
Showing
6 changed files
with
81 additions
and
71 deletions
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainActivityMapper.java
| ... | ... | @@ -6,14 +6,15 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 6 | 6 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 7 | 7 | import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.BargainActivityPageReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityDO; |
| 9 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 9 | 10 | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| 10 | 11 | import org.apache.ibatis.annotations.Mapper; |
| 11 | 12 | import org.apache.ibatis.annotations.Param; |
| 12 | -import org.apache.ibatis.annotations.Select; | |
| 13 | 13 | |
| 14 | 14 | import java.time.LocalDateTime; |
| 15 | 15 | import java.util.Collection; |
| 16 | 16 | import java.util.List; |
| 17 | +import java.util.Map; | |
| 17 | 18 | |
| 18 | 19 | /** |
| 19 | 20 | * 砍价活动 Mapper |
| ... | ... | @@ -86,30 +87,25 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> { |
| 86 | 87 | .last("LIMIT " + count)); |
| 87 | 88 | } |
| 88 | 89 | |
| 89 | - // TODO @puhui999:一个商品,在统一时间,不会参与多个活动;so 是不是不用 inner join 哈? | |
| 90 | - // PS:如果可以参与多个,其实可以这样写 select * from promotion_bargain_activity group by spu_id ORDER BY create_time DESC;通过 group 来过滤 | |
| 91 | - | |
| 92 | 90 | /** |
| 93 | - * 获取指定 spu 编号最近参加的活动,每个 spuId 只返回一条记录 | |
| 91 | + * 查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 | |
| 94 | 92 | * |
| 95 | 93 | * @param spuIds spu 编号 |
| 96 | 94 | * @param status 状态 |
| 97 | - * @return 砍价活动列表 | |
| 95 | + * @return 包含 spuId 和 activityId 的 map 对象列表 | |
| 98 | 96 | */ |
| 99 | - @Select("<script> " + "SELECT p1.* " + | |
| 100 | - "FROM promotion_bargain_activity p1 " + | |
| 101 | - "INNER JOIN ( " + | |
| 102 | - " SELECT spu_id, MAX(DISTINCT create_time) AS max_create_time " + | |
| 103 | - " FROM promotion_bargain_activity " + | |
| 104 | - " WHERE spu_id IN " + | |
| 105 | - "<foreach collection='spuIds' item='spuId' open='(' separator=',' close=')'>" + | |
| 106 | - " #{spuId}" + | |
| 107 | - "</foreach>" + | |
| 108 | - " GROUP BY spu_id " + | |
| 109 | - ") p2 " + | |
| 110 | - "ON p1.spu_id = p2.spu_id AND p1.create_time = p2.max_create_time AND p1.status = #{status} " + | |
| 111 | - "ORDER BY p1.create_time DESC;" + | |
| 112 | - " </script>") | |
| 113 | - List<BargainActivityDO> selectListBySpuIdsAndStatus(@Param("spuIds") Collection<Long> spuIds, @Param("status") Integer status); | |
| 97 | + default List<Map<String, Object>> selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(@Param("spuIds") Collection<Long> spuIds, @Param("status") Integer status) { | |
| 98 | + return selectMaps(new QueryWrapper<BargainActivityDO>() | |
| 99 | + .select("spu_id AS spuId, MAX(DISTINCT(id)) AS activityId") // 时间越大 id 也越大 直接用 id | |
| 100 | + .in("spu_id", spuIds) | |
| 101 | + .eq("status", status) | |
| 102 | + .groupBy("spu_id")); | |
| 103 | + } | |
| 104 | + | |
| 105 | + default List<BargainActivityDO> selectListByIds(Collection<Long> ids) { | |
| 106 | + return selectList(new LambdaQueryWrapperX<BargainActivityDO>() | |
| 107 | + .in(BargainActivityDO::getId, ids) | |
| 108 | + .orderByDesc(BargainActivityDO::getCreateTime)); | |
| 109 | + } | |
| 114 | 110 | |
| 115 | 111 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/combination/CombinationActivityMapper.java
| ... | ... | @@ -6,12 +6,13 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 6 | 6 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 7 | 7 | import cn.iocoder.yudao.module.promotion.controller.admin.combination.vo.activity.CombinationActivityPageReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO; |
| 9 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 9 | 10 | import org.apache.ibatis.annotations.Mapper; |
| 10 | 11 | import org.apache.ibatis.annotations.Param; |
| 11 | -import org.apache.ibatis.annotations.Select; | |
| 12 | 12 | |
| 13 | 13 | import java.util.Collection; |
| 14 | 14 | import java.util.List; |
| 15 | +import java.util.Map; | |
| 15 | 16 | |
| 16 | 17 | /** |
| 17 | 18 | * 拼团活动 Mapper |
| ... | ... | @@ -43,30 +44,24 @@ public interface CombinationActivityMapper extends BaseMapperX<CombinationActivi |
| 43 | 44 | .last("LIMIT " + count)); |
| 44 | 45 | } |
| 45 | 46 | |
| 46 | - // TODO @puhui999:类似 BargainActivityMapper | |
| 47 | - | |
| 48 | 47 | /** |
| 49 | - * 获取指定 spu 编号最近参加的活动,每个 spuId 只返回一条记录 | |
| 50 | - * | |
| 48 | + * 查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 | |
| 51 | 49 | * @param spuIds spu 编号 |
| 52 | 50 | * @param status 状态 |
| 53 | - * @return 拼团活动列表 | |
| 51 | + * @return 包含 spuId 和 activityId 的 map 对象列表 | |
| 54 | 52 | */ |
| 55 | - @Select("<script> " + "SELECT p1.* " + | |
| 56 | - "FROM promotion_combination_activity p1 " + | |
| 57 | - "INNER JOIN ( " + | |
| 58 | - " SELECT spu_id, MAX(DISTINCT create_time) AS max_create_time " + | |
| 59 | - " FROM promotion_combination_activity " + | |
| 60 | - " WHERE spu_id IN " + | |
| 61 | - "<foreach collection='spuIds' item='spuId' open='(' separator=',' close=')'>" + | |
| 62 | - " #{spuId}" + | |
| 63 | - "</foreach>" + | |
| 64 | - " GROUP BY spu_id " + | |
| 65 | - ") p2 " + | |
| 66 | - "ON p1.spu_id = p2.spu_id AND p1.create_time = p2.max_create_time AND p1.status = #{status} " + | |
| 67 | - "ORDER BY p1.create_time DESC;" + | |
| 68 | - " </script>") | |
| 69 | - List<CombinationActivityDO> selectListBySpuIdsAndStatus(@Param("spuIds") Collection<Long> spuIds, @Param("status") Integer status); | |
| 53 | + default List<Map<String, Object>> selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(@Param("spuIds") Collection<Long> spuIds, @Param("status") Integer status) { | |
| 54 | + return selectMaps(new QueryWrapper<CombinationActivityDO>() | |
| 55 | + .select("spu_id AS spuId, MAX(DISTINCT(id)) AS activityId") // 时间越大 id 也越大 直接用 id | |
| 56 | + .in("spu_id", spuIds) | |
| 57 | + .eq("status", status) | |
| 58 | + .groupBy("spu_id")); | |
| 59 | + } | |
| 70 | 60 | |
| 61 | + default List<CombinationActivityDO> selectListByIds(Collection<Long> ids) { | |
| 62 | + return selectList(new LambdaQueryWrapperX<CombinationActivityDO>() | |
| 63 | + .in(CombinationActivityDO::getId, ids) | |
| 64 | + .orderByDesc(CombinationActivityDO::getCreateTime)); | |
| 65 | + } | |
| 71 | 66 | |
| 72 | 67 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/seckill/seckillactivity/SeckillActivityMapper.java
| ... | ... | @@ -7,13 +7,14 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 7 | 7 | import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityPageReqVO; |
| 8 | 8 | import cn.iocoder.yudao.module.promotion.controller.app.seckill.vo.activity.AppSeckillActivityPageReqVO; |
| 9 | 9 | import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillActivityDO; |
| 10 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 10 | 11 | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| 11 | 12 | import org.apache.ibatis.annotations.Mapper; |
| 12 | 13 | import org.apache.ibatis.annotations.Param; |
| 13 | -import org.apache.ibatis.annotations.Select; | |
| 14 | 14 | |
| 15 | 15 | import java.util.Collection; |
| 16 | 16 | import java.util.List; |
| 17 | +import java.util.Map; | |
| 17 | 18 | |
| 18 | 19 | /** |
| 19 | 20 | * 秒杀活动 Mapper |
| ... | ... | @@ -59,29 +60,24 @@ public interface SeckillActivityMapper extends BaseMapperX<SeckillActivityDO> { |
| 59 | 60 | .apply(ObjectUtil.isNotNull(pageReqVO.getConfigId()), "FIND_IN_SET(" + pageReqVO.getConfigId() + ",config_ids) > 0")); |
| 60 | 61 | } |
| 61 | 62 | |
| 62 | - // TODO @puhui999:类似 BargainActivityMapper | |
| 63 | - | |
| 64 | 63 | /** |
| 65 | - * 获取指定 spu 编号最近参加的活动,每个 spuId 只返回一条记录 | |
| 66 | - * | |
| 64 | + * 查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 | |
| 67 | 65 | * @param spuIds spu 编号 |
| 68 | 66 | * @param status 状态 |
| 69 | - * @return 秒杀活动列表 | |
| 67 | + * @return 包含 spuId 和 activityId 的 map 对象列表 | |
| 70 | 68 | */ |
| 71 | - @Select("<script> " + "SELECT p1.* " + | |
| 72 | - "FROM promotion_seckill_activity p1 " + | |
| 73 | - "INNER JOIN ( " + | |
| 74 | - " SELECT spu_id, MAX(DISTINCT create_time) AS max_create_time " + | |
| 75 | - " FROM promotion_seckill_activity " + | |
| 76 | - " WHERE spu_id IN " + | |
| 77 | - "<foreach collection='spuIds' item='spuId' open='(' separator=',' close=')'>" + | |
| 78 | - " #{spuId}" + | |
| 79 | - "</foreach>" + | |
| 80 | - " GROUP BY spu_id " + | |
| 81 | - ") p2 " + | |
| 82 | - "ON p1.spu_id = p2.spu_id AND p1.create_time = p2.max_create_time AND p1.status = #{status} " + | |
| 83 | - "ORDER BY p1.create_time DESC;" + | |
| 84 | - " </script>") | |
| 85 | - List<SeckillActivityDO> selectListBySpuIdsAndStatus(@Param("spuIds") Collection<Long> spuIds, @Param("status") Integer status); | |
| 69 | + default List<Map<String, Object>> selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(@Param("spuIds") Collection<Long> spuIds, @Param("status") Integer status) { | |
| 70 | + return selectMaps(new QueryWrapper<SeckillActivityDO>() | |
| 71 | + .select("spu_id AS spuId, MAX(DISTINCT(id)) AS activityId") // 时间越大 id 也越大 直接用 id | |
| 72 | + .in("spu_id", spuIds) | |
| 73 | + .eq("status", status) | |
| 74 | + .groupBy("spu_id")); | |
| 75 | + } | |
| 76 | + | |
| 77 | + default List<SeckillActivityDO> selectListByIds(Collection<Long> ids) { | |
| 78 | + return selectList(new LambdaQueryWrapperX<SeckillActivityDO>() | |
| 79 | + .in(SeckillActivityDO::getId, ids) | |
| 80 | + .orderByDesc(SeckillActivityDO::getCreateTime)); | |
| 81 | + } | |
| 86 | 82 | |
| 87 | 83 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.service.bargain; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 4 | +import cn.hutool.core.map.MapUtil; | |
| 3 | 5 | import cn.hutool.core.util.ObjUtil; |
| 4 | 6 | import cn.hutool.core.util.ObjectUtil; |
| 5 | 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| ... | ... | @@ -20,12 +22,11 @@ import org.springframework.validation.annotation.Validated; |
| 20 | 22 | |
| 21 | 23 | import javax.annotation.Resource; |
| 22 | 24 | import java.time.LocalDateTime; |
| 23 | -import java.util.Collection; | |
| 24 | -import java.util.List; | |
| 25 | -import java.util.Set; | |
| 25 | +import java.util.*; | |
| 26 | 26 | |
| 27 | 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 28 | 28 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.anyMatch; |
| 29 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | |
| 29 | 30 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS; |
| 30 | 31 | import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; |
| 31 | 32 | |
| ... | ... | @@ -178,7 +179,13 @@ public class BargainActivityServiceImpl implements BargainActivityService { |
| 178 | 179 | |
| 179 | 180 | @Override |
| 180 | 181 | public List<BargainActivityDO> getBargainActivityBySpuIdsAndStatus(Collection<Long> spuIds, Integer status) { |
| 181 | - return bargainActivityMapper.selectListBySpuIdsAndStatus(spuIds, status); | |
| 182 | + // 1.查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 | |
| 183 | + List<Map<String, Object>> spuIdAndActivityIdMaps = bargainActivityMapper.selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(spuIds, status); | |
| 184 | + if (CollUtil.isEmpty(spuIdAndActivityIdMaps)) { | |
| 185 | + return Collections.emptyList(); | |
| 186 | + } | |
| 187 | + // 2.查询活动详情 | |
| 188 | + return bargainActivityMapper.selectListByIds(convertSet(spuIdAndActivityIdMaps, map -> MapUtil.getLong(map, "activityId"))); | |
| 182 | 189 | } |
| 183 | 190 | |
| 184 | 191 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationActivityServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.service.combination; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.map.MapUtil; | |
| 4 | 5 | import cn.hutool.core.util.ObjectUtil; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 6 | 7 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| ... | ... | @@ -25,12 +26,12 @@ import org.springframework.validation.annotation.Validated; |
| 25 | 26 | |
| 26 | 27 | import javax.annotation.Resource; |
| 27 | 28 | import java.util.Collection; |
| 29 | +import java.util.Collections; | |
| 28 | 30 | import java.util.List; |
| 29 | 31 | import java.util.Map; |
| 30 | 32 | |
| 31 | 33 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 32 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 33 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; | |
| 34 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; | |
| 34 | 35 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS; |
| 35 | 36 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SPU_NOT_EXISTS; |
| 36 | 37 | import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; |
| ... | ... | @@ -228,7 +229,13 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic |
| 228 | 229 | |
| 229 | 230 | @Override |
| 230 | 231 | public List<CombinationActivityDO> getCombinationActivityBySpuIdsAndStatus(Collection<Long> spuIds, Integer status) { |
| 231 | - return combinationActivityMapper.selectListBySpuIdsAndStatus(spuIds, status); | |
| 232 | + // 1.查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 | |
| 233 | + List<Map<String, Object>> spuIdAndActivityIdMaps = combinationActivityMapper.selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(spuIds, status); | |
| 234 | + if (CollUtil.isEmpty(spuIdAndActivityIdMaps)) { | |
| 235 | + return Collections.emptyList(); | |
| 236 | + } | |
| 237 | + // 2.查询活动详情 | |
| 238 | + return combinationActivityMapper.selectListByIds(convertSet(spuIdAndActivityIdMaps, map -> MapUtil.getLong(map, "activityId"))); | |
| 232 | 239 | } |
| 233 | 240 | |
| 234 | 241 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/SeckillActivityServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.service.seckill; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 3 | 4 | import cn.hutool.core.collection.CollectionUtil; |
| 5 | +import cn.hutool.core.map.MapUtil; | |
| 4 | 6 | import cn.hutool.core.util.ObjectUtil; |
| 5 | 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 6 | 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| ... | ... | @@ -27,6 +29,7 @@ import org.springframework.validation.annotation.Validated; |
| 27 | 29 | |
| 28 | 30 | import javax.annotation.Resource; |
| 29 | 31 | import java.util.Collection; |
| 32 | +import java.util.Collections; | |
| 30 | 33 | import java.util.List; |
| 31 | 34 | import java.util.Map; |
| 32 | 35 | |
| ... | ... | @@ -312,7 +315,13 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { |
| 312 | 315 | |
| 313 | 316 | @Override |
| 314 | 317 | public List<SeckillActivityDO> getSeckillActivityBySpuIdsAndStatus(Collection<Long> spuIds, Integer status) { |
| 315 | - return seckillActivityMapper.selectListBySpuIdsAndStatus(spuIds, status); | |
| 318 | + // 1.查询出指定 spuId 的 spu 参加的活动最接近现在的一条记录。多个的话,一个 spuId 对应一个最近的活动编号 | |
| 319 | + List<Map<String, Object>> spuIdAndActivityIdMaps = seckillActivityMapper.selectSpuIdAndActivityIdMapsBySpuIdsAndStatus(spuIds, status); | |
| 320 | + if (CollUtil.isEmpty(spuIdAndActivityIdMaps)) { | |
| 321 | + return Collections.emptyList(); | |
| 322 | + } | |
| 323 | + // 2.查询活动详情 | |
| 324 | + return seckillActivityMapper.selectListByIds(convertSet(spuIdAndActivityIdMaps, map -> MapUtil.getLong(map, "activityId"))); | |
| 316 | 325 | } |
| 317 | 326 | |
| 318 | 327 | } | ... | ... |