From 6a57683f50f87ef5976c967caecb4d8fcc9f3c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=90=8C=E6=96=B0?= <13658798+jjhyyds@user.noreply.gitee.com> Date: Fri, 9 Aug 2024 21:35:58 +0800 Subject: [PATCH 1/8] 8.10 --- .../chapter/TemporaryChapterMapper.java | 1 + .../backend/utils/Chapter/ExcelParser.java | 6 ++-- .../backend/utils/Chapter/SheetHandler.java | 29 ++++++++++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) 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/utils/Chapter/ExcelParser.java b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java index 4e764b6..04da20d 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java @@ -10,6 +10,7 @@ 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.xssf.eventusermodel.XSSFReader; @@ -31,7 +32,8 @@ import java.util.List; */ @Component public class ExcelParser { - + @Autowired + TemporaryChapterMapper temporaryChapterMapper; @Autowired IChapterService chapterService; @@ -44,7 +46,7 @@ public class ExcelParser { StylesTable styles = reader.getStylesTable(); XMLReader parser = XMLReaderFactory.createXMLReader(); - SheetHandler sheetHandler = new SheetHandler(chapterService, courseId); + SheetHandler sheetHandler = new SheetHandler(temporaryChapterMapper, chapterService, courseId); parser.setContentHandler(new XSSFSheetXMLHandler(styles, sst, sheetHandler, false)); XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) reader.getSheetsData(); 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..23f80d6 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.mapper.chapter.TemporaryChapterMapper; import com.teaching.backend.model.dto.chapter.ChapterDTO; 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,6 +23,9 @@ import java.util.*; public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { + + private final TemporaryChapterMapper temporaryChapterMapper; + private final IChapterService chapterService; private final String courseId; private ChapterDTO chapterDTO; @@ -30,7 +34,8 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { private boolean hasErrors = false; private List collectedChapters = new ArrayList<>(); - public SheetHandler(IChapterService chapterService, String courseId) { + public SheetHandler(TemporaryChapterMapper temporaryChapterMapper, IChapterService chapterService, String courseId) { + this.temporaryChapterMapper = temporaryChapterMapper; this.chapterService = chapterService; this.courseId = courseId; } @@ -58,6 +63,7 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { collectedChapters.add(chapterDTO); } } + @Transactional(rollbackFor = Exception.class) public List finalizeProcess() { if (hasErrors) { @@ -70,25 +76,30 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { } } - 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(ChapterDTO chapterDTO) { Long pid = chapterDTO.getParentExcelId(); if (pid != null && pid != 0) { + Long parentId = excelIdToDatabaseIdMap.get(pid); if (parentId != null) { chapterDTO.setPid(parentId); } else { + temporaryChapterMapper.deleteAll(); throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,Excel ID 为: " + pid); + } } @@ -105,12 +116,16 @@ public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { 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.getParentExcelId().equals(chapterDTO.getExcelId())) { + errors.add("编号:"+chapterDTO.getExcelId()+"的 上级编号不能等于其自身"); + } return errors; } From f2a53a6e33fa2d98293ba3e7b12784d51395af3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=90=8C=E6=96=B0?= <13658798+jjhyyds@user.noreply.gitee.com> Date: Mon, 12 Aug 2024 16:58:26 +0800 Subject: [PATCH 2/8] 8.12 --- .../backend/model/dto/chapter/ChapterDTO.java | 1 - .../model/dto/chapter/ChapterExcelDTO.java | 100 ++++++++++++ .../entity/chapter/TemporaryChapter.java | 2 +- .../service/chapter/IChapterService.java | 4 +- .../impl/chapter/ChapterServiceImpl.java | 13 +- .../backend/utils/Chapter/ExcelParser.java | 24 ++- .../backend/utils/Chapter/SheetHandler.java | 145 ++++++++++-------- .../utils/Chapter/SheetPreScanner.java | 34 ++++ 8 files changed, 248 insertions(+), 75 deletions(-) create mode 100644 src/main/java/com/teaching/backend/model/dto/chapter/ChapterExcelDTO.java create mode 100644 src/main/java/com/teaching/backend/utils/Chapter/SheetPreScanner.java 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/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/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/utils/Chapter/ExcelParser.java b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java index 04da20d..612e073 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/ExcelParser.java @@ -13,10 +13,13 @@ package com.teaching.backend.utils.Chapter; 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; @@ -40,20 +43,35 @@ public class ExcelParser { 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(temporaryChapterMapper, 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 23f80d6..d791fa6 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java @@ -3,10 +3,17 @@ package com.teaching.backend.utils.Chapter; import com.teaching.backend.common.ErrorCode; import com.teaching.backend.exception.BusinessException; 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.service.chapter.IChapterService; + +import jdk.nashorn.internal.ir.IfNode; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; + import org.apache.poi.xssf.usermodel.XSSFComment; + +import org.apache.poi.xssf.usermodel.XSSFSheet; import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -23,127 +30,139 @@ import java.util.*; public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler { - - private final TemporaryChapterMapper temporaryChapterMapper; - + 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(TemporaryChapterMapper temporaryChapterMapper, 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) + @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); if (parentId != null) { chapterDTO.setPid(parentId); } else { - temporaryChapterMapper.deleteAll(); 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.isSection()) { + if (currentChapterPid != null) { + chapterDTO.setTotalClassHours(chapterHour); + chapterDTO.setPid(currentChapterPid); + saveToTemporaryTable(chapterDTO); + } else { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到父章节,Excel ID 为: " + 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.getParentExcelId().equals(chapterDTO.getExcelId())) { - errors.add("编号:"+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; + } +} From 55777b33a4018429dbc76b450252922166e724c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=BB=E8=A3=81?= <14186472+wenyu441069198@user.noreply.gitee.com> Date: Mon, 12 Aug 2024 17:25:32 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E8=82=96=E6=B5=B7=E5=8D=97=E5=AD=A6?= =?UTF-8?q?=E7=94=9F=E5=AE=9E=E4=BD=93=E7=B1=BB=E5=9E=8B=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/umsAdmin/UmsStudentManageController.java | 2 -- .../teaching/backend/mapper/umsAdmin/UmsStudentMapper.java | 2 +- .../teaching/backend/model/entity/umsAdmin/UmsStudent.java | 2 +- .../backend/model/entity/umsAdmin/UmsStudentManage.java | 2 +- .../service/impl/umsAdmin/UmsStudentManageServiceImpl.java | 2 +- .../backend/service/impl/umsAdmin/UmsUserServiceImpl.java | 6 ++---- 6 files changed, 6 insertions(+), 10 deletions(-) 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/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/model/entity/umsAdmin/UmsStudent.java b/src/main/java/com/teaching/backend/model/entity/umsAdmin/UmsStudent.java index ec40634..6608772 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 @@ -60,7 +60,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/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; From 3070602cb135c111fc0f0bfdf2e055f402fd3f33 Mon Sep 17 00:00:00 2001 From: Alan <3052806735@qq.com> Date: Mon, 12 Aug 2024 17:31:20 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E4=B8=8B=E9=9D=A2=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=8C=E4=BB=A3=E7=A0=81=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/entity/umsAdmin/UmsStudent.java | 2 + .../model/vo/message/CourseUserInfVO.java | 2 - .../impl/courses/CoursesServiceImpl.java | 218 +++++++++++------- .../impl/message/UserInboxServiceImpl.java | 1 - .../backend/utils/Chapter/SheetHandler.java | 7 - 5 files changed, 137 insertions(+), 93 deletions(-) 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 6608772..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; 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/impl/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java index da9f0dc..0056187 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; @@ -640,57 +641,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 +720,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 +816,8 @@ public class CoursesServiceImpl extends ServiceImpl impl return courseUserInfVOS; } + + } diff --git a/src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java index 3dd07ca..654eb61 100644 --- a/src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/message/UserInboxServiceImpl.java @@ -56,7 +56,6 @@ public class UserInboxServiceImpl extends ServiceImpl queryReceiveMessages(MessagesQuery messagesQuery) { // 先按用户的id找到其的消息列表 默认看未删除的,(是否读 作为筛选条件后传进去) 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 d791fa6..9b1e47a 100644 --- a/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java +++ b/src/main/java/com/teaching/backend/utils/Chapter/SheetHandler.java @@ -5,15 +5,8 @@ import com.teaching.backend.exception.BusinessException; import com.teaching.backend.mapper.chapter.TemporaryChapterMapper; import com.teaching.backend.model.dto.chapter.ChapterExcelDTO; import com.teaching.backend.service.chapter.IChapterService; - -import jdk.nashorn.internal.ir.IfNode; -import org.apache.poi.ss.util.CellRangeAddress; -import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; - import org.apache.poi.xssf.usermodel.XSSFComment; - -import org.apache.poi.xssf.usermodel.XSSFSheet; import org.springframework.transaction.annotation.Transactional; import java.util.*; From dd6e7d9dc24cb4f6d98a67151930c1dc9f7a6502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=BB=E8=A3=81?= <14186472+wenyu441069198@user.noreply.gitee.com> Date: Mon, 12 Aug 2024 18:51:02 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E8=82=96=E6=B5=B7=E5=8D=97=E6=9F=A5?= =?UTF-8?q?=E8=80=81=E5=B8=88=E6=89=80=E6=95=99=E7=9A=84=E5=AD=A6=E7=94=9F?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/mapper/umsAdmin/UmsUserMapper.java | 3 +-- .../impl/courses/CoursesServiceImpl.java | 17 ++++++++++------- .../impl/favour/SeCourseFavourServiceImpl.java | 5 +---- .../CourseLearningRecordServiceImpl.java | 4 ++-- .../KnowledgeLearningRecordServiceImpl.java | 8 +------- src/main/resources/mapper/UmsUserMapper.xml | 2 +- 6 files changed, 16 insertions(+), 23 deletions(-) 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/service/impl/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java index 0056187..24f8d03 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 @@ -361,6 +361,7 @@ public class CoursesServiceImpl extends ServiceImpl impl @Override public List queryStudentList(String userId) { LinkedHashSet umsStudentManageList = queryTeacherByStudentList(userId); + System.out.println("1111111"+umsStudentManageList); List personalCenterStudentListVOList = new ArrayList<>(); PersonalCenterStudentListVO personalCenterStudentListVO = null; for (UmsStudentManage umsStudentManage : umsStudentManageList) { @@ -375,7 +376,7 @@ public class CoursesServiceImpl extends ServiceImpl impl @Override public LinkedHashSet queryTeacherByStudentList(String userId) { - String roleId = umsUserMapper.getbyIdRoleId(userId); + String roleId = umsUserMapper.getByIdRoleId(userId); CourseQuery courseQuery = new CourseQuery(); courseQuery.setUserId(userId); LinkedHashSet umsStudentManageList = new LinkedHashSet<>(); @@ -388,13 +389,15 @@ 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; } 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/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/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 From d1da72ad1ab9de09351cdbf55c40065f7da23000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=BB=E8=A3=81?= <14186472+wenyu441069198@user.noreply.gitee.com> Date: Mon, 12 Aug 2024 19:24:35 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E8=82=96=E6=B5=B7=E5=8D=97=E6=9F=A5?= =?UTF-8?q?=E8=80=81=E5=B8=88=E6=89=80=E6=95=99=E7=9A=84=E5=AD=A6=E7=94=9F?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/courses/ICoursesService.java | 2 ++ .../impl/courses/CoursesServiceImpl.java | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) 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/courses/CoursesServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/courses/CoursesServiceImpl.java index 24f8d03..313b3aa 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 @@ -360,8 +360,7 @@ public class CoursesServiceImpl extends ServiceImpl impl @Override public List queryStudentList(String userId) { - LinkedHashSet umsStudentManageList = queryTeacherByStudentList(userId); - System.out.println("1111111"+umsStudentManageList); + LinkedHashSet umsStudentManageList = queryTeacherByStudentLearningRecordList(userId); List personalCenterStudentListVOList = new ArrayList<>(); PersonalCenterStudentListVO personalCenterStudentListVO = null; for (UmsStudentManage umsStudentManage : umsStudentManageList) { @@ -375,7 +374,7 @@ public class CoursesServiceImpl extends ServiceImpl impl } @Override - public LinkedHashSet queryTeacherByStudentList(String userId) { + public LinkedHashSet queryTeacherByStudentLearningRecordList(String userId) { String roleId = umsUserMapper.getByIdRoleId(userId); CourseQuery courseQuery = new CourseQuery(); courseQuery.setUserId(userId); @@ -404,7 +403,27 @@ public class CoursesServiceImpl extends ServiceImpl impl 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())); + umsStudentManageList.add(umsStudentManage); + } + } + return umsStudentManageList; + } + throw new BusinessException(400,"只有老师才有此功能"); + } @Override public List getPagePageSize(int page, int pageSize) { From ce9cc317365bddd3cc79c11f0a9f85633525d381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=BB=E8=A3=81?= <14186472+wenyu441069198@user.noreply.gitee.com> Date: Mon, 12 Aug 2024 22:39:34 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E8=82=96=E6=B5=B7=E5=8D=97=E6=9F=A5?= =?UTF-8?q?=E8=80=81=E5=B8=88=E6=89=80=E6=95=99=E7=9A=84=E5=AD=A6=E7=94=9F?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/service/impl/courses/CoursesServiceImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 313b3aa..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 @@ -417,7 +417,9 @@ public class CoursesServiceImpl extends ServiceImpl impl List studentUsernames = studentCoursesMapper.selectBatchSomeStudent(courseId); for (StudentCourses studentUsername : studentUsernames) { UmsStudentManage umsStudentManage = umsStudentManageMapper.queryStudent(String.valueOf(studentUsername.getStudent())); - umsStudentManageList.add(umsStudentManage); + if (umsStudentManage != null){ + umsStudentManageList.add(umsStudentManage); + } } } return umsStudentManageList; From 908d8086817e279fb6b01f8c6809cdd0a0e351a5 Mon Sep 17 00:00:00 2001 From: Alan <3052806735@qq.com> Date: Tue, 13 Aug 2024 10:52:47 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=EF=BC=88=E5=AD=A6=E7=94=9F=E5=8F=AA=E8=83=BD?= =?UTF-8?q?=E5=8D=95=E5=8F=91=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../teaching/backend/common/ErrorCode.java | 3 +- .../impl/message/MessagesServiceImpl.java | 20 +++++++++++-- .../impl/message/UserInboxServiceImpl.java | 29 ++++++++++++------- 3 files changed, 39 insertions(+), 13 deletions(-) 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/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 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()) @@ -137,7 +137,7 @@ public class UserInboxServiceImpl extends ServiceImpl转换为PageDTO + // 3. 将Page转换为PageDTO PageDTO messages = PageDTO.of(p, SysMessageVO.class); List messagesList = messages.getList(); @@ -145,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) { @@ -200,7 +202,7 @@ public class UserInboxServiceImpl extends ServiceImpl().eq(UmsTeacher::getUserId, id) ); - return umsTeacher != null ? umsTeacher.getName() : null; + return umsTeacher != null ? umsTeacher.getName() : "教师不存在"; } }); @@ -212,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()));