feat(system): 完善编码规则功能实现

- 在添加页面为规则编码、规则名称、编码前缀、日期格式字段添加最大长度限制
- 将流水号长度字段类型改为数字输入并设置最小值1最大值10,默认值4
- 移除当前流水号和上次流水键的必填要求,调整为只读显示
- 在编辑页面将当前流水号和上次流水键改为静态文本显示
- 为下拉选项添加默认选中状态,优化用户体验
- 实现规则编码唯一性校验功能,防止重复创建
- 添加编码规则的业务编号生成功能,支持多种重置周期
- 优化数据表查询条件,添加软删除过滤
- 完善编码规则的增删改查逻辑验证和异常处理
- 重构代码结构,提取常量和工具方法提高代码可维护性
- 更新前端页面的字段显示和验证提示信息
- 集成字典标签显示,统一状态值的展示方式
main
yangk 2 weeks ago
parent c57944a8c6
commit 2d2a5f1c66

@ -99,6 +99,11 @@ public class SysCodeRuleController extends BaseController
@ResponseBody
public AjaxResult addSave(SysCodeRule sysCodeRule)
{
if (!sysCodeRuleService.checkRuleCodeUnique(sysCodeRule))
{
return error("新增编码规则失败,规则编码已存在");
}
sysCodeRule.setCreateBy(getLoginName());
return toAjax(sysCodeRuleService.insertSysCodeRule(sysCodeRule));
}
@ -123,6 +128,11 @@ public class SysCodeRuleController extends BaseController
@ResponseBody
public AjaxResult editSave(SysCodeRule sysCodeRule)
{
if (!sysCodeRuleService.checkRuleCodeUnique(sysCodeRule))
{
return error("修改编码规则失败,规则编码已存在");
}
sysCodeRule.setUpdateBy(getLoginName());
return toAjax(sysCodeRuleService.updateSysCodeRule(sysCodeRule));
}

@ -10,7 +10,7 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">规则编码:</label>
<div class="col-sm-8">
<input name="ruleCode" class="form-control" type="text" required>
<input name="ruleCode" class="form-control" type="text" maxlength="64" required>
</div>
</div>
</div>
@ -18,15 +18,15 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">规则名称:</label>
<div class="col-sm-8">
<input name="ruleName" class="form-control" type="text" required>
<input name="ruleName" class="form-control" type="text" maxlength="100" required>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">编码前缀:</label>
<label class="col-sm-4 control-label">编码前缀:</label>
<div class="col-sm-8">
<input name="codePrefix" class="form-control" type="text" required>
<input name="codePrefix" class="form-control" type="text" maxlength="20">
</div>
</div>
</div>
@ -34,7 +34,7 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">日期格式:</label>
<div class="col-sm-8">
<input name="dateFormat" class="form-control" type="text" required>
<input name="dateFormat" class="form-control" type="text" maxlength="20" value="yyyyMMdd" required>
</div>
</div>
</div>
@ -42,39 +42,23 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">流水号长度:</label>
<div class="col-sm-8">
<input name="serialLength" class="form-control" type="text" required>
<input name="serialLength" class="form-control" type="number" min="1" max="10" value="4" required>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">当前流水号:</label>
<div class="col-sm-8">
<input name="currentSerial" class="form-control" type="text" required>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">重置周期DAY、MONTH、YEAR、NONE</label>
<label class="col-sm-4 control-label is-required">重置周期:</label>
<div class="col-sm-8">
<select name="resetCycle" class="form-control" th:with="type=${@dict.getType('ams_code_reset_cycle')}" required>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${dict.default}"></option>
</select>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label">上次流水键例如20260526</label>
<div class="col-sm-8">
<input name="lastSerialKey" class="form-control" type="text">
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">启用状态Y启用N停用</label>
<label class="col-sm-4 control-label is-required">启用状态:</label>
<div class="col-sm-8">
<div class="radio-box" th:each="dict : ${@dict.getType('ams_enabled_status')}">
<input type="radio" th:id="${'enabled_' + dict.dictCode}" name="enabled" th:value="${dict.dictValue}" th:checked="${dict.default}" required>
@ -91,14 +75,6 @@
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label">删除标志0存在1删除</label>
<div class="col-sm-8">
<input name="delFlag" class="form-control" type="text">
</div>
</div>
</div>
</form>
</div>
<th:block th:include="include :: footer" />
@ -115,4 +91,4 @@
}
</script>
</body>
</html>
</html>

