package top.lvzhiqiang.controller; import com.alibaba.fastjson.JSONObject; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import top.lvzhiqiang.entity.BookmarkCategory; import top.lvzhiqiang.entity.BookmarkInfo; import top.lvzhiqiang.mapper.BookmarkMapper; import javax.annotation.Resource; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; /** * 书签信息Controller * * @author lvzhiqiang * 2025/9/24 15:45 */ @RestController @RequestMapping("/bookmarkInfo") public class BookmarkInfoController { @Resource private BookmarkMapper bookmarkMapper; /** * 获取完整分类树 * * @return */ @GetMapping("/categories/tree") public List getCategoryTree() { List allCategories = bookmarkMapper.selectAllCategories(); return buildCategoryTree(null, allCategories); } /** * 递归构建分类树 * * @param parentId * @param allCategories * @return */ private List buildCategoryTree(Long parentId, List allCategories) { return allCategories.stream() .filter(category -> { if (parentId == null) { return category.getParentId() == null; } else { return parentId.equals(category.getParentId()); } }) .map(category -> { // 设置子分类 category.setChildren(buildCategoryTree(category.getId(), allCategories)); // 设置完整路径 if (category.getPath() != null) { List pathNames = bookmarkMapper.selectCategoryPath(category.getPath()); category.setFullPath(String.join(" > ", pathNames)); } return category; }) .sorted(Comparator.comparing(BookmarkCategory::getSort)) .collect(Collectors.toList()); } /** * 根据父级ID获取子分类(用于级联下拉框) * * @param parentId * @return */ @GetMapping("/categories") public List getCategories(@RequestParam(required = false) Long parentId) { return bookmarkMapper.selectCategoriesByParentId(parentId); } /** * 获取分类下的书签 * * @param categoryId * @return */ @GetMapping("/bookmarks") public List getBookmarks(@RequestParam Long categoryId) { return bookmarkMapper.selectBookmarksByCategoryId(categoryId); } }