|
|
@@ -220,12 +220,14 @@ public class ClassServiceImpl implements ClassService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- Map<Integer, Map<String, Object>> portalResultByRow = registerUsersInPortal(rows, accessToken);
|
|
|
Set<String> fileOfficialNumbers = new HashSet<>();
|
|
|
Set<String> filePhones = new HashSet<>();
|
|
|
|
|
|
+ List<StudentImportRow> rowsToRegister = new ArrayList<>();
|
|
|
+ Map<Integer, BatchStudentImportDetailVO> pendingRegisterDetails = new HashMap<>();
|
|
|
List<User> usersToInsert = new ArrayList<>();
|
|
|
List<ClassStudent> studentsToInsert = new ArrayList<>();
|
|
|
+ Set<Long> enrollmentStudentIds = new HashSet<>();
|
|
|
|
|
|
for (StudentImportRow row : rows) {
|
|
|
BatchStudentImportDetailVO detail = new BatchStudentImportDetailVO();
|
|
|
@@ -233,31 +235,43 @@ public class ClassServiceImpl implements ClassService {
|
|
|
detail.setOfficialNumber(row.getOfficialNumber());
|
|
|
detail.setStuName(row.getStuName());
|
|
|
detail.setPhone(row.getPhone());
|
|
|
+ result.getDetails().add(detail);
|
|
|
|
|
|
String validationError = validateRow(row, fileOfficialNumbers, filePhones);
|
|
|
if (validationError != null) {
|
|
|
detail.setStatus("FAILED");
|
|
|
detail.setMessage(validationError);
|
|
|
- result.getDetails().add(detail);
|
|
|
result.setFailedCount(result.getFailedCount() + 1);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- Map<String, Object> portalRow = portalResultByRow.get(row.getRowIndex());
|
|
|
- if (portalRow == null || !"SUCCESS".equals(String.valueOf(portalRow.get("status")))) {
|
|
|
+ User existsByNumber = userMapper.selectByOfficialNumber(row.getOfficialNumber());
|
|
|
+
|
|
|
+ QueryWrapper<ClassStudent> classStudentWrapper = new QueryWrapper<>();
|
|
|
+ classStudentWrapper.eq("class_id", classId)
|
|
|
+ .eq("official_number", row.getOfficialNumber());
|
|
|
+ if (classStudentMapper.selectCount(classStudentWrapper) > 0) {
|
|
|
detail.setStatus("SKIPPED");
|
|
|
- detail.setMessage(portalRow == null ? "Portal未返回该行处理结果" : String.valueOf(portalRow.get("message")));
|
|
|
- result.getDetails().add(detail);
|
|
|
+ detail.setMessage("学生已在班级中,已跳过");
|
|
|
result.setSkippedCount(result.getSkippedCount() + 1);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- User existsByNumber = userMapper.selectByOfficialNumber(row.getOfficialNumber());
|
|
|
if (existsByNumber != null) {
|
|
|
- detail.setStatus("SKIPPED");
|
|
|
- detail.setMessage("EAI中学号已存在,已跳过");
|
|
|
- result.getDetails().add(detail);
|
|
|
- result.setSkippedCount(result.getSkippedCount() + 1);
|
|
|
+ ClassStudent student = new ClassStudent();
|
|
|
+ student.setClassId(classId);
|
|
|
+ student.setOfficialNumber(row.getOfficialNumber());
|
|
|
+ student.setStuName(row.getStuName());
|
|
|
+ student.setState(ClassStudentState.NOT_JOINED);
|
|
|
+ studentsToInsert.add(student);
|
|
|
+
|
|
|
+ enrollmentStudentIds.add(existsByNumber.getId());
|
|
|
+
|
|
|
+ detail.setStatus("SUCCESS");
|
|
|
+ detail.setMessage("学生已存在,已关联班级和课程");
|
|
|
+ result.setSuccessCount(result.getSuccessCount() + 1);
|
|
|
+ fileOfficialNumbers.add(row.getOfficialNumber());
|
|
|
+ filePhones.add(row.getPhone());
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -265,73 +279,79 @@ public class ClassServiceImpl implements ClassService {
|
|
|
if (existsByPhone != null) {
|
|
|
detail.setStatus("SKIPPED");
|
|
|
detail.setMessage("EAI中手机号已存在,已跳过");
|
|
|
- result.getDetails().add(detail);
|
|
|
- result.setSkippedCount(result.getSkippedCount() + 1);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- QueryWrapper<ClassStudent> classStudentWrapper = new QueryWrapper<>();
|
|
|
- classStudentWrapper.eq("class_id", classId)
|
|
|
- .eq("official_number", row.getOfficialNumber());
|
|
|
- if (classStudentMapper.selectCount(classStudentWrapper) > 0) {
|
|
|
- detail.setStatus("SKIPPED");
|
|
|
- detail.setMessage("学生已在班级中,已跳过");
|
|
|
- result.getDetails().add(detail);
|
|
|
result.setSkippedCount(result.getSkippedCount() + 1);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- User user = new User();
|
|
|
- user.setName(row.getStuName());
|
|
|
- user.setOfficialNumber(row.getOfficialNumber());
|
|
|
- user.setPhone(row.getPhone());
|
|
|
- user.setRole(Role.STUDENT);
|
|
|
- user.setCreateTime(new Date());
|
|
|
- Object portalUserId = portalRow.get("userId");
|
|
|
- if (portalUserId instanceof Number) {
|
|
|
- user.setPid(String.valueOf(((Number) portalUserId).longValue()));
|
|
|
- }
|
|
|
- user.setPassword(passwordEncoder.encode(generateDefaultPassword(row.getOfficialNumber())));
|
|
|
- usersToInsert.add(user);
|
|
|
-
|
|
|
- ClassStudent student = new ClassStudent();
|
|
|
- student.setClassId(classId);
|
|
|
- student.setOfficialNumber(row.getOfficialNumber());
|
|
|
- student.setStuName(row.getStuName());
|
|
|
- student.setState(ClassStudentState.NOT_JOINED);
|
|
|
- studentsToInsert.add(student);
|
|
|
-
|
|
|
- detail.setStatus("SUCCESS");
|
|
|
- detail.setMessage("导入成功");
|
|
|
- result.getDetails().add(detail);
|
|
|
- result.setSuccessCount(result.getSuccessCount() + 1);
|
|
|
+ rowsToRegister.add(row);
|
|
|
+ pendingRegisterDetails.put(row.getRowIndex(), detail);
|
|
|
fileOfficialNumbers.add(row.getOfficialNumber());
|
|
|
filePhones.add(row.getPhone());
|
|
|
}
|
|
|
|
|
|
+ if (!rowsToRegister.isEmpty()) {
|
|
|
+ Map<Integer, Map<String, Object>> portalResultByRow = registerUsersInPortal(rowsToRegister, accessToken);
|
|
|
+ for (StudentImportRow row : rowsToRegister) {
|
|
|
+ BatchStudentImportDetailVO detail = pendingRegisterDetails.get(row.getRowIndex());
|
|
|
+ Map<String, Object> portalRow = portalResultByRow.get(row.getRowIndex());
|
|
|
+ if (portalRow == null || !"SUCCESS".equals(String.valueOf(portalRow.get("status")))) {
|
|
|
+ detail.setStatus("SKIPPED");
|
|
|
+ detail.setMessage(portalRow == null ? "Portal未返回该行处理结果" : String.valueOf(portalRow.get("message")));
|
|
|
+ result.setSkippedCount(result.getSkippedCount() + 1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ User user = new User();
|
|
|
+ user.setName(row.getStuName());
|
|
|
+ user.setOfficialNumber(row.getOfficialNumber());
|
|
|
+ user.setPhone(row.getPhone());
|
|
|
+ user.setRole(Role.STUDENT);
|
|
|
+ user.setCreateTime(new Date());
|
|
|
+ Object portalUserId = portalRow.get("userId");
|
|
|
+ if (portalUserId instanceof Number) {
|
|
|
+ user.setPid(String.valueOf(((Number) portalUserId).longValue()));
|
|
|
+ }
|
|
|
+ user.setPassword(passwordEncoder.encode(generateDefaultPassword(row.getOfficialNumber())));
|
|
|
+ usersToInsert.add(user);
|
|
|
+
|
|
|
+ ClassStudent student = new ClassStudent();
|
|
|
+ student.setClassId(classId);
|
|
|
+ student.setOfficialNumber(row.getOfficialNumber());
|
|
|
+ student.setStuName(row.getStuName());
|
|
|
+ student.setState(ClassStudentState.NOT_JOINED);
|
|
|
+ studentsToInsert.add(student);
|
|
|
+
|
|
|
+ detail.setStatus("SUCCESS");
|
|
|
+ detail.setMessage("导入成功");
|
|
|
+ result.setSuccessCount(result.getSuccessCount() + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (!usersToInsert.isEmpty()) {
|
|
|
userMapper.batchInsert(usersToInsert);
|
|
|
- classStudentMapper.batchInsert(studentsToInsert);
|
|
|
- classMapper.increaseStuNumber(classId, studentsToInsert.size());
|
|
|
-
|
|
|
- List<String> importedOfficialNumbers = studentsToInsert.stream()
|
|
|
- .map(ClassStudent::getOfficialNumber)
|
|
|
+ List<String> importedOfficialNumbers = usersToInsert.stream()
|
|
|
+ .map(User::getOfficialNumber)
|
|
|
.collect(Collectors.toList());
|
|
|
List<User> importedUsers = userMapper.selectUserIdByOfficialNumbers(importedOfficialNumbers);
|
|
|
- List<Enrollment> enrollments = new ArrayList<>();
|
|
|
for (User importedUser : importedUsers) {
|
|
|
+ enrollmentStudentIds.add(importedUser.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!studentsToInsert.isEmpty()) {
|
|
|
+ classStudentMapper.batchInsert(studentsToInsert);
|
|
|
+ classMapper.increaseStuNumber(classId, studentsToInsert.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Long studentId : enrollmentStudentIds) {
|
|
|
+ QueryWrapper<Enrollment> enrollmentWrapper = new QueryWrapper<>();
|
|
|
+ enrollmentWrapper.eq("course_id", cls.getCourseId())
|
|
|
+ .eq("student_id", studentId);
|
|
|
+ if (!courseStudentMapper.exists(enrollmentWrapper)) {
|
|
|
Enrollment enrollment = new Enrollment();
|
|
|
enrollment.setCourseId(cls.getCourseId());
|
|
|
- enrollment.setStudentId(importedUser.getId());
|
|
|
- enrollments.add(enrollment);
|
|
|
- }
|
|
|
- for (Enrollment enrollment : enrollments) {
|
|
|
- QueryWrapper<Enrollment> enrollmentWrapper = new QueryWrapper<>();
|
|
|
- enrollmentWrapper.eq("course_id", enrollment.getCourseId())
|
|
|
- .eq("student_id", enrollment.getStudentId());
|
|
|
- if (!courseStudentMapper.exists(enrollmentWrapper)) {
|
|
|
- courseStudentMapper.insert(enrollment);
|
|
|
- }
|
|
|
+ enrollment.setStudentId(studentId);
|
|
|
+ courseStudentMapper.insert(enrollment);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -359,7 +379,7 @@ public class ClassServiceImpl implements ClassService {
|
|
|
|
|
|
Map<String, Object> response;
|
|
|
try {
|
|
|
- response = webClientUtil.post(endpoint, request, Map.class);
|
|
|
+ response = webClientUtil.postWithToken(endpoint, request, Map.class, accessToken);
|
|
|
} catch (Exception e) {
|
|
|
throw new MyException(502, "调用Portal批量注册失败:" + e.getMessage());
|
|
|
}
|