@ -11,7 +11,7 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">规则编码:</label>
<div class="col-sm-8">
<input name="ruleCode" th:field="*{ruleCode}" class="form-control" type="text" required>
<input name="ruleCode" th:field="*{ruleCode}" class="form-control" type="text" maxlength="64" required>
</div>
</div>
</div>
@ -19,15 +19,15 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">规则名称:</label>
<div class="col-sm-8">
<input name="ruleName" th:field="*{ruleName}" class="form-control" type="text" required>
<input name="ruleName" th:field="*{ruleName}" class="form-control" type="text" maxlength="100" required>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">编码前缀:</label>
<label class="col-sm-4 control-label">编码前缀:</label>
<div class="col-sm-8">
<input name="codePrefix" th:field="*{codePrefix}" class="form-control" type="text" required>
<input name="codePrefix" th:field="*{codePrefix}" class="form-control" type="text" maxlength="20">
</div>
</div>
</div>
@ -35,7 +35,7 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">日期格式:</label>
<div class="col-sm-8">
<input name="dateFormat" th:field="*{dateFormat}" class="form-control" type="text" required>
<input name="dateFormat" th:field="*{dateFormat}" class="form-control" type="text" maxlength="20" required>
</div>
</div>
</div>
@ -43,21 +43,21 @@
<div class="form-group">
<label class="col-sm-4 control-label is-required">流水号长度:</label>
<div class="col-sm-8">
<input name="serialLength" th:field="*{serialLength}" class="form-control" type="text" required>
<input name="serialLength" th:field="*{serialLength}" class="form-control" type="number" min="1" max="10" required>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">当前流水号:</label>
<label class="col-sm-4 control-label">当前流水号:</label>
<div class="col-sm-8">
<input name="currentSerial" th:field="*{currentSerial}" class="form-control" type="text" required>
<p class="form-control-static" th:text="*{currentSerial}"></p>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">重置周期:DAY、MONTH、YEAR、NONE</label>
<label class="col-sm-4 control-label is-required">重置周期:</label>
<div class="col-sm-8">
<select name="resetCycle" class="form-control" th:with="type=${@dict.getType('ams_code_reset_cycle')}" required>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{resetCycle}"></option>
@ -67,15 +67,15 @@
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label">上次流水键例如20260526</label>
<label class="col-sm-4 control-label">上次流水键:</label>
<div class="col-sm-8">
<input name="lastSerialKey" th:field="*{lastSerialKey}" class="form-control" type="text">
<p class="form-control-static" th:text="*{lastSerialKey}"></p>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-sm-4 control-label is-required">启用状态:Y启用N停用</label>
<label class="col-sm-4 control-label is-required">启用状态:</label>
<div class="col-sm-8">
<div class="radio-box" th:each="dict : ${@dict.getType('ams_enabled_status')}">
<input type="radio" th:id="${'enabled_' + dict.dictCode}" name="enabled" th:value="${dict.dictValue}" th:field="*{enabled}" required>
@ -108,4 +108,4 @@
}
</script>
</body>
</html>
</html>

@ -19,34 +19,7 @@
<input type="text" name="ruleName"/>
</li>
<li>
<label>编码前缀:</label>
<input type="text" name="codePrefix"/>
</li>
<li>
<label>日期格式:</label>
<input type="text" name="dateFormat"/>
</li>
<li>
<label>流水号长度:</label>
<input type="text" name="serialLength"/>
</li>
<li>
<label>当前流水号:</label>
<input type="text" name="currentSerial"/>
</li>
<li>
<label>重置周期DAY、MONTH、YEAR、NONE</label>
<select name="resetCycle" th:with="type=${@dict.getType('ams_code_reset_cycle')}">
<option value="">所有</option>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
</select>
</li>
<li>
<label>上次流水键例如20260526</label>
<input type="text" name="lastSerialKey"/>
</li>
<li>
<label>启用状态Y启用N停用</label>
<label>启用状态:</label>
<select name="enabled" th:with="type=${@dict.getType('ams_enabled_status')}">
<option value="">所有</option>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
@ -131,18 +104,18 @@
},
{
field: 'resetCycle',
title: '重置周期DAY、MONTH、YEAR、NONE',
title: '重置周期',
formatter: function(value, row, index) {
return $.table.selectDictLabel(resetCycleDatas, value);
}
},
{
field: 'lastSerialKey',
title: '上次流水键例如20260526'
title: '上次流水键'
},
{
field: 'enabled',
title: '启用状态Y启用N停用',
title: '启用状态',
formatter: function(value, row, index) {
return $.table.selectDictLabel(enabledDatas, value);
}
@ -167,4 +140,4 @@
});
</script>
</body>
</html>
</html>

