mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-24 05:02:43 +08:00
优化学员批量导入
This commit is contained in:
parent
89ae0e0b78
commit
3e8eb9d5c4
@ -154,84 +154,78 @@ public class UserController {
|
|||||||
|
|
||||||
@PostMapping("/store-batch")
|
@PostMapping("/store-batch")
|
||||||
@Transactional
|
@Transactional
|
||||||
public JsonResponse batchStore(@RequestBody @Validated UserImportRequest request) {
|
public JsonResponse batchStore(@RequestBody @Validated UserImportRequest req) {
|
||||||
String[][] users = request.getUsers();
|
List<UserImportRequest.UserItem> users = req.getUsers();
|
||||||
if (users.length == 0) {
|
if (users.size() == 0) {
|
||||||
return JsonResponse.error("数据为空");
|
return JsonResponse.error("数据为空");
|
||||||
}
|
}
|
||||||
if (users.length > 1000) {
|
if (users.size() > 1000) {
|
||||||
return JsonResponse.error("一次最多导入1000条数据");
|
return JsonResponse.error("一次最多导入1000条数据");
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer startLine = request.getStartLine();
|
Integer startLine = req.getStartLine();
|
||||||
|
|
||||||
List<String[]> errorLines = new ArrayList<>();
|
List<String[]> errorLines = new ArrayList<>();
|
||||||
errorLines.add(new String[]{"错误行", "错误信息"});//表头
|
errorLines.add(new String[]{"错误行", "错误信息"});//错误表-表头
|
||||||
|
|
||||||
// 参数长度校验
|
|
||||||
for (int i = 0; i < users.length; i++) {
|
|
||||||
if (users[i].length != 6) {
|
|
||||||
errorLines.add(new String[]{"第" + (i + startLine) + "行", "参数错误"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (errorLines.size() > 1) {
|
|
||||||
return JsonResponse.error("导入数据有误", errorLines);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取存在的部门
|
// 读取存在的部门
|
||||||
List<Integer> depIds = departmentService.allIds();
|
List<Integer> depIds = departmentService.allIds();
|
||||||
|
|
||||||
// 邮箱输入重复检测 || 部门存在检测
|
// 邮箱输入重复检测 || 部门存在检测
|
||||||
HashMap<String, Integer> emailMap = new HashMap<>();
|
HashMap<String, Integer> emailRepeat = new HashMap<>();
|
||||||
HashMap<String, String[]> depMap = new HashMap<>();
|
HashMap<String, String[]> depMap = new HashMap<>();
|
||||||
List<String> emails = new ArrayList<>();
|
List<String> emails = new ArrayList<>();
|
||||||
List<User> insertUsers = new ArrayList<>();
|
List<User> insertUsers = new ArrayList<>();
|
||||||
for (int i = 0; i < users.length; i++) {
|
int i = -1;
|
||||||
//c0: 部门ids数组
|
for (UserImportRequest.UserItem userItem : users) {
|
||||||
//c1: 邮箱
|
i++;//索引值
|
||||||
//c2: 姓名
|
|
||||||
//c3: 密码
|
|
||||||
//c4: 身份证号
|
|
||||||
|
|
||||||
String tmpEmail = users[i][1];
|
if (userItem.getEmail() == null || userItem.getEmail().trim().length() == 0) {
|
||||||
if (emailMap.get(tmpEmail) != null) {//存在重复
|
errorLines.add(new String[]{"第" + (i + startLine) + "行", "未输入邮箱账号"});
|
||||||
errorLines.add(new String[]{"第" + (i + startLine) + "行", "邮箱重复"});
|
|
||||||
} else {
|
} else {
|
||||||
emailMap.put(tmpEmail, i + startLine);
|
// 邮箱重复判断
|
||||||
|
Integer repeatLine = emailRepeat.get(userItem.getEmail());
|
||||||
|
if (repeatLine != null) {
|
||||||
|
errorLines.add(new String[]{"第" + (i + startLine) + "行", "与第" + repeatLine + "行邮箱重复"});
|
||||||
|
} else {
|
||||||
|
emailRepeat.put(userItem.getEmail(), i + startLine);
|
||||||
|
}
|
||||||
|
emails.add(userItem.getEmail());
|
||||||
}
|
}
|
||||||
|
|
||||||
emails.add(tmpEmail);
|
// 部门数据检测
|
||||||
|
if (userItem.getDepIds() == null || userItem.getDepIds().trim().length() == 0) {
|
||||||
// 部门存在检测
|
errorLines.add(new String[]{"第" + (i + startLine) + "行", "未选择部门"});
|
||||||
if (users[i][0] != null && users[i][0].length() > 0) {
|
} else {
|
||||||
String[] tmpDepIds = users[i][0].split(",");
|
String[] tmpDepIds = userItem.getDepIds().trim().split(",");
|
||||||
for (int j = 0; j < tmpDepIds.length; j++) {
|
for (int j = 0; j < tmpDepIds.length; j++) {
|
||||||
if (!depIds.contains(Integer.valueOf(tmpDepIds[j]))) {
|
if (!depIds.contains(Integer.valueOf(tmpDepIds[j]))) {
|
||||||
errorLines.add(new String[]{"第" + (i + startLine) + "行", "部门id[" + tmpDepIds[j] + "]不存在"});
|
errorLines.add(new String[]{"第" + (i + startLine) + "行", "部门id[" + tmpDepIds[j] + "]不存在"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
depMap.put(users[i][1], tmpDepIds);
|
depMap.put(userItem.getEmail(), tmpDepIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 昵称为空检测
|
// 昵称为空检测
|
||||||
if (users[i][2] == null || users[i][2].length() == 0) {
|
String tmpName = userItem.getName();
|
||||||
|
if (tmpName == null || tmpName.trim().length() == 0) {
|
||||||
errorLines.add(new String[]{"第" + (i + startLine) + "行", "昵称为空"});
|
errorLines.add(new String[]{"第" + (i + startLine) + "行", "昵称为空"});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 密码为空检测
|
// 密码为空检测
|
||||||
if (users[i][3] == null || users[i][3].length() == 0) {
|
String tmpPassword = userItem.getPassword();
|
||||||
|
if (tmpPassword == null || tmpPassword.trim().length() == 0) {
|
||||||
errorLines.add(new String[]{"第" + (i + startLine) + "行", "密码为空"});
|
errorLines.add(new String[]{"第" + (i + startLine) + "行", "密码为空"});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 带插入数据
|
// 待插入数据
|
||||||
User tmpInsertUser = new User();
|
User tmpInsertUser = new User();
|
||||||
String tmpSalt = HelperUtil.randomString(6);
|
String tmpSalt = HelperUtil.randomString(6);
|
||||||
String tmpPassword = HelperUtil.MD5(users[i][3] + tmpSalt);
|
tmpInsertUser.setEmail(userItem.getEmail());
|
||||||
tmpInsertUser.setEmail(users[i][1]);
|
tmpInsertUser.setPassword(HelperUtil.MD5(tmpPassword + tmpSalt));
|
||||||
tmpInsertUser.setPassword(tmpPassword);
|
|
||||||
tmpInsertUser.setSalt(tmpSalt);
|
tmpInsertUser.setSalt(tmpSalt);
|
||||||
tmpInsertUser.setName(users[i][4]);
|
tmpInsertUser.setName(tmpName);
|
||||||
tmpInsertUser.setIdCard(users[i][4]);
|
tmpInsertUser.setIdCard(userItem.getIdCard());
|
||||||
tmpInsertUser.setCreateIp(SystemConstant.INTERNAL_IP);
|
tmpInsertUser.setCreateIp(SystemConstant.INTERNAL_IP);
|
||||||
tmpInsertUser.setCreateCity(SystemConstant.INTERNAL_IP_AREA);
|
tmpInsertUser.setCreateCity(SystemConstant.INTERNAL_IP_AREA);
|
||||||
tmpInsertUser.setCreatedAt(new Date());
|
tmpInsertUser.setCreatedAt(new Date());
|
||||||
@ -248,7 +242,7 @@ public class UserController {
|
|||||||
List<String> existsEmails = userService.existsEmailsByEmails(emails);
|
List<String> existsEmails = userService.existsEmailsByEmails(emails);
|
||||||
if (existsEmails.size() > 0) {
|
if (existsEmails.size() > 0) {
|
||||||
for (String tmpEmail : existsEmails) {
|
for (String tmpEmail : existsEmails) {
|
||||||
errorLines.add(new String[]{"第" + emailMap.get(tmpEmail) + "行", "邮箱已注册"});
|
errorLines.add(new String[]{"第" + emailRepeat.get(tmpEmail) + "行", "邮箱已注册"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (errorLines.size() > 1) {
|
if (errorLines.size() > 1) {
|
||||||
@ -258,20 +252,20 @@ public class UserController {
|
|||||||
userService.saveBatch(insertUsers);
|
userService.saveBatch(insertUsers);
|
||||||
|
|
||||||
// 部门关联
|
// 部门关联
|
||||||
List<UserDepartment> userDepartments = new ArrayList<>();
|
List<UserDepartment> insertUserDepartments = new ArrayList<>();
|
||||||
for (User tmpUser : insertUsers) {
|
for (User tmpUser : insertUsers) {
|
||||||
String[] tmpDepIds = depMap.get(tmpUser.getEmail());
|
String[] tmpDepIds = depMap.get(tmpUser.getEmail());
|
||||||
if (tmpDepIds != null) {
|
if (tmpDepIds == null) {
|
||||||
for (int i = 0; i < tmpDepIds.length; i++) {
|
continue;
|
||||||
UserDepartment tmpUserDep = new UserDepartment();
|
}
|
||||||
tmpUserDep.setUserId(tmpUser.getId());
|
for (String tmpDepId : tmpDepIds) {
|
||||||
tmpUserDep.setDepId(Integer.valueOf(tmpDepIds[i]));
|
insertUserDepartments.add(new UserDepartment() {{
|
||||||
|
setUserId(tmpUser.getId());
|
||||||
userDepartments.add(tmpUserDep);
|
setDepId(Integer.valueOf(tmpDepId));
|
||||||
|
}});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
userDepartmentService.saveBatch(insertUserDepartments);
|
||||||
userDepartmentService.saveBatch(userDepartments);
|
|
||||||
|
|
||||||
return JsonResponse.success();
|
return JsonResponse.success();
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
|||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author 杭州白书科技有限公司
|
* @Author 杭州白书科技有限公司
|
||||||
* @create 2023/2/23 16:12
|
* @create 2023/2/23 16:12
|
||||||
@ -11,8 +13,19 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class UserImportRequest {
|
public class UserImportRequest {
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class UserItem {
|
||||||
|
private String email;
|
||||||
|
private String name;
|
||||||
|
private String password;
|
||||||
|
@JsonProperty("id_card")
|
||||||
|
private String idCard;
|
||||||
|
@JsonProperty("dep_ids")
|
||||||
|
private String depIds;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull(message = "请导入数据")
|
@NotNull(message = "请导入数据")
|
||||||
private String[][] users;
|
private List<UserItem> users;
|
||||||
|
|
||||||
@NotNull(message = "起始行")
|
@NotNull(message = "起始行")
|
||||||
@JsonProperty("start_line")
|
@JsonProperty("start_line")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user