Commit a708d82e by 章家宝

feat:优惠券活动分页查询新增模板列表

1 parent 8af81184
...@@ -46,6 +46,7 @@ public class CouponActivityService { ...@@ -46,6 +46,7 @@ public class CouponActivityService {
private final CouponAccountRepository couponAccountRepository; private final CouponAccountRepository couponAccountRepository;
private final CouponDistributionRecordRepository distributionRecordRepository; private final CouponDistributionRecordRepository distributionRecordRepository;
private final CouponVerificationRecordRepository verificationRecordRepository; private final CouponVerificationRecordRepository verificationRecordRepository;
private final CouponTierRuleRepository couponTierRuleRepository;
private final AccountDao accountDao; private final AccountDao accountDao;
private final CouponConverter couponConverter; private final CouponConverter couponConverter;
...@@ -350,7 +351,7 @@ public class CouponActivityService { ...@@ -350,7 +351,7 @@ public class CouponActivityService {
.collect(Collectors.toList()); .collect(Collectors.toList());
// 5. 批量查询价格区间规则(只做一次查询) // 5. 批量查询价格区间规则(只做一次查询)
Map<String, List<CouponActivityPriceRange>> priceRangeMap = new java.util.HashMap<>(); Map<String, List<CouponActivityPriceRange>> priceRangeMap = new HashMap<>();
if (!autoTriggerActivityIds.isEmpty()) { if (!autoTriggerActivityIds.isEmpty()) {
List<CouponActivityPriceRange> allPriceRanges = priceRangeRepository.findByActivityIdInAndDeleteFlag( List<CouponActivityPriceRange> allPriceRanges = priceRangeRepository.findByActivityIdInAndDeleteFlag(
autoTriggerActivityIds, DeleteFlag.NOT_DELETED.getCode()); autoTriggerActivityIds, DeleteFlag.NOT_DELETED.getCode());
...@@ -359,8 +360,68 @@ public class CouponActivityService { ...@@ -359,8 +360,68 @@ public class CouponActivityService {
.collect(Collectors.groupingBy(CouponActivityPriceRange::getActivityId)); .collect(Collectors.groupingBy(CouponActivityPriceRange::getActivityId));
} }
// 6. DTO 转 VO(小驼峰映射) // 6. 批量查询所有关联的优惠券模板配置(避免 N+1 查询)
// 6.1 收集直接发放活动的模板 ID
List<Long> directTemplateIds = dtoPage.getContent().stream()
.filter(dto -> dto.getActivityType() != null
&& dto.getActivityType().equals(CouponActivityTypeEnum.DIRECT_DISTRIBUTION.getCode())
&& dto.getTemplateId() != null)
.map(CouponActivityStatDTO::getTemplateId)
.distinct()
.collect(Collectors.toList());
// 6.2 收集自动触发活动的所有模板 ID(从价格区间规则中)
List<Long> autoTriggerTemplateIds = priceRangeMap.values().stream()
.flatMap(List::stream)
.map(CouponActivityPriceRange::getTemplateId)
.distinct()
.collect(Collectors.toList());
// 6.3 合并所有需要查询的模板 ID
Set<Long> allTemplateIds = new HashSet<>();
allTemplateIds.addAll(directTemplateIds);
allTemplateIds.addAll(autoTriggerTemplateIds);
// 6.4 批量查询所有模板信息(只做一次查询)
Map<Long, CouponTemplate> templateMap = new HashMap<>();
if (!allTemplateIds.isEmpty()) {
List<CouponTemplate> templates = couponTemplateRepository.findAllById(allTemplateIds);
templateMap = templates.stream()
.collect(Collectors.toMap(CouponTemplate::getId, t -> t));
}
// 6.5 收集所有阶梯满减券的模板 ID(用于查询阶梯规则)
List<String> tierTemplateIds = templateMap.values().stream()
.filter(t -> CouponTypeEnum.TIERED_AMOUNT.getCode().equals(t.getCouponType()))
.map(CouponTemplate::getTemplateId)
.distinct()
.collect(Collectors.toList());
// 6.6 批量查询所有阶梯规则(只做一次查询)
Map<String, List<CouponTierRuleRespVO>> tierRuleMap = new HashMap<>();
if (!tierTemplateIds.isEmpty()) {
List<CouponTierRule> allTierRules = couponTierRuleRepository
.findByTemplateIdInAndDeleteFlag(tierTemplateIds, DeleteFlag.NOT_DELETED.getCode());
tierRuleMap = allTierRules.stream()
.collect(Collectors.groupingBy(
CouponTierRule::getTemplateId,
Collectors.mapping(rule -> {
CouponTierRuleRespVO vo = new CouponTierRuleRespVO();
vo.setId(rule.getId());
vo.setTemplateId(rule.getTemplateId());
vo.setTierLevel(rule.getTierLevel());
vo.setThresholdAmount(rule.getThresholdAmount());
vo.setDeductionAmount(rule.getDeductionAmount());
return vo;
}, Collectors.toList())
));
}
// 7. DTO 转 VO(小驼峰映射)
final Map<String, List<CouponActivityPriceRange>> finalPriceRangeMap = priceRangeMap; final Map<String, List<CouponActivityPriceRange>> finalPriceRangeMap = priceRangeMap;
final Map<Long, CouponTemplate> finalTemplateMap = templateMap;
final Map<String, List<CouponTierRuleRespVO>> finalTierRuleMap = tierRuleMap;
return dtoPage.map(dto -> { return dtoPage.map(dto -> {
CouponActivityRespVO vo = new CouponActivityRespVO(); CouponActivityRespVO vo = new CouponActivityRespVO();
vo.setId(dto.getId()); vo.setId(dto.getId());
...@@ -381,8 +442,11 @@ public class CouponActivityService { ...@@ -381,8 +442,11 @@ public class CouponActivityService {
vo.setTemplateId(dto.getTemplateId()); vo.setTemplateId(dto.getTemplateId());
vo.setUsedCount(dto.getUsedCount()); vo.setUsedCount(dto.getUsedCount());
vo.setWriteOffRate(dto.getWriteOffRate()); vo.setWriteOffRate(dto.getWriteOffRate());
// 如果是自动触发活动,从 Map 中获取价格区间规则 // 构建优惠券模板配置列表
List<CouponTemplateConfigVO> couponTemplates = new ArrayList<>();
// 如果是自动触发活动,从 Map 中获取价格区间规则,并构建模板配置列表
if (dto.getActivityType() != null && dto.getActivityType().equals(CouponActivityTypeEnum.AUTO_TRIGGER.getCode())) { if (dto.getActivityType() != null && dto.getActivityType().equals(CouponActivityTypeEnum.AUTO_TRIGGER.getCode())) {
List<CouponActivityPriceRange> priceRanges = finalPriceRangeMap.get(dto.getActivityId()); List<CouponActivityPriceRange> priceRanges = finalPriceRangeMap.get(dto.getActivityId());
if (priceRanges != null && !priceRanges.isEmpty()) { if (priceRanges != null && !priceRanges.isEmpty()) {
...@@ -395,14 +459,54 @@ public class CouponActivityService { ...@@ -395,14 +459,54 @@ public class CouponActivityService {
return prVO; return prVO;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
vo.setPriceRanges(priceRangeVOs); vo.setPriceRanges(priceRangeVOs);
// 构建模板配置列表(每个价格区间对应一个模板配置)
for (CouponActivityPriceRange priceRange : priceRanges) {
CouponTemplate template = finalTemplateMap.get(priceRange.getTemplateId());
if (template != null) {
couponTemplates.add(buildCouponTemplateConfig(template, finalTierRuleMap));
}
}
}
} else if (dto.getActivityType() != null && dto.getActivityType().equals(CouponActivityTypeEnum.DIRECT_DISTRIBUTION.getCode())) {
// 直接发放活动,使用活动的 templateId 查询模板配置
if (dto.getTemplateId() != null) {
CouponTemplate template = finalTemplateMap.get(dto.getTemplateId());
if (template != null) {
couponTemplates.add(buildCouponTemplateConfig(template, finalTierRuleMap));
}
} }
} }
vo.setCouponTemplates(couponTemplates);
return vo; return vo;
}); });
} }
/** /**
* 构建优惠券模板配置 VO
*/
private CouponTemplateConfigVO buildCouponTemplateConfig(CouponTemplate template,
Map<String, List<CouponTierRuleRespVO>> tierRuleMap) {
CouponTemplateConfigVO config = new CouponTemplateConfigVO();
config.setId(template.getId());
config.setTemplateId(template.getTemplateId());
config.setTemplateName(template.getTemplateName());
config.setCouponType(template.getCouponType());
config.setFaceAmount(template.getFaceAmount());
config.setThresholdAmount(template.getThresholdAmount());
config.setDeductionType(template.getDeductionType());
config.setDescription(template.getDescription());
// 如果是阶梯满减券,设置阶梯规则
if (CouponTypeEnum.TIERED_AMOUNT.getCode().equals(template.getCouponType())) {
config.setTierRules(tierRuleMap.getOrDefault(template.getTemplateId(), List.of()));
}
return config;
}
/**
* 活动详情 * 活动详情
*/ */
public CouponActivityDetailRespVO detail(CouponActivityDetailReqVO reqVO) { public CouponActivityDetailRespVO detail(CouponActivityDetailReqVO reqVO) {
......
...@@ -73,4 +73,7 @@ public class CouponActivityRespVO implements Serializable { ...@@ -73,4 +73,7 @@ public class CouponActivityRespVO implements Serializable {
@Schema(description = "价格区间规则列表(仅自动触发活动)") @Schema(description = "价格区间规则列表(仅自动触发活动)")
private List<CouponActivityPriceRangeRespVO> priceRanges; private List<CouponActivityPriceRangeRespVO> priceRanges;
@Schema(description = "关联的优惠券模板配置列表")
private List<CouponTemplateConfigVO> couponTemplates;
} }
package com.glsx.usedcar.module.coupon.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
/**
* 优惠券模板配置 VO
* 用于展示活动关联的优惠券模板配置信息
*/
@Data
@Schema(description = "优惠券模板配置信息")
public class CouponTemplateConfigVO implements Serializable {
@Serial
private static final long serialVersionUID = 1234567890123456789L;
@Schema(description = "模板 ID")
private Long id;
@Schema(description = "模板唯一 ID")
private String templateId;
@Schema(description = "优惠券名称")
private String templateName;
@Schema(description = "优惠券类型 (1:固定面额券;2:阶梯满减券)")
private Integer couponType;
@Schema(description = "券面额/最高抵扣金额(元)")
private BigDecimal faceAmount;
@Schema(description = "使用门槛(元)")
private BigDecimal thresholdAmount;
@Schema(description = "抵扣类型 (1:抵扣车款;2:抵扣佣金)")
private Integer deductionType;
@Schema(description = "使用说明")
private String description;
@Schema(description = "阶梯规则列表(仅阶梯满减券有值)")
private List<CouponTierRuleRespVO> tierRules;
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!