Commit 377067f5 authored by LiuLin's avatar LiuLin

fix(YMT):添加初始化Redis接口和监管码为空的时候,初始化Redis

parent eaaa455f
...@@ -51,4 +51,6 @@ public interface IGenerateCodeService { ...@@ -51,4 +51,6 @@ public interface IGenerateCodeService {
* @return 7位监管编码生成 * @return 7位监管编码生成
*/ */
String createSupervisoryCode(String key); String createSupervisoryCode(String key);
String initCode();
} }
...@@ -5,10 +5,7 @@ import com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService; ...@@ -5,10 +5,7 @@ import com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
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.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation; import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper; import org.typroject.tyboot.core.restful.utils.ResponseHelper;
...@@ -66,4 +63,14 @@ public class GenerateCodeController extends BaseController { ...@@ -66,4 +63,14 @@ public class GenerateCodeController extends BaseController {
public ResponseModel<String> createUseRegistrationCode(@RequestParam("key") String key) { public ResponseModel<String> createUseRegistrationCode(@RequestParam("key") String key) {
return ResponseHelper.buildResponse(generateCodeService.createUseRegistrationCode(key)); return ResponseHelper.buildResponse(generateCodeService.createUseRegistrationCode(key));
} }
/**
* 初始化96333和监管码到Redis
*/
@TycloudOperation(ApiLevel = UserType.AGENCY)
@GetMapping(value = "/init-code")
@ApiOperation(httpMethod = "GET", value = "初始化96333和监管码到Redis", notes = "初始化96333和监管码到Redis")
public ResponseModel<String> initCode() {
return ResponseHelper.buildResponse(generateCodeService.initCode());
}
} }
...@@ -3,9 +3,11 @@ package com.yeejoin.amos.boot.module.ymt.biz.service.impl; ...@@ -3,9 +3,11 @@ 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.common.DateUtils;
import com.yeejoin.amos.boot.module.ymt.api.enums.ApplicationFormTypeEnum; 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.enums.EquipmentCategoryEnum;
import com.yeejoin.amos.boot.module.ymt.api.mapper.CategoryOtherInfoMapper;
import com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService; import com.yeejoin.amos.boot.module.ymt.api.service.IGenerateCodeService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDate; import java.time.LocalDate;
...@@ -33,12 +35,15 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService { ...@@ -33,12 +35,15 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
private static final String SEQUENCE_TYPE = "%07d"; private static final String SEQUENCE_TYPE = "%07d";
private static final long LOCK_EXPIRATION_SECONDS = 60; private static final long LOCK_EXPIRATION_SECONDS = 60;
private final RedisTemplate<String, String> redisTemplate; private final RedisTemplate<String, String> redisTemplate;
private final StringRedisTemplate stringRedisTemplate;
private final CategoryOtherInfoMapper categoryOtherInfoMapper;
private String rulePrefix = ""; private String rulePrefix = "";
public GenerateCodeServiceImpl(RedisTemplate<String, String> redisTemplate) { public GenerateCodeServiceImpl(RedisTemplate<String, String> redisTemplate, StringRedisTemplate stringRedisTemplate, CategoryOtherInfoMapper categoryOtherInfoMapper) {
this.redisTemplate = redisTemplate; this.redisTemplate = redisTemplate;
this.stringRedisTemplate = stringRedisTemplate;
this.categoryOtherInfoMapper = categoryOtherInfoMapper;
} }
/** /**
* 生成申请单编号(13位,GZ20231214000) * 生成申请单编号(13位,GZ20231214000)
* *
...@@ -93,6 +98,15 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService { ...@@ -93,6 +98,15 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
return (key.length() == 5) ? generateSupervisorySequence(key) : "生成码规则不对!"; return (key.length() == 5) ? generateSupervisorySequence(key) : "生成码规则不对!";
} }
@Override
public String initCode() {
categoryOtherInfoMapper.selectSupervisorCodeMaxValue().forEach(vo ->
stringRedisTemplate.opsForValue().set(vo.getName(), String.valueOf(vo.getValue())));
categoryOtherInfoMapper.selectElevatorCodeMaxValue().forEach(vo ->
stringRedisTemplate.opsForValue().set(vo.getName(), String.valueOf(vo.getValue())));
return "ok";
}
private String generateSupervisorySequence(String sequenceKey) { private String generateSupervisorySequence(String sequenceKey) {
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码 // 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean lockAcquired = obtainLock(GenerateCodeServiceImpl.LOCK_KEY_SUPERVISORY); Boolean lockAcquired = obtainLock(GenerateCodeServiceImpl.LOCK_KEY_SUPERVISORY);
...@@ -102,7 +116,7 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService { ...@@ -102,7 +116,7 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue(); ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
String currentSequenceStr = valueOps.get(sequenceKey); String currentSequenceStr = valueOps.get(sequenceKey);
// 如果为空,则初始化为0 // 如果为空,则初始化为0
Long currentSequence = (currentSequenceStr != null) ? Long.parseLong(currentSequenceStr) : 0L; Long currentSequence = (currentSequenceStr != null) ? Long.parseLong(currentSequenceStr) : this.initRedis(sequenceKey);
log.info("===================>获取《{}》当前顺序码:{}<===================", sequenceKey, currentSequenceStr); log.info("===================>获取《{}》当前顺序码:{}<===================", sequenceKey, currentSequenceStr);
currentSequence++; currentSequence++;
...@@ -121,6 +135,18 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService { ...@@ -121,6 +135,18 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
} }
/** /**
* 监管码为空的话,初始化Redis
* @param sequenceKey key
* @return 顺序码
*/
private Long initRedis(String sequenceKey) {
categoryOtherInfoMapper.selectSupervisorCodeMaxValue().forEach(vo ->
stringRedisTemplate.opsForValue().set(vo.getName(), String.valueOf(vo.getValue())));
return Long.parseLong(Objects.requireNonNull(redisTemplate.opsForValue().get(sequenceKey)));
}
/**
* 校验枚举合法性 * 校验枚举合法性
* *
* @param value value * @param value value
......
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