BookmarkInfoController.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package top.lvzhiqiang.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import top.lvzhiqiang.entity.BookmarkCategory;
  8. import top.lvzhiqiang.entity.BookmarkInfo;
  9. import top.lvzhiqiang.mapper.BookmarkMapper;
  10. import javax.annotation.Resource;
  11. import java.util.Comparator;
  12. import java.util.List;
  13. import java.util.stream.Collectors;
  14. /**
  15. * 书签信息Controller
  16. *
  17. * @author lvzhiqiang
  18. * 2025/9/24 15:45
  19. */
  20. @RestController
  21. @RequestMapping("/bookmarkInfo")
  22. public class BookmarkInfoController {
  23. @Resource
  24. private BookmarkMapper bookmarkMapper;
  25. /**
  26. * 获取完整分类树
  27. *
  28. * @return
  29. */
  30. @GetMapping("/categories/tree")
  31. public List<BookmarkCategory> getCategoryTree() {
  32. List<BookmarkCategory> allCategories = bookmarkMapper.selectAllCategories();
  33. return buildCategoryTree(null, allCategories);
  34. }
  35. /**
  36. * 递归构建分类树
  37. *
  38. * @param parentId
  39. * @param allCategories
  40. * @return
  41. */
  42. private List<BookmarkCategory> buildCategoryTree(Long parentId, List<BookmarkCategory> allCategories) {
  43. return allCategories.stream()
  44. .filter(category -> {
  45. if (parentId == null) {
  46. return category.getParentId() == null;
  47. } else {
  48. return parentId.equals(category.getParentId());
  49. }
  50. })
  51. .map(category -> {
  52. // 设置子分类
  53. category.setChildren(buildCategoryTree(category.getId(), allCategories));
  54. // 设置完整路径
  55. if (category.getPath() != null) {
  56. List<String> pathNames = bookmarkMapper.selectCategoryPath(category.getPath());
  57. category.setFullPath(String.join(" > ", pathNames));
  58. }
  59. return category;
  60. })
  61. .sorted(Comparator.comparing(BookmarkCategory::getSort))
  62. .collect(Collectors.toList());
  63. }
  64. /**
  65. * 根据父级ID获取子分类(用于级联下拉框)
  66. *
  67. * @param parentId
  68. * @return
  69. */
  70. @GetMapping("/categories")
  71. public List<JSONObject> getCategories(@RequestParam(required = false) Long parentId) {
  72. return bookmarkMapper.selectCategoriesByParentId(parentId);
  73. }
  74. /**
  75. * 获取分类下的书签
  76. *
  77. * @param categoryId
  78. * @return
  79. */
  80. @GetMapping("/bookmarks")
  81. public List<BookmarkInfo> getBookmarks(@RequestParam Long categoryId) {
  82. return bookmarkMapper.selectBookmarksByCategoryId(categoryId);
  83. }
  84. }