|
|
|
@ -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){ |
|
|
|
|