Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
usedCarAuction
/
aus
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 0052b293
authored
Apr 09, 2026
by
章家宝
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat:优惠券需求,优惠券模板修改
1 parent
60f7c893
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
118 additions
and
7 deletions
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dao/CouponActivityPriceRangeRepository.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dao/CouponActivityRepository.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponTemplateService.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/vo/CouponTemplateUpdateVO.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dao/CouponActivityPriceRangeRepository.java
View file @
0052b29
...
@@ -47,4 +47,13 @@ public interface CouponActivityPriceRangeRepository extends JpaRepository<Coupon
...
@@ -47,4 +47,13 @@ public interface CouponActivityPriceRangeRepository extends JpaRepository<Coupon
@Param
(
"priceMin"
)
Double
priceMin
,
@Param
(
"priceMin"
)
Double
priceMin
,
@Param
(
"priceMax"
)
Double
priceMax
@Param
(
"priceMax"
)
Double
priceMax
);
);
/**
* 根据模板 ID 和删除标识查询价格区间
*
* @param templateId 模板 ID
* @param deleteFlag 删除标识
* @return 价格区间列表
*/
List
<
CouponActivityPriceRange
>
findByTemplateIdAndDeleteFlag
(
Long
templateId
,
String
deleteFlag
);
}
}
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dao/CouponActivityRepository.java
View file @
0052b29
...
@@ -105,4 +105,24 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity,
...
@@ -105,4 +105,24 @@ public interface CouponActivityRepository extends BaseRepository<CouponActivity,
* @return 是否存在
* @return 是否存在
*/
*/
boolean
existsByActivityNameAndDeleteFlag
(
String
activityName
,
String
deleteFlag
);
boolean
existsByActivityNameAndDeleteFlag
(
String
activityName
,
String
deleteFlag
);
/**
* 查询关联了指定模板且状态为启用中的活动
*
* @param templateId 模板 ID
* @param status 活动状态
* @param deleteFlag 删除标识
* @return 活动列表
*/
List
<
CouponActivity
>
findByTemplateIdAndStatusAndDeleteFlag
(
Long
templateId
,
Integer
status
,
String
deleteFlag
);
/**
* 根据活动ID列表和状态批量查询活动
*
* @param activityIds 活动ID列表
* @param status 活动状态
* @param deleteFlag 删除标识
* @return 活动列表
*/
List
<
CouponActivity
>
findByActivityIdInAndStatusAndDeleteFlag
(
List
<
String
>
activityIds
,
Integer
status
,
String
deleteFlag
);
}
}
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponTemplateService.java
View file @
0052b29
package
com
.
glsx
.
usedcar
.
module
.
coupon
.
service
;
package
com
.
glsx
.
usedcar
.
module
.
coupon
.
service
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.glsx.usedcar.enums.common.DeleteFlag
;
import
com.glsx.usedcar.module.coupon.dao.*
;
import
com.glsx.usedcar.module.coupon.dao.*
;
import
com.glsx.usedcar.module.coupon.entity.*
;
import
com.glsx.usedcar.module.coupon.entity.*
;
import
com.glsx.usedcar.module.coupon.enums.*
;
import
com.glsx.usedcar.module.coupon.enums.*
;
...
@@ -18,6 +19,7 @@ import org.summerframework.orm.paging.Pagination;
...
@@ -18,6 +19,7 @@ import org.summerframework.orm.paging.Pagination;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.util.*
;
import
java.util.*
;
import
java.util.stream.Collectors
;
/**
/**
* 优惠券模板服务类
* 优惠券模板服务类
...
@@ -32,6 +34,8 @@ public class CouponTemplateService {
...
@@ -32,6 +34,8 @@ public class CouponTemplateService {
private
final
CouponDistributionRecordRepository
distributionRecordRepository
;
private
final
CouponDistributionRecordRepository
distributionRecordRepository
;
private
final
CouponAccountRepository
couponAccountRepository
;
private
final
CouponAccountRepository
couponAccountRepository
;
private
final
CouponTierRuleRepository
couponTierRuleRepository
;
private
final
CouponTierRuleRepository
couponTierRuleRepository
;
private
final
CouponActivityRepository
couponActivityRepository
;
private
final
CouponActivityPriceRangeRepository
priceRangeRepository
;
/**
/**
* 生成优惠券模板 ID
* 生成优惠券模板 ID
...
@@ -56,7 +60,7 @@ public class CouponTemplateService {
...
@@ -56,7 +60,7 @@ public class CouponTemplateService {
}
}
// 校验阶梯规则
// 校验阶梯规则
validateTierRules
(
addVO
);
validateTierRules
(
addVO
.
getTierRules
(),
addVO
.
getCouponType
()
);
LocalDateTime
now
=
LocalDateTime
.
now
();
LocalDateTime
now
=
LocalDateTime
.
now
();
CouponTemplate
template
=
CouponTemplate
.
builder
()
CouponTemplate
template
=
CouponTemplate
.
builder
()
...
@@ -109,6 +113,20 @@ public class CouponTemplateService {
...
@@ -109,6 +113,20 @@ public class CouponTemplateService {
CouponTemplate
template
=
couponTemplateRepository
.
findByIdAndDeleteFlag
(
updateVO
.
getId
(),
"N"
)
CouponTemplate
template
=
couponTemplateRepository
.
findByIdAndDeleteFlag
(
updateVO
.
getId
(),
"N"
)
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
DATA_NOT_EXISTS
,
"优惠券模板不存在"
));
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
DATA_NOT_EXISTS
,
"优惠券模板不存在"
));
// 检查名称是否重复(排除自身)
if
(!
template
.
getTemplateName
().
equals
(
updateVO
.
getTemplateName
()))
{
boolean
exists
=
couponTemplateRepository
.
existsByTemplateNameAndDeleteFlag
(
updateVO
.
getTemplateName
(),
"N"
);
if
(
exists
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"优惠券名称已存在"
);
}
}
// 校验是否有启用中的活动关联了此模板
checkActiveActivityAssociations
(
template
.
getId
());
// 校验阶梯规则
validateTierRules
(
updateVO
.
getTierRules
(),
updateVO
.
getCouponType
());
// 更新字段
// 更新字段
template
.
setTemplateName
(
updateVO
.
getTemplateName
());
template
.
setTemplateName
(
updateVO
.
getTemplateName
());
template
.
setCouponType
(
updateVO
.
getCouponType
());
template
.
setCouponType
(
updateVO
.
getCouponType
());
...
@@ -127,6 +145,13 @@ public class CouponTemplateService {
...
@@ -127,6 +145,13 @@ public class CouponTemplateService {
template
.
setUpdateTime
(
LocalDateTime
.
now
());
template
.
setUpdateTime
(
LocalDateTime
.
now
());
couponTemplateRepository
.
save
(
template
);
couponTemplateRepository
.
save
(
template
);
// 更新阶梯规则:先删除旧的,再保存新的
couponTierRuleRepository
.
deleteByTemplateIdAndDeleteFlag
(
template
.
getTemplateId
(),
DeleteFlag
.
NOT_DELETED
.
getCode
());
if
(
updateVO
.
getTierRules
()
!=
null
&&
!
updateVO
.
getTierRules
().
isEmpty
())
{
saveTierRules
(
template
.
getTemplateId
(),
updateVO
.
getTierRules
(),
opName
,
LocalDateTime
.
now
());
}
log
.
info
(
"优惠券模板修改成功:ID={}, 操作人={}"
,
template
.
getId
(),
opName
);
log
.
info
(
"优惠券模板修改成功:ID={}, 操作人={}"
,
template
.
getId
(),
opName
);
}
}
...
@@ -318,9 +343,9 @@ public class CouponTemplateService {
...
@@ -318,9 +343,9 @@ public class CouponTemplateService {
/**
/**
* 校验阶梯规则
* 校验阶梯规则
*/
*/
private
void
validateTierRules
(
CouponTemplateAddVO
addVO
)
{
private
void
validateTierRules
(
List
<
CouponTierRuleReqVO
>
tierRules
,
Integer
couponType
)
{
boolean
isTierCoupon
=
CouponTypeEnum
.
TIERED_AMOUNT
.
getCode
().
equals
(
addVO
.
getCouponType
()
);
boolean
isTierCoupon
=
CouponTypeEnum
.
TIERED_AMOUNT
.
getCode
().
equals
(
couponType
);
boolean
hasTierRules
=
addVO
.
getTierRules
()
!=
null
&&
!
addVO
.
getTierRules
()
.
isEmpty
();
boolean
hasTierRules
=
tierRules
!=
null
&&
!
tierRules
.
isEmpty
();
// 如果是阶梯满减券,必须配置阶梯规则
// 如果是阶梯满减券,必须配置阶梯规则
if
(
isTierCoupon
&&
!
hasTierRules
)
{
if
(
isTierCoupon
&&
!
hasTierRules
)
{
...
@@ -335,16 +360,16 @@ public class CouponTemplateService {
...
@@ -335,16 +360,16 @@ public class CouponTemplateService {
// 校验阶梯规则的合理性
// 校验阶梯规则的合理性
if
(
hasTierRules
)
{
if
(
hasTierRules
)
{
// 校验阶梯层级是否重复
// 校验阶梯层级是否重复
long
distinctLevels
=
addVO
.
getTierRules
()
.
stream
()
long
distinctLevels
=
tierRules
.
stream
()
.
map
(
CouponTierRuleReqVO:
:
getTierLevel
)
.
map
(
CouponTierRuleReqVO:
:
getTierLevel
)
.
distinct
()
.
distinct
()
.
count
();
.
count
();
if
(
distinctLevels
!=
addVO
.
getTierRules
()
.
size
())
{
if
(
distinctLevels
!=
tierRules
.
size
())
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"阶梯层级不能重复"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"阶梯层级不能重复"
);
}
}
// 校验门槛金额是否递增
// 校验门槛金额是否递增
List
<
CouponTierRuleReqVO
>
sortedRules
=
addVO
.
getTierRules
()
.
stream
()
List
<
CouponTierRuleReqVO
>
sortedRules
=
tierRules
.
stream
()
.
sorted
(
java
.
util
.
Comparator
.
comparing
(
CouponTierRuleReqVO:
:
getTierLevel
))
.
sorted
(
java
.
util
.
Comparator
.
comparing
(
CouponTierRuleReqVO:
:
getTierLevel
))
.
toList
();
.
toList
();
...
@@ -357,6 +382,59 @@ public class CouponTemplateService {
...
@@ -357,6 +382,59 @@ public class CouponTemplateService {
}
}
/**
/**
* 检查是否有启用中的活动关联了此模板
* 包括两种活动类型:
* 1. 直接发放活动(coupon_activity.template_id)
* 2. 自动触发活动(coupon_activity_price_range.template_id)
*/
private
void
checkActiveActivityAssociations
(
Long
templateId
)
{
Integer
inProgressStatus
=
CouponActivityStatusEnum
.
IN_PROGRESS
.
getCode
();
// 1. 检查直接发放活动(coupon_activity 表)
List
<
CouponActivity
>
directActivities
=
couponActivityRepository
.
findByTemplateIdAndStatusAndDeleteFlag
(
templateId
,
inProgressStatus
,
"N"
);
// 2. 检查自动触发活动(coupon_activity_price_range 表)
List
<
CouponActivityPriceRange
>
priceRanges
=
priceRangeRepository
.
findByTemplateIdAndDeleteFlag
(
templateId
,
"N"
);
// 收集所有关联的活动 ID
Set
<
String
>
relatedActivityIds
=
new
HashSet
<>();
if
(
directActivities
!=
null
&&
!
directActivities
.
isEmpty
())
{
relatedActivityIds
.
addAll
(
directActivities
.
stream
()
.
map
(
CouponActivity:
:
getActivityId
)
.
collect
(
Collectors
.
toSet
()));
}
if
(
priceRanges
!=
null
&&
!
priceRanges
.
isEmpty
())
{
// 获取价格区间关联的活动 ID
List
<
String
>
priceRangeActivityIds
=
priceRanges
.
stream
()
.
map
(
CouponActivityPriceRange:
:
getActivityId
)
.
distinct
()
.
collect
(
Collectors
.
toList
());
// 批量查询这些活动是否是启用中的
if
(!
priceRangeActivityIds
.
isEmpty
())
{
List
<
CouponActivity
>
activePriceRangeActivities
=
couponActivityRepository
.
findByActivityIdInAndStatusAndDeleteFlag
(
priceRangeActivityIds
,
inProgressStatus
,
DeleteFlag
.
NOT_DELETED
.
getCode
());
if
(
activePriceRangeActivities
!=
null
&&
!
activePriceRangeActivities
.
isEmpty
())
{
relatedActivityIds
.
addAll
(
activePriceRangeActivities
.
stream
()
.
map
(
CouponActivity:
:
getActivityId
)
.
collect
(
Collectors
.
toSet
()));
}
}
}
// 如果有关联的启用中活动,抛出异常
if
(!
relatedActivityIds
.
isEmpty
())
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"该模板已被 "
+
relatedActivityIds
.
size
()
+
" 个进行中的活动关联,无法编辑"
);
}
}
/**
* 保存阶梯规则
* 保存阶梯规则
*/
*/
private
void
saveTierRules
(
String
templateId
,
List
<
CouponTierRuleReqVO
>
tierRules
,
private
void
saveTierRules
(
String
templateId
,
List
<
CouponTierRuleReqVO
>
tierRules
,
...
...
aus-core/src/main/java/com/glsx/usedcar/module/coupon/vo/CouponTemplateUpdateVO.java
View file @
0052b29
...
@@ -8,6 +8,7 @@ import java.io.Serial;
...
@@ -8,6 +8,7 @@ import java.io.Serial;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.util.List
;
/**
/**
* 优惠券模板更新请求 VO
* 优惠券模板更新请求 VO
...
@@ -69,4 +70,7 @@ public class CouponTemplateUpdateVO implements Serializable {
...
@@ -69,4 +70,7 @@ public class CouponTemplateUpdateVO implements Serializable {
@Schema
(
description
=
"备注(后台可见)"
,
requiredMode
=
Schema
.
RequiredMode
.
NOT_REQUIRED
,
example
=
"新用户专享"
)
@Schema
(
description
=
"备注(后台可见)"
,
requiredMode
=
Schema
.
RequiredMode
.
NOT_REQUIRED
,
example
=
"新用户专享"
)
@Size
(
max
=
500
,
message
=
"备注长度不能超过 500 个字符"
)
@Size
(
max
=
500
,
message
=
"备注长度不能超过 500 个字符"
)
private
String
remark
;
private
String
remark
;
@Schema
(
description
=
"阶梯规则列表(阶梯满减券类型使用)"
,
requiredMode
=
Schema
.
RequiredMode
.
NOT_REQUIRED
)
private
List
<
CouponTierRuleReqVO
>
tierRules
;
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment