diff --git a/src/main/java/com/teaching/backend/common/ErrorCode.java b/src/main/java/com/teaching/backend/common/ErrorCode.java index 4883d57..041682c 100644 --- a/src/main/java/com/teaching/backend/common/ErrorCode.java +++ b/src/main/java/com/teaching/backend/common/ErrorCode.java @@ -26,7 +26,8 @@ public enum ErrorCode { NO_AUTH_ERROR(40101, "无权限"), NOT_FOUND_ERROR(40400, "请求数据不存在"), CONTENT_EXISTS(41000, "内容存在"), - + STUDENT_ROLE_CANT_MULTIPLESEND(41001, "学生用户不能群发,只能单发"), + LEAST_ONE_USER(41002, "最少选择一个用户"), KNOWS_EXISTS(41001, "存在关联的知识点"), ADD_RELATION_FAILED(41002, "添加关系失败"), diff --git a/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java b/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java index 71c47b8..8e66e13 100644 --- a/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java +++ b/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java @@ -1,6 +1,5 @@ package com.teaching.backend.controller.umsAdmin; -import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.CommonResult; import com.teaching.backend.model.dto.umsAdmin.UmsStudentPageQueryDTO; import com.teaching.backend.model.entity.umsAdmin.UmsStudentManage; @@ -11,7 +10,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; import java.util.List; import java.util.Map; diff --git a/src/main/java/com/teaching/backend/mapper/chapter/TemporaryChapterMapper.java b/src/main/java/com/teaching/backend/mapper/chapter/TemporaryChapterMapper.java index 54c2347..a23bc16 100644 --- a/src/main/java/com/teaching/backend/mapper/chapter/TemporaryChapterMapper.java +++ b/src/main/java/com/teaching/backend/mapper/chapter/TemporaryChapterMapper.java @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.teaching.backend.model.entity.chapter.Chapter; import com.teaching.backend.model.entity.chapter.TemporaryChapter; import io.lettuce.core.dynamic.annotation.Param; +import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; diff --git a/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsStudentMapper.java b/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsStudentMapper.java index bd3b04a..fb58d74 100644 --- a/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsStudentMapper.java +++ b/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsStudentMapper.java @@ -14,6 +14,6 @@ import com.teaching.backend.model.entity.umsAdmin.UmsStudent; public interface UmsStudentMapper extends BaseMapper { boolean updateStudentInfo(UmsStudent umsStudent); - UmsStudent selectByUserId(Integer userId); + UmsStudent selectByUserId(Long userId); } diff --git a/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsUserMapper.java b/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsUserMapper.java index 7f422de..8ce099d 100644 --- a/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsUserMapper.java +++ b/src/main/java/com/teaching/backend/mapper/umsAdmin/UmsUserMapper.java @@ -1,7 +1,6 @@ package com.teaching.backend.mapper.umsAdmin; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.teaching.backend.model.entity.courses.StudentCourses; import com.teaching.backend.model.entity.umsAdmin.UmsRole; import com.teaching.backend.model.entity.umsAdmin.UmsTeacher; import com.teaching.backend.model.entity.umsAdmin.UmsUser; @@ -19,5 +18,5 @@ public interface UmsUserMapper extends BaseMapper { UmsStudentVO getByIdStudent(String id); - String getbyIdRoleId(String userId); + String getByIdRoleId(String userId); } diff --git a/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java index ab71af7..da34698 100644 --- a/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java +++ b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java @@ -67,7 +67,6 @@ public class ChapterDTO implements Serializable { @ApiModelProperty(value = "要求") private String requirement; - @ApiModelProperty(value = "线上学时") private String onlineClassHours; diff --git a/src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java new file mode 100644 index 0000000..f74249b --- /dev/null +++ b/src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java @@ -0,0 +1,100 @@ +package com.teaching.backend.model.dto.chapter; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * ClassName: ChapterExcelDto + * Package: com.teaching.backend.model.dto.chapter + * Description: + * + * @Author 姜钧瀚 + * @Create 2024/8/10 13:44 + * @Version 1.0 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ChapterExcelDTO implements Serializable { + private static final long serialVersionUID = 1L; + + @ApiModelProperty(value = "创建人") + private String createBy; + + + @ApiModelProperty(value = "创建日期") + @JsonFormat(pattern = "yyyy/M/d HH:mm:ss") + private LocalDateTime createTime; + + @ApiModelProperty(value = "更新人") + private String updateBy; + + @ApiModelProperty(value = "更新日期") + @JsonFormat(pattern = "yyyy/M/d HH-mm-ss") + private LocalDateTime updateTime; + + @ApiModelProperty(value = "所属部门") + private String sysOrgCode; + + @ApiModelProperty(value = "序号") + private Double orderNum; + + @ApiModelProperty(value = "章节名称") + private String name; + + @ApiModelProperty(value = "简介") + private String content; + + @ApiModelProperty(value = "父章节") + private Long pid; + + @ApiModelProperty(value = "课程id") + private String courseId; + + @ApiModelProperty(value = "课程目标") + private String courseObjectivesId; + + @ApiModelProperty(value = "总学时") + private double totalClassHours; + + @ApiModelProperty(value = "要求") + private String requirement; + + @TableField(exist = false) + @ApiModelProperty(value = "是否是节") + private boolean isSection; + + + @TableField(exist = false) + @ApiModelProperty(value = "章内序号") + private Integer internalOrder; + + @ApiModelProperty(value = "线上学时") + private String onlineClassHours; + + @ApiModelProperty(value = "周次") + private String zc; + + + @ApiModelProperty(value = "内部序号显示") + private String numshow; + + @ApiModelProperty(value = "excel上级序号") + @TableField(exist = false) + private Long ParentExcelId; + + @ApiModelProperty(value = "excel序号") + @TableField(exist = false) + private Long ExcelId; + + public void setIsSection(boolean b) { + this.isSection=b; + } +} \ No newline at end of file diff --git a/src/main/java/com/teaching/backend/model/entity/chapter/TemporaryChapter.java b/src/main/java/com/teaching/backend/model/entity/chapter/TemporaryChapter.java index d7f371b..756471b 100644 --- a/src/main/java/com/teaching/backend/model/entity/chapter/TemporaryChapter.java +++ b/src/main/java/com/teaching/backend/model/entity/chapter/TemporaryChapter.java @@ -69,7 +69,7 @@ public class TemporaryChapter implements Serializable { private String courseObjectivesId; @ApiModelProperty(value = "总学时") - private String totalClassHours; + private double totalClassHours; @ApiModelProperty(value = "要求") private String requirement; diff --git a/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudent.java b/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudent.java index ec40634..949204d 100644 --- a/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudent.java +++ b/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudent.java @@ -6,6 +6,8 @@ import java.time.LocalDate; import java.time.LocalDateTime; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; + +import io.swagger.models.auth.In; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; @@ -60,7 +62,7 @@ public class UmsStudent implements Serializable { private String className; //user_id - private Integer userId; + private Long userId; } diff --git a/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudentManage.java b/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudentManage.java index 9974bc2..d45e758 100644 --- a/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudentManage.java +++ b/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudentManage.java @@ -78,7 +78,7 @@ public class UmsStudentManage implements Serializable { private String className; //user_id - private Integer userId; + private Long userId; } diff --git a/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java b/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java index 364f456..87e7264 100644 --- a/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java +++ b/src/main/java/com/teaching/backend/model/vo/message/CourseUserInfVO.java @@ -13,8 +13,6 @@ public class CourseUserInfVO { private String courseId; @ApiModelProperty(value = "课程name") private String courseName; - @ApiModelProperty(value = "与课程有关的用户信息集合") private List usersInf; - } diff --git a/src/main/java/com/teaching/backend/service/chapter/IChapterService.java b/src/main/java/com/teaching/backend/service/chapter/IChapterService.java index 6eafd1f..875a4aa 100644 --- a/src/main/java/com/teaching/backend/service/chapter/IChapterService.java +++ b/src/main/java/com/teaching/backend/service/chapter/IChapterService.java @@ -4,12 +4,12 @@ package com.teaching.backend.service.chapter; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import com.teaching.backend.model.dto.chapter.ChapterDTO; +import com.teaching.backend.model.dto.chapter.ChapterExcelDTO; import com.teaching.backend.model.entity.chapter.Chapter; import com.teaching.backend.model.vo.chapter.ChapterVo; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.util.LinkedList; import java.util.List; @@ -54,7 +54,7 @@ public interface IChapterService extends IService { void moveDataFromTemporaryToFinal(); - Long saveToTemporaryTable(ChapterDTO chapterDTO); + Long saveToTemporaryTable(ChapterExcelDTO chapterDTO); void downExcel(HttpServletResponse response) throws IOException; diff --git a/src/main/java/com/teaching/backend/service/courses/ICoursesService.java b/src/main/java/com/teaching/backend/service/courses/ICoursesService.java index ac2e4c7..6a507d9 100644 --- a/src/main/java/com/teaching/backend/service/courses/ICoursesService.java +++ b/src/main/java/com/teaching/backend/service/courses/ICoursesService.java @@ -51,6 +51,8 @@ public interface ICoursesService extends IService { Map getPagePageSize2(int page, int pageSize); List queryStudentList(String userId); + LinkedHashSet queryTeacherByStudentLearningRecordList(String userId); + LinkedHashSet queryTeacherByStudentList(String userId); List getUserInfByCourse(Long userId); diff --git a/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java index 109c5ee..d330a6d 100644 --- a/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java @@ -14,12 +14,12 @@ import com.teaching.backend.mapper.chapter.ChapterMapper; import com.teaching.backend.mapper.chapter.TemporaryChapterMapper; import com.teaching.backend.model.dto.chapter.ChapterDTO; +import com.teaching.backend.model.dto.chapter.ChapterExcelDTO; import com.teaching.backend.model.entity.chapter.Chapter; import com.teaching.backend.model.entity.chapter.TemporaryChapter; import com.teaching.backend.model.entity.knowtmp.Knowtmp; import com.teaching.backend.model.vo.chapter.ChapterVo; -import com.teaching.backend.service.Know.KnowService; import com.teaching.backend.service.chapter.IChapterService; import com.teaching.backend.utils.Chapter.ExcelUtils; import org.springframework.beans.BeanUtils; @@ -29,7 +29,6 @@ import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; @@ -517,7 +516,7 @@ public class ChapterServiceImpl extends ServiceImpl impl @Transactional(rollbackFor = Exception.class) @Override - public Long saveToTemporaryTable(ChapterDTO chapterDTO) { + public Long saveToTemporaryTable(ChapterExcelDTO chapterDTO) { TemporaryChapter temporaryChapter = new TemporaryChapter(); try { BeanUtils.copyProperties(chapterDTO, temporaryChapter); @@ -526,10 +525,13 @@ public class ChapterServiceImpl extends ServiceImpl impl } Long pid = temporaryChapter.getPid(); + //pid=0的时候 if (pid == null || pid == 0) { temporaryChapter.setPid(0L); setChapterOrder(temporaryChapter, 0L); - } else { + } + //pid!=0的时候 + else { TemporaryChapter parentChapter = temporaryChapterMapper.selectById(pid); if (parentChapter != null) { temporaryChapter.setPid(parentChapter.getId()); @@ -538,8 +540,9 @@ public class ChapterServiceImpl extends ServiceImpl impl throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,ID 为: " + pid); } } - +// 执行 temporaryChapterMapper.insert(temporaryChapter); + return temporaryChapter.getId(); } diff --git a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java index da9f0dc..7d5eab9 100644 --- a/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java @@ -52,6 +52,7 @@ import java.io.File; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; import static com.teaching.backend.constant.RoleConstants.TEACHER_ROLE; @@ -359,7 +360,7 @@ public class CoursesServiceImpl extends ServiceImpl impl @Override public List queryStudentList(String userId) { - LinkedHashSet umsStudentManageList = queryTeacherByStudentList(userId); + LinkedHashSet umsStudentManageList = queryTeacherByStudentLearningRecordList(userId); List personalCenterStudentListVOList = new ArrayList<>(); PersonalCenterStudentListVO personalCenterStudentListVO = null; for (UmsStudentManage umsStudentManage : umsStudentManageList) { @@ -373,8 +374,8 @@ public class CoursesServiceImpl extends ServiceImpl impl } @Override - public LinkedHashSet queryTeacherByStudentList(String userId) { - String roleId = umsUserMapper.getbyIdRoleId(userId); + public LinkedHashSet queryTeacherByStudentLearningRecordList(String userId) { + String roleId = umsUserMapper.getByIdRoleId(userId); CourseQuery courseQuery = new CourseQuery(); courseQuery.setUserId(userId); LinkedHashSet umsStudentManageList = new LinkedHashSet<>(); @@ -387,20 +388,44 @@ public class CoursesServiceImpl extends ServiceImpl impl List courseLearningRecordList = new ArrayList<>(); for (StudentCourses studentUsername : studentUsernames) { CourseLearningRecord courseLearningRecord = courseLearningRecordMapper.selectNewRecord(String.valueOf(studentUsername.getStudent())); - courseLearningRecordList.add(courseLearningRecord); - } - courseLearningRecordList = courseLearningRecordList.stream().sorted(Comparator.comparing(CourseLearningRecord::getTimes).reversed()).collect(Collectors.toList()); - for (CourseLearningRecord courseLearningRecord : courseLearningRecordList) { - UmsStudentManage umsStudentManage = umsStudentManageMapper.queryStudent(courseLearningRecord.getUserId()); - umsStudentManageList.add(umsStudentManage); + if (courseLearningRecord != null){ + courseLearningRecordList.add(courseLearningRecord); + } } + courseLearningRecordList = courseLearningRecordList.stream().sorted(Comparator.comparing(CourseLearningRecord::getTimes).reversed()).collect(Collectors.toList()); + for (CourseLearningRecord courseLearningRecord : courseLearningRecordList) { + UmsStudentManage umsStudentManage = umsStudentManageMapper.queryStudent(courseLearningRecord.getUserId()); + umsStudentManageList.add(umsStudentManage); + } } return umsStudentManageList; } throw new BusinessException(400,"只有老师才有此功能"); } - + @Override + public LinkedHashSet queryTeacherByStudentList(String userId) { + String roleId = umsUserMapper.getByIdRoleId(userId); + CourseQuery courseQuery = new CourseQuery(); + courseQuery.setUserId(userId); + LinkedHashSet umsStudentManageList = new LinkedHashSet<>(); + if (roleId.equals("1")){ + PageDTO queryCourses = queryCourses(courseQuery); + List coursesList = queryCourses.getList(); + List courseIds = coursesList.stream().map(CoursesVO::getId).collect(Collectors.toList()); + for (String courseId : courseIds) { + List studentUsernames = studentCoursesMapper.selectBatchSomeStudent(courseId); + for (StudentCourses studentUsername : studentUsernames) { + UmsStudentManage umsStudentManage = umsStudentManageMapper.queryStudent(String.valueOf(studentUsername.getStudent())); + if (umsStudentManage != null){ + umsStudentManageList.add(umsStudentManage); + } + } + } + return umsStudentManageList; + } + throw new BusinessException(400,"只有老师才有此功能"); + } @Override public List getPagePageSize(int page, int pageSize) { @@ -640,57 +665,76 @@ public class CoursesServiceImpl extends ServiceImpl impl } private List queryCourseUserForTeacher(Long userId) { - // 查询所有与该教师相关的课程 - List teacherCoursesInf = coursesMapper.selectList( - new LambdaQueryWrapper() - .apply("FIND_IN_SET({0}, teacher)", userId) - .select(Courses::getId, Courses::getTeacher) + // 1. 获取该教师负责的所有课程 + List teacherCoursesInf = coursesMapper.selectList(new LambdaQueryWrapper() + .apply("FIND_IN_SET({0}, teacher)", userId) + .select(Courses::getId, Courses::getTeacher, Courses::getName) + ); + + // 2. 提取所有课程的ID + Set courseIds = teacherCoursesInf.stream() + .map(Courses::getId) + .collect(Collectors.toSet()); + + // 3. 批量查询所有课程的学生ID + List studentCourses = studentCoursesMapper.selectList( + new LambdaQueryWrapper().in(StudentCourses::getCourse, courseIds) + ); + + // 4. 获取所有的学生ID + Set studentIds = studentCourses.stream() + .map(StudentCourses::getStudent) + .collect(Collectors.toSet()); + + // 5. 批量查询所有学生的信息 + List students = umsStudentMapper.selectList( + new LambdaQueryWrapper().in(UmsStudent::getUserId, studentIds) ); + // 6. 将学生信息缓存到Map中,Key为学生ID + Map studentMap = students.stream() + .collect(Collectors.toMap(UmsStudent::getUserId, Function.identity())); + + // 7. 处理教师信息并构建返回结果 List courseUserInfVOS = new ArrayList<>(); - for (Courses courses : teacherCoursesInf) { + for (Courses course : teacherCoursesInf) { CourseUserInfVO courseUserInfVO = new CourseUserInfVO(); + courseUserInfVO.setCourseId(course.getId()); + courseUserInfVO.setCourseName(course.getName()); + List userReadMessageTimeVOS = new ArrayList<>(); - courseUserInfVO.setCourseId(courses.getId()); - - // 查询与课程相关的所有学生 - List studentIds = studentCoursesMapper.selectList( - new LambdaQueryWrapper<>(StudentCourses.class) - .eq(StudentCourses::getCourse, courses.getId()) - ).stream().map(StudentCourses::getStudent).collect(Collectors.toList()); - - // 为每个学生创建一个新的 UserReadMessageTimeVO 对象 - for (Long studentId : studentIds) { - UmsStudent umsStudent = umsStudentMapper.selectOne( - new LambdaQueryWrapper<>(UmsStudent.class) - .eq(UmsStudent::getUserId, studentId) - .select(UmsStudent::getName) - ); - if (umsStudent == null) { - log.error("用户不存在: {}",studentId); - throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); + + // 添加学生信息 + for (StudentCourses sc : studentCourses) { + if (sc.getCourse().equals(course.getId())) { + UmsStudent student = studentMap.get(sc.getStudent()); // 类型转换 + if (student != null) { + UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); + userReadMessageTimeVO.setId(student.getUserId()); + userReadMessageTimeVO.setReceiverName(student.getName()); + userReadMessageTimeVOS.add(userReadMessageTimeVO); + } else { + log.error("学生不存在: {}", sc.getStudent()); + throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); + } } - UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象 - userReadMessageTimeVO.setId(studentId); - userReadMessageTimeVO.setReceiverName(umsStudent.getName()); - userReadMessageTimeVOS.add(userReadMessageTimeVO); } - // 为每个教师创建一个新的 UserReadMessageTimeVO 对象 - String[] teacherUserIds = courses.getTeacher().split(","); + // 添加教师信息 + String[] teacherUserIds = course.getTeacher().split(","); for (String teacherId : teacherUserIds) { UmsTeacher umsTeacher = umsTeacherMapper.selectOne( - new LambdaQueryWrapper() - .eq(UmsTeacher::getUserId, teacherId) + new LambdaQueryWrapper().eq(UmsTeacher::getUserId, teacherId) ); - if (umsTeacher == null) { - log.error("教师不存在: {}",teacherId); + if (umsTeacher != null) { + UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); + userReadMessageTimeVO.setId(umsTeacher.getUserId()); + userReadMessageTimeVO.setReceiverName(umsTeacher.getName()); + userReadMessageTimeVOS.add(userReadMessageTimeVO); + } else { + log.error("教师不存在: {}", teacherId); throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); } - UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象 - userReadMessageTimeVO.setId(umsTeacher.getUserId()); - userReadMessageTimeVO.setReceiverName(umsTeacher.getName()); - userReadMessageTimeVOS.add(userReadMessageTimeVO); } courseUserInfVO.setUsersInf(userReadMessageTimeVOS); @@ -700,64 +744,94 @@ public class CoursesServiceImpl extends ServiceImpl impl } + + private List queryCourseUserForStudent(Long userId) { - // 查询学生所参与的所有课程 - List studentCoursesList = studentCoursesMapper.selectList( - new LambdaQueryWrapper() - .eq(StudentCourses::getStudent, userId) - .select(StudentCourses::getCourse) + // 1. 获取学生参与的所有课程 + List studentCourses = studentCoursesMapper.selectList( + new LambdaQueryWrapper().eq(StudentCourses::getStudent, userId) ); + // 2. 提取所有课程的ID + Set courseIds = studentCourses.stream() + .map(StudentCourses::getCourse) + .collect(Collectors.toSet()); + + // 3. 批量查询所有课程的信息 + List coursesList = coursesMapper.selectBatchIds(courseIds); + + // 4. 获取所有的教师ID + Set teacherIds = coursesList.stream() + .flatMap(course -> Arrays.stream(course.getTeacher().split(","))) + .map(Long::parseLong) + .collect(Collectors.toSet()); + + // 5. 批量查询所有教师的信息 + List teachers = umsTeacherMapper.selectList( + new LambdaQueryWrapper().in(UmsTeacher::getUserId, teacherIds) + ); + + // 6. 将教师信息缓存到Map中,Key为教师ID + Map teacherMap = teachers.stream() + .collect(Collectors.toMap(UmsTeacher::getUserId, Function.identity())); + + // 7. 批量查询所有课程的学生信息 + List allStudentCourses = studentCoursesMapper.selectList( + new LambdaQueryWrapper().in(StudentCourses::getCourse, courseIds) + ); + + // 8. 提取所有学生的ID + Set allStudentIds = allStudentCourses.stream() + .map(StudentCourses::getStudent) + .collect(Collectors.toSet()); + + // 9. 批量查询所有学生的信息 + List students = umsStudentMapper.selectList( + new LambdaQueryWrapper().in(UmsStudent::getUserId, allStudentIds) + ); + + // 10. 将学生信息缓存到Map中,Key为学生ID + Map studentMap = students.stream() + .collect(Collectors.toMap(UmsStudent::getUserId, Function.identity())); + + // 11. 处理课程信息并构建返回结果 List courseUserInfVOS = new ArrayList<>(); - for (StudentCourses studentCourse : studentCoursesList) { + for (Courses course : coursesList) { CourseUserInfVO courseUserInfVO = new CourseUserInfVO(); + courseUserInfVO.setCourseId(course.getId()); + courseUserInfVO.setCourseName(course.getName()); + List userReadMessageTimeVOS = new ArrayList<>(); - courseUserInfVO.setCourseId(studentCourse.getCourse()); - - // 查询与课程相关的所有学生 - List studentIds = studentCoursesMapper.selectList( - new LambdaQueryWrapper<>(StudentCourses.class) - .eq(StudentCourses::getCourse, studentCourse.getCourse()) - ).stream().map(StudentCourses::getStudent).collect(Collectors.toList()); - - // 为每个学生创建一个新的 UserReadMessageTimeVO 对象 - for (Long studentId : studentIds) { - UmsStudent umsStudent = umsStudentMapper.selectOne( - new LambdaQueryWrapper<>(UmsStudent.class) - .eq(UmsStudent::getUserId, studentId) - .select(UmsStudent::getName) - ); - if (umsStudent == null) { - log.error("用户不存在: {}", studentId); + + // 添加教师信息 + String[] courseTeacherIds = course.getTeacher().split(","); + for (String teacherId : courseTeacherIds) { + UmsTeacher umsTeacher = teacherMap.get(Long.valueOf(teacherId)); + if (umsTeacher != null) { + UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); + userReadMessageTimeVO.setId(umsTeacher.getUserId()); + userReadMessageTimeVO.setReceiverName(umsTeacher.getName()); + userReadMessageTimeVOS.add(userReadMessageTimeVO); + } else { + log.error("教师不存在: {}", teacherId); throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); } - UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象 - userReadMessageTimeVO.setId(studentId); - userReadMessageTimeVO.setReceiverName(umsStudent.getName()); - userReadMessageTimeVOS.add(userReadMessageTimeVO); } - // 查询与课程相关的所有教师 - Courses course = coursesMapper.selectById(studentCourse.getCourse()); - if (course != null) { - String[] teacherUserIds = course.getTeacher().split(","); - for (String teacherId : teacherUserIds) { - UmsTeacher umsTeacher = umsTeacherMapper.selectOne( - new LambdaQueryWrapper() - .eq(UmsTeacher::getUserId, teacherId) - ); - if (umsTeacher == null) { - log.error("教师不存在: {}", teacherId); + // 添加学生信息 + for (StudentCourses sc : allStudentCourses) { + if (sc.getCourse().equals(course.getId())) { + UmsStudent student = studentMap.get(sc.getStudent()); // 类型转换 + if (student != null) { + UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); + userReadMessageTimeVO.setId(student.getUserId()); + userReadMessageTimeVO.setReceiverName(student.getName()); + userReadMessageTimeVOS.add(userReadMessageTimeVO); + } else { + log.error("学生不存在: {}", sc.getStudent()); throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); } - UserReadMessageTimeVO userReadMessageTimeVO = new UserReadMessageTimeVO(); // 在循环中创建新对象 - userReadMessageTimeVO.setId(umsTeacher.getUserId()); - userReadMessageTimeVO.setReceiverName(umsTeacher.getName()); - userReadMessageTimeVOS.add(userReadMessageTimeVO); } - } else { - log.error("课程不存在: {}", studentCourse.getCourse()); - throw new BusinessException(ErrorCode.PARAMS_COURSE_NOTEXISTS); } courseUserInfVO.setUsersInf(userReadMessageTimeVOS); @@ -766,6 +840,8 @@ public class CoursesServiceImpl extends ServiceImpl impl return courseUserInfVOS; } + + } diff --git a/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java index a260644..5cac41a 100644 --- a/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/favour/SeCourseFavourServiceImpl.java @@ -16,9 +16,6 @@ import com.teaching.backend.mapper.favour.SeCourseFavourMapper; import com.teaching.backend.mapper.umsAdmin.UmsUserMapper; import com.teaching.backend.model.entity.courses.Courses; import com.teaching.backend.model.entity.favour.SeCourseFavour; -import com.teaching.backend.model.entity.favour.SeKnowFavour; -import com.teaching.backend.model.entity.favour.SeResourceFavour; -import com.teaching.backend.model.entity.umsAdmin.UmsUser; import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.model.vo.favour.CourseFavourDetailVO; import com.teaching.backend.service.courses.ICoursesService; @@ -157,7 +154,7 @@ public class SeCourseFavourServiceImpl extends ServiceImpl queryNewCollect(String userId) { - String roleId = umsUserMapper.getbyIdRoleId(userId); + String roleId = umsUserMapper.getByIdRoleId(userId); List coursesList = new ArrayList<>(); if (roleId.equals("1")){ //老师 diff --git a/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java index 6205d54..6b7b2c4 100644 --- a/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/message/MessagesServiceImpl.java @@ -7,9 +7,11 @@ import com.teaching.backend.common.ErrorCode; import com.teaching.backend.exception.BusinessException; import com.teaching.backend.mapper.message.MessagesMapper; import com.teaching.backend.mapper.message.UserInboxMapper; +import com.teaching.backend.mapper.umsAdmin.UmsUserMapper; import com.teaching.backend.model.dto.message.SysMessageDTO; import com.teaching.backend.model.entity.message.SysMessages; import com.teaching.backend.model.entity.message.UserInbox; +import com.teaching.backend.model.entity.umsAdmin.UmsUser; import com.teaching.backend.service.message.IMessagesService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; @@ -38,6 +40,8 @@ public class MessagesServiceImpl extends ServiceImpl receiverIds = Arrays.stream(sysMessageDTO.getReceiverId().split(",")) .map(Long::valueOf) .collect(Collectors.toList()); + if (receiverIds.size()<1){ + throw new BusinessException(ErrorCode.LEAST_ONE_USER); + } + + // 4.判断是什么用户,只有教师可以群发,学生只能单发 + String roleId = umsUserMapper.selectOne(new LambdaQueryWrapper<>(UmsUser.class) + .eq(UmsUser::getId, sysMessageDTO.getSenderId())).getRoleId(); + if(roleId.equals("2")){ + if (receiverIds.size()>1){ + throw new BusinessException(ErrorCode.STUDENT_ROLE_CANT_MULTIPLESEND); + } + } - // 4.数据写入收件箱 + // 5.数据写入收件箱 List userInboxes = receiverIds.stream() .map(receiverId -> { UserInbox userInbox = new UserInbox(); @@ -68,7 +84,7 @@ public class MessagesServiceImpl extends ServiceImpl queryReceiveMessages(MessagesQuery messagesQuery) { // 先按用户的id找到其的消息列表 默认看未删除的,(是否读 作为筛选条件后传进去) @@ -126,10 +125,10 @@ public class UserInboxServiceImpl extends ServiceImpl querySendMessages(MessagesQuery messagesQuery) { - // 初始化一个按发送时间降序排序的Page对象 + // 1. 初始化一个按发送时间降序排序的Page对象 Page page = messagesQuery.toMpPage("send_time", false); - // 根据条件查询SysMessages表 + // 2. 根据条件查询SysMessages表 Page p = messagesService.page(page, new LambdaQueryWrapper() .eq(SysMessages::getSenderId, messagesQuery.getUserId()) @@ -138,7 +137,7 @@ public class UserInboxServiceImpl extends ServiceImpl转换为PageDTO + // 3. 将Page转换为PageDTO PageDTO messages = PageDTO.of(p, SysMessageVO.class); List messagesList = messages.getList(); @@ -146,31 +145,33 @@ public class UserInboxServiceImpl extends ServiceImpl courseIds = messagesList.stream() .map(SysMessageVO::getCourseId) .filter(Objects::nonNull) .collect(Collectors.toSet()); + // 5. 批量查询课程ID对应的课程名称 并把ID:name 对应到一起 Map courseNames = coursesMapper.selectBatchIds(courseIds).stream() .collect(Collectors.toMap(Courses::getId, Courses::getName)); - // 批量查询所有用户收件箱数据 + // 6. 批量查询当前用户对应的消息集合messageIds Set messageIds = messagesList.stream() .map(SysMessageVO::getId) .collect(Collectors.toSet()); + // 在用户邮箱中批量查询 对应消息的 信息(消息id,接受者id,是否已读) List allUserInboxes = userInboxMapper.selectList( new LambdaQueryWrapper() .in(UserInbox::getMessageId, messageIds) .select(UserInbox::getMessageId, UserInbox::getReceiverId, UserInbox::getIsRead) ); - // 将UserInbox数据按消息ID进行分组 + // 7. 将UserInbox数据按消息ID进行分组(已经初步得到了消息的详情) Map> userInboxesByMessageId = allUserInboxes.stream() .collect(Collectors.groupingBy(UserInbox::getMessageId)); - // 缓存用户名称以避免重复查询 + // 8. 缓存用户名称以避免重复查询 Map userNamesCache = new HashMap<>(); for (SysMessageVO sysMessageVO : messagesList) { @@ -201,7 +202,7 @@ public class UserInboxServiceImpl extends ServiceImpl().eq(UmsTeacher::getUserId, id) ); - return umsTeacher != null ? umsTeacher.getName() : null; + return umsTeacher != null ? umsTeacher.getName() : "教师不存在"; } }); @@ -213,20 +214,27 @@ public class UserInboxServiceImpl extends ServiceImpl(UserInbox.class) + .eq(UserInbox::getMessageId, messagesQuery.getMessageId()) + .eq(UserInbox::getReceiverId, messagesQuery.getUserId())); + if (!(count > 0)){ + throw new BusinessException(ErrorCode.MESSAGE_NO_EXIT); + } SysMessages sysMessage = messagesService.getById(messagesQuery.getMessageId()); SysMessageVO sysMessageVO = new SysMessageVO(); sysMessageVO.setId(messagesQuery.getMessageId()); sysMessageVO.setContent(sysMessage.getContent()); sysMessageVO.setSenderId(messagesQuery.getUserId()); -// 获取发件人信息 + //获取发件人信息 UmsTeacher umsTeacher = umsTeacherMapper.selectOne( new LambdaQueryWrapper() .eq(UmsTeacher::getUserId, sysMessage.getSenderId())); diff --git a/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java index 4c9eae5..fb3e253 100644 --- a/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/records/CourseLearningRecordServiceImpl.java @@ -123,7 +123,7 @@ public class CourseLearningRecordServiceImpl extends ServiceImpl queryCourseList(String userId) { - String roleId = umsUserMapper.getbyIdRoleId(userId); + String roleId = umsUserMapper.getByIdRoleId(userId); List coursesList = new ArrayList<>(); if (roleId.equals("1")){ List courseIds = courseLearningRecordMapper.selectMaxNumber(); @@ -148,7 +148,7 @@ public class CourseLearningRecordServiceImpl extends ServiceImpl queryrecommendCourses(String userId) { - String roleId = umsUserMapper.getbyIdRoleId(userId); + String roleId = umsUserMapper.getByIdRoleId(userId); if (roleId.equals("1")) { diff --git a/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java index cfe8d3e..c392c53 100644 --- a/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/records/KnowledgeLearningRecordServiceImpl.java @@ -12,18 +12,12 @@ import com.teaching.backend.mapper.umsAdmin.UmsUserMapper; import com.teaching.backend.model.entity.KnowGraph.KnowledgeLearningNumber; -import com.teaching.backend.model.entity.courses.CourseLearningNumber; - -import com.teaching.backend.model.entity.courses.Courses; import com.teaching.backend.model.entity.knowtmp.Knowtmp; import com.teaching.backend.model.entity.records.KnowledgeLearningRecord; -import com.teaching.backend.model.vo.courses.CoursesVO; import com.teaching.backend.model.vo.knowGraph.KnowVO; import com.teaching.backend.model.vo.records.KnowledgeLearningRecordVo; -import com.teaching.backend.service.Know.KnowService; -import com.teaching.backend.service.impl.courses.CourseLearningNumberServiceImpl; import com.teaching.backend.service.impl.knowledge.KnowledgeLearningNumberServiceImpl; import com.teaching.backend.service.knowtmp.KnowtmpService; import com.teaching.backend.service.records.IKnowledgeLearningRecordService; @@ -141,7 +135,7 @@ public class KnowledgeLearningRecordServiceImpl extends ServiceImpl queryMaxKnowledge(String userId) { - String roleId = umsUserMapper.getbyIdRoleId(userId); + String roleId = umsUserMapper.getByIdRoleId(userId); List coursesList = new ArrayList<>(); KnowVO knowVO = null; if (roleId.equals("1")){ diff --git a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java index a81f5f0..12eb47d 100644 --- a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java @@ -91,7 +91,7 @@ public class UmsStudentManageServiceImpl extends ServiceImpl implements UmsUserService { @@ -90,7 +88,7 @@ public class UmsUserServiceImpl extends ServiceImpl impl UmsUser byUsername = lambdaQuery().eq(UmsUser::getUsername, umsUser.getUsername()) .one(); UmsStudent umsStudent = new UmsStudent(); - umsStudent.setUserId(Math.toIntExact(byUsername.getId())); + umsStudent.setUserId(byUsername.getId()); umsStudentMapper.insert(umsStudent); return umsUser; @@ -311,7 +309,7 @@ public class UmsUserServiceImpl extends ServiceImpl impl UmsUser byUsername = lambdaQuery().eq(UmsUser::getPhone, umsUser.getPhone()) .one(); UmsStudent umsStudent = new UmsStudent(); - umsStudent.setUserId(Math.toIntExact(byUsername.getId())); + umsStudent.setUserId(byUsername.getId()); umsStudentMapper.insert(umsStudent); return umsUser; diff --git a/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java index 4e764b6..612e073 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java @@ -10,12 +10,16 @@ package com.teaching.backend.utils.Chapter; * @Version 1.0 */ +import com.teaching.backend.mapper.chapter.TemporaryChapterMapper; import com.teaching.backend.service.chapter.IChapterService; import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.xml.sax.InputSource; @@ -31,27 +35,43 @@ import java.util.List; */ @Component public class ExcelParser { - + @Autowired + TemporaryChapterMapper temporaryChapterMapper; @Autowired IChapterService chapterService; public List parse(InputStream inputStream, String courseId) throws Exception { OPCPackage pkg = OPCPackage.open(inputStream); List validationErrors = new ArrayList<>(); + int totalRows = 0; try { XSSFReader reader = new XSSFReader(pkg); SharedStringsTable sst = reader.getSharedStringsTable(); StylesTable styles = reader.getStylesTable(); XMLReader parser = XMLReaderFactory.createXMLReader(); - SheetHandler sheetHandler = new SheetHandler(chapterService, courseId); - parser.setContentHandler(new XSSFSheetXMLHandler(styles, sst, sheetHandler, false)); - XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) reader.getSheetsData(); + SheetPreScanner preScanner = new SheetPreScanner(); + while (sheets.hasNext()) { + InputStream sheetstream = sheets.next(); + InputSource sheetSource = new InputSource(sheetstream); + try { + parser.setContentHandler(new XSSFSheetXMLHandler(styles, sst, preScanner, false)); + parser.parse(sheetSource); + } finally { + sheetstream.close(); + } + } + totalRows = preScanner.getRowCount(); + + sheets = (XSSFReader.SheetIterator) reader.getSheetsData(); + SheetHandler sheetHandler = null; while (sheets.hasNext()) { InputStream sheetstream = sheets.next(); InputSource sheetSource = new InputSource(sheetstream); try { + sheetHandler = new SheetHandler(temporaryChapterMapper, chapterService, courseId, totalRows); + parser.setContentHandler(new XSSFSheetXMLHandler(styles, sst, sheetHandler, false)); parser.parse(sheetSource); } finally { sheetstream.close(); diff --git a/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java b/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java index c78d062..9b1e47a 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java @@ -1,12 +1,13 @@ package com.teaching.backend.utils.Chapter; import com.teaching.backend.common.ErrorCode; -import com.teaching.backend.common.ResultUtils; import com.teaching.backend.exception.BusinessException; -import com.teaching.backend.model.dto.chapter.ChapterDTO; +import com.teaching.backend.mapper.chapter.TemporaryChapterMapper; +import com.teaching.backend.model.dto.chapter.ChapterExcelDTO; import com.teaching.backend.service.chapter.IChapterService; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; import org.apache.poi.xssf.usermodel.XSSFComment; +import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -22,66 +23,52 @@ import java.util.*; public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { + private Long currentChapterPid; + private final TemporaryChapterMapper temporaryChapterMapper; + private Double chapterHour; private final IChapterService chapterService; private final String courseId; - private ChapterDTO chapterDTO; + private ChapterExcelDTO chapterDTO; private Map excelIdToDatabaseIdMap = new HashMap<>(); private List validationErrors = new ArrayList<>(); private boolean hasErrors = false; - private List collectedChapters = new ArrayList<>(); + private int totalRows; - public SheetHandler(IChapterService chapterService, String courseId) { + + + public SheetHandler(TemporaryChapterMapper temporaryChapterMapper, IChapterService chapterService, String courseId,int totalRows) { + this.temporaryChapterMapper = temporaryChapterMapper; this.chapterService = chapterService; this.courseId = courseId; + this.totalRows=totalRows; } @Override public void startRow(int rowIndex) { - if (rowIndex == 0) { - chapterDTO = null; + if (rowIndex < totalRows - 1) { + if (rowIndex == 0 || rowIndex == 1) { + chapterDTO = null; + } else { + chapterDTO = new ChapterExcelDTO(); + chapterDTO.setCourseId(courseId); + } } else { - chapterDTO = new ChapterDTO(); - chapterDTO.setCourseId(courseId); + chapterDTO = null; } - } - @Override - public void endRow(int rowIndex) { - if (rowIndex != 0 && chapterDTO != null) { - List errors = validateChapter(chapterDTO); - if (!errors.isEmpty()) { - validationErrors.addAll(errors); - hasErrors = true; - return; - } - - collectedChapters.add(chapterDTO); - } } + @Transactional(rollbackFor = Exception.class) public List finalizeProcess() { if (hasErrors) { return validationErrors; } - for (ChapterDTO chapter : collectedChapters) { - if (chapter.getParentExcelId() == null || chapter.getParentExcelId() == 0) { - saveToTemporaryTable(chapter); - } - } - - for (ChapterDTO chapter : collectedChapters) { - if (chapter.getParentExcelId() != null && chapter.getParentExcelId() != 0) { - saveToTemporaryTable(chapter); - } - } - - chapterService.moveDataFromTemporaryToFinal(); return Collections.emptyList(); } - private void saveToTemporaryTable(ChapterDTO chapterDTO) { + private void saveToTemporaryTable(ChapterExcelDTO chapterDTO) { Long pid = chapterDTO.getParentExcelId(); if (pid != null && pid != 0) { Long parentId = excelIdToDatabaseIdMap.get(pid); @@ -91,44 +78,84 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,Excel ID 为: " + pid); } } - - Long savedChapterId = chapterService.saveToTemporaryTable(chapterDTO); excelIdToDatabaseIdMap.put(chapterDTO.getExcelId(), savedChapterId); - } + } + private List validateChapter(ChapterExcelDTO chapterDTO) { + List errors = new ArrayList<>(); + return errors; + } + @Override + public void endRow(int rowIndex) { + if (rowIndex != 0 && chapterDTO != null) { + List errors = validateChapter(chapterDTO); + if (!errors.isEmpty()) { + validationErrors.addAll(errors); + hasErrors = true; + return; + } - private List validateChapter(ChapterDTO chapterDTO) { - List errors = new ArrayList<>(); - if (chapterDTO.getName() == null || chapterDTO.getName().isEmpty()) { - errors.add("编号:"+chapterDTO.getExcelId()+"的章节名字不能为空"); - } - if (chapterDTO.getExcelId() == null || chapterDTO.getExcelId() <= 0) { - errors.add("编号不能小于0"); - } - if (chapterDTO.getParentExcelId() == null || chapterDTO.getParentExcelId() < 0) { - errors.add("编号:"+chapterDTO.getExcelId()+"的上级编号不能小于0"); + if (chapterDTO.isSection()) { + if (currentChapterPid != null) { + chapterDTO.setTotalClassHours(chapterHour); + chapterDTO.setPid(currentChapterPid); + saveToTemporaryTable(chapterDTO); + } else { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,Excel ID 为: " + chapterDTO.getExcelId()); + } + } } - return errors; + } @Override public void cell(String cellReference, String formattedValue, XSSFComment comment) { - if (chapterDTO != null && formattedValue != null) { + if (chapterDTO != null) { String letter = cellReference.substring(0, 1); + switch (letter) { case "A": chapterDTO.setExcelId(Long.valueOf(formattedValue)); break; + case "B": chapterDTO.setName(formattedValue); break; + case "C": - chapterDTO.setParentExcelId(Long.valueOf(formattedValue)); + if (!chapterDTO.isSection()) { + chapterDTO.setTotalClassHours(Double.parseDouble(formattedValue)); + saveToTemporaryTable(chapterDTO); + currentChapterPid = excelIdToDatabaseIdMap.get(chapterDTO.getExcelId()); + } + break; + + case "D": + chapterDTO.setOrderNum(Double.valueOf(formattedValue)); + break; + + case "E": + if (!formattedValue.isEmpty()) { + chapterDTO.setName(formattedValue); + chapterDTO.setIsSection(true); + } break; + + case "F": + if (chapterDTO.isSection()) { + chapterDTO.setTotalClassHours(Double.parseDouble(formattedValue)); + chapterHour = chapterDTO.getTotalClassHours(); + } + break; + } + } + + } + } \ No newline at end of file diff --git a/src/main/java/com/teaching/backend/utils/Chapter/SheetPreScanner.java b/src/main/java/com/teaching/backend/utils/Chapter/SheetPreScanner.java new file mode 100644 index 0000000..95caa75 --- /dev/null +++ b/src/main/java/com/teaching/backend/utils/Chapter/SheetPreScanner.java @@ -0,0 +1,34 @@ +package com.teaching.backend.utils.Chapter; + +import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; +import org.apache.poi.xssf.usermodel.XSSFComment; + +/** + * ClassName: Sheet + * Package: com.teaching.backend.utils.Chapter + * Description: + * + * @Author 姜钧瀚 + * @Create 2024/8/11 18:04 + * @Version 1.0 + */ +public class SheetPreScanner implements XSSFSheetXMLHandler.SheetContentsHandler { + private int rowCount = 0; + + @Override + public void startRow(int rowNum) { + rowCount++; + } + + @Override + public void endRow(int rowNum) { + } + + @Override + public void cell(String cellReference, String formattedValue, XSSFComment comment) { + } + + public int getRowCount() { + return rowCount; + } +} diff --git a/src/main/resources/mapper/UmsUserMapper.xml b/src/main/resources/mapper/UmsUserMapper.xml index 5b56d4a..6ea4599 100644 --- a/src/main/resources/mapper/UmsUserMapper.xml +++ b/src/main/resources/mapper/UmsUserMapper.xml @@ -76,7 +76,7 @@ WHERE s.user_id = #{id} - SELECT us.role_id roleId FROM