@ -64,7 +64,7 @@
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="col-sm-4 control-label">重置周期:DAY、MONTH、YEAR、NONE</label>
<label class="col-sm-4 control-label">重置周期:</label>
<div class="col-sm-8">
<p class="form-control-plaintext" th:text="*{@dict.getLabel('ams_code_reset_cycle', resetCycle)}"></p>
</div>
@ -72,7 +72,7 @@
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="col-sm-4 control-label">上次流水键例如20260526</label>
<label class="col-sm-4 control-label">上次流水键:</label>
<div class="col-sm-8">
<p class="form-control-plaintext" th:text="*{lastSerialKey}"></p>
</div>
@ -82,7 +82,7 @@
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="col-sm-4 control-label">启用状态:Y启用N停用</label>
<label class="col-sm-4 control-label">启用状态:</label>
<div class="col-sm-8">
<p class="form-control-plaintext" th:text="*{@dict.getLabel('ams_enabled_status', enabled)}"></p>
</div>

@ -39,19 +39,19 @@ public class SysCodeRule extends BaseEntity
private Long serialLength;
/** 当前流水号 */
@Excel(name = "当前流水号")
@Excel(name = "当前流水号", type = Excel.Type.EXPORT)
private Long currentSerial;
/** 重置周期DAY、MONTH、YEAR、NONE */
@Excel(name = "重置周期DAY、MONTH、YEAR、NONE")
@Excel(name = "重置周期", dictType = "ams_code_reset_cycle", comboReadDict = true)
private String resetCycle;
/** 上次流水键例如20260526 */
@Excel(name = "上次流水键例如20260526")
@Excel(name = "上次流水键", type = Excel.Type.EXPORT)
private String lastSerialKey;
/** 启用状态Y启用N停用 */
@Excel(name = "启用状态Y启用N停用")
@Excel(name = "启用状态", dictType = "ams_enabled_status", comboReadDict = true)
private String enabled;
/** 删除标志0存在1删除 */

