parent
5fa033bc9e
commit
e52c8584e4
15 changed files with 748 additions and 137 deletions
@ -0,0 +1,42 @@ |
||||
package com.huoran.users.controller; |
||||
|
||||
import com.huoran.common.response.R; |
||||
import com.huoran.common.utils.TokenUtils; |
||||
import com.huoran.users.entity.req.AddUsersInBatchesReq; |
||||
import com.huoran.users.service.IHrUserAccountService; |
||||
import com.huoran.users.service.IHrUserInfoService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
//@Api(tags = "批量新增用户", tags = "批量新增用户")
|
||||
@Api(value = "批量新增用户相关", tags = "批量新增用户相关") |
||||
@RequestMapping("/users/batchProcessing") |
||||
|
||||
@RestController |
||||
public class AddUsersInBatchesController { |
||||
|
||||
@Autowired |
||||
private IHrUserInfoService userService; |
||||
|
||||
@Autowired |
||||
private IHrUserAccountService userAccountService; |
||||
|
||||
@ApiOperation(value = "生成用户") |
||||
@PostMapping("/users/generate") |
||||
public R generateUsers(@RequestBody AddUsersInBatchesReq addUsersInBatchesReq) { |
||||
Integer school_id = TokenUtils.getSchoolId(); |
||||
addUsersInBatchesReq.setSchoolId(school_id); |
||||
userService.generateUsersWithParams(addUsersInBatchesReq); |
||||
return R.ok("用户生成成功"); |
||||
} |
||||
|
||||
//校验姓名/学号工号是否存在
|
||||
@ApiOperation(value = "校验姓名/学号工号是否存在") |
||||
@PostMapping("/checkUserNameOrWorkNumber") |
||||
public R checkUserNameOrWorkNumber(){ |
||||
return userService.checkUserNameOrWorkNumber(TokenUtils.getAccountId()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,40 @@ |
||||
package com.huoran.users.entity.req; |
||||
|
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class AddUsersInBatchesReq { |
||||
|
||||
@ApiModelProperty(value = "生成用户数量") |
||||
private Integer numberOfUsers; |
||||
|
||||
@ApiModelProperty(value = "批量生成数量") |
||||
private Integer batchSize; |
||||
|
||||
|
||||
|
||||
@ApiModelProperty(value = "平台id") |
||||
private String platformId; |
||||
|
||||
@ApiModelProperty(value = "学校id", hidden = true) |
||||
private Integer schoolId; |
||||
|
||||
@ApiModelProperty("平台端id区分:0->教师端 1->学生端") |
||||
private Integer type; |
||||
|
||||
@ApiModelProperty("班级id") |
||||
private Integer classId; |
||||
|
||||
//一级部门
|
||||
@ApiModelProperty("员工架构ID/一级部门id") |
||||
private Integer staffArchitectureId; |
||||
|
||||
//二级部门
|
||||
@ApiModelProperty("二级部门ID") |
||||
private Integer gradeId; |
||||
|
||||
@ApiModelProperty(value = "角色id") |
||||
private String roleId; |
||||
|
||||
} |
@ -0,0 +1,155 @@ |
||||
/* |
||||
package com.huoran.users.utils; |
||||
|
||||
import java.sql.*; |
||||
import java.util.Date; |
||||
|
||||
public class UserGenerator { |
||||
|
||||
private static final String DB_URL = "jdbc:mysql://localhost:3306/data?serverTimezone=UTC"; |
||||
private static final String USER = "root"; |
||||
private static final String PASS = "123456"; |
||||
|
||||
public static void main(String[] args) { |
||||
int numberOfUsers = 9900; // 默认生成的用户数量
|
||||
int batchSize = 1000; // 默认批量插入的数量
|
||||
|
||||
if (args.length > 0) { |
||||
try { |
||||
numberOfUsers = Integer.parseInt(args[0]); |
||||
} catch (NumberFormatException e) { |
||||
System.err.println("Invalid number of users specified. Using default value: 1000"); |
||||
} |
||||
} |
||||
|
||||
if (args.length > 1) { |
||||
try { |
||||
batchSize = Integer.parseInt(args[1]); |
||||
} catch (NumberFormatException e) { |
||||
System.err.println("Invalid batch size specified. Using default value: 1000"); |
||||
} |
||||
} |
||||
|
||||
generateUsers(numberOfUsers, batchSize); |
||||
} |
||||
|
||||
public static void generateUsers(int numberOfUsers, int batchSize) { |
||||
Connection conn = null; |
||||
PreparedStatement pstmtUserInfo = null; |
||||
PreparedStatement pstmtUserAccount = null; |
||||
|
||||
try { |
||||
// 连接数据库
|
||||
conn = DriverManager.getConnection(DB_URL, USER, PASS); |
||||
conn.setAutoCommit(false); // 开启事务
|
||||
|
||||
// 查询上次最后一条记录的主键ID
|
||||
int lastUserId = getLastUserId(conn); |
||||
|
||||
// 准备 SQL 语句
|
||||
String sqlUserInfo = "INSERT INTO hr_user_info (unique_identification, create_time, school_id) VALUES (?, ?, 1)"; |
||||
String sqlUserAccount = "INSERT INTO hr_user_account (user_id, password, role_id, is_enable, create_time, account) VALUES (?, ?, ?, ?, ?, ?)"; |
||||
|
||||
pstmtUserInfo = conn.prepareStatement(sqlUserInfo, Statement.RETURN_GENERATED_KEYS); |
||||
pstmtUserAccount = conn.prepareStatement(sqlUserAccount, Statement.RETURN_GENERATED_KEYS); |
||||
|
||||
// 生成用户信息
|
||||
long baseTime = System.currentTimeMillis(); |
||||
for (int i = 1; i <= numberOfUsers; i++) { |
||||
String uniqueIdentification = generateUniqueIdentification(baseTime, i); |
||||
Date createTime = new Date(); |
||||
|
||||
// 插入用户信息
|
||||
pstmtUserInfo.setString(1, uniqueIdentification); |
||||
pstmtUserInfo.setTimestamp(2, new java.sql.Timestamp(createTime.getTime())); |
||||
pstmtUserInfo.addBatch(); |
||||
|
||||
if (i % batchSize == 0) { // 每 batchSize 条记录执行一次批量插入
|
||||
pstmtUserInfo.executeBatch(); |
||||
pstmtUserInfo.clearBatch(); // 清空批处理中的对象
|
||||
} |
||||
} |
||||
|
||||
// 执行剩余的批量插入
|
||||
pstmtUserInfo.executeBatch(); |
||||
pstmtUserInfo.clearBatch(); // 清空批处理中的对象
|
||||
|
||||
// 获取新生成的用户 ID 并生成账户信息
|
||||
String selectUserIdSql = "SELECT user_id FROM hr_user_info WHERE user_id > ? ORDER BY user_id"; |
||||
try (PreparedStatement selectUserIdStmt = conn.prepareStatement(selectUserIdSql)) { |
||||
selectUserIdStmt.setInt(1, lastUserId); |
||||
try (ResultSet rs = selectUserIdStmt.executeQuery()) { |
||||
while (rs.next()) { |
||||
int userId = rs.getInt("user_id"); |
||||
String password = "e10adc3949ba59abbe56e057f20f883e"; // 实际应用中应使用安全方式生成随机密码
|
||||
String roleId = "0"; |
||||
int isEnable = 1; |
||||
Date createTime = new Date(); |
||||
String account = formatAccount(userId); |
||||
|
||||
// 插入账户信息
|
||||
pstmtUserAccount.setInt(1, userId); |
||||
pstmtUserAccount.setString(2, password); |
||||
pstmtUserAccount.setString(3, roleId); |
||||
pstmtUserAccount.setInt(4, isEnable); |
||||
pstmtUserAccount.setTimestamp(5, new java.sql.Timestamp(createTime.getTime())); |
||||
pstmtUserAccount.setString(6, account); |
||||
pstmtUserAccount.addBatch(); |
||||
|
||||
if ((userId - lastUserId) % batchSize == 0) { // 每 batchSize 条记录执行一次批量插入
|
||||
pstmtUserAccount.executeBatch(); |
||||
pstmtUserAccount.clearBatch(); // 清空批处理中的对象
|
||||
} |
||||
} |
||||
|
||||
// 执行剩余的批量插入
|
||||
pstmtUserAccount.executeBatch(); |
||||
pstmtUserAccount.clearBatch(); // 清空批处理中的对象
|
||||
} |
||||
} |
||||
|
||||
// 提交事务
|
||||
conn.commit(); |
||||
System.out.println("成功生成 " + numberOfUsers + " 个用户"); |
||||
|
||||
} catch (SQLException e) { |
||||
e.printStackTrace(); |
||||
if (conn != null) { |
||||
try { |
||||
conn.rollback(); // 回滚事务
|
||||
} catch (SQLException ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
} |
||||
} finally { |
||||
// 关闭资源
|
||||
try { |
||||
if (pstmtUserAccount != null) pstmtUserAccount.close(); |
||||
if (pstmtUserInfo != null) pstmtUserInfo.close(); |
||||
if (conn != null) conn.close(); |
||||
} catch (SQLException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static int getLastUserId(Connection conn) throws SQLException { |
||||
String sql = "SELECT COALESCE(MAX(user_id), 0) AS last_user_id FROM hr_user_info"; |
||||
try (PreparedStatement pstmt = conn.prepareStatement(sql); |
||||
ResultSet rs = pstmt.executeQuery()) { |
||||
if (rs.next()) { |
||||
return rs.getInt("last_user_id"); |
||||
} |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
private static String generateUniqueIdentification(long baseTime, int sequence) { |
||||
return String.valueOf(baseTime * 1000 + sequence); |
||||
} |
||||
|
||||
private static String formatAccount(int userId) { |
||||
return String.format("%010d", 9000000000L + userId); |
||||
} |
||||
} |
||||
*/ |
@ -0,0 +1,171 @@ |
||||
/* |
||||
package com.huoran.users.utils; |
||||
|
||||
import java.sql.*; |
||||
import java.util.Date; |
||||
|
||||
public class UserGenerator1 { |
||||
|
||||
private static final String DB_URL = "jdbc:mysql://localhost:3306/data?serverTimezone=UTC"; |
||||
private static final String USER = "root"; |
||||
private static final String PASS = "123456"; |
||||
|
||||
public static void main(String[] args) { |
||||
int numberOfUsers = 10001; // 默认生成的用户数量
|
||||
int batchSize = 1000; // 默认批量插入的数量
|
||||
String role_id = "0"; // 默认角色ID
|
||||
String school_id = "1"; // 默认学校ID
|
||||
String platform_id = "1"; // 默认平台ID
|
||||
|
||||
if (args.length > 0) { |
||||
try { |
||||
numberOfUsers = Integer.parseInt(args[0]); |
||||
} catch (NumberFormatException e) { |
||||
System.err.println("Invalid number of users specified. Using default value: 9900"); |
||||
} |
||||
} |
||||
|
||||
if (args.length > 1) { |
||||
try { |
||||
batchSize = Integer.parseInt(args[1]); |
||||
} catch (NumberFormatException e) { |
||||
System.err.println("Invalid batch size specified. Using default value: 1000"); |
||||
} |
||||
} |
||||
|
||||
if (args.length > 2) { |
||||
role_id = args[2]; |
||||
} |
||||
|
||||
if (args.length > 3) { |
||||
school_id = args[3]; |
||||
} |
||||
|
||||
if (args.length > 4) { |
||||
platform_id = args[4]; |
||||
} |
||||
|
||||
generateUsersWithParams(numberOfUsers, batchSize, role_id, school_id, platform_id); |
||||
} |
||||
|
||||
public static void generateUsersWithParams(int numberOfUsers, int batchSize, String role_id, String school_id, String platform_id) { |
||||
Connection conn = null; |
||||
PreparedStatement pstmtUserInfo = null; |
||||
PreparedStatement pstmtUserAccount = null; |
||||
|
||||
try { |
||||
// 连接数据库
|
||||
conn = DriverManager.getConnection(DB_URL, USER, PASS); |
||||
conn.setAutoCommit(false); // 开启事务
|
||||
|
||||
// 查询上次最后一条记录的主键ID
|
||||
int lastUserId = getLastUserId(conn); |
||||
|
||||
// 准备 SQL 语句
|
||||
String sqlUserInfo = "INSERT INTO hr_user_info (unique_identification, create_time, school_id) VALUES (?, ?, ?)"; |
||||
String sqlUserAccount = "INSERT INTO hr_user_account (user_id, password, role_id, is_enable, create_time, account, platform_id) VALUES (?, ?, ?, ?, ?, ?, ?)"; |
||||
|
||||
pstmtUserInfo = conn.prepareStatement(sqlUserInfo, Statement.RETURN_GENERATED_KEYS); |
||||
pstmtUserAccount = conn.prepareStatement(sqlUserAccount, Statement.RETURN_GENERATED_KEYS); |
||||
|
||||
// 生成用户信息
|
||||
long baseTime = System.currentTimeMillis(); |
||||
for (int i = 1; i <= numberOfUsers; i++) { |
||||
String uniqueIdentification = generateUniqueIdentification(baseTime, i); |
||||
Date createTime = new Date(); |
||||
|
||||
// 插入用户信息
|
||||
pstmtUserInfo.setString(1, uniqueIdentification); |
||||
pstmtUserInfo.setTimestamp(2, new java.sql.Timestamp(createTime.getTime())); |
||||
pstmtUserInfo.setString(3, school_id); |
||||
pstmtUserInfo.addBatch(); |
||||
|
||||
if (i % batchSize == 0) { // 每 batchSize 条记录执行一次批量插入
|
||||
pstmtUserInfo.executeBatch(); |
||||
pstmtUserInfo.clearBatch(); // 清空批处理中的对象
|
||||
} |
||||
} |
||||
|
||||
// 执行剩余的批量插入
|
||||
pstmtUserInfo.executeBatch(); |
||||
pstmtUserInfo.clearBatch(); // 清空批处理中的对象
|
||||
|
||||
// 获取新生成的用户 ID 并生成账户信息
|
||||
String selectUserIdSql = "SELECT user_id FROM hr_user_info WHERE user_id > ? ORDER BY user_id"; |
||||
try (PreparedStatement selectUserIdStmt = conn.prepareStatement(selectUserIdSql)) { |
||||
selectUserIdStmt.setInt(1, lastUserId); |
||||
try (ResultSet rs = selectUserIdStmt.executeQuery()) { |
||||
while (rs.next()) { |
||||
int userId = rs.getInt("user_id"); |
||||
String password = "e10adc3949ba59abbe56e057f20f883e"; // 实际应用中应使用安全方式生成随机密码
|
||||
int isEnable = 1; |
||||
Date createTime = new Date(); |
||||
String account = formatAccount(userId); |
||||
|
||||
// 插入账户信息
|
||||
pstmtUserAccount.setInt(1, userId); |
||||
pstmtUserAccount.setString(2, password); |
||||
pstmtUserAccount.setString(3, role_id); |
||||
pstmtUserAccount.setInt(4, isEnable); |
||||
pstmtUserAccount.setTimestamp(5, new java.sql.Timestamp(createTime.getTime())); |
||||
pstmtUserAccount.setString(6, account); |
||||
pstmtUserAccount.setString(7, platform_id); |
||||
pstmtUserAccount.addBatch(); |
||||
|
||||
if ((userId - lastUserId) % batchSize == 0) { // 每 batchSize 条记录执行一次批量插入
|
||||
pstmtUserAccount.executeBatch(); |
||||
pstmtUserAccount.clearBatch(); // 清空批处理中的对象
|
||||
} |
||||
} |
||||
|
||||
// 执行剩余的批量插入
|
||||
pstmtUserAccount.executeBatch(); |
||||
pstmtUserAccount.clearBatch(); // 清空批处理中的对象
|
||||
} |
||||
} |
||||
|
||||
// 提交事务
|
||||
conn.commit(); |
||||
System.out.println("成功生成 " + numberOfUsers + " 个用户"); |
||||
|
||||
} catch (SQLException e) { |
||||
e.printStackTrace(); |
||||
if (conn != null) { |
||||
try { |
||||
conn.rollback(); // 回滚事务
|
||||
} catch (SQLException ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
} |
||||
} finally { |
||||
// 关闭资源
|
||||
try { |
||||
if (pstmtUserAccount != null) pstmtUserAccount.close(); |
||||
if (pstmtUserInfo != null) pstmtUserInfo.close(); |
||||
if (conn != null) conn.close(); |
||||
} catch (SQLException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static int getLastUserId(Connection conn) throws SQLException { |
||||
String sql = "SELECT COALESCE(MAX(user_id), 0) AS last_user_id FROM hr_user_info"; |
||||
try (PreparedStatement pstmt = conn.prepareStatement(sql); |
||||
ResultSet rs = pstmt.executeQuery()) { |
||||
if (rs.next()) { |
||||
return rs.getInt("last_user_id"); |
||||
} |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
private static String generateUniqueIdentification(long baseTime, int sequence) { |
||||
return String.valueOf(baseTime * 1000 + sequence); |
||||
} |
||||
|
||||
private static String formatAccount(int userId) { |
||||
return String.format("%010d", 9000000000L + userId); |
||||
} |
||||
} |
||||
*/ |
Loading…
Reference in new issue