班次编号生成,改为XYD+年度+补零序号

This commit is contained in:
2026-09-21 16:36:09 +08:00
parent 035c01e90b
commit 1482bfd31c
9 changed files with 112 additions and 18 deletions
@@ -0,0 +1,69 @@
package com.roomroot.jwgl.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.roomroot.jwgl.entity.XYDB;
import com.roomroot.jwgl.mapper.XYDBMapper;
import org.springframework.stereotype.Component;
import jakarta.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 班次(学员队)编号生成器。
* <p>
* 规则:XYD + 年度(4位) + 四位补零序号,序号在年度内递增,如 XYD20260001。
* 所有系统建队入口统一走此生成器;调用方传入手工编号时仍按手工编号落库。
* </p>
*/
@Component
public class TeamCodeUtil {
private static final String PREFIX = "XYD";
private static final Pattern YEAR_PATTERN = Pattern.compile("\\d{4}");
@Resource
private XYDBMapper xydbMapper;
/**
* 取指定年度下一个班次编号。
* synchronized 避免单实例内并发取到同一序号;跨实例并发由主键约束兜底。
*/
public synchronized String nextXydbh(Integer year) {
int y = (year != null && year > 0) ? year : LocalDate.now().getYear();
String prefix = PREFIX + y;
List<XYDB> rows = xydbMapper.selectList(new LambdaQueryWrapper<XYDB>()
.select(XYDB::getXydbh)
.likeRight(XYDB::getXydbh, prefix));
int max = 0;
for (XYDB row : rows) {
String code = row.getXydbh();
if (code == null || code.length() <= prefix.length()) {
continue;
}
String tail = code.substring(prefix.length());
// 只把 1-4 位纯数字后缀计入序号(兼容 XYD202601 这类两位序号存量数据),其它形态跳过
if (tail.length() > 4 || !tail.chars().allMatch(Character::isDigit)) {
continue;
}
max = Math.max(max, Integer.parseInt(tail));
}
return prefix + String.format("%04d", max + 1);
}
/**
* 按年级文本解析年度后取号(如 "2026级" → 2026),解析不出取当前年度。
*/
public String nextXydbhForNj(String nj) {
Integer year = null;
if (nj != null) {
Matcher m = YEAR_PATTERN.matcher(nj);
if (m.find()) {
year = Integer.valueOf(m.group());
}
}
return nextXydbh(year);
}
}