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 d17e9b5f
authored
Apr 14, 2026
by
章家宝
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat:【优惠券需求】小程序获取订单优惠券
1 parent
b1e380ec
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
252 additions
and
2 deletions
aus-api/src/main/java/com/glsx/usedcar/web/api/coupon/CouponAccountController.java
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponVerificationService.java
aus-api/src/main/java/com/glsx/usedcar/web/api/coupon/CouponAccountController.java
View file @
d17e9b5
package
com
.
glsx
.
usedcar
.
web
.
api
.
coupon
;
package
com
.
glsx
.
usedcar
.
web
.
api
.
coupon
;
import
com.glsx.usedcar.module.account.entity.Account
;
import
com.glsx.usedcar.module.account.entity.Account
;
import
com.glsx.usedcar.module.auction.entity.AuctionOrder
;
import
com.glsx.usedcar.module.auction.service.AuctionOrderService
;
import
com.glsx.usedcar.module.coupon.service.CouponAccountService
;
import
com.glsx.usedcar.module.coupon.service.CouponAccountService
;
import
com.glsx.usedcar.module.coupon.service.CouponVerificationService
;
import
com.glsx.usedcar.module.coupon.service.CouponVerificationService
;
import
com.glsx.usedcar.module.coupon.vo.CouponAccountRespVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponAccountRespVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponPopupRespVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponPopupRespVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponVerifyReqVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponVerifyReqVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponVerifyRespVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponVerifyRespVO
;
import
com.glsx.usedcar.module.coupon.vo.OrderCouponRespVO
;
import
com.glsx.usedcar.resp.BaseResp
;
import
com.glsx.usedcar.resp.BaseResp
;
import
io.swagger.v3.oas.annotations.Operation
;
import
io.swagger.v3.oas.annotations.Operation
;
import
io.swagger.v3.oas.annotations.Parameter
;
import
io.swagger.v3.oas.annotations.Parameter
;
...
@@ -34,9 +37,10 @@ public class CouponAccountController {
...
@@ -34,9 +37,10 @@ public class CouponAccountController {
private
final
CouponAccountService
couponAccountService
;
private
final
CouponAccountService
couponAccountService
;
private
final
CouponVerificationService
couponVerificationService
;
private
final
CouponVerificationService
couponVerificationService
;
private
final
AuctionOrderService
auctionOrderService
;
@Operation
(
summary
=
"获取我的优惠券列表"
,
description
=
"查询当前登录用户的优惠券列表,支持按状态筛选"
)
@Operation
(
summary
=
"获取我的优惠券列表"
,
description
=
"查询当前登录用户的优惠券列表,支持按状态筛选"
)
@GetMapping
(
"/
myL
ist"
)
@GetMapping
(
"/
l
ist"
)
public
BaseResp
<
List
<
CouponAccountRespVO
>>
getMyCoupons
(
public
BaseResp
<
List
<
CouponAccountRespVO
>>
getMyCoupons
(
@Parameter
(
description
=
"状态 (1:待使用;2:已核销;3:已过期)"
,
example
=
"1"
)
@Parameter
(
description
=
"状态 (1:待使用;2:已核销;3:已过期)"
,
example
=
"1"
)
@RequestParam
(
name
=
"status"
,
required
=
false
)
Integer
status
,
@RequestParam
(
name
=
"status"
,
required
=
false
)
Integer
status
,
...
@@ -142,4 +146,44 @@ public class CouponAccountController {
...
@@ -142,4 +146,44 @@ public class CouponAccountController {
return
BaseResp
.
fail
(
"系统异常:核销优惠券失败"
);
return
BaseResp
.
fail
(
"系统异常:核销优惠券失败"
);
}
}
}
}
@Operation
(
summary
=
"获取订单可用优惠券列表"
,
description
=
"根据订单ID查询该订单可用的优惠券列表,按优先级排序"
)
@GetMapping
(
"/order-coupons/{orderId}"
)
public
BaseResp
<
List
<
OrderCouponRespVO
>>
getOrderCoupons
(
@Parameter
(
description
=
"订单 ID"
,
required
=
true
,
example
=
"123"
)
@PathVariable
(
"orderId"
)
Integer
orderId
,
@RequestAttribute
(
value
=
LOGIN_ACCOUNT
,
required
=
false
)
Account
account
)
{
log
.
info
(
"【接口请求】获取订单可用优惠券列表,orderId={}"
,
orderId
);
try
{
Integer
accountId
=
account
.
getAccountId
();
// 1. 查询订单信息
AuctionOrder
auctionOrder
=
auctionOrderService
.
getAuctionOrderById
(
orderId
);
if
(
auctionOrder
==
null
)
{
return
BaseResp
.
fail
(
"订单不存在"
);
}
// 2. 校验订单归属(确保是当前用户的订单)
if
(
auctionOrder
.
getAccount
().
getAccountId
()
!=
accountId
)
{
return
BaseResp
.
fail
(
"无权查看该订单的优惠券"
);
}
// 3. 获取订单金额信息
BigDecimal
orderPrice
=
BigDecimal
.
valueOf
(
auctionOrder
.
getOrderPrice
());
BigDecimal
commissionPrice
=
BigDecimal
.
valueOf
(
auctionOrder
.
getCommissionPrice
());
// 4. 获取优惠券列表
List
<
OrderCouponRespVO
>
result
=
couponVerificationService
.
getOrderCoupons
(
accountId
,
orderId
,
orderPrice
,
commissionPrice
);
log
.
info
(
"【接口响应】获取订单可用优惠券列表成功,orderId={},共{}张券"
,
orderId
,
result
.
size
());
return
BaseResp
.
success
(
result
);
}
catch
(
BusinessCodeException
be
)
{
log
.
error
(
"【接口响应】获取订单可用优惠券列表失败,orderId={}"
,
orderId
,
be
);
return
BaseResp
.
fail
(
be
.
getCode
().
toString
(),
be
.
getMessage
());
}
catch
(
Exception
e
)
{
log
.
error
(
"【接口响应】获取订单可用优惠券列表异常,orderId={}"
,
orderId
,
e
);
return
BaseResp
.
fail
(
"系统异常:获取优惠券列表失败"
);
}
}
}
}
aus-core/src/main/java/com/glsx/usedcar/module/coupon/service/CouponVerificationService.java
View file @
d17e9b5
...
@@ -6,6 +6,7 @@ import com.glsx.usedcar.module.coupon.entity.*;
...
@@ -6,6 +6,7 @@ import com.glsx.usedcar.module.coupon.entity.*;
import
com.glsx.usedcar.module.coupon.enums.CouponTypeEnum
;
import
com.glsx.usedcar.module.coupon.enums.CouponTypeEnum
;
import
com.glsx.usedcar.module.coupon.enums.CouponUserStatusEnum
;
import
com.glsx.usedcar.module.coupon.enums.CouponUserStatusEnum
;
import
com.glsx.usedcar.module.coupon.vo.CouponAvailableVO
;
import
com.glsx.usedcar.module.coupon.vo.CouponAvailableVO
;
import
com.glsx.usedcar.module.coupon.vo.OrderCouponRespVO
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -101,7 +102,7 @@ public class CouponVerificationService {
...
@@ -101,7 +102,7 @@ public class CouponVerificationService {
}
}
/**
/**
* 计算优惠券实际抵扣金额
* 计算优惠券实际抵扣金额
(用于抵扣车款)
*/
*/
private
BigDecimal
calculateActualDeduction
(
CouponAccount
coupon
,
BigDecimal
transactionAmount
)
{
private
BigDecimal
calculateActualDeduction
(
CouponAccount
coupon
,
BigDecimal
transactionAmount
)
{
BigDecimal
faceAmount
=
coupon
.
getFaceAmount
();
BigDecimal
faceAmount
=
coupon
.
getFaceAmount
();
...
@@ -117,6 +118,23 @@ public class CouponVerificationService {
...
@@ -117,6 +118,23 @@ public class CouponVerificationService {
}
}
/**
/**
* 计算优惠券实际抵扣金额(用于抵扣佣金)
* 佣金抵扣券通常是固定面额,但如果是阶梯券也需要支持
*/
private
BigDecimal
calculateActualDeductionForCommission
(
CouponAccount
coupon
,
BigDecimal
commissionAmount
)
{
BigDecimal
faceAmount
=
coupon
.
getFaceAmount
();
// 根据优惠券类型计算抵扣金额
if
(
CouponTypeEnum
.
TIERED_AMOUNT
.
getCode
().
equals
(
coupon
.
getCouponType
()))
{
// 阶梯满减券:根据佣金金额和阶梯规则计算
return
calculateTieredDeduction
(
coupon
.
getTemplateId
(),
commissionAmount
,
faceAmount
);
}
else
{
// 固定面额券:直接返回面额
return
faceAmount
;
}
}
/**
* 计算阶梯满减券的抵扣金额
* 计算阶梯满减券的抵扣金额
*
*
* @param templateDbId 模板数据库 ID(Long 类型)
* @param templateDbId 模板数据库 ID(Long 类型)
...
@@ -355,6 +373,194 @@ public class CouponVerificationService {
...
@@ -355,6 +373,194 @@ public class CouponVerificationService {
}
}
/**
/**
* 获取订单优惠券列表(包含可用和不可用,按优先级排序)
*
* @param accountId 用户账户 ID
* @param orderId 订单 ID
* @param orderPrice 订单成交价(车款)
* @param commissionPrice 订单佣金
* @return 优惠券列表(可用券按优先级排序在前,不可用券在后)
*/
public
List
<
OrderCouponRespVO
>
getOrderCoupons
(
Integer
accountId
,
Integer
orderId
,
BigDecimal
orderPrice
,
BigDecimal
commissionPrice
)
{
LocalDateTime
now
=
LocalDateTime
.
now
();
// 查询用户所有待使用的优惠券(未过期、未核销、未退回)
List
<
CouponAccount
>
allCoupons
=
couponAccountRepository
.
findAvailableCoupons
(
accountId
,
CouponUserStatusEnum
.
UNUSED
.
getCode
(),
"N"
,
now
);
if
(
allCoupons
.
isEmpty
())
{
return
List
.
of
();
}
// 计算每种类型的最大可抵扣金额
BigDecimal
maxVehicleDeduction
=
orderPrice
;
// 车款最大抵扣金额为订单成交价
BigDecimal
maxCommissionDeduction
=
commissionPrice
;
// 佣金最大抵扣金额为订单佣金
// 转换为VO并判断可用性
List
<
OrderCouponRespVO
>
couponVOList
=
allCoupons
.
stream
()
.
map
(
coupon
->
convertToOrderCouponVO
(
coupon
,
orderPrice
,
commissionPrice
,
maxVehicleDeduction
,
maxCommissionDeduction
,
now
))
.
toList
();
// 分离可用和不可用券
List
<
OrderCouponRespVO
>
availableCoupons
=
couponVOList
.
stream
()
.
filter
(
OrderCouponRespVO:
:
getAvailable
)
.
collect
(
Collectors
.
toList
());
List
<
OrderCouponRespVO
>
unavailableCoupons
=
couponVOList
.
stream
()
.
filter
(
vo
->
!
vo
.
getAvailable
())
.
toList
();
// 对可用券进行排序
// 优先级 1:实际抵扣金额降序(让车商省最多)
// 优先级 2:过期时间升序(优先消耗快过期的券)
// 优先级 3:领取时间升序(先领取的在前)
availableCoupons
=
availableCoupons
.
stream
()
.
sorted
(
Comparator
.
comparing
(
OrderCouponRespVO:
:
getActualDeduction
,
Comparator
.
reverseOrder
())
.
thenComparing
(
OrderCouponRespVO:
:
getValidEndTime
)
.
thenComparing
(
OrderCouponRespVO:
:
getReceiveTime
)
)
.
toList
();
// 标记最推荐的券(实际抵扣金额最高的可用券)
if
(!
availableCoupons
.
isEmpty
())
{
availableCoupons
.
get
(
0
).
setRecommended
(
true
);
}
// 合并列表:可用券在前,不可用券在后
List
<
OrderCouponRespVO
>
result
=
new
ArrayList
<>();
result
.
addAll
(
availableCoupons
);
result
.
addAll
(
unavailableCoupons
);
return
result
;
}
/**
* 转换为订单优惠券 VO
*/
private
OrderCouponRespVO
convertToOrderCouponVO
(
CouponAccount
coupon
,
BigDecimal
orderPrice
,
BigDecimal
commissionPrice
,
BigDecimal
maxVehicleDeduction
,
BigDecimal
maxCommissionDeduction
,
LocalDateTime
now
)
{
OrderCouponRespVO
vo
=
new
OrderCouponRespVO
();
vo
.
setCouponId
(
coupon
.
getCouponId
());
vo
.
setTemplateName
(
coupon
.
getTemplateName
());
vo
.
setCouponType
(
coupon
.
getCouponType
());
vo
.
setDeductionType
(
coupon
.
getDeductionType
());
vo
.
setFaceAmount
(
coupon
.
getFaceAmount
());
vo
.
setThresholdAmount
(
coupon
.
getThresholdAmount
());
vo
.
setValidStartTime
(
coupon
.
getValidStartTime
());
vo
.
setValidEndTime
(
coupon
.
getValidEndTime
());
vo
.
setReceiveTime
(
coupon
.
getCreateTime
());
vo
.
setRecommended
(
false
);
// 判断优惠券是否可用
String
unavailableReason
=
checkCouponUnavailableReason
(
coupon
,
orderPrice
,
commissionPrice
,
now
);
if
(
unavailableReason
==
null
)
{
// 可用,计算实际抵扣金额
vo
.
setAvailable
(
true
);
vo
.
setUnavailableReason
(
null
);
// 根据抵扣类型确定最大可抵扣金额
BigDecimal
maxDeduction
;
if
(
coupon
.
getDeductionType
()
==
1
)
{
// 抵扣车款
maxDeduction
=
maxVehicleDeduction
;
}
else
{
// 抵扣佣金
maxDeduction
=
maxCommissionDeduction
;
}
// 计算实际抵扣金额(根据抵扣类型使用对应的金额计算)
BigDecimal
calculatedDeduction
;
if
(
coupon
.
getDeductionType
()
==
1
)
{
// 抵扣车款,使用订单成交价计算
calculatedDeduction
=
calculateActualDeduction
(
coupon
,
orderPrice
);
}
else
{
// 抵扣佣金,使用佣金金额计算(固定面额券直接返回面额,阶梯券根据佣金金额计算)
calculatedDeduction
=
calculateActualDeductionForCommission
(
coupon
,
commissionPrice
);
}
// 实际抵扣金额不能超过最大可抵扣金额
if
(
calculatedDeduction
.
compareTo
(
maxDeduction
)
>
0
)
{
vo
.
setActualDeduction
(
maxDeduction
);
}
else
{
vo
.
setActualDeduction
(
calculatedDeduction
);
}
}
else
{
// 不可用
vo
.
setAvailable
(
false
);
vo
.
setUnavailableReason
(
unavailableReason
);
vo
.
setActualDeduction
(
BigDecimal
.
ZERO
);
}
return
vo
;
}
/**
* 检查优惠券不可用的原因
* @return 不可用原因,如果可用返回 null
*/
private
String
checkCouponUnavailableReason
(
CouponAccount
coupon
,
BigDecimal
orderPrice
,
BigDecimal
commissionPrice
,
LocalDateTime
now
)
{
// 1. 检查是否已过期
if
(
now
.
isAfter
(
coupon
.
getValidEndTime
()))
{
return
"优惠券已过期"
;
}
// 2. 检查是否已被其他订单使用
CouponOrderUsage
usage
=
couponOrderUsageRepository
.
findByCouponIdAndDeleteFlag
(
coupon
.
getCouponId
(),
DeleteFlag
.
NOT_DELETED
.
getCode
()).
orElse
(
null
);
if
(
usage
!=
null
&&
usage
.
getUsageStatus
()
==
1
)
{
return
"优惠券已被使用"
;
}
// 3. 根据抵扣类型检查门槛金额
BigDecimal
transactionAmount
;
if
(
coupon
.
getDeductionType
()
==
1
)
{
// 抵扣车款,使用订单成交价判断
transactionAmount
=
orderPrice
;
}
else
{
// 抵扣佣金,使用佣金金额判断
transactionAmount
=
commissionPrice
;
}
if
(
coupon
.
getThresholdAmount
()
!=
null
&&
transactionAmount
.
compareTo
(
coupon
.
getThresholdAmount
())
<
0
)
{
return
"未达到使用门槛"
;
}
// 4. 对于阶梯券,检查是否有匹配的阶梯规则
if
(
CouponTypeEnum
.
TIERED_AMOUNT
.
getCode
().
equals
(
coupon
.
getCouponType
()))
{
BigDecimal
actualDeduction
;
if
(
coupon
.
getDeductionType
()
==
1
)
{
// 抵扣车款,使用订单成交价判断
actualDeduction
=
calculateActualDeduction
(
coupon
,
orderPrice
);
}
else
{
// 抵扣佣金,使用佣金金额判断
actualDeduction
=
calculateActualDeductionForCommission
(
coupon
,
commissionPrice
);
}
if
(
actualDeduction
.
compareTo
(
BigDecimal
.
ZERO
)
<=
0
)
{
return
"未达到阶梯门槛"
;
}
}
// 5. 检查是否有可抵扣金额(车款或佣金必须大于0)
if
(
coupon
.
getDeductionType
()
==
1
&&
orderPrice
.
compareTo
(
BigDecimal
.
ZERO
)
<=
0
)
{
return
"订单车款为0,无法使用"
;
}
if
(
coupon
.
getDeductionType
()
==
2
&&
commissionPrice
.
compareTo
(
BigDecimal
.
ZERO
)
<=
0
)
{
return
"订单佣金为0,无法使用"
;
}
return
null
;
// 可用
}
/**
* 转换为可用优惠券 VO
* 转换为可用优惠券 VO
*/
*/
private
CouponAvailableVO
convertToAvailableVO
(
CouponAccount
coupon
,
BigDecimal
transactionAmount
)
{
private
CouponAvailableVO
convertToAvailableVO
(
CouponAccount
coupon
,
BigDecimal
transactionAmount
)
{
...
...
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