小萌新 4 months ago
parent e20d575436
commit f2086ebf35
  1. 18
      pom.xml
  2. 19
      src/main/java/com/teaching/backend/controller/chapter/ChapterController2.java
  3. 4
      src/main/java/com/teaching/backend/model/dto/chapter/ChapterDTO.java
  4. 2
      src/main/java/com/teaching/backend/model/entity/chapter/Chapter.java
  5. 9
      src/main/java/com/teaching/backend/service/chapter/IChapterService.java
  6. 185
      src/main/java/com/teaching/backend/service/impl/chapter/ChapterServiceImpl.java

@ -36,6 +36,14 @@
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
@ -246,5 +254,13 @@
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

@ -52,6 +52,12 @@ public BaseResponse<List<Chapter>> getCourseChapters2(@RequestParam String cours
return ResultUtils.success("添加成功!!!!!!!!");
}
@ApiOperation("插入章节")
@PostMapping("/insert")
public BaseResponse<String> addChapter3(@RequestBody ChapterDTO chapterDTO, Long chapterId1, Long chapterId2){
chapterService.insertChapter(chapterId1,chapterId2,chapterDTO);
return ResultUtils.success("添加成功!!!!!!!!");
}
@ApiOperation("删除章节")
@DeleteMapping("/delete/{id}")
public BaseResponse<String> dlChapter(@PathVariable Long id){
@ -68,6 +74,19 @@ public BaseResponse<List<Chapter>> getCourseChapters2(@RequestParam String cours
chapterService.updateById(chapter);
return ResultUtils.success("修改成功");
}
@ApiOperation("将章节上移")
@PostMapping("/upper")
public BaseResponse<String> increase(@RequestParam Long chapterId){
chapterService.upChapter(chapterId);
return ResultUtils.success("上移成功");
}
@ApiOperation("将章节下移")
@PostMapping("/down")
public BaseResponse<String> decrease(@RequestParam Long chapterId){
chapterService.downChapter(chapterId);
return ResultUtils.success("下移成功");
}
}

@ -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;

@ -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;

@ -31,5 +31,14 @@ public interface IChapterService extends IService<Chapter> {
void saveChapter(ChapterDTO chapterDTO);
void insertChapter(Long chapterId1, Long chapterId2, ChapterDTO newChapterDTO);
void deleteChapter(Long id);
void upChapter(Long chapterId);
void downChapter(Long chapterId);
}

@ -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<ChapterMapper, Chapter> impl
@Override
public List<Chapter> getChapterTree(String courseId) {
List<Chapter> chapters=baseMapper.selectSectionsByCourseId(courseId);
List<Chapter> chapters=baseMapper.selectSectionsByCourseId(courseId);
Map<String,Chapter> chapterMap=new HashMap<>();
Map<Long,Chapter> chapterMap=new HashMap<>();
List<Chapter> roots=new ArrayList<>();
List<Chapter> 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<ChapterMapper, Chapter> impl
throw new RuntimeException("复制数据出错", e);
}
Long pid = chapter.getPid();
if (pid == null ) {
chapter.setPid(0L);
LambdaQueryWrapper<Chapter> 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<ChapterMapper, Chapter> 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<ChapterMapper, Chapter> 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<Chapter> 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<Chapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(Chapter::getPid, pid)
.eq(Chapter::getCourseId, courseId);
List<Chapter> 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){

Loading…
Cancel
Save