From ff05d5fd16cfcc4138505f4264db77606f62033f Mon Sep 17 00:00:00 2001 From: Alan <3052806735@qq.com> Date: Fri, 21 Jun 2024 17:16:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../teaching/backend/common/ErrorCode.java | 3 + .../controller/courses/CoursesController.java | 3 + .../ObjectiveContentKnowController.java | 2 +- .../exception/GlobalExceptionHandler.java | 8 +- .../backend/filter/BodyReaderWrapper.java | 5 +- .../backend/filter/ReplaceStreamFilter.java | 1 - .../impl/courses/CoursesServiceImpl.java | 117 ++++++++++++++---- .../mapper/CourseObjectivesMapper.xml | 4 +- 8 files changed, 107 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/teaching/backend/common/ErrorCode.java b/src/main/java/com/teaching/backend/common/ErrorCode.java index 352cffa..1a038df 100644 --- a/src/main/java/com/teaching/backend/common/ErrorCode.java +++ b/src/main/java/com/teaching/backend/common/ErrorCode.java @@ -25,6 +25,9 @@ public enum ErrorCode { NO_AUTH_ERROR(40101, "无权限"), NOT_FOUND_ERROR(40400, "请求数据不存在"), CONTENT_EXISTS(41000, "内容存在"), + + KNOWS_EXISTS(41001, "存在关联的知识点"), + FORBIDDEN_ERROR(40300, "禁止访问"), PARAMS_ILLEGAL(42000, "请求参数违法"), diff --git a/src/main/java/com/teaching/backend/controller/courses/CoursesController.java b/src/main/java/com/teaching/backend/controller/courses/CoursesController.java index 3d4cb48..642fcb6 100644 --- a/src/main/java/com/teaching/backend/controller/courses/CoursesController.java +++ b/src/main/java/com/teaching/backend/controller/courses/CoursesController.java @@ -74,6 +74,7 @@ public class CoursesController { } @ApiOperation("根据id查询课程") + @ValidateParams({"id"}) @GetMapping("/{id}") public BaseResponse getByIdCourse(@PathVariable String id){ Courses course = coursesService.getById(id); @@ -91,6 +92,8 @@ public class CoursesController { } //TODO:删除功能暂未完善,数据缺失 + // 暂时发现有个漏洞 就是目标关联的有知识点的时候还是可以直接删除 + // (当有内容的时候本来就是得先删除内容--关联也有知识点 所以内容下面的不用校验 ) @ApiOperation("根据id删除课程") @ValidateParams({"id"}) @DeleteMapping("/{id}") diff --git a/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java b/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java index 628dec6..0f0df16 100644 --- a/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java +++ b/src/main/java/com/teaching/backend/controller/courses/ObjectiveContentKnowController.java @@ -32,7 +32,7 @@ public class ObjectiveContentKnowController { @ApiOperation("给分项目标或者是内容添加知识点") - @ValidateParams({"objectiveId","know"}) +// @ValidateParams({"objectiveId","know"}) @PostMapping("/addknows") public BaseResponse saveKnowsWithObjectiveOrContent(@RequestBody ObjectiveContentKnowDTO objectiveContentKnowDTO){ String data = objectiveContentKnowService.saveKnowsWithObjectiveOrContent(objectiveContentKnowDTO); diff --git a/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java b/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java index b904f85..b52916b 100644 --- a/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/teaching/backend/exception/GlobalExceptionHandler.java @@ -17,10 +17,10 @@ public class GlobalExceptionHandler { return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); } - @ExceptionHandler(Exception.class) - public BaseResponse handleException(Exception ex) { - return ResultUtils.error(ErrorCode.UNKNOW_ERROR, ex.getMessage()); - } +// @ExceptionHandler(Exception.class) +// public BaseResponse handleException(Exception ex) { +// return ResultUtils.error(ErrorCode.UNKNOW_ERROR, ex.getMessage()); +// } // 其他异常处理器可以按需添加 diff --git a/src/main/java/com/teaching/backend/filter/BodyReaderWrapper.java b/src/main/java/com/teaching/backend/filter/BodyReaderWrapper.java index adcab71..8c59da5 100644 --- a/src/main/java/com/teaching/backend/filter/BodyReaderWrapper.java +++ b/src/main/java/com/teaching/backend/filter/BodyReaderWrapper.java @@ -1,5 +1,4 @@ package com.teaching.backend.filter; - import org.apache.commons.io.IOUtils; import javax.servlet.ReadListener; @@ -13,9 +12,7 @@ public class BodyReaderWrapper extends HttpServletRequestWrapper { public BodyReaderWrapper(HttpServletRequest request) throws IOException { super(request); - try (InputStream inputStream = request.getInputStream()) { - requestBody = IOUtils.toByteArray(inputStream); - } + requestBody = IOUtils.toByteArray(request.getInputStream()); } @Override diff --git a/src/main/java/com/teaching/backend/filter/ReplaceStreamFilter.java b/src/main/java/com/teaching/backend/filter/ReplaceStreamFilter.java index b462e1a..9639501 100644 --- a/src/main/java/com/teaching/backend/filter/ReplaceStreamFilter.java +++ b/src/main/java/com/teaching/backend/filter/ReplaceStreamFilter.java @@ -34,4 +34,3 @@ public class ReplaceStreamFilter implements Filter { // no-op } } - 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 d343e48..f634140 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 @@ -38,6 +38,7 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.math.BigDecimal; import java.math.BigInteger; @@ -76,6 +77,8 @@ public class CoursesServiceImpl extends ServiceImpl impl UmsAdminRoleRelationMapper umsAdminRoleRelationMapper; @Autowired StudentCoursesMapper studentCoursesMapper; + @Autowired + ObjectiveContentKnowMapper objectiveContentKnowMapper; @Override @@ -212,8 +215,13 @@ public class CoursesServiceImpl extends ServiceImpl impl if (umsAdminTea == null) { throw new BusinessException(ErrorCode.OPERATION_ERROR); } +// System.out.println(umsAdminTea); UmsTeacher umsTeacher = umsTeacherMapper.selectOne(new LambdaQueryWrapper() .eq(UmsTeacher::getUserId, umsAdminTea.getId())); + System.out.println(umsTeacher); + if (umsTeacher == null){ + throw new BusinessException(ErrorCode.PARAMS_USER_NOTEXISTS); + } teacherNameList.add(umsTeacher.getName()); } coursesVO.setTeacher(String.join(",", teacherNameList)); @@ -234,21 +242,28 @@ public class CoursesServiceImpl extends ServiceImpl impl String objectivesId = courseObjectives.getId(); List courseObjectivesTreeVOList = courseObjectivesMapper.selectTreeNodes(objectivesId); int contents = 0; + int knows = 0; List objectiveIds = new ArrayList<>(); for (CourseObjectivesTreeVO courseObjectivesTreeVO : courseObjectivesTreeVOList) { String objectiveId = courseObjectivesTreeVO.getId(); objectiveIds.add(objectiveId); - Long content = objectiveContentsMapper - .selectCount(new LambdaQueryWrapper().eq(ObjectiveContents::getObjectiveId, objectiveId)); + Long content = objectiveContentsMapper.selectCount(new LambdaQueryWrapper() + .eq(ObjectiveContents::getObjectiveId, objectiveId)); contents+=content; + + Long know = objectiveContentKnowMapper.selectCount(new LambdaQueryWrapper() + .eq(ObjectiveContentKnow::getObjectiveOrContent, objectiveId)); + knows+=know; } - if (contents==0){ - coursesMapper.deleteById(id); - courseObjectivesMapper.deleteBatchIds(objectiveIds); - } - else{ + if(contents > 0){ throw new BusinessException(ErrorCode.CONTENT_EXISTS, "该课程的目标下面还有内容,请把相关内容清空后再来删除课程"); } + if (knows > 0){ + throw new BusinessException(ErrorCode.KNOWS_EXISTS,"该课程的目标下面还有关联的知识点,请把相关知识点清空后再来删除课程"); + } + courseObjectivesMapper.deleteBatchIds(objectiveIds); + coursesMapper.deleteById(id); + } @Override @@ -329,8 +344,15 @@ public class CoursesServiceImpl extends ServiceImpl impl File rootPath = new File(ResourceUtils.getURL("classpath:").getPath()); //SpringBoot项目获取根目录的方式 File templatePath = new File(rootPath.getAbsolutePath(),"/templates/courses.docx");//------------------需要模板的地址 + FileInputStream template1; + try { + template1 = new FileInputStream(templatePath); + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new Exception("模板文件未找到"); + } // 加载Word模板------------------路径换成服务器里文件的绝对路径(要是相对路径也行也OK) - FileInputStream template = new FileInputStream("D:\\Users\\Desktop\\teaching-backend\\src\\main\\resources\\templates\\courses.docx"); + FileInputStream template = new FileInputStream("D:\\IDEA\\AAATEA\\teaching-backend\\src\\main\\resources\\templates\\courses.docx"); XWPFDocument document = new XWPFDocument(template); //准备导出数据 @@ -391,18 +413,11 @@ public class CoursesServiceImpl extends ServiceImpl impl document = WordExportUtil.exportWord07(templatePath.getPath(), params); - //----------------修改表格模版在导入参数之后 - - //获取第2个表格---------- XWPFTable table = document.getTables().get(1); - for (int i = 0; i < course_number - 1; i++) { //新增一行单元格 XWPFTableRow newRow = table.createRow(); newRow.getCell(0).setText("课程目标" + (i + 2)); - newRow.getCell(0).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);//水平居中 - newRow.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);//垂直居中 - } //获取第5个表格 @@ -413,21 +428,16 @@ public class CoursesServiceImpl extends ServiceImpl impl XWPFTableRow newRow = table5.createRow(); newRow.getCell(0).setText(Integer.valueOf(i+2).toString()); - newRow.getCell(0).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);//水平居中 - newRow.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);//垂直居中 + newRow.getCell(0).getParagraphArray(0).setIndentationLeft(0); + newRow.getCell(0).getParagraphArray(0).setIndentationHanging(0); - //课程目标 - newRow.getCell(1).setText("课程目标" + (i + 2)); - newRow.getCell(1).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);//水平居中 - newRow.getCell(1).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);//垂直居中 + newRow.getCell(1).setText(" 课程目标" + (i + 2)); //扩四列 for (int j = 0; j < 4; j++) { newRow.createCell(); } } - - - + table5.setTableAlignment(TableRowAlign.CENTER); String filename= courses.getName()+"课程标准.docx"; //设置文件的打开方式和mime类型 @@ -437,6 +447,65 @@ public class CoursesServiceImpl extends ServiceImpl impl document.write(outputStream); } +// public static void insertRow(XWPFTable table, int copyrowIndex, int newrowIndex) { +// // 在表格中指定的位置新增一行 +// XWPFTableRow targetRow = table.insertNewTableRow(newrowIndex); +// // 获取需要复制行对象 +// XWPFTableRow copyRow = table.getRow(copyrowIndex); +// //复制行对象给新增的行对象 +// targetRow.getCtRow().setTrPr(copyRow.getCtRow().getTrPr()); +// //获取需要复制行对象的列 +// List copyCells = copyRow.getTableCells(); +// //新增的对象的列 +// XWPFTableCell targetCell = null; +// //遍历复制行对象的列 +// for (int i = 0; i < copyCells.size(); i++) { +// //复制行对象的列 +// XWPFTableCell copyCell = copyCells.get(i); +// //新增的行对象创建一列 +// targetCell = targetRow.addNewTableCell(); +// //格式复制 +// targetCell.getCTTc().setTcPr(copyCell.getCTTc().getTcPr()); +// if (copyCell.getParagraphs() != null && copyCell.getParagraphs().size() > 0) { +// targetCell.getParagraphs().get(0).getCTP().setPPr(copyCell.getParagraphs().get(0).getCTP().getPPr()); +// if (copyCell.getParagraphs().get(0).getRuns() != null +// && copyCell.getParagraphs().get(0).getRuns().size() > 0) { +// XWPFRun cellR = targetCell.getParagraphs().get(0).createRun(); +// cellR.setBold(copyCell.getParagraphs().get(0).getRuns().get(0).isBold()); +// } +// } +// } +// +// } +// +// private static void replaceVariable(XWPFDocument document, String variable, String replacement) { +// for (XWPFParagraph paragraph : document.getParagraphs()) { +// for (XWPFRun run : paragraph.getRuns()) { +// String text = run.getText(0); +// if (text != null && text.contains(variable)) { +// text = text.replace(variable, replacement); +// run.setText(text, 0); +// // run.setFontFamily("Arial"); +// // run.setFontSize(14); +// } +// } +// } +// } +// private static void replaceVariableInTable(XWPFTable table, String variable, String replacement) { +// for (XWPFTableRow row : table.getRows()) { +// for (XWPFTableCell cell : row.getTableCells()) { +// for (XWPFParagraph paragraph : cell.getParagraphs()) { +// for (XWPFRun run : paragraph.getRuns()) { +// String text = run.getText(0); +// if (text != null && text.contains(variable)) { +// text = text.replace(variable, replacement); +// run.setText(text, 0); +// } +// } +// } +// } +// } +// } } diff --git a/src/main/resources/mapper/CourseObjectivesMapper.xml b/src/main/resources/mapper/CourseObjectivesMapper.xml index 91dd5a0..2acf2b7 100644 --- a/src/main/resources/mapper/CourseObjectivesMapper.xml +++ b/src/main/resources/mapper/CourseObjectivesMapper.xml @@ -14,10 +14,10 @@ - INSERT INTO course_objectives (id, type, name, pid) + INSERT INTO course_objectives (id, type, pid) VALUES - (#{item.id}, #{item.type}, #{item.name},#{item.pid}) + (#{item.id}, #{item.type},#{item.pid})