Commit d93339b0 by 章家宝

feat:代码优化

1 parent b71b0d52
...@@ -115,12 +115,12 @@ public class CouponTemplateService { ...@@ -115,12 +115,12 @@ public class CouponTemplateService {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "模板 ID 不能为空"); throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "模板 ID 不能为空");
} }
CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(updateVO.getId(), "N") CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(updateVO.getId(), DeleteFlag.NOT_DELETED.getCode())
.orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在")); .orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在"));
// 检查名称是否重复(排除自身) // 检查名称是否重复(排除自身)
if (!template.getTemplateName().equals(updateVO.getTemplateName())) { if (!template.getTemplateName().equals(updateVO.getTemplateName())) {
boolean exists = couponTemplateRepository.existsByTemplateNameAndDeleteFlag(updateVO.getTemplateName(), "N"); boolean exists = couponTemplateRepository.existsByTemplateNameAndDeleteFlag(updateVO.getTemplateName(), DeleteFlag.NOT_DELETED.getCode());
if (exists) { if (exists) {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券名称已存在"); throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券名称已存在");
} }
...@@ -215,7 +215,7 @@ public class CouponTemplateService { ...@@ -215,7 +215,7 @@ public class CouponTemplateService {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "参数不能为空"); throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "参数不能为空");
} }
CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(vo.getId(), "N") CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(vo.getId(), DeleteFlag.NOT_DELETED.getCode())
.orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在")); .orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在"));
// 校验状态是否发生变化 // 校验状态是否发生变化
...@@ -251,7 +251,7 @@ public class CouponTemplateService { ...@@ -251,7 +251,7 @@ public class CouponTemplateService {
// 1. 禁用直接发放活动(coupon_activity 表) // 1. 禁用直接发放活动(coupon_activity 表)
List<CouponActivity> directActivities = couponActivityRepository List<CouponActivity> directActivities = couponActivityRepository
.findByTemplateIdAndStatusAndDeleteFlag(templateId, CouponActivityStatusEnum.IN_PROGRESS.getCode(), "N"); .findByTemplateIdAndStatusAndDeleteFlag(templateId, CouponActivityStatusEnum.IN_PROGRESS.getCode(), DeleteFlag.NOT_DELETED.getCode());
if (directActivities != null && !directActivities.isEmpty()) { if (directActivities != null && !directActivities.isEmpty()) {
for (CouponActivity activity : directActivities) { for (CouponActivity activity : directActivities) {
...@@ -266,7 +266,7 @@ public class CouponTemplateService { ...@@ -266,7 +266,7 @@ public class CouponTemplateService {
// 2. 禁用自动触发活动(通过 coupon_activity_price_range 关联) // 2. 禁用自动触发活动(通过 coupon_activity_price_range 关联)
List<CouponActivityPriceRange> priceRanges = priceRangeRepository List<CouponActivityPriceRange> priceRanges = priceRangeRepository
.findByTemplateIdAndDeleteFlag(templateId, "N"); .findByTemplateIdAndDeleteFlag(templateId, DeleteFlag.NOT_DELETED.getCode());
if (priceRanges != null && !priceRanges.isEmpty()) { if (priceRanges != null && !priceRanges.isEmpty()) {
// 获取所有关联的活动 ID // 获取所有关联的活动 ID
...@@ -277,7 +277,7 @@ public class CouponTemplateService { ...@@ -277,7 +277,7 @@ public class CouponTemplateService {
// 批量查询这些进行中的活动 // 批量查询这些进行中的活动
List<CouponActivity> priceRangeActivities = couponActivityRepository List<CouponActivity> priceRangeActivities = couponActivityRepository
.findByActivityIdInAndStatusAndDeleteFlag(activityIds, CouponActivityStatusEnum.IN_PROGRESS.getCode(), "N"); .findByActivityIdInAndStatusAndDeleteFlag(activityIds, CouponActivityStatusEnum.IN_PROGRESS.getCode(), DeleteFlag.NOT_DELETED.getCode());
if (priceRangeActivities != null && !priceRangeActivities.isEmpty()) { if (priceRangeActivities != null && !priceRangeActivities.isEmpty()) {
for (CouponActivity activity : priceRangeActivities) { for (CouponActivity activity : priceRangeActivities) {
...@@ -464,7 +464,7 @@ public class CouponTemplateService { ...@@ -464,7 +464,7 @@ public class CouponTemplateService {
* 详情查询 * 详情查询
*/ */
public CouponTemplateDetailRespVO detail(CouponTemplateDetailReqVO reqVO) { public CouponTemplateDetailRespVO detail(CouponTemplateDetailReqVO reqVO) {
CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(reqVO.getId(), "N") CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(reqVO.getId(), DeleteFlag.NOT_DELETED.getCode())
.orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在")); .orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在"));
CouponTemplateDetailRespVO respVO = couponConverter.couponTemplateMapDetailRespVO(template); CouponTemplateDetailRespVO respVO = couponConverter.couponTemplateMapDetailRespVO(template);
...@@ -504,7 +504,7 @@ public class CouponTemplateService { ...@@ -504,7 +504,7 @@ public class CouponTemplateService {
// 1. 查询直接发放活动(coupon_activity 表) // 1. 查询直接发放活动(coupon_activity 表)
List<CouponActivity> directActivities = couponActivityRepository List<CouponActivity> directActivities = couponActivityRepository
.findByTemplateIdAndDeleteFlag(templateId, "N"); .findByTemplateIdAndDeleteFlag(templateId, DeleteFlag.NOT_DELETED.getCode());
if (directActivities != null && !directActivities.isEmpty()) { if (directActivities != null && !directActivities.isEmpty()) {
activityList.addAll(directActivities); activityList.addAll(directActivities);
...@@ -512,7 +512,7 @@ public class CouponTemplateService { ...@@ -512,7 +512,7 @@ public class CouponTemplateService {
// 2. 查询自动触发活动(通过 coupon_activity_price_range 关联) // 2. 查询自动触发活动(通过 coupon_activity_price_range 关联)
List<CouponActivityPriceRange> priceRanges = priceRangeRepository List<CouponActivityPriceRange> priceRanges = priceRangeRepository
.findByTemplateIdAndDeleteFlag(templateId, "N"); .findByTemplateIdAndDeleteFlag(templateId, DeleteFlag.NOT_DELETED.getCode());
if (priceRanges != null && !priceRanges.isEmpty()) { if (priceRanges != null && !priceRanges.isEmpty()) {
// 获取所有关联的活动 ID // 获取所有关联的活动 ID
...@@ -523,7 +523,7 @@ public class CouponTemplateService { ...@@ -523,7 +523,7 @@ public class CouponTemplateService {
// 批量查询这些活动 // 批量查询这些活动
List<CouponActivity> priceRangeActivities = couponActivityRepository List<CouponActivity> priceRangeActivities = couponActivityRepository
.findByActivityIdInAndDeleteFlag(activityIds, "N"); .findByActivityIdInAndDeleteFlag(activityIds, DeleteFlag.NOT_DELETED.getCode());
if (priceRangeActivities != null && !priceRangeActivities.isEmpty()) { if (priceRangeActivities != null && !priceRangeActivities.isEmpty()) {
for (CouponActivity activity : priceRangeActivities) { for (CouponActivity activity : priceRangeActivities) {
...@@ -557,7 +557,7 @@ public class CouponTemplateService { ...@@ -557,7 +557,7 @@ public class CouponTemplateService {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
// 1. 查询优惠券模板 // 1. 查询优惠券模板
CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(vo.getTemplateId(), "N") CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(vo.getTemplateId(), DeleteFlag.NOT_DELETED.getCode())
.orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在")); .orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在"));
// 2. 校验模板状态 // 2. 校验模板状态
...@@ -627,7 +627,7 @@ public class CouponTemplateService { ...@@ -627,7 +627,7 @@ public class CouponTemplateService {
.validEndTime(validEndTime) .validEndTime(validEndTime)
.distributionType(CouponDistributionTypeEnum.MANUAL.getCode()) .distributionType(CouponDistributionTypeEnum.MANUAL.getCode())
.distributionSource("手动发放") .distributionSource("手动发放")
.deleteFlag("N") .deleteFlag(DeleteFlag.NOT_DELETED.getCode())
.build(); .build();
coupon.setCreateBy(opName); coupon.setCreateBy(opName);
...@@ -649,7 +649,7 @@ public class CouponTemplateService { ...@@ -649,7 +649,7 @@ public class CouponTemplateService {
.distributionRemark(vo.getRemark()) .distributionRemark(vo.getRemark())
.distributionTime(now) .distributionTime(now)
.operator(opName) .operator(opName)
.deleteFlag("N") .deleteFlag(DeleteFlag.NOT_DELETED.getCode())
.build(); .build();
record.setCreateBy(opName); record.setCreateBy(opName);
...@@ -812,11 +812,11 @@ public class CouponTemplateService { ...@@ -812,11 +812,11 @@ public class CouponTemplateService {
// 1. 检查直接发放活动(coupon_activity 表) // 1. 检查直接发放活动(coupon_activity 表)
List<CouponActivity> directActivities = couponActivityRepository List<CouponActivity> directActivities = couponActivityRepository
.findByTemplateIdAndStatusAndDeleteFlag(templateId, inProgressStatus, "N"); .findByTemplateIdAndStatusAndDeleteFlag(templateId, inProgressStatus, DeleteFlag.NOT_DELETED.getCode());
// 2. 检查自动触发活动(coupon_activity_price_range 表) // 2. 检查自动触发活动(coupon_activity_price_range 表)
List<CouponActivityPriceRange> priceRanges = priceRangeRepository List<CouponActivityPriceRange> priceRanges = priceRangeRepository
.findByTemplateIdAndDeleteFlag(templateId, "N"); .findByTemplateIdAndDeleteFlag(templateId, DeleteFlag.NOT_DELETED.getCode());
// 收集所有关联的活动 ID // 收集所有关联的活动 ID
Set<String> relatedActivityIds = new HashSet<>(); Set<String> relatedActivityIds = new HashSet<>();
...@@ -866,7 +866,7 @@ public class CouponTemplateService { ...@@ -866,7 +866,7 @@ public class CouponTemplateService {
.tierLevel(ruleVO.getTierLevel()) .tierLevel(ruleVO.getTierLevel())
.thresholdAmount(ruleVO.getThresholdAmount()) .thresholdAmount(ruleVO.getThresholdAmount())
.deductionAmount(ruleVO.getDeductionAmount()) .deductionAmount(ruleVO.getDeductionAmount())
.deleteFlag("N") .deleteFlag(DeleteFlag.NOT_DELETED.getCode())
.build(); .build();
rule.setCreateBy(opName); rule.setCreateBy(opName);
rule.setCreateTime(now); rule.setCreateTime(now);
......
...@@ -55,7 +55,7 @@ public class CouponVerificationService { ...@@ -55,7 +55,7 @@ public class CouponVerificationService {
// 查询用户所有待使用的优惠券 // 查询用户所有待使用的优惠券
List<CouponAccount> allCoupons = couponAccountRepository.findAvailableCoupons( List<CouponAccount> allCoupons = couponAccountRepository.findAvailableCoupons(
accountId, CouponUserStatusEnum.UNUSED.getCode(), "N", now); accountId, CouponUserStatusEnum.UNUSED.getCode(), DeleteFlag.NOT_DELETED.getCode(), now);
if (allCoupons.isEmpty()) { if (allCoupons.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
...@@ -112,7 +112,7 @@ public class CouponVerificationService { ...@@ -112,7 +112,7 @@ public class CouponVerificationService {
if (templateIds.isEmpty()) { if (templateIds.isEmpty()) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
return couponTemplateRepository.findByIdInAndDeleteFlag(templateIds, "N").stream() return couponTemplateRepository.findByIdInAndDeleteFlag(templateIds, DeleteFlag.NOT_DELETED.getCode()).stream()
.collect(Collectors.toMap(CouponTemplate::getId, t -> t, (a, b) -> a)); .collect(Collectors.toMap(CouponTemplate::getId, t -> t, (a, b) -> a));
} }
...@@ -130,7 +130,7 @@ public class CouponVerificationService { ...@@ -130,7 +130,7 @@ public class CouponVerificationService {
if (templateIds.isEmpty()) { if (templateIds.isEmpty()) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
return couponTierRuleRepository.findByTemplateIdInAndDeleteFlag(templateIds, "N").stream() return couponTierRuleRepository.findByTemplateIdInAndDeleteFlag(templateIds, DeleteFlag.NOT_DELETED.getCode()).stream()
.collect(Collectors.groupingBy(CouponTierRule::getTemplateId)); .collect(Collectors.groupingBy(CouponTierRule::getTemplateId));
} }
...@@ -221,12 +221,12 @@ public class CouponVerificationService { ...@@ -221,12 +221,12 @@ public class CouponVerificationService {
*/ */
private BigDecimal calculateTieredDeduction(Long templateDbId, BigDecimal transactionAmount, BigDecimal faceAmount) { private BigDecimal calculateTieredDeduction(Long templateDbId, BigDecimal transactionAmount, BigDecimal faceAmount) {
// 1. 查询模板获取 template_id(字符串) // 1. 查询模板获取 template_id(字符串)
CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(templateDbId, "N") CouponTemplate template = couponTemplateRepository.findByIdAndDeleteFlag(templateDbId, DeleteFlag.NOT_DELETED.getCode())
.orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在")); .orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.DATA_NOT_EXISTS, "优惠券模板不存在"));
// 2. 查询该模板的所有阶梯规则 // 2. 查询该模板的所有阶梯规则
List<CouponTierRule> tierRules = couponTierRuleRepository List<CouponTierRule> tierRules = couponTierRuleRepository
.findByTemplateIdAndDeleteFlag(template.getTemplateId(), "N"); .findByTemplateIdAndDeleteFlag(template.getTemplateId(), DeleteFlag.NOT_DELETED.getCode());
if (tierRules == null || tierRules.isEmpty()) { if (tierRules == null || tierRules.isEmpty()) {
log.warn("阶梯券未配置阶梯规则,使用面额:templateId={}", templateDbId); log.warn("阶梯券未配置阶梯规则,使用面额:templateId={}", templateDbId);
...@@ -283,7 +283,7 @@ public class CouponVerificationService { ...@@ -283,7 +283,7 @@ public class CouponVerificationService {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
// 1. 查询优惠券 // 1. 查询优惠券
CouponAccount coupon = couponAccountRepository.findByCouponIdAndDeleteFlag(couponId, "N") CouponAccount coupon = couponAccountRepository.findByCouponIdAndDeleteFlag(couponId, DeleteFlag.NOT_DELETED.getCode())
.orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券不存在")); .orElseThrow(() -> new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券不存在"));
// 2. 校验优惠券归属 // 2. 校验优惠券归属
...@@ -356,7 +356,7 @@ public class CouponVerificationService { ...@@ -356,7 +356,7 @@ public class CouponVerificationService {
.usageStatus(1) .usageStatus(1)
.usageTime(now) .usageTime(now)
.deductionType(coupon.getDeductionType()) .deductionType(coupon.getDeductionType())
.deleteFlag("N") .deleteFlag(DeleteFlag.NOT_DELETED.getCode())
.build(); .build();
usage.setCreateBy("SYSTEM"); usage.setCreateBy("SYSTEM");
usage.setCreateTime(now); usage.setCreateTime(now);
...@@ -379,7 +379,7 @@ public class CouponVerificationService { ...@@ -379,7 +379,7 @@ public class CouponVerificationService {
.isDowngraded(actualDeduction.compareTo(coupon.getFaceAmount()) < 0 ? 1 : 0) .isDowngraded(actualDeduction.compareTo(coupon.getFaceAmount()) < 0 ? 1 : 0)
.deductionType(coupon.getDeductionType()) .deductionType(coupon.getDeductionType())
.writeOffTime(now) .writeOffTime(now)
.deleteFlag("N") .deleteFlag(DeleteFlag.NOT_DELETED.getCode())
.build(); .build();
record.setCreateBy("SYSTEM"); record.setCreateBy("SYSTEM");
record.setCreateTime(now); record.setCreateTime(now);
...@@ -526,7 +526,7 @@ public class CouponVerificationService { ...@@ -526,7 +526,7 @@ public class CouponVerificationService {
// 2. 查询用户所有待使用的优惠券(未过期、未核销、未退回) // 2. 查询用户所有待使用的优惠券(未过期、未核销、未退回)
List<CouponAccount> allCoupons = couponAccountRepository.findAvailableCoupons( List<CouponAccount> allCoupons = couponAccountRepository.findAvailableCoupons(
accountId, CouponUserStatusEnum.UNUSED.getCode(), "N", now); accountId, CouponUserStatusEnum.UNUSED.getCode(), DeleteFlag.NOT_DELETED.getCode(), now);
if (allCoupons.isEmpty()) { if (allCoupons.isEmpty()) {
return List.of(); return List.of();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!