| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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<BookmarkCategory> getCategoryTree() {
- List<BookmarkCategory> allCategories = bookmarkMapper.selectAllCategories();
- return buildCategoryTree(null, allCategories);
- }
- /**
- * 递归构建分类树
- *
- * @param parentId
- * @param allCategories
- * @return
- */
- private List<BookmarkCategory> buildCategoryTree(Long parentId, List<BookmarkCategory> 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<String> 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<JSONObject> getCategories(@RequestParam(required = false) Long parentId) {
- return bookmarkMapper.selectCategoriesByParentId(parentId);
- }
- /**
- * 获取分类下的书签
- *
- * @param categoryId
- * @return
- */
- @GetMapping("/bookmarks")
- public List<BookmarkInfo> getBookmarks(@RequestParam Long categoryId) {
- return bookmarkMapper.selectBookmarksByCategoryId(categoryId);
- }
- }
|