feat: 新增编码规则管理Service
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package cn.code.nl.module.base.service.codegen;
|
||||
|
||||
import cn.code.nl.framework.common.pojo.PageResult;
|
||||
import cn.code.nl.module.base.controller.admin.codegen.vo.CodeRulePageReqVO;
|
||||
import cn.code.nl.module.base.controller.admin.codegen.vo.CodeRuleSaveReqVO;
|
||||
import cn.code.nl.module.base.dal.dataobject.codegen.CodeRuleDO;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 编码规则管理 Service 接口
|
||||
*
|
||||
* @author zhouz
|
||||
*/
|
||||
public interface CodeRuleService {
|
||||
|
||||
/**
|
||||
* 创建编码规则
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 规则ID
|
||||
*/
|
||||
Long createRule(@Valid CodeRuleSaveReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新编码规则
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updateRule(@Valid CodeRuleSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除编码规则
|
||||
*
|
||||
* @param id 规则ID
|
||||
*/
|
||||
void deleteRule(Long id);
|
||||
|
||||
/**
|
||||
* 获取编码规则详情
|
||||
*
|
||||
* @param id 规则ID
|
||||
* @return 编码规则
|
||||
*/
|
||||
CodeRuleDO getRule(Long id);
|
||||
|
||||
/**
|
||||
* 根据规则编码获取规则(带缓存)
|
||||
*
|
||||
* @param ruleCode 规则编码
|
||||
* @return 编码规则
|
||||
*/
|
||||
CodeRuleDO getRuleByCode(String ruleCode);
|
||||
|
||||
/**
|
||||
* 分页查询编码规则
|
||||
*
|
||||
* @param pageReqVO 分页条件
|
||||
* @return 分页结果
|
||||
*/
|
||||
PageResult<CodeRuleDO> getRulePage(CodeRulePageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 获取启用状态的编码规则精简列表
|
||||
*
|
||||
* @return 规则列表
|
||||
*/
|
||||
List<CodeRuleDO> getEnabledRuleList();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package cn.code.nl.module.base.service.codegen;
|
||||
|
||||
import cn.code.nl.framework.common.pojo.PageResult;
|
||||
import cn.code.nl.framework.common.util.object.BeanUtils;
|
||||
import cn.code.nl.module.base.controller.admin.codegen.vo.CodeRulePageReqVO;
|
||||
import cn.code.nl.module.base.controller.admin.codegen.vo.CodeRuleSaveReqVO;
|
||||
import cn.code.nl.module.base.dal.dataobject.codegen.CodeRuleDO;
|
||||
import cn.code.nl.module.base.dal.mysql.codegen.CodeRuleMapper;
|
||||
import cn.code.nl.module.base.service.codegen.enums.SeqStrategyEnum;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static cn.code.nl.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.code.nl.module.base.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 编码规则管理 Service 实现类
|
||||
*
|
||||
* @author zhouz
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@Validated
|
||||
public class CodeRuleServiceImpl implements CodeRuleService {
|
||||
|
||||
/**
|
||||
* Redis 缓存 key 前缀
|
||||
*/
|
||||
private static final String CACHE_KEY_PREFIX = "codegen:rule:";
|
||||
|
||||
/**
|
||||
* 缓存过期时间(30分钟)
|
||||
*/
|
||||
private static final long CACHE_TTL_MINUTES = 30;
|
||||
|
||||
@Resource
|
||||
private CodeRuleMapper codeRuleMapper;
|
||||
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long createRule(CodeRuleSaveReqVO createReqVO) {
|
||||
// 校验规则编码唯一性
|
||||
validateRuleCodeUnique(createReqVO.getRuleCode(), null);
|
||||
|
||||
// 插入
|
||||
CodeRuleDO rule = BeanUtils.toBean(createReqVO, CodeRuleDO.class);
|
||||
rule.setSegmentConfig(createReqVO.getSegments());
|
||||
// 默认策略
|
||||
if (StrUtil.isBlank(rule.getSeqStrategy())) {
|
||||
rule.setSeqStrategy(SeqStrategyEnum.SEGMENT.getCode());
|
||||
}
|
||||
codeRuleMapper.insert(rule);
|
||||
log.info("创建编码规则成功,ruleCode={}", rule.getRuleCode());
|
||||
return rule.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateRule(CodeRuleSaveReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
CodeRuleDO existRule = validateRuleExists(updateReqVO.getId());
|
||||
// 校验规则编码唯一性
|
||||
validateRuleCodeUnique(updateReqVO.getRuleCode(), updateReqVO.getId());
|
||||
|
||||
// 更新
|
||||
CodeRuleDO updateObj = BeanUtils.toBean(updateReqVO, CodeRuleDO.class);
|
||||
updateObj.setSegmentConfig(updateReqVO.getSegments());
|
||||
codeRuleMapper.updateById(updateObj);
|
||||
|
||||
// 清除缓存
|
||||
clearCache(existRule.getRuleCode());
|
||||
// 如果 ruleCode 变更了,也清除旧 key
|
||||
if (!existRule.getRuleCode().equals(updateReqVO.getRuleCode())) {
|
||||
clearCache(existRule.getRuleCode());
|
||||
}
|
||||
log.info("更新编码规则成功,ruleCode={}", updateReqVO.getRuleCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteRule(Long id) {
|
||||
// 校验存在
|
||||
CodeRuleDO rule = validateRuleExists(id);
|
||||
// 删除
|
||||
codeRuleMapper.deleteById(id);
|
||||
// 清除缓存
|
||||
clearCache(rule.getRuleCode());
|
||||
log.info("删除编码规则成功,ruleCode={}", rule.getRuleCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodeRuleDO getRule(Long id) {
|
||||
return validateRuleExists(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodeRuleDO getRuleByCode(String ruleCode) {
|
||||
// 先从缓存读取
|
||||
String cacheKey = CACHE_KEY_PREFIX + ruleCode;
|
||||
String cached = stringRedisTemplate.opsForValue().get(cacheKey);
|
||||
if (StrUtil.isNotBlank(cached)) {
|
||||
try {
|
||||
return objectMapper.readValue(cached, CodeRuleDO.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warn("解析编码规则缓存失败,ruleCode={}", ruleCode, e);
|
||||
}
|
||||
}
|
||||
|
||||
// 缓存未命中,查数据库
|
||||
CodeRuleDO rule = codeRuleMapper.selectByRuleCode(ruleCode);
|
||||
if (rule == null) {
|
||||
throw exception(CODE_RULE_NOT_EXISTS);
|
||||
}
|
||||
// 校验启用状态
|
||||
if (rule.getStatus() != null && rule.getStatus() == 1) {
|
||||
throw exception(CODE_RULE_DISABLED);
|
||||
}
|
||||
|
||||
// 写入缓存
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(rule);
|
||||
stringRedisTemplate.opsForValue().set(cacheKey, json, CACHE_TTL_MINUTES, TimeUnit.MINUTES);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warn("序列化编码规则缓存失败,ruleCode={}", ruleCode, e);
|
||||
}
|
||||
return rule;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<CodeRuleDO> getRulePage(CodeRulePageReqVO pageReqVO) {
|
||||
return codeRuleMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodeRuleDO> getEnabledRuleList() {
|
||||
// 查询启用状态的规则
|
||||
return codeRuleMapper.selectList(CodeRuleDO::getStatus, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除规则缓存
|
||||
*/
|
||||
private void clearCache(String ruleCode) {
|
||||
stringRedisTemplate.delete(CACHE_KEY_PREFIX + ruleCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验规则编码唯一性
|
||||
*/
|
||||
private void validateRuleCodeUnique(String ruleCode, Long excludeId) {
|
||||
CodeRuleDO exist = codeRuleMapper.selectByRuleCode(ruleCode);
|
||||
if (exist == null) {
|
||||
return;
|
||||
}
|
||||
if (excludeId == null || !exist.getId().equals(excludeId)) {
|
||||
throw exception(CODE_RULE_CODE_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验规则存在
|
||||
*/
|
||||
private CodeRuleDO validateRuleExists(Long id) {
|
||||
if (id == null) {
|
||||
throw exception(CODE_RULE_NOT_EXISTS);
|
||||
}
|
||||
CodeRuleDO rule = codeRuleMapper.selectById(id);
|
||||
if (rule == null) {
|
||||
throw exception(CODE_RULE_NOT_EXISTS);
|
||||
}
|
||||
return rule;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user