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 60f7c893
authored
Apr 09, 2026
by
章家宝
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat:优惠券需求,优惠券模板新增
1 parent
fda71de5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
102 additions
and
24 deletions
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponAccountService.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponActivityService.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponTemplateService.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponVerificationService.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/vo/CouponTemplateAddVO.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponAccountService.java
View file @
60f7c89
...
...
@@ -89,7 +89,7 @@ public class CouponAccountService {
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
confirmPopup
(
Integer
accountId
,
String
notificationId
)
{
if
(
accountId
==
null
||
StrUtil
.
isBlank
(
notificationId
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"参数不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"参数不能为空"
);
}
LocalDateTime
now
=
LocalDateTime
.
now
();
...
...
@@ -101,7 +101,7 @@ public class CouponAccountService {
// 2. 校验弹窗归属
if
(!
notification
.
getAccountId
().
equals
(
accountId
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"弹窗通知不属于当前用户"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"弹窗通知不属于当前用户"
);
}
// 3. 校验弹窗状态(只能确认待通知或已展示的弹窗)
...
...
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponActivityService.java
View file @
60f7c89
...
...
@@ -14,11 +14,13 @@ import org.springframework.data.domain.Page;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.summerframework.core.enums.ErrorCodeEnum
;
import
org.summerframework.core.exception.BusinessCodeException
;
import
org.summerframework.orm.paging.Pagination
;
import
java.time.LocalDateTime
;
import
java.util.*
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
/**
...
...
@@ -51,20 +53,20 @@ public class CouponActivityService {
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
addActivity
(
CouponActivityAddVO
addVO
,
Integer
opId
,
String
opName
)
{
if
(
opId
==
null
||
StrUtil
.
isBlank
(
opName
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"操作人信息不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"操作人信息不能为空"
);
}
LocalDateTime
now
=
LocalDateTime
.
now
();
// 校验活动有效期
if
(
addVO
.
getValidEndTime
().
isBefore
(
addVO
.
getValidStartTime
()))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"活动结束时间必须晚于开始时间"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"活动结束时间必须晚于开始时间"
);
}
// 校验模板是否存在
if
(
addVO
.
getTemplateId
()
!=
null
)
{
couponTemplateRepository
.
findByIdAndDeleteFlag
(
addVO
.
getTemplateId
(),
"N"
)
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券模板不存在"
));
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券模板不存在"
));
}
// 创建活动
...
...
@@ -120,7 +122,7 @@ public class CouponActivityService {
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
updateActivity
(
CouponActivityUpdateVO
updateVO
,
Integer
opId
,
String
opName
)
{
if
(
updateVO
.
getId
()
==
null
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"活动 ID 不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"活动 ID 不能为空"
);
}
CouponActivity
activity
=
couponActivityRepository
.
findByIdAndDeleteFlag
(
updateVO
.
getId
(),
"N"
)
...
...
@@ -128,7 +130,7 @@ public class CouponActivityService {
// 只能编辑待启动状态的活动
if
(!
activity
.
getStatus
().
equals
(
CouponActivityStatusEnum
.
PENDING
.
getCode
()))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"只能编辑待启动状态的活动"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"只能编辑待启动状态的活动"
);
}
// 更新字段
...
...
@@ -308,7 +310,7 @@ public class CouponActivityService {
// 获取优惠券模板
CouponTemplate
template
=
couponTemplateRepository
.
findByIdAndDeleteFlag
(
activity
.
getTemplateId
(),
"N"
)
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券模板不存在"
));
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券模板不存在"
));
// 为每个用户发放优惠券
for
(
Integer
accountId
:
targetAccountIds
)
{
...
...
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponTemplateService.java
View file @
60f7c89
...
...
@@ -3,8 +3,7 @@ package com.glsx.usedcar.module.coupon.service;
import
cn.hutool.core.util.StrUtil
;
import
com.glsx.usedcar.module.coupon.dao.*
;
import
com.glsx.usedcar.module.coupon.entity.*
;
import
com.glsx.usedcar.module.coupon.enums.CouponDistributionTypeEnum
;
import
com.glsx.usedcar.module.coupon.enums.CouponTemplateStatusEnum
;
import
com.glsx.usedcar.module.coupon.enums.*
;
import
com.glsx.usedcar.module.coupon.vo.*
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -18,7 +17,7 @@ import org.summerframework.core.exception.BusinessCodeException;
import
org.summerframework.orm.paging.Pagination
;
import
java.time.LocalDateTime
;
import
java.util.
UUID
;
import
java.util.
*
;
/**
* 优惠券模板服务类
...
...
@@ -32,6 +31,7 @@ public class CouponTemplateService {
private
final
CouponTemplateRepository
couponTemplateRepository
;
private
final
CouponDistributionRecordRepository
distributionRecordRepository
;
private
final
CouponAccountRepository
couponAccountRepository
;
private
final
CouponTierRuleRepository
couponTierRuleRepository
;
/**
* 生成优惠券模板 ID
...
...
@@ -46,15 +46,18 @@ public class CouponTemplateService {
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
addCouponTemplate
(
CouponTemplateAddVO
addVO
,
Integer
opId
,
String
opName
)
{
if
(
opId
==
null
||
StrUtil
.
isBlank
(
opName
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"操作人信息不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"操作人信息不能为空"
);
}
// 检查名称是否重复
boolean
exists
=
couponTemplateRepository
.
existsByTemplateNameAndDeleteFlag
(
addVO
.
getTemplateName
(),
"N"
);
if
(
exists
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券名称已存在"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券名称已存在"
);
}
// 校验阶梯规则
validateTierRules
(
addVO
);
LocalDateTime
now
=
LocalDateTime
.
now
();
CouponTemplate
template
=
CouponTemplate
.
builder
()
.
templateId
(
generateTemplateId
())
...
...
@@ -82,6 +85,12 @@ public class CouponTemplateService {
template
.
setUpdateTime
(
now
);
couponTemplateRepository
.
save
(
template
);
// 保存阶梯规则
if
(
addVO
.
getTierRules
()
!=
null
&&
!
addVO
.
getTierRules
().
isEmpty
())
{
saveTierRules
(
template
.
getTemplateId
(),
addVO
.
getTierRules
(),
opName
,
now
);
}
log
.
info
(
"优惠券模板新增成功:ID={}, 名称={}, 操作人={}"
,
template
.
getId
(),
template
.
getTemplateName
(),
opName
);
}
...
...
@@ -91,10 +100,10 @@ public class CouponTemplateService {
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
updateCouponTemplate
(
CouponTemplateUpdateVO
updateVO
,
Integer
opId
,
String
opName
)
{
if
(
opId
==
null
||
StrUtil
.
isBlank
(
opName
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"操作人信息不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"操作人信息不能为空"
);
}
if
(
updateVO
==
null
||
updateVO
.
getId
()
==
null
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"模板 ID 不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"模板 ID 不能为空"
);
}
CouponTemplate
template
=
couponTemplateRepository
.
findByIdAndDeleteFlag
(
updateVO
.
getId
(),
"N"
)
...
...
@@ -127,7 +136,7 @@ public class CouponTemplateService {
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
changeStatus
(
CouponTemplateChangeStatusVO
vo
,
Integer
opId
,
String
opName
)
{
if
(
vo
.
getId
()
==
null
||
vo
.
getStatus
()
==
null
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"参数不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"参数不能为空"
);
}
CouponTemplate
template
=
couponTemplateRepository
.
findByIdAndDeleteFlag
(
vo
.
getId
(),
"N"
)
...
...
@@ -164,7 +173,7 @@ public class CouponTemplateService {
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
manualDistribution
(
CouponManualDistributionVO
vo
,
Integer
opId
,
String
opName
)
{
if
(
vo
.
getTemplateId
()
==
null
||
vo
.
getAccountIds
()
==
null
||
vo
.
getAccountIds
().
isEmpty
())
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"模板 ID 和用户列表不能为空"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"模板 ID 和用户列表不能为空"
);
}
LocalDateTime
now
=
LocalDateTime
.
now
();
...
...
@@ -175,12 +184,12 @@ public class CouponTemplateService {
// 2. 校验模板状态
if
(!
template
.
getStatus
().
equals
(
CouponTemplateStatusEnum
.
ENABLED
.
getCode
()))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券模板已停用,无法发放"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券模板已停用,无法发放"
);
}
// 3. 校验库存
if
(
template
.
getRemainingQuantity
()
!=
null
&&
template
.
getRemainingQuantity
()
<
vo
.
getAccountIds
().
size
())
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"库存不足,剩余库存:"
+
template
.
getRemainingQuantity
());
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"库存不足,剩余库存:"
+
template
.
getRemainingQuantity
());
}
// 4. 计算有效期
...
...
@@ -305,4 +314,67 @@ public class CouponTemplateService {
vo
.
setUpdateTime
(
template
.
getUpdateTime
());
return
vo
;
}
/**
* 校验阶梯规则
*/
private
void
validateTierRules
(
CouponTemplateAddVO
addVO
)
{
boolean
isTierCoupon
=
CouponTypeEnum
.
TIERED_AMOUNT
.
getCode
().
equals
(
addVO
.
getCouponType
());
boolean
hasTierRules
=
addVO
.
getTierRules
()
!=
null
&&
!
addVO
.
getTierRules
().
isEmpty
();
// 如果是阶梯满减券,必须配置阶梯规则
if
(
isTierCoupon
&&
!
hasTierRules
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"阶梯满减券必须配置阶梯规则"
);
}
// 如果不是阶梯满减券,不能配置阶梯规则
if
(!
isTierCoupon
&&
hasTierRules
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"固定面额券不能配置阶梯规则"
);
}
// 校验阶梯规则的合理性
if
(
hasTierRules
)
{
// 校验阶梯层级是否重复
long
distinctLevels
=
addVO
.
getTierRules
().
stream
()
.
map
(
CouponTierRuleReqVO:
:
getTierLevel
)
.
distinct
()
.
count
();
if
(
distinctLevels
!=
addVO
.
getTierRules
().
size
())
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"阶梯层级不能重复"
);
}
// 校验门槛金额是否递增
List
<
CouponTierRuleReqVO
>
sortedRules
=
addVO
.
getTierRules
().
stream
()
.
sorted
(
java
.
util
.
Comparator
.
comparing
(
CouponTierRuleReqVO:
:
getTierLevel
))
.
toList
();
for
(
int
i
=
1
;
i
<
sortedRules
.
size
();
i
++)
{
if
(
sortedRules
.
get
(
i
).
getThresholdAmount
().
compareTo
(
sortedRules
.
get
(
i
-
1
).
getThresholdAmount
())
<=
0
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"阶梯门槛金额必须递增"
);
}
}
}
}
/**
* 保存阶梯规则
*/
private
void
saveTierRules
(
String
templateId
,
List
<
CouponTierRuleReqVO
>
tierRules
,
String
opName
,
LocalDateTime
now
)
{
List
<
CouponTierRule
>
ruleList
=
new
ArrayList
<>();
for
(
CouponTierRuleReqVO
ruleVO
:
tierRules
)
{
CouponTierRule
rule
=
CouponTierRule
.
builder
()
.
templateId
(
templateId
)
.
tierLevel
(
ruleVO
.
getTierLevel
())
.
thresholdAmount
(
ruleVO
.
getThresholdAmount
())
.
deductionAmount
(
ruleVO
.
getDeductionAmount
())
.
deleteFlag
(
"N"
)
.
build
();
rule
.
setCreateBy
(
opName
);
rule
.
setCreateTime
(
now
);
ruleList
.
add
(
rule
);
}
couponTierRuleRepository
.
saveAll
(
ruleList
);
log
.
info
(
"保存阶梯规则成功:templateId={}, 规则数={}"
,
templateId
,
tierRules
.
size
());
}
}
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponVerificationService.java
View file @
60f7c89
...
...
@@ -195,21 +195,21 @@ public class CouponVerificationService {
// 1. 查询优惠券
CouponAccount
coupon
=
couponAccountRepository
.
findByCouponIdAndDeleteFlag
(
couponId
,
"N"
)
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券不存在"
));
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券不存在"
));
// 2. 校验优惠券归属
if
(!
coupon
.
getAccountId
().
equals
(
accountId
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券不属于当前用户"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券不属于当前用户"
);
}
// 3. 校验优惠券状态
if
(!
coupon
.
getStatus
().
equals
(
CouponUserStatusEnum
.
UNUSED
.
getCode
()))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券状态不可用"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券状态不可用"
);
}
// 4. 校验有效期
if
(
now
.
isBefore
(
coupon
.
getValidStartTime
())
||
now
.
isAfter
(
coupon
.
getValidEndTime
()))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
PARAM
_ERROR
,
"优惠券已过期或未生效"
);
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON
_ERROR
,
"优惠券已过期或未生效"
);
}
// 5. 计算实际抵扣金额
...
...
aus-core/src/main/java/com/glsx/usedcar/module/coupon/vo/CouponTemplateAddVO.java
View file @
60f7c89
...
...
@@ -8,6 +8,7 @@ import java.io.Serial;
import
java.io.Serializable
;
import
java.math.BigDecimal
;
import
java.time.LocalDateTime
;
import
java.util.List
;
/**
* 优惠券模板新增请求 VO
...
...
@@ -69,4 +70,7 @@ public class CouponTemplateAddVO implements Serializable {
@Schema
(
description
=
"备注(后台可见)"
,
requiredMode
=
Schema
.
RequiredMode
.
NOT_REQUIRED
,
example
=
"新用户专享"
)
@Size
(
max
=
200
,
message
=
"备注长度不能超过 200 个字符"
)
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