From f2086ebf35ede885d9fe63465baf513c64c942ee 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, 2 Aug 2024 16:37:37 +0800 Subject: [PATCH] 8.2 --- pom.xml | 18 +- .../chapter/ChapterController2.java | 19 ++ .../backend/model/dto/chapter/ChapterDTO.java | 4 +- .../backend/model/entity/chapter/Chapter.java | 2 +- .../service/chapter/IChapterService.java | 9 + .../impl/chapter/ChapterServiceImpl.java | 185 +++++++++++++++--- 6 files changed, 202 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index 6fdc2db..bfd5080 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,14 @@ 5.1.5.RELEASE + + org.apache.poi + poi + + + org.apache.poi + poi-ooxml + @@ -246,5 +254,13 @@ - + + + + org.apache.poi + poi-ooxml + 5.2.0 + + + diff --git a/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java b/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java index 46e776a..68f58f1 100644 --- a/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java +++ b/src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java @@ -52,6 +52,12 @@ public BaseResponse> getCourseChapters2(@RequestParam String cours return ResultUtils.success("添加成功!!!!!!!!"); } + @ApiOperation("插入章节") + @PostMapping("/insert") + public BaseResponse addChapter3(@RequestBody ChapterDTO chapterDTO, Long chapterId1, Long chapterId2){ + chapterService.insertChapter(chapterId1,chapterId2,chapterDTO); + return ResultUtils.success("添加成功!!!!!!!!"); + } @ApiOperation("删除章节") @DeleteMapping("/delete/{id}") public BaseResponse dlChapter(@PathVariable Long id){ @@ -68,6 +74,19 @@ public BaseResponse> getCourseChapters2(@RequestParam String cours chapterService.updateById(chapter); return ResultUtils.success("修改成功"); } + @ApiOperation("将章节上移") + @PostMapping("/upper") + public BaseResponse increase(@RequestParam Long chapterId){ + chapterService.upChapter(chapterId); +return ResultUtils.success("上移成功"); + } + + @ApiOperation("将章节下移") + @PostMapping("/down") + public BaseResponse decrease(@RequestParam Long chapterId){ + chapterService.downChapter(chapterId); + return ResultUtils.success("下移成功"); + } } 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 9581382..7ceb140 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 @@ -40,7 +40,7 @@ public class ChapterDTO implements Serializable { private String sysOrgCode; @ApiModelProperty(value = "序号") - private int orderNum; + private Double orderNum; @ApiModelProperty(value = "章节名称") private String name; @@ -49,7 +49,7 @@ public class ChapterDTO implements Serializable { private String content; @ApiModelProperty(value = "父章节") - private String pid; + private Long pid; @ApiModelProperty(value = "课程id") private String courseId; diff --git a/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java b/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java index e03dfdf..4cadcb9 100644 --- a/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java +++ b/src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java @@ -51,7 +51,7 @@ public class Chapter implements Serializable { private String sysOrgCode; @ApiModelProperty(value = "序号") - private int orderNum; + private Double orderNum; @ApiModelProperty(value = "名称") private String name; 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 f867f4b..9f50a31 100644 --- a/src/main/java/com/teaching/backend/service/chapter/IChapterService.java +++ b/src/main/java/com/teaching/backend/service/chapter/IChapterService.java @@ -31,5 +31,14 @@ public interface IChapterService extends IService { void saveChapter(ChapterDTO chapterDTO); + + void insertChapter(Long chapterId1, Long chapterId2, ChapterDTO newChapterDTO); + void deleteChapter(Long id); + + void upChapter(Long chapterId); + + void downChapter(Long chapterId); + + } 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 fb81fdc..eea5e3d 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 @@ -10,13 +10,11 @@ import com.teaching.backend.exception.BusinessException; import com.teaching.backend.mapper.Knowtemp.KnowtempMapper; import com.teaching.backend.mapper.chapter.ChapterMapper; import com.teaching.backend.model.dto.chapter.ChapterDTO; -import com.teaching.backend.model.entity.KnowGraph.Know; import com.teaching.backend.model.entity.Knowtemp.Knowtemp; import com.teaching.backend.model.entity.chapter.Chapter; import com.teaching.backend.model.vo.chapter.ChapterVo; import com.teaching.backend.service.KnowGraph.KnowService; import com.teaching.backend.service.chapter.IChapterService; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -166,31 +164,30 @@ public class ChapterServiceImpl extends ServiceImpl impl @Override public List getChapterTree(String courseId) { - List chapters=baseMapper.selectSectionsByCourseId(courseId); + List chapters=baseMapper.selectSectionsByCourseId(courseId); - Map chapterMap=new HashMap<>(); + Map chapterMap=new HashMap<>(); - List roots=new ArrayList<>(); + List roots=new ArrayList<>(); - for (Chapter chapter:chapters){ + for (Chapter chapter:chapters){ - chapter.setChildren(new ArrayList<>()); - chapter.setKnowledgePoints(new ArrayList<>()); - chapterMap.put(String.valueOf(chapter.getId()),chapter); - } - for (Chapter chapter:chapters){ - Long parentId=chapter.getPid(); - - if (!parentId.equals("0")){ - Chapter parentChapter=chapterMap.get(parentId); - if (parentChapter != null) { - parentChapter.getChildren().add(chapter); - } - - }else { - roots.add(chapter); + chapter.setChildren(new ArrayList<>()); + chapter.setKnowledgePoints(new ArrayList<>()); + chapterMap.put(chapter.getId(),chapter); + } + for (Chapter chapter:chapters){ + Long parentId=chapter.getPid(); + if (parentId!=0){ + Chapter parentChapter=chapterMap.get(parentId); + if (parentChapter != null) { + parentChapter.getChildren().add(chapter); } + + }else { + roots.add(chapter); } + } roots = roots.stream() .sorted(Comparator.comparing(Chapter::getOrderNum)) .collect(Collectors.toList()); @@ -228,29 +225,22 @@ public class ChapterServiceImpl extends ServiceImpl impl throw new RuntimeException("复制数据出错", e); } Long pid = chapter.getPid(); - if (pid == null ) { - chapter.setPid(0L); - LambdaQueryWrapper maxSortWrapper = new LambdaQueryWrapper<>(); - maxSortWrapper.eq(Chapter::getPid, "0").orderByDesc(Chapter::getOrderNum).last("limit 1"); - + maxSortWrapper.eq(Chapter::getPid, 0).orderByDesc(Chapter::getOrderNum).last("limit 1"); Chapter maxSortChapter = this.getOne(maxSortWrapper); - int newSort = 1; + double newSort = 1; if (maxSortChapter != null) { newSort = maxSortChapter.getOrderNum() + 1; } - chapter.setOrderNum(newSort); - chapter.setCreateTime(LocalDateTime.now()); chapter.setUpdateTime(LocalDateTime.now()); this.save(chapter); } else { - Chapter parentChapter = this.getById(pid); if (parentChapter != null) { @@ -259,7 +249,7 @@ public class ChapterServiceImpl extends ServiceImpl impl parentChapter.setUpdateTime(LocalDateTime.now()); Chapter maxSortChapter = this.getOne(maxSortWrapper); - int newSort = 1; + double newSort = 1; if (maxSortChapter != null) { newSort = maxSortChapter.getOrderNum() + 1; } @@ -277,6 +267,139 @@ public class ChapterServiceImpl extends ServiceImpl impl } } + @Override + public void insertChapter(Long chapterId1, Long chapterId2, ChapterDTO newChapterDTO) { + + Chapter chapter = new Chapter(); + try { + BeanUtils.copyProperties(newChapterDTO, chapter); + } catch (Exception e) { + throw new RuntimeException("复制数据出错", e); + } + + if (chapterId1 == null && chapterId2 == null||chapterId1!=null && chapterId2==null) { + handleRootChapterInsertion(chapter); + } else { + handleChapterInsertion(chapterId1, chapterId2, chapter); + } + + } + private void handleRootChapterInsertion(Chapter chapter) { + Long pid = chapter.getPid(); + if (pid == null) { + chapter.setPid(0L); + } + + double newOrderNum = getMaxOrderNumByPid(chapter.getPid()) + 1; + chapter.setOrderNum(newOrderNum); + chapter.setCreateTime(LocalDateTime.now()); + chapter.setUpdateTime(LocalDateTime.now()); + + this.save(chapter); + } + + private void handleChapterInsertion(Long chapterId1, Long chapterId2, Chapter newChapter) { + Chapter chapter1 = this.getById(chapterId1); + Chapter chapter2 = this.getById(chapterId2); + + if (chapter1 == null || chapter2 == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "未找到指定的章节"); + } + + double newOrderNum; + double orderNum1 = chapter1.getOrderNum(); + double orderNum2 = chapter2.getOrderNum(); + + if (orderNum1 == orderNum2) { + newOrderNum = orderNum1 - 0.0001; + System.out.println("New Order Num: " + newOrderNum); + } else { + newOrderNum = (orderNum1 + orderNum2) / 2; + } + + if (newChapter.getPid() == null) { + newChapter.setPid(0L); + } + + newChapter.setOrderNum(newOrderNum); + newChapter.setCreateTime(LocalDateTime.now()); + newChapter.setUpdateTime(LocalDateTime.now()); + + + this.save(newChapter); + } + + private double getMaxOrderNumByPid(Long pid) { + LambdaQueryWrapper maxSortWrapper = new LambdaQueryWrapper<>(); + maxSortWrapper.eq(Chapter::getPid, pid).orderByDesc(Chapter::getOrderNum).last("limit 1"); + Chapter maxSortChapter = this.getOne(maxSortWrapper); + return (maxSortChapter != null) ? maxSortChapter.getOrderNum() : 0; + } + + + @Override + public void upChapter(Long chapterId) { + moveChapter(chapterId, true); + } + @Override + public void downChapter(Long chapterId) { + moveChapter(chapterId, false); + } + + + + public void moveChapter(Long chapterId, boolean moveUp) { + Chapter chapter = this.getById(chapterId); + if (chapter == null) { + throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "章节不存在"); + } + + Long pid = chapter.getPid(); + String courseId = chapter.getCourseId(); + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + lambdaQueryWrapper.eq(Chapter::getPid, pid) + .eq(Chapter::getCourseId, courseId); + + List siblingChapters = this.list(lambdaQueryWrapper.orderByAsc(Chapter::getOrderNum)); + int currentIndex = siblingChapters.indexOf(chapter); + + + if (currentIndex == -1) { + throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "未找到章节在兄弟章节中的索引"); + } + + if (moveUp) { + + if (currentIndex == 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "章节已在顶部,无法上移"); + } + Chapter chapterAbove = siblingChapters.get(currentIndex - 1); + swapOrderNum(chapter, chapterAbove); + } else { + + if (currentIndex >= siblingChapters.size() - 1) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "章节已在底部,无法下移"); + } + Chapter chapterBelow = siblingChapters.get(currentIndex + 1); + swapOrderNum(chapter, chapterBelow); + } + } + + private void swapOrderNum(Chapter chapter1, Chapter chapter2) { + double tempOrderNum = chapter1.getOrderNum(); + chapter1.setOrderNum(chapter2.getOrderNum()); + chapter2.setOrderNum(tempOrderNum); + + this.updateOrderNum(chapter1.getId(), chapter1.getOrderNum()); + this.updateOrderNum(chapter2.getId(), chapter2.getOrderNum()); + } + + private void updateOrderNum(Long chapterId, double newOrderNum) { + Chapter updateChapter = new Chapter(); + updateChapter.setId(chapterId); + updateChapter.setOrderNum(newOrderNum); + this.updateById(updateChapter); + } @Override public void deleteChapter(Long id) { if (id==null){