feat: 学生Excel导入取消院系/专业列、班级直接按本校行政班匹配;添加教师账号默认继承操作者学校/院系/专业

main
chenyuan 2 days ago
parent 179d18e754
commit b3a9a50fc8

@ -466,8 +466,6 @@ public class SchoolAdminController {
String name = StringUtils.trimToEmpty(row.getName()); String name = StringUtils.trimToEmpty(row.getName());
String username = StringUtils.trimToEmpty(row.getUsername()); String username = StringUtils.trimToEmpty(row.getUsername());
String facultyName = StringUtils.trimToEmpty(row.getSchoolFacultyName());
String majorName = StringUtils.trimToEmpty(row.getSchoolMajorName());
String className = StringUtils.trimToEmpty(row.getSchoolClassName()); String className = StringUtils.trimToEmpty(row.getSchoolClassName());
String sex = normalizeImportSex(row.getSex()); String sex = normalizeImportSex(row.getSex());
@ -486,32 +484,16 @@ public class SchoolAdminController {
} else { } else {
errors.add("第 " + rowNumber + " 行:性别请填写男或女"); errors.add("第 " + rowNumber + " 行:性别请填写男或女");
} }
if (StringUtils.isBlank(facultyName)) {
errors.add("第 " + rowNumber + " 行:所属院系不能为空");
}
if (StringUtils.isBlank(majorName)) {
errors.add("第 " + rowNumber + " 行:所属专业不能为空");
}
if (StringUtils.isBlank(className)) { if (StringUtils.isBlank(className)) {
errors.add("第 " + rowNumber + " 行:所属班级不能为空"); errors.add("第 " + rowNumber + " 行:所属班级不能为空");
} }
SchoolFaculty faculty = StringUtils.isBlank(facultyName) ? null : findFacultyByName(schoolId, facultyName); //院系/专业列已取消,班级直接按本校行政班名称匹配
SchoolMajor major = null;
SchoolClass schoolClass = null; SchoolClass schoolClass = null;
if (StringUtils.isNotBlank(facultyName) && faculty == null) { if (StringUtils.isNotBlank(className)) {
errors.add("第 " + rowNumber + " 行:未找到院系“" + facultyName + "”"); schoolClass = findAdminClassByName(schoolId, className);
}
if (faculty != null && StringUtils.isNotBlank(majorName)) {
major = findMajorByName(faculty.getSchoolFacultyId(), majorName);
if (major == null) {
errors.add("第 " + rowNumber + " 行:院系“" + facultyName + "”下未找到专业“" + majorName + "”");
}
}
if (major != null && StringUtils.isNotBlank(className)) {
schoolClass = findClassByName(schoolId, major.getSchoolMajorId(), className);
if (schoolClass == null) { if (schoolClass == null) {
errors.add("第 " + rowNumber + " 行:专业“" + majorName + "”下未找到班级“" + className + "”"); errors.add("第 " + rowNumber + " 行:未找到行政班:" + className);
} }
} }
@ -524,8 +506,6 @@ public class SchoolAdminController {
student.setSex(sex); student.setSex(sex);
student.setPhone(StringUtils.trimToEmpty(row.getPhone())); student.setPhone(StringUtils.trimToEmpty(row.getPhone()));
student.setEmail(StringUtils.trimToEmpty(row.getEmail())); student.setEmail(StringUtils.trimToEmpty(row.getEmail()));
student.setSchoolFacultyId(faculty.getSchoolFacultyId());
student.setSchoolMajorId(major.getSchoolMajorId());
student.setSchoolClassId(schoolClass.getSchoolClassId()); student.setSchoolClassId(schoolClass.getSchoolClassId());
student.setCreateTime(new Date()); student.setCreateTime(new Date());
student.setCodeFrom("Excel导入"); student.setCodeFrom("Excel导入");
@ -543,8 +523,6 @@ public class SchoolAdminController {
return StringUtils.isBlank(row.getName()) return StringUtils.isBlank(row.getName())
&& StringUtils.isBlank(row.getUsername()) && StringUtils.isBlank(row.getUsername())
&& StringUtils.isBlank(row.getSex()) && StringUtils.isBlank(row.getSex())
&& StringUtils.isBlank(row.getSchoolFacultyName())
&& StringUtils.isBlank(row.getSchoolMajorName())
&& StringUtils.isBlank(row.getSchoolClassName()) && StringUtils.isBlank(row.getSchoolClassName())
&& StringUtils.isBlank(row.getPhone()) && StringUtils.isBlank(row.getPhone())
&& StringUtils.isBlank(row.getEmail()); && StringUtils.isBlank(row.getEmail());
@ -574,30 +552,13 @@ public class SchoolAdminController {
return false; return false;
} }
private SchoolFaculty findFacultyByName(String schoolId, String facultyName) { //学生账号导入不再区分院系/专业,班级直接按本校行政班名称匹配
SchoolFacultyExample example = new SchoolFacultyExample(); private SchoolClass findAdminClassByName(String schoolId, String className) {
example.createCriteria()
.andSchoolIdEqualTo(schoolId)
.andSchoolFacultyNameEqualTo(facultyName);
List<SchoolFaculty> faculties = schoolFacultyMapper.selectByExample(example);
return faculties.isEmpty() ? null : faculties.get(0);
}
private SchoolMajor findMajorByName(String facultyId, String majorName) {
SchoolMajorExample example = new SchoolMajorExample();
example.createCriteria()
.andSchoolFacultyIdEqualTo(facultyId)
.andSchoolMajorNameEqualTo(majorName);
List<SchoolMajor> majors = schoolMajorMapper.selectByExample(example);
return majors.isEmpty() ? null : majors.get(0);
}
private SchoolClass findClassByName(String schoolId, String majorId, String className) {
SchoolClassExample example = new SchoolClassExample(); SchoolClassExample example = new SchoolClassExample();
example.createCriteria() example.createCriteria()
.andSchoolIdEqualTo(schoolId) .andSchoolIdEqualTo(schoolId)
.andSchoolMajorIdEqualTo(majorId) .andClassNameEqualTo(className)
.andClassNameEqualTo(className); .andClassTypeEqualTo("ADMIN");
List<SchoolClass> classes = schoolClassMapper.selectByExample(example); List<SchoolClass> classes = schoolClassMapper.selectByExample(example);
return classes.isEmpty() ? null : classes.get(0); return classes.isEmpty() ? null : classes.get(0);
} }

@ -557,32 +557,36 @@ public class UserController {
@PostMapping("/addTeacherAccount") @PostMapping("/addTeacherAccount")
@ApiOperation("教师管理-添加教师账号") @ApiOperation("教师管理-添加教师账号")
@AnonymousAccess @AnonymousAccess
public ResultEntity addTeacherAccount(@ApiParam("学校ID") @RequestParam String schoolId, public ResultEntity addTeacherAccount(@ApiParam("学校ID") @RequestParam(required = false) String schoolId,
@ApiParam("学校名称") @RequestParam String schoolName, @ApiParam("学校名称") @RequestParam(required = false) String schoolName,
@ApiParam("院系ID") @RequestParam String schoolFacultyId, @ApiParam("院系ID") @RequestParam(required = false) String schoolFacultyId,
@ApiParam("专业ID") @RequestParam String schoolMajorId, @ApiParam("专业ID") @RequestParam(required = false) String schoolMajorId,
@ApiParam("专业名称") @RequestParam String schoolMajorName, @ApiParam("专业名称") @RequestParam(required = false) String schoolMajorName,
@ApiParam("教师名称") @RequestParam String name, @ApiParam("教师名称") @RequestParam String name,
@ApiParam("教师工号") @RequestParam String userName, @ApiParam("教师工号") @RequestParam String userName,
@ApiParam("电话号码") @RequestParam(required = false) String phone, @ApiParam("电话号码") @RequestParam(required = false) String phone,
@ApiParam("电子邮箱") @RequestParam(required = false) String email, @ApiParam("电子邮箱") @RequestParam(required = false) String email,
@RequestParam(required = false) String operatorId) { @RequestParam(required = false) String operatorId) {
//先根据schoolId进行查询 如果不存在则返回 //学校/院系/专业默认从当前操作用户继承;平台管理员可显式指定学校
School school = schoolMapper.selectByPrimaryKey(schoolId); Userinfo operator = StringUtils.isBlank(operatorId) ? null : userinfoMapper.selectByPrimaryKey(operatorId);
String effectiveSchoolId = isSchoolTeacher(operator) ? operator.getSchoolId() : StringUtils.trimToNull(schoolId);
if (StringUtils.isBlank(effectiveSchoolId)) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "无法确定教师所属学校");
}
School school = schoolMapper.selectByPrimaryKey(effectiveSchoolId);
if (school == null) { if (school == null) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该学校不存在"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该学校不存在");
} }
if (!canManageSchoolStructure(operatorId, schoolId)) { if (!canManageSchoolStructure(operatorId, effectiveSchoolId)) {
return noManagePermission(); return noManagePermission();
} }
//再根据schoolFacultyId查询 如果不存在则返回 String effectiveFacultyId = isSchoolTeacher(operator) ? StringUtils.trimToNull(operator.getSchoolFacultyId()) : StringUtils.trimToNull(schoolFacultyId);
SchoolFaculty schoolFaculty = schoolFacultyMapper.selectByPrimaryKey(schoolFacultyId); String effectiveMajorId = isSchoolTeacher(operator) ? StringUtils.trimToNull(operator.getSchoolMajorId()) : StringUtils.trimToNull(schoolMajorId);
if (schoolFaculty == null) { //院系/专业选填:有值才校验
if (StringUtils.isNotBlank(effectiveFacultyId) && schoolFacultyMapper.selectByPrimaryKey(effectiveFacultyId) == null) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该院系不存在"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该院系不存在");
} }
//再根据schoolMajorId查询 如果不存在则返回 if (StringUtils.isNotBlank(effectiveMajorId) && schoolMajorMapper.selectByPrimaryKey(effectiveMajorId) == null) {
SchoolMajor schoolMajor = schoolMajorMapper.selectByPrimaryKey(schoolMajorId);
if (schoolMajor == null) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该专业不存在"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该专业不存在");
} }
//检查userName是否已经存在 //检查userName是否已经存在
@ -599,9 +603,9 @@ public class UserController {
userInfo.setEmail(email); userInfo.setEmail(email);
userInfo.setCreateTime(new Date()); userInfo.setCreateTime(new Date());
userInfo.setRole(3); userInfo.setRole(3);
userInfo.setSchoolId(schoolId); userInfo.setSchoolId(effectiveSchoolId);
userInfo.setSchoolFacultyId(schoolFacultyId); userInfo.setSchoolFacultyId(effectiveFacultyId);
userInfo.setSchoolMajorId(schoolMajorId); userInfo.setSchoolMajorId(effectiveMajorId);
userInfo.setSchoolClassId(null); userInfo.setSchoolClassId(null);
userInfo.setTeacherAdmin(false); userInfo.setTeacherAdmin(false);
userInfo.setCodeFrom("添加"); userInfo.setCodeFrom("添加");
@ -765,15 +769,18 @@ public class UserController {
if (!errorMessages.isEmpty()) { if (!errorMessages.isEmpty()) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "数据校验失败!", errorMessages); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "数据校验失败!", errorMessages);
} }
for (TeacherImportDTO teacher : teacherList) { // 教师的学校/院系/专业直接取自当前操作用户,不再从Excel的院校名称列解析
String schoolId = schoolService.selectSchoolIdBySchoolName(teacher.getSchoolName()); Userinfo operator = userinfoMapper.selectByPrimaryKey(operatorId);
if (!canManageSchoolStructure(operatorId, schoolId)) { String effectiveSchoolId = operator == null ? null : StringUtils.trimToEmpty(operator.getSchoolId());
return noManagePermission(); if (StringUtils.isBlank(effectiveSchoolId)) {
} return new ResultEntity<>(HttpStatus.BAD_REQUEST, "无法确定当前用户所属学校,导入失败");
}
if (!canManageSchoolStructure(operatorId, effectiveSchoolId)) {
return noManagePermission();
} }
// 异步处理导入***** // 异步处理导入*****
importService.processTeacherImport(teacherList); importService.processTeacherImport(teacherList, operator);
return new ResultEntity<>(HttpStatus.OK, "导入成功!"); return new ResultEntity<>(HttpStatus.OK, "导入成功!");
} catch (Exception e) { } catch (Exception e) {
@ -815,9 +822,9 @@ public class UserController {
@AnonymousAccess @AnonymousAccess
public ResultEntity addStudentAccount(@ApiParam("学校ID") @RequestParam String schoolId, public ResultEntity addStudentAccount(@ApiParam("学校ID") @RequestParam String schoolId,
@ApiParam("学校名称") @RequestParam String schoolName, @ApiParam("学校名称") @RequestParam String schoolName,
@ApiParam("院系ID") @RequestParam String schoolFacultyId, @ApiParam("院系ID") @RequestParam(required = false) String schoolFacultyId,
@ApiParam("专业ID") @RequestParam String schoolMajorId, @ApiParam("专业ID") @RequestParam(required = false) String schoolMajorId,
@ApiParam("专业名称") @RequestParam String schoolMajorName, @ApiParam("专业名称") @RequestParam(required = false) String schoolMajorName,
@ApiParam("班级名称") @RequestParam String schoolClassName, @ApiParam("班级名称") @RequestParam String schoolClassName,
@ApiParam("班级Id") @RequestParam String schoolClassId, @ApiParam("班级Id") @RequestParam String schoolClassId,
@ApiParam("学生姓名") @RequestParam String name, @ApiParam("学生姓名") @RequestParam String name,
@ -832,14 +839,11 @@ public class UserController {
if (school == null) { if (school == null) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该学校不存在"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该学校不存在");
} }
//再根据schoolFacultyId查询 如果不存在则返回 //院系/专业改为选填:传了才校验,不传直接入库为空
SchoolFaculty schoolFaculty = schoolFacultyMapper.selectByPrimaryKey(schoolFacultyId); if (StringUtils.isNotBlank(schoolFacultyId) && schoolFacultyMapper.selectByPrimaryKey(schoolFacultyId) == null) {
if (schoolFaculty == null) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该院系不存在"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该院系不存在");
} }
//再根据schoolMajorId查询 如果不存在则返回 if (StringUtils.isNotBlank(schoolMajorId) && schoolMajorMapper.selectByPrimaryKey(schoolMajorId) == null) {
SchoolMajor schoolMajor = schoolMajorMapper.selectByPrimaryKey(schoolMajorId);
if (schoolMajor == null) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该专业不存在"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "该专业不存在");
} }
SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(schoolClassId); SchoolClass schoolClass = schoolClassMapper.selectByPrimaryKey(schoolClassId);
@ -865,8 +869,8 @@ public class UserController {
userInfo.setRole(4); userInfo.setRole(4);
userInfo.setTeacherAdmin(false); userInfo.setTeacherAdmin(false);
userInfo.setSchoolId(effectiveSchoolId); userInfo.setSchoolId(effectiveSchoolId);
userInfo.setSchoolFacultyId(schoolFacultyId); userInfo.setSchoolFacultyId(StringUtils.trimToNull(schoolFacultyId));
userInfo.setSchoolMajorId(schoolMajorId); userInfo.setSchoolMajorId(StringUtils.trimToNull(schoolMajorId));
String adminClassId = "TEACHING".equals(schoolClass.getClassType()) String adminClassId = "TEACHING".equals(schoolClass.getClassType())
? resolveAdminClassIdForTeachingClass(schoolClass) ? resolveAdminClassIdForTeachingClass(schoolClass)
: schoolClassId; : schoolClassId;
@ -2129,8 +2133,6 @@ public class UserController {
String name = StringUtils.trimToEmpty(row.getName()); String name = StringUtils.trimToEmpty(row.getName());
String username = StringUtils.trimToEmpty(row.getUsername()); String username = StringUtils.trimToEmpty(row.getUsername());
String facultyName = StringUtils.trimToEmpty(row.getSchoolFacultyName());
String majorName = StringUtils.trimToEmpty(row.getSchoolMajorName());
String className = StringUtils.trimToEmpty(row.getSchoolClassName()); String className = StringUtils.trimToEmpty(row.getSchoolClassName());
String sex = normalizeTeacherImportSex(row.getSex()); String sex = normalizeTeacherImportSex(row.getSex());
@ -2147,32 +2149,16 @@ public class UserController {
if (StringUtils.isNotBlank(row.getSex()) && sex == null) { if (StringUtils.isNotBlank(row.getSex()) && sex == null) {
errors.add("第" + rowNumber + "行:性别请填写男或女"); errors.add("第" + rowNumber + "行:性别请填写男或女");
} }
if (StringUtils.isBlank(facultyName)) {
errors.add("第" + rowNumber + "行:所属院系不能为空");
}
if (StringUtils.isBlank(majorName)) {
errors.add("第" + rowNumber + "行:所属专业不能为空");
}
if (StringUtils.isBlank(className)) { if (StringUtils.isBlank(className)) {
errors.add("第" + rowNumber + "行:所属班级不能为空"); errors.add("第" + rowNumber + "行:所属班级不能为空");
} }
SchoolFaculty faculty = StringUtils.isBlank(facultyName) ? null : findTeacherImportFaculty(operator.getSchoolId(), facultyName); //院系/专业列已取消,班级直接按本校行政班名称匹配
SchoolMajor major = null;
SchoolClass schoolClass = null; SchoolClass schoolClass = null;
if (StringUtils.isNotBlank(facultyName) && faculty == null) { if (StringUtils.isNotBlank(className)) {
errors.add("第" + rowNumber + "行:未找到院系“" + facultyName + "”"); schoolClass = findTeacherImportAdminClass(operator.getSchoolId(), className);
}
if (faculty != null && StringUtils.isNotBlank(majorName)) {
major = findTeacherImportMajor(faculty.getSchoolFacultyId(), majorName);
if (major == null) {
errors.add("第" + rowNumber + "行:院系“" + facultyName + "”下未找到专业“" + majorName + "”");
}
}
if (major != null && StringUtils.isNotBlank(className)) {
schoolClass = findTeacherImportClass(operator.getSchoolId(), major.getSchoolMajorId(), className);
if (schoolClass == null) { if (schoolClass == null) {
errors.add("第" + rowNumber + "行:专业“" + majorName + "”下未找到班级“" + className + "”"); errors.add("第" + rowNumber + "行:未找到行政班“" + className + "”");
} else if (!isAdminClass(schoolClass)) { } else if (!isAdminClass(schoolClass)) {
errors.add("第" + rowNumber + "行:学生账号只能导入行政班"); errors.add("第" + rowNumber + "行:学生账号只能导入行政班");
} else if (!canManageAdminClass(operator, schoolClass)) { } else if (!canManageAdminClass(operator, schoolClass)) {
@ -2192,8 +2178,6 @@ public class UserController {
student.setRole(4); student.setRole(4);
student.setTeacherAdmin(false); student.setTeacherAdmin(false);
student.setSchoolId(operator.getSchoolId()); student.setSchoolId(operator.getSchoolId());
student.setSchoolFacultyId(faculty.getSchoolFacultyId());
student.setSchoolMajorId(major.getSchoolMajorId());
student.setSchoolClassId(schoolClass.getSchoolClassId()); student.setSchoolClassId(schoolClass.getSchoolClassId());
student.setCreateTime(new Date()); student.setCreateTime(new Date());
student.setCodeFrom("Excel导入"); student.setCodeFrom("Excel导入");
@ -2210,8 +2194,6 @@ public class UserController {
return StringUtils.isBlank(row.getName()) return StringUtils.isBlank(row.getName())
&& StringUtils.isBlank(row.getUsername()) && StringUtils.isBlank(row.getUsername())
&& StringUtils.isBlank(row.getSex()) && StringUtils.isBlank(row.getSex())
&& StringUtils.isBlank(row.getSchoolFacultyName())
&& StringUtils.isBlank(row.getSchoolMajorName())
&& StringUtils.isBlank(row.getSchoolClassName()) && StringUtils.isBlank(row.getSchoolClassName())
&& StringUtils.isBlank(row.getPhone()) && StringUtils.isBlank(row.getPhone())
&& StringUtils.isBlank(row.getEmail()); && StringUtils.isBlank(row.getEmail());
@ -2241,30 +2223,23 @@ public class UserController {
return false; return false;
} }
private SchoolFaculty findTeacherImportFaculty(String schoolId, String facultyName) { private SchoolClass findTeacherImportClass(String schoolId, String majorId, String className) {
SchoolFacultyExample example = new SchoolFacultyExample(); SchoolClassExample example = new SchoolClassExample();
example.createCriteria() example.createCriteria()
.andSchoolIdEqualTo(schoolId) .andSchoolIdEqualTo(schoolId)
.andSchoolFacultyNameEqualTo(facultyName); .andSchoolMajorIdEqualTo(majorId)
List<SchoolFaculty> faculties = schoolFacultyMapper.selectByExample(example); .andClassNameEqualTo(className);
return faculties.isEmpty() ? null : faculties.get(0); List<SchoolClass> classes = schoolClassMapper.selectByExample(example);
} return classes.isEmpty() ? null : classes.get(0);
private SchoolMajor findTeacherImportMajor(String facultyId, String majorName) {
SchoolMajorExample example = new SchoolMajorExample();
example.createCriteria()
.andSchoolFacultyIdEqualTo(facultyId)
.andSchoolMajorNameEqualTo(majorName);
List<SchoolMajor> majors = schoolMajorMapper.selectByExample(example);
return majors.isEmpty() ? null : majors.get(0);
} }
private SchoolClass findTeacherImportClass(String schoolId, String majorId, String className) { //学生账号导入不再区分院系/专业,班级直接按本校行政班名称匹配
private SchoolClass findTeacherImportAdminClass(String schoolId, String className) {
SchoolClassExample example = new SchoolClassExample(); SchoolClassExample example = new SchoolClassExample();
example.createCriteria() example.createCriteria()
.andSchoolIdEqualTo(schoolId) .andSchoolIdEqualTo(schoolId)
.andSchoolMajorIdEqualTo(majorId) .andClassNameEqualTo(className)
.andClassNameEqualTo(className); .andClassTypeEqualTo("ADMIN");
List<SchoolClass> classes = schoolClassMapper.selectByExample(example); List<SchoolClass> classes = schoolClassMapper.selectByExample(example);
return classes.isEmpty() ? null : classes.get(0); return classes.isEmpty() ? null : classes.get(0);
} }

@ -18,14 +18,6 @@ public class SchoolAdminStudentImportDTO {
@ApiModelProperty("性别") @ApiModelProperty("性别")
private String sex; private String sex;
@ExcelProperty("所属院系")
@ApiModelProperty("所属院系")
private String schoolFacultyName;
@ExcelProperty("所属专业")
@ApiModelProperty("所属专业")
private String schoolMajorName;
@ExcelProperty("所属班级") @ExcelProperty("所属班级")
@ApiModelProperty("所属班级") @ApiModelProperty("所属班级")
private String schoolClassName; private String schoolClassName;

@ -1,28 +1,24 @@
package com.sztzjy.linkCommerce.entity.importDto; package com.sztzjy.linkCommerce.entity.importDto;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@Data @Data
public class TeacherImportDTO { public class TeacherImportDTO {
@ApiModelProperty("院校名称") @ExcelProperty("教师姓名")
private String schoolName;
@ApiModelProperty("院系名称")
private String schoolFacultyName;
@ApiModelProperty("专业名称")
private String schoolMajorName;
@ApiModelProperty("教师姓名") @ApiModelProperty("教师姓名")
private String teacherName; private String teacherName;
@ExcelProperty("工号")
@ApiModelProperty("工号") @ApiModelProperty("工号")
private String userName; private String userName;
@ExcelProperty("电话")
@ApiModelProperty("电话") @ApiModelProperty("电话")
private String phone; private String phone;
@ExcelProperty("邮箱")
@ApiModelProperty("邮箱") @ApiModelProperty("邮箱")
private String email; private String email;
} }

@ -1,12 +1,13 @@
package com.sztzjy.linkCommerce.service; package com.sztzjy.linkCommerce.service;
import com.sztzjy.linkCommerce.entity.Userinfo;
import com.sztzjy.linkCommerce.entity.importDto.StudentImportDTO; import com.sztzjy.linkCommerce.entity.importDto.StudentImportDTO;
import com.sztzjy.linkCommerce.entity.importDto.TeacherImportDTO; import com.sztzjy.linkCommerce.entity.importDto.TeacherImportDTO;
import java.util.List; import java.util.List;
public interface ImportService { public interface ImportService {
void processTeacherImport(List<TeacherImportDTO> teacherList); void processTeacherImport(List<TeacherImportDTO> teacherList, Userinfo operator);
void processStudentImport(List<StudentImportDTO> studentList); void processStudentImport(List<StudentImportDTO> studentList);
} }

@ -21,7 +21,8 @@ public class ImportServiceImpl implements ImportService {
@Autowired @Autowired
UserinfoMapper userInfoMapper; UserinfoMapper userInfoMapper;
@Override @Override
public void processTeacherImport(List<TeacherImportDTO> teacherList) { public void processTeacherImport(List<TeacherImportDTO> teacherList, Userinfo operator) {
// 教师的学校/院系/专业直接继承当前操作用户
for (int i = 0; i < teacherList.size(); i++) { for (int i = 0; i < teacherList.size(); i++) {
TeacherImportDTO teacherImportDTO = teacherList.get(i); TeacherImportDTO teacherImportDTO = teacherList.get(i);
Userinfo userInfo = new Userinfo(); Userinfo userInfo = new Userinfo();
@ -34,9 +35,9 @@ public class ImportServiceImpl implements ImportService {
userInfo.setCreateTime(new Date()); userInfo.setCreateTime(new Date());
userInfo.setRole(3); userInfo.setRole(3);
userInfo.setTeacherAdmin(false); userInfo.setTeacherAdmin(false);
userInfo.setSchoolId(schoolService.selectSchoolIdBySchoolName(teacherImportDTO.getSchoolName())); userInfo.setSchoolId(operator == null ? null : operator.getSchoolId());
userInfo.setSchoolFacultyId(schoolService.selectFacultyIdBySchoolIdAndFacultyName(userInfo.getSchoolId(),teacherImportDTO.getSchoolFacultyName())); userInfo.setSchoolFacultyId(operator == null ? null : operator.getSchoolFacultyId());
userInfo.setSchoolMajorId(schoolService.selectMajorIdByFacultyIdAndMajorName(userInfo.getSchoolFacultyId(),teacherImportDTO.getSchoolMajorName())); userInfo.setSchoolMajorId(operator == null ? null : operator.getSchoolMajorId());
userInfo.setSchoolClassId(null); userInfo.setSchoolClassId(null);
userInfo.setCodeFrom("导入"); userInfo.setCodeFrom("导入");
userInfoMapper.insert(userInfo); userInfoMapper.insert(userInfo);

@ -38,25 +38,7 @@ public class ValidateServiceImpl implements ValidateService {
TeacherImportDTO teacher = teacherList.get(i); TeacherImportDTO teacher = teacherList.get(i);
int rowNum = i + 2; // Excel行号(标题行+1) int rowNum = i + 2; // Excel行号(标题行+1)
// 1. 校验院校名称 // 1. 校验教师姓名(教师归属的学校/院系/专业直接取自当前操作用户,不再从Excel读取)
if (StringUtils.isBlank(teacher.getSchoolName())) {
errors.add("第" + rowNum + "行: 院校名称不能为空");
}
// 2. 校验院系名称
if (StringUtils.isBlank(teacher.getSchoolFacultyName())) {
errors.add("第" + rowNum + "行: 院系名称不能为空");
}
// 3. 校验专业名称
if (StringUtils.isBlank(teacher.getSchoolMajorName())) {
errors.add("第" + rowNum + "行: 专业名称不能为空");
} else if (!schoolService.existsBySchoolMajorName(teacher.getSchoolName(),teacher.getSchoolMajorName(), teacher.getSchoolFacultyName())) {
errors.add("第" + rowNum + "行: 专业'" + teacher.getSchoolMajorName() +
"'在院系'" + teacher.getSchoolFacultyName() + "'下不存在");
}
// 4. 校验教师姓名
if (StringUtils.isBlank(teacher.getTeacherName())) { if (StringUtils.isBlank(teacher.getTeacherName())) {
errors.add("第" + rowNum + "行: 教师姓名不能为空"); errors.add("第" + rowNum + "行: 教师姓名不能为空");
} }

@ -1304,14 +1304,12 @@ class UserControllerTeacherAdminTest {
adminClass.setClassType("ADMIN"); adminClass.setClassType("ADMIN");
adminClass.setCreatedBy("teacher-1"); adminClass.setCreatedBy("teacher-1");
when(controller.userinfoMapper.selectByPrimaryKey("teacher-1")).thenReturn(teacher("teacher-1", true)); when(controller.userinfoMapper.selectByPrimaryKey("teacher-1")).thenReturn(teacher("teacher-1", true));
when(controller.schoolFacultyMapper.selectByExample(any())).thenReturn(Collections.singletonList(faculty("faculty-1", "院系")));
when(controller.schoolMajorMapper.selectByExample(any())).thenReturn(Collections.singletonList(major("major-1", "faculty-1", "专业")));
when(controller.schoolClassMapper.selectByExample(any())).thenReturn(Collections.singletonList(adminClass)); when(controller.schoolClassMapper.selectByExample(any())).thenReturn(Collections.singletonList(adminClass));
when(controller.userInfoService.existsByUserName("20260001")).thenReturn(false); when(controller.userInfoService.existsByUserName("20260001")).thenReturn(false);
when(controller.userinfoMapper.insertSelective(any(Userinfo.class))).thenReturn(1); when(controller.userinfoMapper.insertSelective(any(Userinfo.class))).thenReturn(1);
ResultEntity result = controller.importTeacherStudentRows( ResultEntity result = controller.importTeacherStudentRows(
Collections.singletonList(studentAccountRow("张三", "20260001", "男", "院系", "专业", "行政一班")), Collections.singletonList(studentAccountRow("张三", "20260001", "男", "行政一班")),
"teacher-1"); "teacher-1");
ArgumentCaptor<Userinfo> captor = ArgumentCaptor.forClass(Userinfo.class); ArgumentCaptor<Userinfo> captor = ArgumentCaptor.forClass(Userinfo.class);
@ -1335,14 +1333,12 @@ class UserControllerTeacherAdminTest {
adminClass.setClassType("ADMIN"); adminClass.setClassType("ADMIN");
adminClass.setCreatedBy("teacher-2"); adminClass.setCreatedBy("teacher-2");
when(controller.userinfoMapper.selectByPrimaryKey("teacher-1")).thenReturn(teacher("teacher-1", true)); when(controller.userinfoMapper.selectByPrimaryKey("teacher-1")).thenReturn(teacher("teacher-1", true));
when(controller.schoolFacultyMapper.selectByExample(any())).thenReturn(Collections.singletonList(faculty("faculty-1", "院系")));
when(controller.schoolMajorMapper.selectByExample(any())).thenReturn(Collections.singletonList(major("major-1", "faculty-1", "专业")));
when(controller.schoolClassMapper.selectByExample(any())).thenReturn(Collections.singletonList(adminClass)); when(controller.schoolClassMapper.selectByExample(any())).thenReturn(Collections.singletonList(adminClass));
when(controller.userInfoService.existsByUserName("20260001")).thenReturn(false); when(controller.userInfoService.existsByUserName("20260001")).thenReturn(false);
when(controller.userinfoMapper.insertSelective(any(Userinfo.class))).thenReturn(1); when(controller.userinfoMapper.insertSelective(any(Userinfo.class))).thenReturn(1);
ResultEntity result = controller.importTeacherStudentRows( ResultEntity result = controller.importTeacherStudentRows(
Collections.singletonList(studentAccountRow("张三", "20260001", "男", "院系", "专业", "行政一班")), Collections.singletonList(studentAccountRow("张三", "20260001", "男", "行政一班")),
"teacher-1"); "teacher-1");
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
@ -1464,15 +1460,11 @@ class UserControllerTeacherAdminTest {
private SchoolAdminStudentImportDTO studentAccountRow(String name, private SchoolAdminStudentImportDTO studentAccountRow(String name,
String username, String username,
String sex, String sex,
String facultyName,
String majorName,
String className) { String className) {
SchoolAdminStudentImportDTO row = new SchoolAdminStudentImportDTO(); SchoolAdminStudentImportDTO row = new SchoolAdminStudentImportDTO();
row.setName(name); row.setName(name);
row.setUsername(username); row.setUsername(username);
row.setSex(sex); row.setSex(sex);
row.setSchoolFacultyName(facultyName);
row.setSchoolMajorName(majorName);
row.setSchoolClassName(className); row.setSchoolClassName(className);
row.setPhone("13800000000"); row.setPhone("13800000000");
row.setEmail("student@example.com"); row.setEmail("student@example.com");

@ -3,37 +3,65 @@ package com.sztzjy.linkCommerce.service.impl;
import com.sztzjy.linkCommerce.entity.Userinfo; import com.sztzjy.linkCommerce.entity.Userinfo;
import com.sztzjy.linkCommerce.entity.importDto.TeacherImportDTO; import com.sztzjy.linkCommerce.entity.importDto.TeacherImportDTO;
import com.sztzjy.linkCommerce.mapper.UserinfoMapper; import com.sztzjy.linkCommerce.mapper.UserinfoMapper;
import com.sztzjy.linkCommerce.service.SchoolService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import java.util.Collections; import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class ImportServiceImplTest { class ImportServiceImplTest {
@Test @Test
void teacherImportCreatesOrdinaryTeacher() { void teacherImportCreatesOrdinaryTeacher() {
ImportServiceImpl service = new ImportServiceImpl(); ImportServiceImpl service = new ImportServiceImpl();
service.schoolService = mock(SchoolService.class);
service.userInfoMapper = mock(UserinfoMapper.class); service.userInfoMapper = mock(UserinfoMapper.class);
when(service.schoolService.selectSchoolIdBySchoolName(any())).thenReturn("school-1");
when(service.schoolService.selectFacultyIdBySchoolIdAndFacultyName(any(), any())).thenReturn("faculty-1"); // 操作用户的学校/院系/专业应直接继承给导入的教师
when(service.schoolService.selectMajorIdByFacultyIdAndMajorName(any(), any())).thenReturn("major-1"); Userinfo operator = new Userinfo();
operator.setUserId("operator-1");
operator.setSchoolId("school-1");
operator.setSchoolFacultyId("faculty-1");
operator.setSchoolMajorId("major-1");
TeacherImportDTO row = new TeacherImportDTO(); TeacherImportDTO row = new TeacherImportDTO();
row.setTeacherName("张老师"); row.setTeacherName("张老师");
row.setUserName("t001"); row.setUserName("t001");
service.processTeacherImport(Collections.singletonList(row)); service.processTeacherImport(Collections.singletonList(row), operator);
ArgumentCaptor<Userinfo> captor = ArgumentCaptor.forClass(Userinfo.class); ArgumentCaptor<Userinfo> captor = ArgumentCaptor.forClass(Userinfo.class);
verify(service.userInfoMapper).insert(captor.capture()); verify(service.userInfoMapper).insert(captor.capture());
assertFalse(captor.getValue().getTeacherAdmin()); assertFalse(captor.getValue().getTeacherAdmin());
assertEquals("school-1", captor.getValue().getSchoolId());
assertEquals("faculty-1", captor.getValue().getSchoolFacultyId());
assertEquals("major-1", captor.getValue().getSchoolMajorId());
}
@Test
void teacherImportWithoutOperatorOrganizationLeavesFieldsEmpty() {
ImportServiceImpl service = new ImportServiceImpl();
service.userInfoMapper = mock(UserinfoMapper.class);
Userinfo operator = new Userinfo();
operator.setUserId("operator-2");
operator.setSchoolId("school-1");
TeacherImportDTO row = new TeacherImportDTO();
row.setTeacherName("李老师");
row.setUserName("t002");
service.processTeacherImport(Collections.singletonList(row), operator);
ArgumentCaptor<Userinfo> captor = ArgumentCaptor.forClass(Userinfo.class);
verify(service.userInfoMapper).insert(captor.capture());
assertEquals("school-1", captor.getValue().getSchoolId());
assertNull(captor.getValue().getSchoolFacultyId());
assertNull(captor.getValue().getSchoolMajorId());
} }
} }

Loading…
Cancel
Save