@ -1,6 +1,8 @@
package com.ruoyi.system.mapper;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.system.domain.SysCodeRule;
/**
@ -27,6 +29,30 @@ public interface SysCodeRuleMapper
*/
public List<SysCodeRule> selectSysCodeRuleList(SysCodeRule sysCodeRule);
/**
*
*
* @param ruleCode
* @return
*/
public SysCodeRule selectSysCodeRuleByRuleCode(String ruleCode);
/**
*
*
* @param ruleCode
* @return
*/
public SysCodeRule selectSysCodeRuleByRuleCodeForUpdate(String ruleCode);
/**
*
*
* @param ruleId
* @return
*/
public SysCodeRule selectSysCodeRuleByRuleIdForUpdate(Long ruleId);
/**
*
*
@ -43,6 +69,20 @@ public interface SysCodeRuleMapper
*/
public int updateSysCodeRule(SysCodeRule sysCodeRule);
/**
*
*
* @param ruleId ID
* @param currentSerial
* @param lastSerialKey
* @param updateTime
* @return
*/
public int updateSerialState(@Param("ruleId") Long ruleId,
@Param("currentSerial") Long currentSerial,
@Param("lastSerialKey") String lastSerialKey,
@Param("updateTime") Date updateTime);
/**
*
*

@ -27,6 +27,22 @@ public interface ISysCodeRuleService
*/
public List<SysCodeRule> selectSysCodeRuleList(SysCodeRule sysCodeRule);
/**
*
*
* @param sysCodeRule
* @return
*/
public boolean checkRuleCodeUnique(SysCodeRule sysCodeRule);
/**
*
*
* @param ruleCode
* @return
*/
public String nextCode(String ruleCode);
/**
*
*

@ -1,9 +1,15 @@
package com.ruoyi.system.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.SysCodeRuleMapper;
import com.ruoyi.system.domain.SysCodeRule;
import com.ruoyi.system.service.ISysCodeRuleService;
@ -18,6 +24,28 @@ import com.ruoyi.common.core.text.Convert;
@Service
public class SysCodeRuleServiceImpl implements ISysCodeRuleService
{
private static final String ENABLED_YES = "Y";
private static final String ENABLED_NO = "N";
private static final String DEL_FLAG_NORMAL = "0";
private static final String RESET_CYCLE_DAY = "DAY";
private static final String RESET_CYCLE_MONTH = "MONTH";
private static final String RESET_CYCLE_YEAR = "YEAR";
private static final String RESET_CYCLE_NONE = "NONE";
private static final String DEFAULT_DATE_FORMAT = "yyyyMMdd";
private static final Long DEFAULT_SERIAL_LENGTH = 4L;
private static final int MAX_SERIAL_LENGTH = 10;
private static final int MAX_BUSINESS_CODE_LENGTH = 64;
@Autowired
private SysCodeRuleMapper sysCodeRuleMapper;
@ -45,6 +73,83 @@ public class SysCodeRuleServiceImpl implements ISysCodeRuleService
return sysCodeRuleMapper.selectSysCodeRuleList(sysCodeRule);
}
/**
*
*
* @param sysCodeRule
* @return
*/
@Override
public boolean checkRuleCodeUnique(SysCodeRule sysCodeRule)
{
if (StringUtils.isNull(sysCodeRule) || StringUtils.isEmpty(sysCodeRule.getRuleCode()))
{
return UserConstants.UNIQUE;
}
Long ruleId = StringUtils.isNull(sysCodeRule.getRuleId()) ? -1L : sysCodeRule.getRuleId();
SysCodeRule info = sysCodeRuleMapper.selectSysCodeRuleByRuleCode(StringUtils.trim(sysCodeRule.getRuleCode()));
if (StringUtils.isNotNull(info) && info.getRuleId().longValue() != ruleId.longValue())
{
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
}
/**
*
*
* @param ruleCode
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public String nextCode(String ruleCode)
{
String normalizedRuleCode = StringUtils.trim(ruleCode);
if (StringUtils.isEmpty(normalizedRuleCode))
{
throw new ServiceException("编码规则编码不能为空");
}
SysCodeRule rule = sysCodeRuleMapper.selectSysCodeRuleByRuleCodeForUpdate(normalizedRuleCode);
if (StringUtils.isNull(rule))
{
throw new ServiceException(String.format("编码规则【%1$s】不存在或已删除", normalizedRuleCode));
}
if (!StringUtils.equals(ENABLED_YES, rule.getEnabled()))
{
throw new ServiceException(String.format("编码规则【%1$s】已停用", normalizedRuleCode));
}
validateRule(rule);
Date now = DateUtils.getNowDate();
String serialKey = buildSerialKey(rule, now);
long currentSerial = StringUtils.nvl(rule.getCurrentSerial(), 0L);
long nextSerial = shouldReset(rule, serialKey) ? 1L : currentSerial + 1L;
long maxSerial = maxSerial(rule.getSerialLength().intValue());
if (nextSerial > maxSerial)
{
throw new ServiceException(String.format("编码规则【%1$s】流水号已超出%2$s位长度",
rule.getRuleCode(), rule.getSerialLength()));
}
String datePart = formatDate(rule.getDateFormat(), now);
String serialPart = String.format("%0" + rule.getSerialLength().intValue() + "d", nextSerial);
String nextCode = rule.getCodePrefix() + datePart + serialPart;
if (nextCode.length() > MAX_BUSINESS_CODE_LENGTH)
{
throw new ServiceException(String.format("编码规则【%1$s】生成的编号长度超过%2$s位",
rule.getRuleCode(), MAX_BUSINESS_CODE_LENGTH));
}
int rows = sysCodeRuleMapper.updateSerialState(rule.getRuleId(), nextSerial, serialKey, now);
if (rows != 1)
{
throw new ServiceException(String.format("编码规则【%1$s】流水号更新失败", rule.getRuleCode()));
}
return nextCode;
}
/**
*
*
@ -54,6 +159,17 @@ public class SysCodeRuleServiceImpl implements ISysCodeRuleService
@Override
public int insertSysCodeRule(SysCodeRule sysCodeRule)
{
normalizeRule(sysCodeRule);
sysCodeRule.setCurrentSerial(0L);
sysCodeRule.setLastSerialKey(null);
sysCodeRule.setDelFlag(DEL_FLAG_NORMAL);
sysCodeRule.setUpdateBy(null);
sysCodeRule.setUpdateTime(null);
validateRule(sysCodeRule);
if (!checkRuleCodeUnique(sysCodeRule))
{
throw new ServiceException("规则编码已存在");
}
sysCodeRule.setCreateTime(DateUtils.getNowDate());
return sysCodeRuleMapper.insertSysCodeRule(sysCodeRule);
}
@ -65,8 +181,28 @@ public class SysCodeRuleServiceImpl implements ISysCodeRuleService
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int updateSysCodeRule(SysCodeRule sysCodeRule)
{
if (StringUtils.isNull(sysCodeRule) || StringUtils.isNull(sysCodeRule.getRuleId()))
{
throw new ServiceException("编码规则ID不能为空");
}
SysCodeRule current = selectExistingRule(sysCodeRule.getRuleId());
normalizeRule(sysCodeRule);
if (isUsed(current) && !StringUtils.equals(current.getRuleCode(), sysCodeRule.getRuleCode()))
{
// 规则编码是业务服务调用的稳定标识,已生成过编号后修改会导致后续单据找不到原规则。
throw new ServiceException("已使用的编码规则不允许修改规则编码");
}
sysCodeRule.setCurrentSerial(current.getCurrentSerial());
sysCodeRule.setLastSerialKey(current.getLastSerialKey());
sysCodeRule.setDelFlag(current.getDelFlag());
validateRule(sysCodeRule);
if (!checkRuleCodeUnique(sysCodeRule))
{
throw new ServiceException("规则编码已存在");
}
sysCodeRule.setUpdateTime(DateUtils.getNowDate());
return sysCodeRuleMapper.updateSysCodeRule(sysCodeRule);
}
@ -78,9 +214,15 @@ public class SysCodeRuleServiceImpl implements ISysCodeRuleService
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int deleteSysCodeRuleByRuleIds(String ruleIds)
{
return sysCodeRuleMapper.deleteSysCodeRuleByRuleIds(Convert.toStrArray(ruleIds));
String[] ids = Convert.toStrArray(ruleIds);
for (String id : ids)
{
checkDeleteAllowed(Long.valueOf(id));
}
return sysCodeRuleMapper.deleteSysCodeRuleByRuleIds(ids);
}
/**
@ -90,8 +232,169 @@ public class SysCodeRuleServiceImpl implements ISysCodeRuleService
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int deleteSysCodeRuleByRuleId(Long ruleId)
{
checkDeleteAllowed(ruleId);
return sysCodeRuleMapper.deleteSysCodeRuleByRuleId(ruleId);
}
private SysCodeRule selectExistingRule(Long ruleId)
{
SysCodeRule rule = sysCodeRuleMapper.selectSysCodeRuleByRuleIdForUpdate(ruleId);
if (StringUtils.isNull(rule))
{
throw new ServiceException("编码规则不存在或已删除");
}
return rule;
}
private void normalizeRule(SysCodeRule rule)
{
if (StringUtils.isNull(rule))
{
throw new ServiceException("编码规则不能为空");
}
rule.setRuleCode(StringUtils.trim(rule.getRuleCode()));
rule.setRuleName(StringUtils.trim(rule.getRuleName()));
rule.setCodePrefix(StringUtils.isNull(rule.getCodePrefix()) ? "" : StringUtils.trim(rule.getCodePrefix()));
rule.setDateFormat(StringUtils.isEmpty(rule.getDateFormat())
? DEFAULT_DATE_FORMAT : StringUtils.trim(rule.getDateFormat()));
rule.setSerialLength(StringUtils.isNull(rule.getSerialLength())
? DEFAULT_SERIAL_LENGTH : rule.getSerialLength());
rule.setResetCycle(StringUtils.isEmpty(rule.getResetCycle())
? RESET_CYCLE_DAY : StringUtils.trim(rule.getResetCycle()));
rule.setEnabled(StringUtils.isEmpty(rule.getEnabled())
? ENABLED_YES : StringUtils.trim(rule.getEnabled()));
}
private void validateRule(SysCodeRule rule)
{
if (StringUtils.isEmpty(rule.getRuleCode()))
{
throw new ServiceException("规则编码不能为空");
}
if (rule.getRuleCode().length() > 64)
{
throw new ServiceException("规则编码长度不能超过64个字符");
}
if (StringUtils.isEmpty(rule.getRuleName()))
{
throw new ServiceException("规则名称不能为空");
}
if (rule.getRuleName().length() > 100)
{
throw new ServiceException("规则名称长度不能超过100个字符");
}
if (rule.getCodePrefix().length() > 20)
{
throw new ServiceException("编码前缀长度不能超过20个字符");
}
if (StringUtils.isEmpty(rule.getDateFormat()) || rule.getDateFormat().length() > 20)
{
throw new ServiceException("日期格式不能为空且长度不能超过20个字符");
}
if (StringUtils.isNull(rule.getSerialLength())
|| rule.getSerialLength() <= 0
|| rule.getSerialLength() > MAX_SERIAL_LENGTH)
{
throw new ServiceException("流水号长度必须在1到10之间");
}
if (StringUtils.isNull(rule.getCurrentSerial()) || rule.getCurrentSerial() < 0)
{
throw new ServiceException("当前流水号不能小于0");
}
if (rule.getCurrentSerial() > maxSerial(rule.getSerialLength().intValue()))
{
throw new ServiceException("当前流水号已超出配置的流水号长度");
}
if (!StringUtils.equalsAny(rule.getResetCycle(), RESET_CYCLE_DAY, RESET_CYCLE_MONTH,
RESET_CYCLE_YEAR, RESET_CYCLE_NONE))
{
throw new ServiceException("重置周期不合法");
}
if (!StringUtils.equalsAny(rule.getEnabled(), ENABLED_YES, ENABLED_NO))
{
throw new ServiceException("启用状态不合法");
}
if (StringUtils.isNotEmpty(rule.getLastSerialKey()) && rule.getLastSerialKey().length() > 20)
{
throw new ServiceException("上次流水键长度不能超过20个字符");
}
String datePart = formatDate(rule.getDateFormat(), DateUtils.getNowDate());
if (rule.getCodePrefix().length() + datePart.length() + rule.getSerialLength() > MAX_BUSINESS_CODE_LENGTH)
{
throw new ServiceException("编码前缀、日期和流水号组成的编号长度不能超过64位");
}
}
private String formatDate(String pattern, Date date)
{
try
{
return new SimpleDateFormat(pattern).format(date);
}
catch (IllegalArgumentException e)
{
throw new ServiceException("日期格式不合法");
}
}
private String buildSerialKey(SysCodeRule rule, Date date)
{
if (StringUtils.equals(RESET_CYCLE_DAY, rule.getResetCycle()))
{
return DateUtils.parseDateToStr("yyyyMMdd", date);
}
if (StringUtils.equals(RESET_CYCLE_MONTH, rule.getResetCycle()))
{
return DateUtils.parseDateToStr("yyyyMM", date);
}
if (StringUtils.equals(RESET_CYCLE_YEAR, rule.getResetCycle()))
{
return DateUtils.parseDateToStr("yyyy", date);
}
return rule.getLastSerialKey();
}
private boolean shouldReset(SysCodeRule rule, String serialKey)
{
return !StringUtils.equals(RESET_CYCLE_NONE, rule.getResetCycle())
&& !StringUtils.equals(serialKey, rule.getLastSerialKey());
}
private long maxSerial(int serialLength)
{
if (serialLength >= MAX_SERIAL_LENGTH)
{
return Integer.MAX_VALUE;
}
long max = 1L;
for (int i = 0; i < serialLength; i++)
{
max *= 10L;
}
return max - 1L;
}
private void checkDeleteAllowed(Long ruleId)
{
SysCodeRule rule = sysCodeRuleMapper.selectSysCodeRuleByRuleIdForUpdate(ruleId);
if (StringUtils.isNull(rule))
{
return;
}
if (isUsed(rule))
{
throw new ServiceException(String.format("编码规则【%1$s】已生成过业务编号不允许删除可停用保留",
rule.getRuleName()));
}
}
private boolean isUsed(SysCodeRule rule)
{
return StringUtils.nvl(rule.getCurrentSerial(), 0L) > 0L
|| StringUtils.isNotEmpty(rule.getLastSerialKey());
}
}

@ -29,22 +29,35 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectSysCodeRuleList" parameterType="SysCodeRule" resultMap="SysCodeRuleResult">
<include refid="selectSysCodeRuleVo"/>
<where>
<where>
del_flag = '0'
<if test="ruleCode != null and ruleCode != ''"> and rule_code = #{ruleCode}</if>
<if test="ruleName != null and ruleName != ''"> and rule_name like concat('%', #{ruleName}, '%')</if>
<if test="codePrefix != null and codePrefix != ''"> and code_prefix = #{codePrefix}</if>
<if test="dateFormat != null and dateFormat != ''"> and date_format = #{dateFormat}</if>
<if test="serialLength != null "> and serial_length = #{serialLength}</if>
<if test="currentSerial != null "> and current_serial = #{currentSerial}</if>
<if test="resetCycle != null and resetCycle != ''"> and reset_cycle = #{resetCycle}</if>
<if test="lastSerialKey != null and lastSerialKey != ''"> and last_serial_key = #{lastSerialKey}</if>
<if test="enabled != null and enabled != ''"> and enabled = #{enabled}</if>
</where>
</select>
<select id="selectSysCodeRuleByRuleId" parameterType="Long" resultMap="SysCodeRuleResult">
<include refid="selectSysCodeRuleVo"/>
where rule_id = #{ruleId}
where rule_id = #{ruleId} and del_flag = '0'
</select>
<select id="selectSysCodeRuleByRuleCode" parameterType="String" resultMap="SysCodeRuleResult">
<include refid="selectSysCodeRuleVo"/>
where rule_code = #{ruleCode}
limit 1
</select>
<select id="selectSysCodeRuleByRuleCodeForUpdate" parameterType="String" resultMap="SysCodeRuleResult">
<include refid="selectSysCodeRuleVo"/>
where rule_code = #{ruleCode} and del_flag = '0'
for update
</select>
<select id="selectSysCodeRuleByRuleIdForUpdate" parameterType="Long" resultMap="SysCodeRuleResult">
<include refid="selectSysCodeRuleVo"/>
where rule_id = #{ruleId} and del_flag = '0'
for update
</select>
<insert id="insertSysCodeRule" parameterType="SysCodeRule" useGeneratedKeys="true" keyProperty="ruleId">
@ -90,32 +103,35 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<trim prefix="SET" suffixOverrides=",">
<if test="ruleCode != null and ruleCode != ''">rule_code = #{ruleCode},</if>
<if test="ruleName != null and ruleName != ''">rule_name = #{ruleName},</if>
<if test="codePrefix != null and codePrefix != ''">code_prefix = #{codePrefix},</if>
code_prefix = #{codePrefix},
<if test="dateFormat != null and dateFormat != ''">date_format = #{dateFormat},</if>
<if test="serialLength != null">serial_length = #{serialLength},</if>
<if test="currentSerial != null">current_serial = #{currentSerial},</if>
<if test="resetCycle != null and resetCycle != ''">reset_cycle = #{resetCycle},</if>
<if test="lastSerialKey != null">last_serial_key = #{lastSerialKey},</if>
<if test="enabled != null and enabled != ''">enabled = #{enabled},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="delFlag != null">del_flag = #{delFlag},</if>
</trim>
where rule_id = #{ruleId}
where rule_id = #{ruleId} and del_flag = '0'
</update>
<delete id="deleteSysCodeRuleByRuleId" parameterType="Long">
delete from sys_code_rule where rule_id = #{ruleId}
</delete>
<update id="updateSerialState">
update sys_code_rule
set current_serial = #{currentSerial},
last_serial_key = #{lastSerialKey},
update_time = #{updateTime}
where rule_id = #{ruleId} and del_flag = '0'
</update>
<delete id="deleteSysCodeRuleByRuleIds" parameterType="String">
delete from sys_code_rule where rule_id in
<update id="deleteSysCodeRuleByRuleId" parameterType="Long">
update sys_code_rule set del_flag = '1' where rule_id = #{ruleId} and del_flag = '0'
</update>
<update id="deleteSysCodeRuleByRuleIds" parameterType="String">
update sys_code_rule set del_flag = '1' where del_flag = '0' and rule_id in
<foreach item="ruleId" collection="array" open="(" separator="," close=")">
#{ruleId}
</foreach>
</delete>
</update>
</mapper>
</mapper>

Loading…
Cancel
Save