Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
项目统一框架
amos-boot-biz
Commits
b5bc3343
Commit
b5bc3343
authored
Dec 14, 2023
by
LiuLin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(tcm):申请单编码生成代码开发
parent
08710569
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
10 deletions
+50
-10
CreateCodeController.java
.../boot/module/tcm/api/controller/CreateCodeController.java
+3
-3
ICreateCodeService.java
.../amos/boot/module/tcm/api/service/ICreateCodeService.java
+6
-1
CreateCodeServiceImpl.java
...ot/module/tcm/biz/service/impl/CreateCodeServiceImpl.java
+41
-6
No files found.
amos-boot-system-tzs/amos-boot-module-tcm/amos-boot-module-tcm-api/src/main/java/com/yeejoin/amos/boot/module/tcm/api/controller/CreateCodeController.java
View file @
b5bc3343
...
...
@@ -38,8 +38,8 @@ public class CreateCodeController extends BaseController {
@PostMapping
(
value
=
"/ANCode"
)
@ApiOperation
(
httpMethod
=
"POST"
,
value
=
"申请单编号生成"
,
notes
=
"申请单编号生成"
)
public
ResponseModel
<
List
<
String
>>
CreateANCode
(
@RequestParam
(
"type"
)
String
type
,
@RequestParam
(
"
num"
)
int
num
)
{
return
ResponseHelper
.
buildResponse
(
createCodeService
.
createApplicationFormCode
(
type
,
num
));
@RequestParam
(
"
batchSize"
)
int
batchSize
)
{
return
ResponseHelper
.
buildResponse
(
createCodeService
.
createApplicationFormCode
(
type
,
batchSize
));
}
/**
...
...
@@ -53,6 +53,6 @@ public class CreateCodeController extends BaseController {
@ApiOperation
(
httpMethod
=
"POST"
,
value
=
"申请单编号生成"
,
notes
=
"申请单编号生成"
)
public
ResponseModel
<
List
<
String
>>
CreateANCode1
(
@RequestParam
(
"type"
)
String
type
,
@RequestParam
(
"num"
)
int
num
)
{
return
ResponseHelper
.
buildResponse
(
createCodeService
.
CreateAN
Code
(
type
,
num
));
return
ResponseHelper
.
buildResponse
(
createCodeService
.
createApplicationForm
Code
(
type
,
num
));
}
}
amos-boot-system-tzs/amos-boot-module-tcm/amos-boot-module-tcm-api/src/main/java/com/yeejoin/amos/boot/module/tcm/api/service/ICreateCodeService.java
View file @
b5bc3343
...
...
@@ -13,5 +13,10 @@ public interface ICreateCodeService {
List
<
String
>
createApplicationFormCode
(
String
type
,
int
batchSize
);
String
createDeviceRegistrationCode
();
/**
* 生成设备注册编码(20位)
* @param key key
* @return 顺序编号
*/
String
createDeviceRegistrationCode
(
String
key
);
}
amos-boot-system-tzs/amos-boot-module-tcm/amos-boot-module-tcm-biz/src/main/java/com/yeejoin/amos/boot/module/tcm/biz/service/impl/CreateCodeServiceImpl.java
View file @
b5bc3343
...
...
@@ -18,7 +18,8 @@ import java.util.concurrent.TimeUnit;
public
class
CreateCodeServiceImpl
implements
ICreateCodeService
{
private
static
final
String
LOCK_VALUE
=
"locked"
;
private
static
final
String
LOCK_KEY
=
"sequence_lock"
;
private
static
final
String
LOCK_KEY_AF
=
"sequence_lock_af"
;
private
static
final
String
LOCK_KEY_DR
=
"sequence_lock_dr"
;
private
final
RedisTemplate
<
String
,
String
>
redisTemplate
;
private
String
rulePrefix
;
...
...
@@ -32,12 +33,12 @@ public class CreateCodeServiceImpl implements ICreateCodeService {
return
Collections
.
emptyList
();
}
rulePrefix
=
type
+
LocalDate
.
now
().
format
(
DateTimeFormatter
.
ofPattern
(
"yyyyMMdd"
));
return
this
.
generateBatchSequence
(
type
,
batchSize
);
return
generateBatchSequence
(
type
,
batchSize
);
}
@Override
public
String
createDeviceRegistrationCode
()
{
return
null
;
public
String
createDeviceRegistrationCode
(
String
key
)
{
return
generateSequence
(
key
)
;
}
public
boolean
isValueInEnum
(
String
value
)
{
...
...
@@ -48,7 +49,7 @@ public class CreateCodeServiceImpl implements ICreateCodeService {
public
List
<
String
>
generateBatchSequence
(
String
sequenceKey
,
int
batchSize
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
LOCK_KEY
,
LOCK_VALUE
);
Boolean
lockAcquired
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
LOCK_KEY
_AF
,
LOCK_VALUE
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
try
{
// 获取当前顺序码
...
...
@@ -76,7 +77,41 @@ public class CreateCodeServiceImpl implements ICreateCodeService {
return
sequenceList
;
}
finally
{
redisTemplate
.
delete
(
LOCK_KEY
);
redisTemplate
.
delete
(
LOCK_KEY_AF
);
}
}
else
{
// 获取锁失败,可以选择重试或采取其他策略
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
public
String
generateSequence
(
String
sequenceKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
LOCK_KEY_DR
,
LOCK_VALUE
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
try
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
// 如果为空,则初始化为0
if
(
currentSequenceStr
==
null
)
{
currentSequenceStr
=
"0"
;
}
// 将当前顺序码加1
Long
currentSequence
=
Long
.
parseLong
(
currentSequenceStr
);
currentSequence
++;
// 生成10位顺序码
String
formattedSequence
=
String
.
format
(
"%04d"
,
currentSequence
);
// 更新顺序码
setValueWithMonthlyExpiration
(
sequenceKey
,
String
.
valueOf
(
formattedSequence
));
return
sequenceKey
+
formattedSequence
;
}
finally
{
redisTemplate
.
delete
(
LOCK_KEY_DR
);
}
}
else
{
// 获取锁失败,可以选择重试或采取其他策略
...
...
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