Commit 81721226 by 章家宝

feat:优惠券活动筛选

1 parent ea330f04
...@@ -150,6 +150,8 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity, ...@@ -150,6 +150,8 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity,
* 活动分页统计查询(小驼峰·纯数字·支持条件+排序+新增字段) * 活动分页统计查询(小驼峰·纯数字·支持条件+排序+新增字段)
* 使用 JOIN 替代子查询,符合公司规范 * 使用 JOIN 替代子查询,符合公司规范
* 统计时排除已退回的优惠券(coupon_account.status = 4) * 统计时排除已退回的优惠券(coupon_account.status = 4)
*
* @param activityIdsByTemplate 根据模板ID反查出的活动ID列表,传空列表表示不过滤
*/ */
@Query(value = """ @Query(value = """
SELECT SELECT
...@@ -183,6 +185,7 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity, ...@@ -183,6 +185,7 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity,
AND (:activityType IS NULL OR ca.activity_type = :activityType) AND (:activityType IS NULL OR ca.activity_type = :activityType)
AND (:startTime IS NULL OR ca.valid_end_time >= :startTime) AND (:startTime IS NULL OR ca.valid_end_time >= :startTime)
AND (:endTime IS NULL OR ca.valid_start_time <= :endTime) AND (:endTime IS NULL OR ca.valid_start_time <= :endTime)
AND ('__ALL__' IN :activityIdsByTemplate OR ca.activity_id IN :activityIdsByTemplate)
GROUP BY ca.id, ca.activity_id, ca.activity_name, ca.valid_start_time, ca.valid_end_time, GROUP BY ca.id, ca.activity_id, ca.activity_name, ca.valid_start_time, ca.valid_end_time,
ca.activity_type, ca.target_type, ca.distribution_quantity, ca.status, ca.activity_type, ca.target_type, ca.distribution_quantity, ca.status,
ca.create_time, ca.send_notification, ca.remark, ca.update_time, ca.template_id ca.create_time, ca.send_notification, ca.remark, ca.update_time, ca.template_id
...@@ -211,6 +214,7 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity, ...@@ -211,6 +214,7 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity,
AND (:activityType IS NULL OR ca.activity_type = :activityType) AND (:activityType IS NULL OR ca.activity_type = :activityType)
AND (:startTime IS NULL OR ca.valid_end_time >= :startTime) AND (:startTime IS NULL OR ca.valid_end_time >= :startTime)
AND (:endTime IS NULL OR ca.valid_start_time <= :endTime) AND (:endTime IS NULL OR ca.valid_start_time <= :endTime)
AND ('__ALL__' IN :activityIdsByTemplate OR ca.activity_id IN :activityIdsByTemplate)
""", """,
nativeQuery = true) nativeQuery = true)
Page<CouponActivityStatDTO> pageActivityStat( Page<CouponActivityStatDTO> pageActivityStat(
...@@ -221,6 +225,7 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity, ...@@ -221,6 +225,7 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity,
@Param("endTime") LocalDateTime endTime, @Param("endTime") LocalDateTime endTime,
@Param("sortField") String sortField, @Param("sortField") String sortField,
@Param("sortOrder") String sortOrder, @Param("sortOrder") String sortOrder,
@Param("activityIdsByTemplate") List<String> activityIdsByTemplate,
Pageable pageable Pageable pageable
); );
......
...@@ -481,6 +481,31 @@ public class CouponActivityService { ...@@ -481,6 +481,31 @@ public class CouponActivityService {
LocalDateTime startTime = parsedRange[0]; LocalDateTime startTime = parsedRange[0];
LocalDateTime endTime = parsedRange[1]; LocalDateTime endTime = parsedRange[1];
// 提取优惠券模板ID参数,并反查关联的活动ID列表
String couponTemplateId = (String)pagination.getFilterValueOrElseNull("couponTemplateId");
// 根据模板ID反查活动ID列表
List<String> activityIdsByTemplate = Collections.singletonList("__ALL__"); // 默认不过滤
if (couponTemplateId != null) {
Optional<CouponTemplate> template = couponTemplateRepository.findByTemplateIdAndDeleteFlag(couponTemplateId, DeleteFlag.NOT_DELETED.getCode());
if (template.isEmpty()) {
return Page.empty();
}
Long templateId = template.get().getId();
Set<String> activityIdSet = new HashSet<>();
// 从活动阶梯表查询(自动触发类型活动)
List<CouponActivityPriceRange> priceRanges = priceRangeRepository.findByTemplateIdAndDeleteFlag(
templateId, DeleteFlag.NOT_DELETED.getCode());
priceRanges.forEach(pr -> activityIdSet.add(pr.getActivityId()));
// 从活动表查询(直接发放类型活动)
List<CouponActivity> directActivities = couponActivityRepository.findByTemplateIdAndDeleteFlag(
templateId, DeleteFlag.NOT_DELETED.getCode());
directActivities.forEach(a -> activityIdSet.add(a.getActivityId()));
if (activityIdSet.isEmpty()) {
return Page.empty();
}
activityIdsByTemplate = new ArrayList<>(activityIdSet);
}
// 2. 排序参数 // 2. 排序参数
String sortField = pagination.getSort() == null ? "createTime" : pagination.getSort(); String sortField = pagination.getSort() == null ? "createTime" : pagination.getSort();
String sortOrder = pagination.getOrder() == null ? "DESC" : pagination.getOrder(); String sortOrder = pagination.getOrder() == null ? "DESC" : pagination.getOrder();
...@@ -488,7 +513,7 @@ public class CouponActivityService { ...@@ -488,7 +513,7 @@ public class CouponActivityService {
// 3. 执行查询 // 3. 执行查询
Page<CouponActivityStatDTO> dtoPage = couponActivityRepository.pageActivityStat( Page<CouponActivityStatDTO> dtoPage = couponActivityRepository.pageActivityStat(
searchKey, status, activityType, startTime, endTime, searchKey, status, activityType, startTime, endTime,
sortField, sortOrder, pagination.getPageRequest() sortField, sortOrder, activityIdsByTemplate, pagination.getPageRequest()
); );
// 4. 一次遍历按类型分组 // 4. 一次遍历按类型分组
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!