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 c2f594e5
authored
Apr 13, 2026
by
章家宝
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat:【优惠券需求】优惠券发放记录-重新发放
1 parent
c1ccb0c0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
251 additions
and
3 deletions
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dao/CouponDistributionRecordRepository.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dto/CouponDistributionRecordDTO.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/entity/CouponDistributionRecord.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponActivityService.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/vo/CouponDistributionRecordRespVO.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dao/CouponDistributionRecordRepository.java
View file @
c2f594e
...
...
@@ -12,6 +12,7 @@ import org.summerframework.orm.BaseRepository;
import
java.time.LocalDateTime
;
import
java.util.List
;
import
java.util.Optional
;
/**
* 优惠券发放记录数据访问层
...
...
@@ -132,7 +133,8 @@ public interface CouponDistributionRecordRepository extends BaseRepository<Coupo
v.transactionAmount,
v.actualDeduction,
v.writeOffTime,
c.validEndTime
c.validEndTime,
r.reissueStatus
)
FROM CouponDistributionRecord r
LEFT JOIN CouponAccount c ON r.couponId = c.couponId AND c.deleteFlag = 'N'
...
...
@@ -161,4 +163,13 @@ public interface CouponDistributionRecordRepository extends BaseRepository<Coupo
@Param
(
"endTime"
)
LocalDateTime
endTime
,
Pageable
pageable
);
/**
* 根据主键 ID 和删除标识查询
*
* @param id 主键 ID
* @param deleteFlag 删除标识
* @return 发放记录
*/
Optional
<
CouponDistributionRecord
>
findByIdAndDeleteFlag
(
Long
id
,
String
deleteFlag
);
}
aus-core/src/main/java/com/glsx/usedcar/module/coupon/dto/CouponDistributionRecordDTO.java
View file @
c2f594e
...
...
@@ -42,4 +42,5 @@ public class CouponDistributionRecordDTO implements Serializable {
private
BigDecimal
actualDeduction
;
private
LocalDateTime
useTime
;
private
LocalDateTime
expireTime
;
private
Integer
reissueStatus
;
}
\ No newline at end of file
aus-core/src/main/java/com/glsx/usedcar/module/coupon/entity/CouponDistributionRecord.java
View file @
c2f594e
...
...
@@ -155,6 +155,28 @@ public class CouponDistributionRecord extends AuditingEntity implements Serializ
private
String
operator
;
/**
* 重新发放状态 (0:未重新发放;1:已重新发放)
*/
@JsonView
(
value
=
Summary
.
class
)
@Column
(
name
=
"reissue_status"
,
nullable
=
false
)
@Builder
.
Default
private
Integer
reissueStatus
=
0
;
/**
* 重新发放时间
*/
@JsonView
(
value
=
Summary
.
class
)
@Column
(
name
=
"reissue_time"
,
nullable
=
true
)
private
LocalDateTime
reissueTime
;
/**
* 重新发放生成的新记录ID
*/
@JsonView
(
value
=
Summary
.
class
)
@Column
(
name
=
"reissue_record_id"
,
nullable
=
true
,
length
=
50
)
private
String
reissueRecordId
;
/**
* 删除标识 N:未删除 D:已删除
*/
@Builder
.
Default
...
...
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponActivityService.java
View file @
c2f594e
...
...
@@ -490,6 +490,7 @@ public class CouponActivityService {
);
// 3. DTO 转 VO
LocalDateTime
now
=
LocalDateTime
.
now
();
return
dtoPage
.
map
(
dto
->
{
CouponDistributionRecordRespVO
vo
=
new
CouponDistributionRecordRespVO
();
vo
.
setId
(
dto
.
getId
());
...
...
@@ -516,17 +517,224 @@ public class CouponActivityService {
vo
.
setActualDeduction
(
dto
.
getActualDeduction
());
vo
.
setUseTime
(
dto
.
getUseTime
());
vo
.
setExpireTime
(
dto
.
getExpireTime
());
// 判断是否可以重新发放
evaluateCanReissue
(
dto
,
vo
,
now
);
return
vo
;
});
}
/**
* 判断是否可以重新发放,并设置原因
*
* 按钮显隐逻辑:仅当状态为「已失效(3)」或「已退回(4)」且发放类型为「手动发放(2)」时才显示按钮
* 按钮可点击判定:模板必须处于启用中(1)状态,且库存充足
*/
private
void
evaluateCanReissue
(
CouponDistributionRecordDTO
dto
,
CouponDistributionRecordRespVO
vo
,
LocalDateTime
now
)
{
// 1. 按钮显隐:仅当状态为「已失效(3)」或「已退回(4)」且发放类型为「手动发放(2)」时才显示按钮
boolean
isExpiredOrReturned
=
dto
.
getCouponStatus
()
!=
null
&&
(
dto
.
getCouponStatus
().
equals
(
CouponUserStatusEnum
.
EXPIRED
.
getCode
())
||
dto
.
getCouponStatus
().
equals
(
CouponUserStatusEnum
.
RETURNED
.
getCode
()));
boolean
isManualDistribution
=
dto
.
getDistributionType
()
!=
null
&&
dto
.
getDistributionType
().
equals
(
CouponDistributionTypeEnum
.
MANUAL
.
getCode
());
if
(!
isExpiredOrReturned
||
!
isManualDistribution
)
{
// 不满足显示条件,按钮不显示(canReissue设为null表示不显示)
vo
.
setCanReissue
(
null
);
vo
.
setReissueDenyReason
(
null
);
return
;
}
// 2. 检查是否已经重新发放过
if
(
dto
.
getReissueStatus
()
!=
null
&&
dto
.
getReissueStatus
()
==
1
)
{
vo
.
setCanReissue
(
false
);
vo
.
setReissueDenyReason
(
"已重发"
);
return
;
}
// 3. 查询优惠券模板进行深度校验
CouponTemplate
template
=
couponTemplateRepository
.
findByIdAndDeleteFlag
(
dto
.
getTemplateId
(),
DeleteFlag
.
NOT_DELETED
.
getCode
())
.
orElse
(
null
);
if
(
template
==
null
)
{
vo
.
setCanReissue
(
false
);
vo
.
setReissueDenyReason
(
"原券已下架/停用无法补发"
);
return
;
}
// 4. 判断A:模板周期有效性 - 检查模板是否处于"启用中"状态(1)
if
(!
template
.
getStatus
().
equals
(
1
))
{
vo
.
setCanReissue
(
false
);
vo
.
setReissueDenyReason
(
"原券已下架/停用无法补发"
);
return
;
}
// 5. 判断B:模板库存 - 剩余库存 > 0 或 无限制
if
(
template
.
getRemainingQuantity
()
!=
null
&&
template
.
getRemainingQuantity
()
<=
0
)
{
vo
.
setCanReissue
(
false
);
vo
.
setReissueDenyReason
(
"原券可用库存不足"
);
return
;
}
// 所有检查通过,可以重新发放
vo
.
setCanReissue
(
true
);
vo
.
setReissueDenyReason
(
null
);
}
/**
* 重新发放优惠券
*/
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
reissueCoupon
(
CouponReissueVO
vo
,
Integer
opId
,
String
opName
)
{
// TODO: 实现重新发放逻辑
log
.
info
(
"重新发放优惠券:recordId={}, 操作人={}"
,
vo
.
getRecordId
(),
opName
);
if
(
opId
==
null
||
StrUtil
.
isBlank
(
opName
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"操作人信息不能为空"
);
}
LocalDateTime
now
=
LocalDateTime
.
now
();
// 1. 查询发放记录
CouponDistributionRecord
distributionRecord
=
distributionRecordRepository
.
findByIdAndDeleteFlag
(
vo
.
getRecordId
(),
DeleteFlag
.
NOT_DELETED
.
getCode
())
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
DATA_NOT_EXISTS
,
"发放记录不存在"
));
// 2. 校验发放类型必须为手动发放(2)
if
(!
distributionRecord
.
getDistributionType
().
equals
(
CouponDistributionTypeEnum
.
MANUAL
.
getCode
()))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"仅支持手动发放的优惠券重新发放"
);
}
// 3. 检查是否已经重新发放过
if
(
distributionRecord
.
getReissueStatus
()
!=
null
&&
distributionRecord
.
getReissueStatus
()
==
1
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"该发放记录已重新发放过,不能重复重新发放"
);
}
// 4. 查询原优惠券并校验状态必须为已失效(3)或已退回(4)
if
(
StrUtil
.
isBlank
(
distributionRecord
.
getCouponId
()))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"原优惠券 ID 不存在"
);
}
CouponAccount
originalCoupon
=
couponAccountRepository
.
findByCouponIdAndDeleteFlag
(
distributionRecord
.
getCouponId
(),
DeleteFlag
.
NOT_DELETED
.
getCode
())
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
DATA_NOT_EXISTS
,
"原优惠券不存在"
));
// 校验原优惠券状态必须为已失效(3)或已退回(4)
boolean
isExpiredOrReturned
=
originalCoupon
.
getStatus
().
equals
(
CouponUserStatusEnum
.
EXPIRED
.
getCode
())
||
originalCoupon
.
getStatus
().
equals
(
CouponUserStatusEnum
.
RETURNED
.
getCode
());
if
(!
isExpiredOrReturned
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"仅已失效或已退回的优惠券可以重新发放"
);
}
// 5. 查询优惠券模板并校验
CouponTemplate
template
=
couponTemplateRepository
.
findByIdAndDeleteFlag
(
distributionRecord
.
getTemplateId
(),
DeleteFlag
.
NOT_DELETED
.
getCode
())
.
orElseThrow
(()
->
new
BusinessCodeException
(
ErrorCodeEnum
.
DATA_NOT_EXISTS
,
"优惠券模板不存在"
));
// 5.1 校验模板状态必须为启用中(1)
if
(!
template
.
getStatus
().
equals
(
1
))
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"原券已下架/停用无法补发"
);
}
// 5.2 校验模板库存
if
(
template
.
getRemainingQuantity
()
!=
null
&&
template
.
getRemainingQuantity
()
<=
0
)
{
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"原券可用库存不足"
);
}
// 7. 重新发放优惠券(调用现有的发放逻辑,会自动扣减库存)
String
newCouponId
=
"CPN"
+
System
.
currentTimeMillis
()
+
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
).
substring
(
0
,
6
).
toUpperCase
();
String
newRecordId
=
"DIST"
+
System
.
currentTimeMillis
()
+
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
).
substring
(
0
,
6
).
toUpperCase
();
// 计算有效期
LocalDateTime
validStartTime
=
now
;
LocalDateTime
validEndTime
;
if
(
template
.
getValidityType
()
==
1
)
{
// 固定时间
validStartTime
=
template
.
getValidStartTime
();
validEndTime
=
template
.
getValidEndTime
();
}
else
{
// 相对时间
int
delayDays
=
template
.
getDelayDays
()
!=
null
?
template
.
getDelayDays
()
:
0
;
int
validDays
=
template
.
getValidDays
();
validStartTime
=
now
.
plusDays
(
delayDays
);
validEndTime
=
validStartTime
.
plusDays
(
validDays
);
}
// 先扣减库存(原子操作,防止并发超发)
if
(
template
.
getRemainingQuantity
()
!=
null
)
{
int
affectedRows
=
couponTemplateRepository
.
deductStock
(
template
.
getId
(),
1
);
if
(
affectedRows
==
0
)
{
log
.
warn
(
"优惠券模板库存不足:templateId={}, templateName={}"
,
template
.
getId
(),
template
.
getTemplateName
());
throw
new
BusinessCodeException
(
ErrorCodeEnum
.
COMMON_ERROR
,
"优惠券库存不足,模板:"
+
template
.
getTemplateName
());
}
}
try
{
// 生成新的优惠券实例
CouponAccount
newCoupon
=
CouponAccount
.
builder
()
.
couponId
(
newCouponId
)
.
templateId
(
template
.
getId
())
.
templateName
(
template
.
getTemplateName
())
.
accountId
(
originalCoupon
.
getAccountId
())
.
couponType
(
template
.
getCouponType
())
.
faceAmount
(
template
.
getFaceAmount
())
.
thresholdAmount
(
template
.
getThresholdAmount
())
.
deductionType
(
template
.
getDeductionType
())
.
status
(
CouponUserStatusEnum
.
UNUSED
.
getCode
())
.
validStartTime
(
validStartTime
)
.
validEndTime
(
validEndTime
)
.
distributionType
(
CouponDistributionTypeEnum
.
MANUAL
.
getCode
())
.
distributionSource
(
"重新发放"
+
(
StrUtil
.
isNotBlank
(
vo
.
getRemark
())
?
" - "
+
vo
.
getRemark
()
:
""
))
.
notificationStatus
(
0
)
// 待通知
.
deleteFlag
(
"N"
)
.
build
();
newCoupon
.
setCreateBy
(
opName
);
newCoupon
.
setCreateTime
(
now
);
couponAccountRepository
.
save
(
newCoupon
);
// 创建新的发放记录
CouponDistributionRecord
newRecord
=
CouponDistributionRecord
.
builder
()
.
recordId
(
newRecordId
)
.
activityId
(
distributionRecord
.
getActivityId
())
.
activityName
(
distributionRecord
.
getActivityName
())
.
templateId
(
template
.
getId
())
.
templateName
(
template
.
getTemplateName
())
.
couponId
(
newCouponId
)
.
accountId
(
originalCoupon
.
getAccountId
())
.
mobile
(
distributionRecord
.
getMobile
())
.
distributionType
(
CouponDistributionTypeEnum
.
MANUAL
.
getCode
())
.
faceAmount
(
template
.
getFaceAmount
())
.
distributionRemark
(
"[由原记录单号("
+
distributionRecord
.
getRecordId
()
+
")重新发放触发]"
+
(
StrUtil
.
isNotBlank
(
vo
.
getRemark
())
?
" "
+
vo
.
getRemark
()
:
""
))
.
distributionTime
(
now
)
.
operator
(
opName
)
.
reissueStatus
(
0
)
.
deleteFlag
(
"N"
)
.
build
();
newRecord
.
setCreateBy
(
opName
);
newRecord
.
setCreateTime
(
now
);
distributionRecordRepository
.
save
(
newRecord
);
// 8. 更新原发放记录的重新发放状态
distributionRecord
.
setReissueStatus
(
1
);
distributionRecord
.
setReissueTime
(
now
);
distributionRecord
.
setReissueRecordId
(
newRecordId
);
distributionRecord
.
setUpdateBy
(
opName
);
distributionRecord
.
setUpdateTime
(
now
);
distributionRecordRepository
.
save
(
distributionRecord
);
log
.
info
(
"重新发放优惠券成功:原记录 ID={}, 新优惠券 ID={}, 新用户 ID={}, 操作人={}"
,
distributionRecord
.
getId
(),
newCouponId
,
originalCoupon
.
getAccountId
(),
opName
);
}
catch
(
Exception
e
)
{
log
.
error
(
"重新发放优惠券失败:原记录 ID={}, templateId={}"
,
distributionRecord
.
getId
(),
template
.
getId
(),
e
);
throw
e
;
}
}
/**
...
...
aus-core/src/main/java/com/glsx/usedcar/module/coupon/vo/CouponDistributionRecordRespVO.java
View file @
c2f594e
...
...
@@ -90,4 +90,10 @@ public class CouponDistributionRecordRespVO implements Serializable {
@Schema
(
description
=
"过期时间"
)
private
LocalDateTime
expireTime
;
@Schema
(
description
=
"是否可以重新发放(用于前端控制按钮显示)"
)
private
Boolean
canReissue
;
@Schema
(
description
=
"不可重新发放的原因(用于前端提示)"
)
private
String
reissueDenyReason
;
}
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