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
10722f11
Commit
10722f11
authored
Dec 14, 2023
by
LiuLin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(tcm):申请单编码生成代码开发
parent
a14c3d28
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
227 additions
and
0 deletions
+227
-0
CreateCodeController.java
.../boot/module/tcm/api/controller/CreateCodeController.java
+58
-0
ApplicationFormTypeEnum.java
...os/boot/module/tcm/api/enums/ApplicationFormTypeEnum.java
+25
-0
ICreateCodeService.java
.../amos/boot/module/tcm/api/service/ICreateCodeService.java
+14
-0
CreateCodeServiceImpl.java
...ot/module/tcm/biz/service/impl/CreateCodeServiceImpl.java
+130
-0
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
0 → 100644
View file @
10722f11
package
com
.
yeejoin
.
amos
.
boot
.
module
.
tcm
.
api
.
controller
;
import
com.yeejoin.amos.boot.biz.common.controller.BaseController
;
import
com.yeejoin.amos.boot.module.tcm.api.dto.AlertCalledDto
;
import
com.yeejoin.amos.boot.module.tcm.api.service.ICreateCodeService
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
import
org.typroject.tyboot.core.foundation.enumeration.UserType
;
import
org.typroject.tyboot.core.restful.doc.TycloudOperation
;
import
org.typroject.tyboot.core.restful.utils.ResponseHelper
;
import
org.typroject.tyboot.core.restful.utils.ResponseModel
;
import
java.util.List
;
/**
*
* 生成顺序码
* @author LiuLin
* @date 2023-12-14
*/
@RestController
@Api
(
tags
=
"生成顺序码"
)
@RequestMapping
(
value
=
"/code"
)
public
class
CreateCodeController
extends
BaseController
{
@Autowired
private
ICreateCodeService
createCodeService
;
/**
* 申请单编号生成
* @param type
* @param num
* @return
*/
@TycloudOperation
(
ApiLevel
=
UserType
.
AGENCY
)
@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
));
}
/**
* 申请单编号生成
* @param type
* @param num
* @return
*/
@TycloudOperation
(
ApiLevel
=
UserType
.
AGENCY
)
@PostMapping
(
value
=
"/Device registration code"
)
@ApiOperation
(
httpMethod
=
"POST"
,
value
=
"申请单编号生成"
,
notes
=
"申请单编号生成"
)
public
ResponseModel
<
List
<
String
>>
CreateANCode1
(
@RequestParam
(
"type"
)
String
type
,
@RequestParam
(
"num"
)
int
num
)
{
return
ResponseHelper
.
buildResponse
(
createCodeService
.
CreateANCode
(
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/enums/ApplicationFormTypeEnum.java
0 → 100644
View file @
10722f11
package
com
.
yeejoin
.
amos
.
boot
.
module
.
tcm
.
api
.
enums
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
/**
* 申请单枚举
* @author LiuLin
*/
@Getter
@AllArgsConstructor
public
enum
ApplicationFormTypeEnum
{
/**
* 申请单枚举
*/
GZ
(
"GZ"
),
JY
(
"JY"
),
SY
(
"SY"
);
/**
* 编号
*/
private
final
String
code
;
}
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
0 → 100644
View file @
10722f11
package
com
.
yeejoin
.
amos
.
boot
.
module
.
tcm
.
api
.
service
;
import
java.util.List
;
public
interface
ICreateCodeService
{
/**
* 生成申请单编号
* @param type 枚举类型
* @param num 生成个数
* @return List
*/
List
<
String
>
createApplicationFormCode
(
String
type
,
int
num
);
}
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
0 → 100644
View file @
10722f11
package
com
.
yeejoin
.
amos
.
boot
.
module
.
tcm
.
biz
.
service
.
impl
;
import
com.yeejoin.amos.boot.module.tcm.api.enums.ApplicationFormTypeEnum
;
import
com.yeejoin.amos.boot.module.tcm.api.service.ICreateCodeService
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.ValueOperations
;
import
org.springframework.stereotype.Service
;
import
java.time.LocalDate
;
import
java.time.format.DateTimeFormatter
;
import
java.util.*
;
import
java.util.concurrent.TimeUnit
;
/**
* @author LiuLin
* @date 2023-12-14
*/
@Service
public
class
CreateCodeServiceImpl
implements
ICreateCodeService
{
private
static
final
String
LOCK_VALUE
=
"locked"
;
private
static
final
String
LOCK_KEY
=
"sequence_lock"
;
private
final
RedisTemplate
<
String
,
String
>
redisTemplate
;
private
String
rulePrefix
;
public
CreateCodeServiceImpl
(
RedisTemplate
<
String
,
String
>
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
}
@Override
public
List
<
String
>
createApplicationFormCode
(
String
type
,
int
batchSize
)
{
if
(!
isValueInEnum
(
type
))
{
return
Collections
.
emptyList
();
}
rulePrefix
=
type
+
LocalDate
.
now
().
format
(
DateTimeFormatter
.
ofPattern
(
"yyyyMMdd"
));
return
this
.
generateBatchSequence
(
type
,
batchSize
);
}
public
boolean
isValueInEnum
(
String
value
)
{
return
EnumSet
.
allOf
(
ApplicationFormTypeEnum
.
class
)
.
stream
()
.
anyMatch
(
enumValue
->
enumValue
.
name
().
equals
(
value
));
}
public
List
<
String
>
generateBatchSequence
(
String
sequenceKey
,
int
batchSize
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
LOCK_KEY
,
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
);
// 生成批量顺序码
List
<
String
>
sequenceList
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
batchSize
;
i
++)
{
currentSequence
++;
// 生成3位顺序码
String
formattedSequence
=
String
.
format
(
"%03d"
,
currentSequence
);
sequenceList
.
add
(
rulePrefix
+
formattedSequence
);
// 更新顺序码
setValueWithDailyExpiration
(
sequenceKey
,
String
.
valueOf
(
formattedSequence
));
}
return
sequenceList
;
}
finally
{
redisTemplate
.
delete
(
LOCK_KEY
);
}
}
else
{
// 获取锁失败,可以选择重试或采取其他策略
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
/**
* redis 根据自然月过期
*
* @param key key
* @param value value
*/
public
void
setValueWithMonthlyExpiration
(
String
key
,
String
value
)
{
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
valueOps
.
set
(
key
,
value
);
Date
currentDate
=
new
Date
();
Date
endOfMonth
=
calculateEndOfMonth
(
currentDate
);
long
expirationTimeInSeconds
=
endOfMonth
.
getTime
()
-
currentDate
.
getTime
();
redisTemplate
.
expire
(
key
,
expirationTimeInSeconds
,
TimeUnit
.
MILLISECONDS
);
}
public
void
setValueWithDailyExpiration
(
String
key
,
String
value
)
{
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
valueOps
.
set
(
key
,
value
);
Date
currentDate
=
new
Date
();
Date
endOfDay
=
calculateEndOfDay
(
currentDate
);
long
expirationTimeInSeconds
=
endOfDay
.
getTime
()
-
currentDate
.
getTime
();
redisTemplate
.
expire
(
key
,
expirationTimeInSeconds
,
TimeUnit
.
MILLISECONDS
);
}
private
Date
calculateEndOfMonth
(
Date
currentDate
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
currentDate
);
calendar
.
set
(
Calendar
.
DAY_OF_MONTH
,
calendar
.
getActualMaximum
(
Calendar
.
DAY_OF_MONTH
));
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
23
);
calendar
.
set
(
Calendar
.
MINUTE
,
59
);
calendar
.
set
(
Calendar
.
SECOND
,
59
);
calendar
.
set
(
Calendar
.
MILLISECOND
,
999
);
return
calendar
.
getTime
();
}
private
Date
calculateEndOfDay
(
Date
currentDate
)
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
currentDate
);
calendar
.
set
(
Calendar
.
HOUR_OF_DAY
,
23
);
calendar
.
set
(
Calendar
.
MINUTE
,
59
);
calendar
.
set
(
Calendar
.
SECOND
,
59
);
calendar
.
set
(
Calendar
.
MILLISECOND
,
999
);
return
calendar
.
getTime
();
}
}
\ No newline at end of file
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