生成规则可控
This commit is contained in:
parent
582707f996
commit
c4ed9c982e
|
@ -0,0 +1,40 @@
|
|||
package org.jeecg.modules.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 生成规则实体类
|
||||
*
|
||||
* @author Xubx
|
||||
* @date 2025/04/27
|
||||
*/
|
||||
@Data
|
||||
@TableName("cees_generative_rules")
|
||||
@ApiModel(value = "cees_generative_rules对象", description = "生成规则表")
|
||||
public class CeesGenerativeRules {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@ApiModelProperty(value = "主键")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 账号类型的位置(第几位,1-8)
|
||||
* 生成时位置顺序:管理员- 学生 -本校老师 -外校老师
|
||||
*/
|
||||
private Integer groupType;
|
||||
|
||||
/**
|
||||
* 学科类型的位置(第几位,1-8)
|
||||
* 生成时位置顺序
|
||||
*/
|
||||
private Integer majorId;
|
||||
|
||||
/**
|
||||
* 为了唯一查询
|
||||
*/
|
||||
private String Is_default;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.jeecg.modules.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.jeecg.modules.entity.CeesGenerativeRules;
|
||||
|
||||
public interface CeesGenerativeRulesMapper extends BaseMapper<CeesGenerativeRules> {
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.jeecg.modules.mapper.CeesGenerativeRulesMapper">
|
||||
|
||||
</mapper>
|
|
@ -6,10 +6,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.entity.CeesLocalTeacher;
|
||||
import org.jeecg.modules.entity.CeesUser;
|
||||
import org.jeecg.modules.entity.CeesWaiTeacher;
|
||||
import org.jeecg.modules.entity.Student;
|
||||
import org.jeecg.modules.entity.*;
|
||||
import org.jeecg.modules.entity.dto.CreateAccountDto;
|
||||
import org.jeecg.modules.enums.identity.MajorLetterEnum;
|
||||
import org.jeecg.modules.enums.identity.StudentLetterEnum;
|
||||
|
@ -44,6 +41,8 @@ public class CeesUserServiceImpl extends ServiceImpl<CeesUserMapper, CeesUser> i
|
|||
CeesLocalTeacherMapper ceesLocalTeacherMapper;
|
||||
@Resource
|
||||
CeesWaiTeacherMapper ceesWaiTeacherMapper;
|
||||
@Resource
|
||||
CeesGenerativeRulesMapper ceesGenerativeRulesMapper;
|
||||
|
||||
/**
|
||||
* @param ceesUser
|
||||
|
@ -270,6 +269,12 @@ public class CeesUserServiceImpl extends ServiceImpl<CeesUserMapper, CeesUser> i
|
|||
throw new IllegalArgumentException("参数无效");
|
||||
}
|
||||
|
||||
// 获取生成规则
|
||||
CeesGenerativeRules rules = getGenerativeRules();
|
||||
if (rules == null) {
|
||||
throw new IllegalArgumentException("找不到对应的账号生成规则");
|
||||
}
|
||||
|
||||
// 存储已存在的用户ID
|
||||
Set<String> set = new HashSet<>();
|
||||
// 存储与majorID相关的用户专业id
|
||||
|
@ -290,7 +295,7 @@ public class CeesUserServiceImpl extends ServiceImpl<CeesUserMapper, CeesUser> i
|
|||
// 根据专业 ID 生成账号
|
||||
String prefix = getPrefixByMajorId(majorId);
|
||||
String startId = userMIDList.isEmpty() ? getDefaultStartId(majorId) : userMIDList.get(userMIDList.size() - 1);
|
||||
saveAccount(num, set, groupType, h5Users, majorId, startId, prefix);
|
||||
saveAccount(num, set, groupType, h5Users, majorId, startId, prefix, rules);
|
||||
|
||||
// 保存生成的用户账号
|
||||
this.saveBatch(h5Users);
|
||||
|
@ -309,6 +314,13 @@ public class CeesUserServiceImpl extends ServiceImpl<CeesUserMapper, CeesUser> i
|
|||
userMIDList.clear();
|
||||
}
|
||||
|
||||
// 获取生成规则
|
||||
private CeesGenerativeRules getGenerativeRules() {
|
||||
LambdaQueryWrapper<CeesGenerativeRules> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(CeesGenerativeRules::getIs_default, "1");
|
||||
return ceesGenerativeRulesMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
private String getPrefixByMajorId(Integer majorId) {
|
||||
switch (majorId) {
|
||||
case 1:
|
||||
|
@ -340,7 +352,7 @@ public class CeesUserServiceImpl extends ServiceImpl<CeesUserMapper, CeesUser> i
|
|||
}
|
||||
public void saveAccount(
|
||||
Integer num, Set<String> set, Integer groupType, List<CeesUser> h5Users,
|
||||
Integer majorId, @NotNull String code, String label
|
||||
Integer majorId, @NotNull String code, String label, CeesGenerativeRules rules
|
||||
) {
|
||||
// 参数校验
|
||||
if (num == null || num <= 0) {
|
||||
|
@ -352,13 +364,16 @@ public class CeesUserServiceImpl extends ServiceImpl<CeesUserMapper, CeesUser> i
|
|||
if (code == null || code.isEmpty()) {
|
||||
throw new IllegalArgumentException("起始用户专业 ID 不能为空");
|
||||
}
|
||||
if (rules == null) {
|
||||
throw new IllegalArgumentException("生成规则不能为空");
|
||||
}
|
||||
|
||||
int newCode = Integer.parseInt(code.substring(1)); // 提取数字部分
|
||||
|
||||
while (num != 0) {
|
||||
String randomNum = getRandomNum(8);
|
||||
// 生成账号
|
||||
String account = generateAccount(randomNum, groupType, majorId);
|
||||
// 生成账号 - 使用规则中的位置信息
|
||||
String account = generateAccount(randomNum, groupType, majorId, rules);
|
||||
|
||||
if (!set.contains(account)) {
|
||||
num--;
|
||||
|
@ -377,11 +392,34 @@ public class CeesUserServiceImpl extends ServiceImpl<CeesUserMapper, CeesUser> i
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
private String generateAccount(String randomNum, Integer groupType, Integer majorId) {
|
||||
//从随机数中提取前 3 位和后 3 位,拼接 groupType 和 majorId,生成 account。
|
||||
String prefix = randomNum.substring(0, 3);
|
||||
String suffix = randomNum.substring(5, 8);
|
||||
return prefix + groupType + majorId + suffix;
|
||||
// 使用规则中的位置信息
|
||||
private String generateAccount(String randomNum, Integer groupType, Integer majorId, CeesGenerativeRules rules) {
|
||||
// 根据规则中的位置信息构建账号
|
||||
char[] accountParts = new char[8]; // 账号长度为8
|
||||
char[] randomDigits = randomNum.toCharArray();
|
||||
|
||||
// 填充随机数部分
|
||||
int randomIndex = 0;
|
||||
for (int i = 0; i < accountParts.length; i++) {
|
||||
// 检查当前位置是否被规则占用
|
||||
boolean isGroupTypePos = (i + 1) == rules.getGroupType();
|
||||
boolean isMajorIdPos = (i + 1) == rules.getMajorId();
|
||||
|
||||
if (isGroupTypePos) {
|
||||
accountParts[i] = groupType.toString().charAt(0);
|
||||
} else if (isMajorIdPos) {
|
||||
accountParts[i] = majorId.toString().charAt(0);
|
||||
} else {
|
||||
// 使用随机数填充
|
||||
if (randomIndex < randomDigits.length) {
|
||||
accountParts[i] = randomDigits[randomIndex++];
|
||||
} else {
|
||||
accountParts[i] = '0'; // 默认值,理论上不会走到这里
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new String(accountParts);
|
||||
}
|
||||
|
||||
private CeesUser createH5User(String account, Integer groupType, Integer majorId, String label, int newCode) {
|
||||
|
|
Loading…
Reference in New Issue