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
5c02680a
Commit
5c02680a
authored
Dec 20, 2023
by
LiuLin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(ymt):生成顺序码调整代码结构,需要feign接口调用,参考TzsServiceFeignClient,接口在GenerateCodeController中
parent
fed7ba69
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
546 additions
and
120 deletions
+546
-120
TzsServiceFeignClient.java
.../amos/boot/module/jg/biz/feign/TzsServiceFeignClient.java
+13
-1
CreateCodeController.java
.../boot/module/ymt/api/controller/CreateCodeController.java
+0
-90
IGenerateCodeService.java
...mos/boot/module/ymt/api/service/IGenerateCodeService.java
+54
-0
GenerateCodeController.java
...oot/module/ymt/biz/controller/GenerateCodeController.java
+69
-0
EquipmentCategoryServiceImpl.java
...le/ymt/biz/service/impl/EquipmentCategoryServiceImpl.java
+38
-29
GenerateCodeServiceImpl.java
.../module/ymt/biz/service/impl/GenerateCodeServiceImpl.java
+372
-0
No files found.
amos-boot-system-tzs/amos-boot-module-jg/amos-boot-module-jg-biz/src/main/java/com/yeejoin/amos/boot/module/jg/biz/feign/TzsServiceFeignClient.java
View file @
5c02680a
...
...
@@ -6,8 +6,8 @@ import org.springframework.cloud.openfeign.FeignClient;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.typroject.tyboot.core.restful.utils.ResponseModel
;
import
java.util.Map
;
@FeignClient
(
name
=
"TZS-YMT"
,
path
=
"/ymt"
,
configuration
=
...
...
@@ -33,4 +33,16 @@ public interface TzsServiceFeignClient {
@RequestMapping
(
value
=
"/equipment-category/commonUpdateEsData"
,
method
=
RequestMethod
.
POST
)
ResponseModel
<
Map
<
String
,
Object
>>
commonUpdateEsDataByIds
(
@RequestBody
Map
<
String
,
Map
<
String
,
Object
>>
paramMap
);
/**
* 申请单编号生成
* @param type type
* @param batchSize batchSize
* @return List
*/
@RequestMapping
(
value
=
"/generate-code/applicationFormCode"
,
method
=
RequestMethod
.
POST
)
ResponseModel
<
Map
<
String
,
Object
>>
applicationFormCode
(
@RequestParam
(
"type"
)
String
type
,
@RequestParam
(
"batchSize"
)
int
batchSize
);
}
amos-boot-system-tzs/amos-boot-module-ymt/amos-boot-module-ymt-api/src/main/java/com/yeejoin/amos/boot/module/ymt/api/controller/CreateCodeController.java
deleted
100644 → 0
View file @
fed7ba69
//package com.yeejoin.amos.boot.module.ymt.api.controller;
//
//import com.yeejoin.amos.boot.biz.common.controller.BaseController;
//import com.yeejoin.amos.boot.module.ymt.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 type
// * @param batchSize batchSize
// * @return List
// */
// @TycloudOperation(ApiLevel = UserType.AGENCY)
// @PostMapping(value = "/ANCode")
// @ApiOperation(httpMethod = "POST", value = "申请单编号生成", notes = "申请单编号生成")
// public ResponseModel<List<String>> createANCode(@RequestParam("type") String type,
// @RequestParam("batchSize") int batchSize) {
// return ResponseHelper.buildResponse(createCodeService.createApplicationFormCode(type,batchSize));
// }
//
// /**
// * 生成设备注册编码
// * @param key key
// * @return String
// */
// @TycloudOperation(ApiLevel = UserType.AGENCY)
// @PostMapping(value = "/DRCode")
// @ApiOperation(httpMethod = "POST", value = "生成设备注册编码", notes = "生成设备注册编码")
// public ResponseModel<String> createDRCode(@RequestParam("key") String key) {
// return ResponseHelper.buildResponse(createCodeService.createDeviceRegistrationCode(key));
// }
//
// /**
// * 使用登记证生成
// * @param key key
// * @return String
// */
// @TycloudOperation(ApiLevel = UserType.AGENCY)
// @PostMapping(value = "/URCode")
// @ApiOperation(httpMethod = "POST", value = "使用登记证生成", notes = "使用登记证生成")
// public ResponseModel<String> createURCode(@RequestParam("key") String key) {
// return ResponseHelper.buildResponse(createCodeService.createUseRegistrationCode(key));
// }
//
// /**
// * 96333顺序码生成
// * @param key key
// * @return String
// */
// @TycloudOperation(ApiLevel = UserType.AGENCY)
// @PostMapping(value = "/elevatorCode")
// @ApiOperation(httpMethod = "POST", value = "96333顺序码生成", notes = "96333顺序码生成")
// public ResponseModel<String> elevatorCode(@RequestParam("key") String key) {
// return ResponseHelper.buildResponse(createCodeService.createElevatorCode(key));
// }
//
// /**
// * 监管顺序码生成
// * @param key key
// * @return String
// */
// @TycloudOperation(ApiLevel = UserType.AGENCY)
// @PostMapping(value = "/sequenceKeyCode")
// @ApiOperation(httpMethod = "POST", value = "监管顺序码生成", notes = "监管顺序码生成")
// public ResponseModel<String> sequenceKeyCode(@RequestParam("key") String key) {
// return ResponseHelper.buildResponse(createCodeService.createSupervisoryCode(key));
// }
//}
amos-boot-system-tzs/amos-boot-module-ymt/amos-boot-module-ymt-api/src/main/java/com/yeejoin/amos/boot/module/ymt/api/service/IGenerateCodeService.java
0 → 100644
View file @
5c02680a
package
com
.
yeejoin
.
amos
.
boot
.
module
.
ymt
.
api
.
service
;
import
java.util.List
;
/**
* 生成码服务类
* @author LiuLin
* @date 2023-12-14
*/
public
interface
IGenerateCodeService
{
/**
* 生成申请单编号(13位,GZ20231214000)
* @param type 枚举类型
* @param batchSize 生成个数
* @return List
*/
List
<
String
>
createApplicationFormCode
(
String
type
,
int
batchSize
);
/**
* 生成设备注册编码(20位)
* @param key key
* @return 顺序编号
*/
String
createDeviceRegistrationCode
(
String
key
);
/**
* 生成使用登记证编号(13位,起11陕C00001(23))
* @param key key
* @return 顺序编号
*/
String
createUseRegistrationCode
(
String
key
);
/**
* 96333编码生成(7位)
* @param key key
* @return 96333顺序码
*/
String
createElevatorCode
(
String
key
);
/**
* 回退顺序码
* @param key redisKey
* @return bool
*/
boolean
reduceElevatorCode
(
String
key
);
/**
* 监管编码生成(7位)
* @param key key
* @return 7位监管编码生成
*/
String
createSupervisoryCode
(
String
key
);
}
amos-boot-system-tzs/amos-boot-module-ymt/amos-boot-module-ymt-biz/src/main/java/com/yeejoin/amos/boot/module/ymt/biz/controller/GenerateCodeController.java
0 → 100644
View file @
5c02680a
package
com
.
yeejoin
.
amos
.
boot
.
module
.
ymt
.
biz
.
controller
;
import
com.yeejoin.amos.boot.biz.common.controller.BaseController
;
import
com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
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
=
"/generate-code"
)
public
class
GenerateCodeController
extends
BaseController
{
@Autowired
private
IGenerateCodeService
generateCodeService
;
/**
* 申请单编号生成
* @param type type
* @param batchSize batchSize
* @return List
*/
@TycloudOperation
(
ApiLevel
=
UserType
.
AGENCY
)
@PostMapping
(
value
=
"/applicationFormCode"
)
@ApiOperation
(
httpMethod
=
"POST"
,
value
=
"申请单编号生成"
,
notes
=
"申请单编号生成"
)
public
ResponseModel
<
List
<
String
>>
createApplicationFormCode
(
@RequestParam
(
"type"
)
String
type
,
@RequestParam
(
"batchSize"
)
int
batchSize
)
{
return
ResponseHelper
.
buildResponse
(
generateCodeService
.
createApplicationFormCode
(
type
,
batchSize
));
}
/**
* 生成设备注册编码
* @param key key
* @return String
*/
@TycloudOperation
(
ApiLevel
=
UserType
.
AGENCY
)
@PostMapping
(
value
=
"/deviceRegistrationCode"
)
@ApiOperation
(
httpMethod
=
"POST"
,
value
=
"生成设备注册编码"
,
notes
=
"生成设备注册编码"
)
public
ResponseModel
<
String
>
createDeviceRegistrationCode
(
@RequestParam
(
"key"
)
String
key
)
{
return
ResponseHelper
.
buildResponse
(
generateCodeService
.
createDeviceRegistrationCode
(
key
));
}
/**
* 使用登记证生成
* @param key key
* @return String
*/
@TycloudOperation
(
ApiLevel
=
UserType
.
AGENCY
)
@PostMapping
(
value
=
"/useRegistrationCode"
)
@ApiOperation
(
httpMethod
=
"POST"
,
value
=
"使用登记证生成"
,
notes
=
"使用登记证生成"
)
public
ResponseModel
<
String
>
createUseRegistrationCode
(
@RequestParam
(
"key"
)
String
key
)
{
return
ResponseHelper
.
buildResponse
(
generateCodeService
.
createUseRegistrationCode
(
key
));
}
}
amos-boot-system-tzs/amos-boot-module-ymt/amos-boot-module-ymt-biz/src/main/java/com/yeejoin/amos/boot/module/ymt/biz/service/impl/EquipmentCategoryServiceImpl.java
View file @
5c02680a
...
...
@@ -59,6 +59,7 @@ import com.yeejoin.amos.boot.module.ymt.api.mapper.IdxBizJgTechParamsVesselMappe
import
com.yeejoin.amos.boot.module.ymt.api.mapper.SuperviseInfoMapper
;
import
com.yeejoin.amos.boot.module.ymt.api.mapper.SupervisoryCodeInfoMapper
;
import
com.yeejoin.amos.boot.module.ymt.api.service.IEquipmentCategoryService
;
import
com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService
;
import
com.yeejoin.amos.boot.module.ymt.api.vo.EquipExportVo
;
import
com.yeejoin.amos.boot.module.ymt.biz.dao.ESElavtorRepository
;
import
com.yeejoin.amos.boot.module.ymt.biz.dao.ESEquipmentCategory
;
...
...
@@ -105,7 +106,6 @@ import org.typroject.tyboot.core.rdbms.service.BaseService;
import
org.typroject.tyboot.core.restful.exception.instance.BadRequest
;
import
org.typroject.tyboot.core.restful.utils.ResponseHelper
;
import
org.typroject.tyboot.core.restful.utils.ResponseModel
;
import
java.io.IOException
;
import
java.sql.Timestamp
;
import
java.text.SimpleDateFormat
;
...
...
@@ -126,7 +126,6 @@ import java.util.concurrent.ExecutorService;
import
java.util.concurrent.Executors
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
import
static
com
.
alibaba
.
fastjson
.
JSON
.
toJSONString
;
/**
...
...
@@ -269,6 +268,9 @@ public class EquipmentCategoryServiceImpl extends BaseService<EquipmentCategoryD
IdxFeignService
idxFeignService
;
@Autowired
IGenerateCodeService
generateCodeService
;
@Autowired
private
static
final
String
TABLENAME
=
"tableName"
;
@Autowired
private
ESElavtorRepository
esElavtorRepository
;
...
...
@@ -707,12 +709,12 @@ public class EquipmentCategoryServiceImpl extends BaseService<EquipmentCategoryD
* 具体生成监管码和电梯96333识别码逻辑
*/
@Transactional
(
rollbackFor
=
Exception
.
class
,
propagation
=
Propagation
.
REQUIRES_NEW
)
public
synchronized
Map
<
String
,
String
>
creatCode
(
String
isNotXiXian
,
String
city
,
String
county
,
String
equipCategory
,
String
code96333
,
String
supervisionCode
)
{
RLock
lock
=
redissonClient
.
getLock
(
LOCK_KEY
);
public
Map
<
String
,
String
>
creatCode
(
String
isNotXiXian
,
String
city
,
String
county
,
String
equipCategory
,
String
code96333
,
String
supervisionCode
)
{
//
RLock lock = redissonClient.getLock(LOCK_KEY);
Map
<
String
,
String
>
resultMap
=
null
;
try
{
lock
.
lock
();
// 获取锁
log
.
info
(
"加锁成功"
);
//
lock.lock(); // 获取锁
//
log.info("加锁成功");
resultMap
=
new
HashMap
<>();
StringBuilder
supervisorCode
=
new
StringBuilder
();
StringBuilder
elevatorCode
=
new
StringBuilder
();
...
...
@@ -731,7 +733,7 @@ public class EquipmentCategoryServiceImpl extends BaseService<EquipmentCategoryD
//查询未使用的电梯码
categoryOtherInfo
=
categoryOtherInfoMapper
.
selectElevatorCode
(
prefix
,
EquipmentCategoryEnum
.
WSY
.
getCode
());
//如果存在未使用的电梯码则启用未使用的否则创建
String
elevator
=
ObjectUtils
.
isEmpty
(
categoryOtherInfo
)
?
createElevatorCode
(
prefix
)
:
categoryOtherInfo
.
getCode
();
String
elevator
=
ObjectUtils
.
isEmpty
(
categoryOtherInfo
)
?
generateCodeService
.
createElevatorCode
(
"96333_"
+
prefix
)
:
categoryOtherInfo
.
getCode
();
if
(!
ObjectUtils
.
isEmpty
(
categoryOtherInfo
))
{
supervisoryCodeInfoMapper
.
delete
(
new
QueryWrapper
<
SupervisoryCodeInfo
>().
eq
(
"code96333"
,
categoryOtherInfo
.
getCode
()));
}
...
...
@@ -775,10 +777,11 @@ public class EquipmentCategoryServiceImpl extends BaseService<EquipmentCategoryD
resultMap
.
put
(
"qrCode"
,
ObjectUtils
.
isEmpty
(
supervisorCode
)
?
null
:
supervisorCode
.
toString
());
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
finally
{
lock
.
unlock
();
// 释放锁
log
.
info
(
"释放锁"
);
}
//finally {
// lock.unlock(); // 释放锁
// log.info("释放锁");
//}
return
resultMap
;
}
...
...
@@ -803,34 +806,39 @@ public class EquipmentCategoryServiceImpl extends BaseService<EquipmentCategoryD
Map
<
String
,
Object
>
divisionMap
=
equipmentCategoryMapper
.
getAdministrativeDivision
(
EquipmentCategoryEnum
.
XZQH
.
getCode
(),
county
);
division
=
ObjectUtils
.
isEmpty
(
divisionMap
)
?
equipmentCategoryMapper
.
getAdministrativeDivision
(
EquipmentCategoryEnum
.
XZQH
.
getCode
(),
city
).
get
(
"code"
).
toString
()
:
divisionMap
.
get
(
"code"
).
toString
();
}
supervisorCode
.
append
(
division
).
append
(
equipCategory
).
append
(
"-"
);
//supervisorCode.append(division).append(equipCategory).append("-");
supervisorCode
.
append
(
division
).
append
(
equipCategory
);
return
generateCodeService
.
createSupervisoryCode
(
String
.
valueOf
(
supervisorCode
));
//获取行政区划区县、市是否存在历史监管码
CategoryOtherInfo
supervisor
=
categoryOtherInfoMapper
.
selectSupervisorCode
(
supervisorCode
.
toString
());
//
CategoryOtherInfo supervisor = categoryOtherInfoMapper.selectSupervisorCode(supervisorCode.toString());
//生成对应监管码
if
(!
ObjectUtils
.
isEmpty
(
supervisor
)
&&
supervisor
.
getSupervisoryCode
()
!=
null
)
{
//获取补零位长度
String
supervisoryCode
=
supervisor
.
getSupervisoryCode
().
substring
(
6
);
long
num
=
Long
.
valueOf
(
supervisoryCode
)
+
1
;
int
numLength
=
String
.
valueOf
(
num
).
length
();
int
a
=
7
-
numLength
;
StringBuilder
zero
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
a
;
i
++)
{
zero
.
append
(
EquipmentCategoryEnum
.
BLW
.
getCode
());
}
zero
.
append
(
num
);
supervisorCode
.
append
(
zero
);
}
else
{
supervisorCode
.
append
(
EquipmentCategoryEnum
.
JGM
.
getCode
());
}
return
supervisorCode
.
toString
();
//
if (!ObjectUtils.isEmpty(supervisor) && supervisor.getSupervisoryCode() != null) {
//
//获取补零位长度
//
String supervisoryCode = supervisor.getSupervisoryCode().substring(6);
//
long num = Long.valueOf(supervisoryCode) + 1;
//
int numLength = String.valueOf(num).length();
//
int a = 7 - numLength;
//
StringBuilder zero = new StringBuilder();
//
for (int i = 0; i < a; i++) {
//
zero.append(EquipmentCategoryEnum.BLW.getCode());
//
}
//
zero.append(num);
//
supervisorCode.append(zero);
//
} else {
//
supervisorCode.append(EquipmentCategoryEnum.JGM.getCode());
//
}
//
return supervisorCode.toString();
}
/**
*
* 生成96333电梯识别码
*
* @param prefix 电梯码前缀
* @return 96333电梯识别码
*/
@Deprecated
public
String
createElevatorCode
(
String
prefix
)
{
StringBuilder
elevatorCode
=
new
StringBuilder
();
//生成生成96333电梯码前缀
...
...
@@ -1622,7 +1630,8 @@ public class EquipmentCategoryServiceImpl extends BaseService<EquipmentCategoryD
supervisoryCodeInfo
.
setStatus
(
EquipmentCategoryEnum
.
YSY
.
getCode
());
if
(
category
.
startsWith
(
"3"
))
{
if
(
"null"
.
equals
(
map
.
get
(
"code"
)))
{
code96333
=
createElevatorCode
(
"31"
);
//code96333 = createElevatorCode("31");
code96333
=
generateCodeService
.
createElevatorCode
(
"96333_31"
);
supervisoryCodeInfo
.
setCreateStatus
(
CREATE
);
}
else
{
supervisoryCodeInfo
.
setCreateStatus
(
NOT_CREATE
);
...
...
amos-boot-system-tzs/amos-boot-module-ymt/amos-boot-module-ymt-biz/src/main/java/com/yeejoin/amos/boot/module/ymt/biz/service/impl/GenerateCodeServiceImpl.java
0 → 100644
View file @
5c02680a
package
com
.
yeejoin
.
amos
.
boot
.
module
.
ymt
.
biz
.
service
.
impl
;
import
com.yeejoin.amos.boot.module.ymt.api.common.DateUtils
;
import
com.yeejoin.amos.boot.module.ymt.api.enums.ApplicationFormTypeEnum
;
import
com.yeejoin.amos.boot.module.ymt.api.enums.EquipmentCategoryEnum
;
import
com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService
;
import
lombok.extern.slf4j.Slf4j
;
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
*/
@Slf4j
@Service
public
class
GenerateCodeServiceImpl
implements
IGenerateCodeService
{
private
static
final
String
LOCK_VALUE
=
"locked"
;
private
static
final
String
LOCK_KEY_AF
=
"sequence_lock_af"
;
private
static
final
String
SEQUENCE_TYPE_AF
=
"%03d"
;
private
static
final
String
LOCK_KEY_DR
=
"sequence_lock_dr"
;
private
static
final
String
SEQUENCE_TYPE_DR
=
"%04d"
;
private
static
final
String
LOCK_KEY_UR
=
"sequence_lock_ur"
;
private
static
final
String
LOCK_KEY_ELEVATOR
=
"sequence_lock_elevator"
;
private
static
final
String
LOCK_KEY_SUPERVISORY
=
"sequence_lock_supervisory"
;
private
static
final
String
SEQUENCE_TYPE_UR
=
"%05d"
;
private
static
final
String
SEQUENCE_TYPE
=
"%07d"
;
private
final
RedisTemplate
<
String
,
String
>
redisTemplate
;
private
String
rulePrefix
=
""
;
public
GenerateCodeServiceImpl
(
RedisTemplate
<
String
,
String
>
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
}
/**
* 生成申请单编号(13位,GZ20231214000)
*
* @param type 枚举类型
* @param batchSize 生成个数
* @return List
*/
@Override
public
List
<
String
>
createApplicationFormCode
(
String
type
,
int
batchSize
)
{
if
(!
isValueInEnum
(
type
))
{
return
Collections
.
emptyList
();
}
rulePrefix
=
type
+
LocalDate
.
now
().
format
(
DateTimeFormatter
.
ofPattern
(
"yyyyMMdd"
));
return
generateBatchSequence
(
type
,
batchSize
);
}
/**
* 生成设备注册编码(20位)
*
* @param key key
* @return 顺序编号
*/
@Override
public
String
createDeviceRegistrationCode
(
String
key
)
{
return
(
key
.
length
()
==
16
)
?
generateSequence
(
key
,
SEQUENCE_TYPE_DR
,
LOCK_KEY_DR
)
:
"生成码规则不对!"
;
}
/**
* 生成使用登记证编号(13位,起11陕C00001(23))
*
* @param key key
* @return 顺序编号
*/
@Override
public
String
createUseRegistrationCode
(
String
key
)
{
rulePrefix
=
"("
+
LocalDate
.
now
().
format
(
DateTimeFormatter
.
ofPattern
(
"yy"
))
+
")"
;
return
generateSequence
(
key
,
SEQUENCE_TYPE_UR
,
LOCK_KEY_UR
);
}
@Override
public
String
createElevatorCode
(
String
key
)
{
return
(
key
.
length
()
==
8
)
?
generateElevatorSequence
(
key
,
SEQUENCE_TYPE
,
LOCK_KEY_ELEVATOR
)
:
"生成码规则不对!"
;
}
@Override
public
boolean
reduceElevatorCode
(
String
key
)
{
return
reduceSequence
(
key
,
SEQUENCE_TYPE
,
LOCK_KEY_ELEVATOR
);
}
@Override
public
String
createSupervisoryCode
(
String
key
)
{
return
(
key
.
length
()
==
5
)
?
generateSupervisorySequence
(
key
)
:
"生成码规则不对!"
;
}
private
String
generateSupervisorySequence
(
String
sequenceKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtainLock
(
GenerateCodeServiceImpl
.
LOCK_KEY_SUPERVISORY
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
try
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
// 如果为空,则初始化为0
Long
currentSequence
=
(
currentSequenceStr
!=
null
)
?
Long
.
parseLong
(
currentSequenceStr
)
:
0L
;
log
.
info
(
"===================>获取《{}》当前顺序码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
currentSequence
++;
// 生成顺序码
String
formattedSequence
=
String
.
format
(
GenerateCodeServiceImpl
.
SEQUENCE_TYPE
,
currentSequence
);
log
.
info
(
"===================>更新《{}》顺序码:{}<==================="
,
sequenceKey
,
formattedSequence
);
// 更新顺序码
valueOps
.
set
(
sequenceKey
,
formattedSequence
);
return
sequenceKey
+
"-"
+
formattedSequence
;
}
finally
{
releaseLock
(
GenerateCodeServiceImpl
.
LOCK_KEY_SUPERVISORY
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
/**
* 校验枚举合法性
*
* @param value value
* @return bool
*/
public
boolean
isValueInEnum
(
String
value
)
{
return
EnumSet
.
allOf
(
ApplicationFormTypeEnum
.
class
)
.
stream
()
.
anyMatch
(
enumValue
->
enumValue
.
name
().
equals
(
value
));
}
/**
* 批量生成顺序码
*
* @param sequenceKey sequenceKey
* @param batchSize 批量生成个数
* @return List
*/
public
List
<
String
>
generateBatchSequence
(
String
sequenceKey
,
int
batchSize
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
LOCK_KEY_AF
,
LOCK_VALUE
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
try
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
// 如果为空,则初始化为0
Long
currentSequence
=
(
currentSequenceStr
!=
null
)
?
Long
.
parseLong
(
currentSequenceStr
)
:
0L
;
log
.
info
(
"===================>获取《{}》当前顺序码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
// 生成批量顺序码
List
<
String
>
sequenceList
=
new
ArrayList
<>();
log
.
info
(
"===================>批量生成{}个《{}》顺序码<==================="
,
batchSize
,
sequenceKey
);
for
(
int
i
=
0
;
i
<
batchSize
;
i
++)
{
currentSequence
++;
// 生成3位顺序码
String
formattedSequence
=
String
.
format
(
SEQUENCE_TYPE_AF
,
currentSequence
);
sequenceList
.
add
(
rulePrefix
+
formattedSequence
);
// 更新顺序码
log
.
info
(
"===================>更新《{}》顺序码:{}<==================="
,
sequenceKey
,
formattedSequence
);
setValueWithDailyExpiration
(
sequenceKey
,
String
.
valueOf
(
formattedSequence
));
}
return
sequenceList
;
}
finally
{
redisTemplate
.
delete
(
LOCK_KEY_AF
);
}
}
else
{
// 获取锁失败,可以选择重试或采取其他策略
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
/**
* 生成顺序码
*
* @param sequenceKey redisKey
* @param sequenceType 生成码类型
* @param lockKey redis锁
* @return s
*/
public
String
generateSequence
(
String
sequenceKey
,
String
sequenceType
,
String
lockKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtainLock
(
lockKey
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
try
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
// 如果为空,则初始化为0
Long
currentSequence
=
(
currentSequenceStr
!=
null
)
?
Long
.
parseLong
(
currentSequenceStr
)
:
0L
;
log
.
info
(
"===================>获取《{}》当前顺序码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
currentSequence
++;
// 生成顺序码
String
formattedSequence
=
String
.
format
(
sequenceType
,
currentSequence
);
log
.
info
(
"===================>更新《{}》顺序码:{}<==================="
,
sequenceKey
,
formattedSequence
);
// 更新顺序码
if
(
lockKey
.
equals
(
LOCK_KEY_DR
))
{
setValueWithMonthlyExpiration
(
sequenceKey
,
String
.
valueOf
(
formattedSequence
));
}
else
{
setValueWithYearlyExpiration
(
sequenceKey
,
String
.
valueOf
(
formattedSequence
));
}
String
generatedSequence
=
sequenceKey
+
formattedSequence
;
String
result
=
generatedSequence
+
(
lockKey
.
equals
(
LOCK_KEY_DR
)
?
""
:
rulePrefix
);
log
.
info
(
"===================>返回《{}》顺序码:{}<==================="
,
sequenceKey
,
result
);
return
result
;
}
finally
{
releaseLock
(
lockKey
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
/**
* 生成顺序码
*
* @param sequenceKey redisKey
* @param sequenceType 生成码类型
* @param lockKey redis锁
* @return s
*/
public
String
generateElevatorSequence
(
String
sequenceKey
,
String
sequenceType
,
String
lockKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtainLock
(
lockKey
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
try
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
// 如果为空,则初始化为0
if
(
currentSequenceStr
==
null
)
{
currentSequenceStr
=
EquipmentCategoryEnum
.
getCodeByValue
(
sequenceKey
);
log
.
info
(
"===================>获取《{}》初始码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
return
currentSequenceStr
;
}
Long
currentSequence
=
Long
.
parseLong
(
currentSequenceStr
);
log
.
info
(
"===================>获取《{}》当前顺序码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
currentSequence
++;
// 生成顺序码
String
formattedSequence
=
String
.
format
(
sequenceType
,
currentSequence
);
log
.
info
(
"===================>更新《{}》顺序码:{}<==================="
,
sequenceKey
,
formattedSequence
);
// 更新顺序码
valueOps
.
set
(
sequenceKey
,
formattedSequence
);
return
formattedSequence
;
}
finally
{
releaseLock
(
lockKey
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
/**
* 回退顺序码
*
* @param sequenceKey redisKey
* @param sequenceType 码类型
* @param lockKey 分布锁
* @return 操作是否成功
*/
public
boolean
reduceSequence
(
String
sequenceKey
,
String
sequenceType
,
String
lockKey
)
{
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean
lockAcquired
=
obtainLock
(
lockKey
);
if
(
Boolean
.
TRUE
.
equals
(
lockAcquired
))
{
try
{
// 获取当前顺序码
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
String
currentSequenceStr
=
valueOps
.
get
(
sequenceKey
);
// 如果为空,则初始化为0
long
currentSequence
=
(
currentSequenceStr
!=
null
)
?
Long
.
parseLong
(
currentSequenceStr
)
:
0L
;
log
.
info
(
"===================>获取《{}》当前顺序码:{}<==================="
,
sequenceKey
,
currentSequenceStr
);
// 判断是否有可回退的操作
if
(
currentSequence
>
0
)
{
currentSequence
--;
// 生成顺序码
String
formattedSequence
=
String
.
format
(
sequenceType
,
currentSequence
);
log
.
info
(
"===================>回退《{}》顺序码:{}<==================="
,
sequenceKey
,
formattedSequence
);
// 更新顺序码
valueOps
.
set
(
sequenceKey
,
String
.
valueOf
(
formattedSequence
));
return
true
;
// 回退成功
}
else
{
log
.
warn
(
"===================>无法回退《{}》顺序码,当前顺序码已为0<==================="
,
sequenceKey
);
return
false
;
// 无法回退,当前顺序码已为0
}
}
finally
{
releaseLock
(
lockKey
);
}
}
else
{
throw
new
RuntimeException
(
"Failed to acquire lock for sequence generation"
);
}
}
/**
* 分布式锁
*
* @param lockKey lockKey
* @return bool
*/
private
Boolean
obtainLock
(
String
lockKey
)
{
return
redisTemplate
.
opsForValue
().
setIfAbsent
(
lockKey
,
LOCK_VALUE
);
}
/**
* 释放锁
*
* @param lockKey lockKey
*/
private
void
releaseLock
(
String
lockKey
)
{
redisTemplate
.
delete
(
lockKey
);
}
/**
* redis 根据自然日过期
*
* @param key key
* @param value value
*/
public
void
setValueWithDailyExpiration
(
String
key
,
String
value
)
{
Date
endOfDay
=
DateUtils
.
calculateEndOfDay
(
new
Date
());
setValueWithExpiration
(
key
,
value
,
endOfDay
);
}
/**
* redis 根据自然月过期
*
* @param key key
* @param value value
*/
public
void
setValueWithMonthlyExpiration
(
String
key
,
String
value
)
{
Date
endOfMonth
=
DateUtils
.
calculateEndOfMonth
(
new
Date
());
setValueWithExpiration
(
key
,
value
,
endOfMonth
);
}
/**
* redis 根据自然年过期
*
* @param key key
* @param value value
*/
public
void
setValueWithYearlyExpiration
(
String
key
,
String
value
)
{
Date
endOfYear
=
DateUtils
.
calculateEndOfYear
(
new
Date
());
setValueWithExpiration
(
key
,
value
,
endOfYear
);
}
/**
* redis设置key
*
* @param key key
* @param value value
* @param expirationDate 过期时间
*/
public
void
setValueWithExpiration
(
String
key
,
String
value
,
Date
expirationDate
)
{
ValueOperations
<
String
,
String
>
valueOps
=
redisTemplate
.
opsForValue
();
valueOps
.
set
(
key
,
value
);
long
expirationTimeInSeconds
=
expirationDate
.
getTime
()
-
System
.
currentTimeMillis
();
redisTemplate
.
expire
(
key
,
expirationTimeInSeconds
,
TimeUnit
.
MILLISECONDS
);
}
}
\ 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