Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jiedao-api-boot-master
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lanbaoming
jiedao-api-boot-master
Commits
fd7d53f8
Commit
fd7d53f8
authored
Aug 01, 2024
by
zhangfeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
兑换礼品换锁
parent
06d7e08e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
16 deletions
+24
-16
RedeemRewardApiImpl.java
...r/yudao/module/reward/api/reward/RedeemRewardApiImpl.java
+24
-16
No files found.
yudao-module-reward/yudao-module-reward-impl/src/main/java/cn/iocoder/yudao/module/reward/api/reward/RedeemRewardApiImpl.java
View file @
fd7d53f8
...
...
@@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.reward.api.reward;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.lang.generator.SnowflakeGenerator
;
import
cn.iocoder.yudao.framework.
redis.helper.RedisDistributedLock
;
import
cn.iocoder.yudao.framework.
common.exception.ServiceException
;
import
cn.iocoder.yudao.module.member.api.score.MemberUserScoreApi
;
import
cn.iocoder.yudao.module.member.api.score.dto.MemberUserScoreOperateReqDTO
;
import
cn.iocoder.yudao.module.member.api.user.MemberUserApi
;
...
...
@@ -18,12 +18,18 @@ import cn.iocoder.yudao.module.reward.dal.mysql.reward.RewardMapper;
import
cn.iocoder.yudao.module.reward.enums.RewardRedeemStatusEnum
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
lombok.extern.slf4j.Slf4j
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RedissonClient
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.validation.annotation.Validated
;
import
javax.annotation.Resource
;
import
java.util.*
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
exception
.
util
.
ServiceExceptionUtil
.
exception
;
import
static
cn
.
iocoder
.
yudao
.
module
.
infra
.
enums
.
ErrorCodeConstants
.
GET_LOCK_FAILED
;
...
...
@@ -42,7 +48,7 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
@Resource
private
MemberUserApi
memberUserApi
;
@Resource
private
Redis
DistributedLock
redisDistributedLock
;
private
Redis
sonClient
redissonClient
;
@Resource
private
MemberUserScoreApi
memberUserScoreApi
;
@Resource
...
...
@@ -71,10 +77,10 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
throw
exception
(
REWARD_PICK_METHOD_NOT_ALLOW_CREATE
);
}
verifyMemberUser
(
redeemRewardReqVO
,
rewardDO
);
boolean
lock
=
false
;
String
lockKey
=
"reward:redeem:lock:"
+
redeemRewardReqVO
.
getRewardId
();
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
try
{
lock
=
redisDistributedLock
.
lock
(
"reward:redeem:lock:"
+
redeemRewardReqVO
.
getRewardId
());
if
(!
lock
)
{
if
(!
lock
.
tryLock
(
2
,
10
,
TimeUnit
.
SECONDS
))
{
throw
exception
(
GET_LOCK_FAILED
);
}
// 添加兑换记录
...
...
@@ -83,10 +89,10 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
updateMemberScore
(
redeemRewardReqVO
,
rewardDO
,
redeemId
);
// 更新礼品
redeemReward
(
rewardDO
,
redeemRewardReqVO
.
getRewardCount
());
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
Interrupted
Exception
e
)
{
throw
exception
(
GET_LOCK_FAILED
);
}
finally
{
redisDistributedLock
.
releaseLock
(
"reward:redeem:lock:"
+
redeemRewardReqVO
.
getRewardId
()
);
lock
.
unlock
(
);
}
return
null
;
}
...
...
@@ -118,7 +124,7 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
RewardRedeemDO
rewardRedeemDO
=
BeanUtil
.
copyProperties
(
redeemRewardReqVO
,
RewardRedeemDO
.
class
);
rewardRedeemDO
.
setId
(
snowflakeGenerator
.
next
());
rewardRedeemDO
.
setStatus
(
RewardRedeemStatusEnum
.
REDEEMING
.
getValue
());
rewardRedeemDO
.
setScoreCount
(
redeemRewardReqVO
.
getRewardCount
()*
rewardDO
.
getPointsRequire
());
rewardRedeemDO
.
setScoreCount
(
redeemRewardReqVO
.
getRewardCount
()
*
rewardDO
.
getPointsRequire
());
rewardRedeemMapper
.
insert
(
rewardRedeemDO
);
return
rewardRedeemDO
.
getId
();
}
...
...
@@ -156,10 +162,10 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
throw
exception
(
REWARD_COUNT_NOT_ENOUGH
);
}
}
boolean
lock
=
false
;
String
lockKey
=
"reward:redeem:lock:"
+
rewardId
;
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
try
{
lock
=
redisDistributedLock
.
lock
(
"reward:redeem:lock:"
+
rewardId
);
if
(!
lock
)
{
if
(!
lock
.
tryLock
(
2
,
10
,
TimeUnit
.
SECONDS
))
{
throw
exception
(
GET_LOCK_FAILED
);
}
for
(
RedeemRewardReqVO
redeemRewardReqVO
:
redeemRewardReqVOList
)
{
...
...
@@ -170,11 +176,13 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
// 更新礼品
redeemReward
(
rewardDO
,
redeemRewardReqVO
.
getRewardCount
());
}
}
catch
(
Exception
e
)
{
log
.
error
(
"
redeem reward exception"
,
e
);
}
catch
(
Service
Exception
e
)
{
log
.
error
(
"
batch redeem rewards exception"
,
e
);
throw
e
;
}
catch
(
InterruptedException
e
)
{
throw
exception
(
GET_LOCK_FAILED
);
}
finally
{
redisDistributedLock
.
releaseLock
(
"reward:redeem:lock:"
+
rewardId
);
lock
.
unlock
(
);
}
return
null
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment