Commit b5bc3343 authored by LiuLin's avatar LiuLin

fix(tcm):申请单编码生成代码开发

parent 08710569
...@@ -38,8 +38,8 @@ public class CreateCodeController extends BaseController { ...@@ -38,8 +38,8 @@ public class CreateCodeController extends BaseController {
@PostMapping(value = "/ANCode") @PostMapping(value = "/ANCode")
@ApiOperation(httpMethod = "POST", value = "申请单编号生成", notes = "申请单编号生成") @ApiOperation(httpMethod = "POST", value = "申请单编号生成", notes = "申请单编号生成")
public ResponseModel<List<String>> CreateANCode(@RequestParam("type") String type, public ResponseModel<List<String>> CreateANCode(@RequestParam("type") String type,
@RequestParam("num") int num) { @RequestParam("batchSize") int batchSize) {
return ResponseHelper.buildResponse(createCodeService.createApplicationFormCode(type,num)); return ResponseHelper.buildResponse(createCodeService.createApplicationFormCode(type,batchSize));
} }
/** /**
...@@ -53,6 +53,6 @@ public class CreateCodeController extends BaseController { ...@@ -53,6 +53,6 @@ public class CreateCodeController extends BaseController {
@ApiOperation(httpMethod = "POST", value = "申请单编号生成", notes = "申请单编号生成") @ApiOperation(httpMethod = "POST", value = "申请单编号生成", notes = "申请单编号生成")
public ResponseModel<List<String>> CreateANCode1(@RequestParam("type") String type, public ResponseModel<List<String>> CreateANCode1(@RequestParam("type") String type,
@RequestParam("num") int num) { @RequestParam("num") int num) {
return ResponseHelper.buildResponse(createCodeService.CreateANCode(type,num)); return ResponseHelper.buildResponse(createCodeService.createApplicationFormCode(type,num));
} }
} }
...@@ -13,5 +13,10 @@ public interface ICreateCodeService { ...@@ -13,5 +13,10 @@ public interface ICreateCodeService {
List<String> createApplicationFormCode(String type, int batchSize); List<String> createApplicationFormCode(String type, int batchSize);
String createDeviceRegistrationCode(); /**
* 生成设备注册编码(20位)
* @param key key
* @return 顺序编号
*/
String createDeviceRegistrationCode(String key);
} }
...@@ -18,7 +18,8 @@ import java.util.concurrent.TimeUnit; ...@@ -18,7 +18,8 @@ import java.util.concurrent.TimeUnit;
public class CreateCodeServiceImpl implements ICreateCodeService { public class CreateCodeServiceImpl implements ICreateCodeService {
private static final String LOCK_VALUE = "locked"; 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 final RedisTemplate<String, String> redisTemplate;
private String rulePrefix; private String rulePrefix;
...@@ -32,12 +33,12 @@ public class CreateCodeServiceImpl implements ICreateCodeService { ...@@ -32,12 +33,12 @@ public class CreateCodeServiceImpl implements ICreateCodeService {
return Collections.emptyList(); return Collections.emptyList();
} }
rulePrefix = type + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); rulePrefix = type + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
return this.generateBatchSequence(type, batchSize); return generateBatchSequence(type, batchSize);
} }
@Override @Override
public String createDeviceRegistrationCode() { public String createDeviceRegistrationCode(String key) {
return null; return generateSequence(key);
} }
public boolean isValueInEnum(String value) { public boolean isValueInEnum(String value) {
...@@ -48,7 +49,7 @@ public class CreateCodeServiceImpl implements ICreateCodeService { ...@@ -48,7 +49,7 @@ public class CreateCodeServiceImpl implements ICreateCodeService {
public List<String> generateBatchSequence(String sequenceKey, int batchSize) { 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)) { if (Boolean.TRUE.equals(lockAcquired)) {
try { try {
// 获取当前顺序码 // 获取当前顺序码
...@@ -76,7 +77,41 @@ public class CreateCodeServiceImpl implements ICreateCodeService { ...@@ -76,7 +77,41 @@ public class CreateCodeServiceImpl implements ICreateCodeService {
return sequenceList; return sequenceList;
} finally { } 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 { } else {
// 获取锁失败,可以选择重试或采取其他策略 // 获取锁失败,可以选择重试或采取其他策略
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment