Commit 377067f5 authored by LiuLin's avatar LiuLin

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

parent eaaa455f
......@@ -51,4 +51,6 @@ public interface IGenerateCodeService {
* @return 7位监管编码生成
*/
String createSupervisoryCode(String key);
String initCode();
}
......@@ -5,10 +5,7 @@ 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.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;
......@@ -66,4 +63,14 @@ public class GenerateCodeController extends BaseController {
public ResponseModel<String> createUseRegistrationCode(@RequestParam("key") String 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;
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.mapper.CategoryOtherInfoMapper;
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.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
......@@ -33,12 +35,15 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
private static final String SEQUENCE_TYPE = "%07d";
private static final long LOCK_EXPIRATION_SECONDS = 60;
private final RedisTemplate<String, String> redisTemplate;
private final StringRedisTemplate stringRedisTemplate;
private final CategoryOtherInfoMapper categoryOtherInfoMapper;
private String rulePrefix = "";
public GenerateCodeServiceImpl(RedisTemplate<String, String> redisTemplate) {
public GenerateCodeServiceImpl(RedisTemplate<String, String> redisTemplate, StringRedisTemplate stringRedisTemplate, CategoryOtherInfoMapper categoryOtherInfoMapper) {
this.redisTemplate = redisTemplate;
this.stringRedisTemplate = stringRedisTemplate;
this.categoryOtherInfoMapper = categoryOtherInfoMapper;
}
/**
* 生成申请单编号(13位,GZ20231214000)
*
......@@ -93,6 +98,15 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
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) {
// 使用分布式锁,确保在并发情况下只有一个线程能够生成顺序码
Boolean lockAcquired = obtainLock(GenerateCodeServiceImpl.LOCK_KEY_SUPERVISORY);
......@@ -102,7 +116,7 @@ public class GenerateCodeServiceImpl implements IGenerateCodeService {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
String currentSequenceStr = valueOps.get(sequenceKey);
// 如果为空,则初始化为0
Long currentSequence = (currentSequenceStr != null) ? Long.parseLong(currentSequenceStr) : 0L;
Long currentSequence = (currentSequenceStr != null) ? Long.parseLong(currentSequenceStr) : this.initRedis(sequenceKey);
log.info("===================>获取《{}》当前顺序码:{}<===================", sequenceKey, currentSequenceStr);
currentSequence++;
......@@ -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
......
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