BookmarkInfoController.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package top.lvzhiqiang.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.web.bind.annotation.*;
  4. import top.lvzhiqiang.dto.R;
  5. import top.lvzhiqiang.entity.BookmarkCategory;
  6. import top.lvzhiqiang.entity.BookmarkInfo;
  7. import top.lvzhiqiang.enumeration.ResultCodeEnum;
  8. import top.lvzhiqiang.exception.BusinessException;
  9. import top.lvzhiqiang.exception.ParameterException;
  10. import top.lvzhiqiang.mapper.BookmarkMapper;
  11. import top.lvzhiqiang.mapper.CoinMapper;
  12. import top.lvzhiqiang.util.StringUtils;
  13. import javax.annotation.Resource;
  14. import java.util.Comparator;
  15. import java.util.List;
  16. import java.util.stream.Collectors;
  17. /**
  18. * 书签信息Controller
  19. *
  20. * @author lvzhiqiang
  21. * 2025/9/24 15:45
  22. */
  23. @RestController
  24. @RequestMapping("/bookmarkInfo")
  25. public class BookmarkInfoController {
  26. @Resource
  27. private BookmarkMapper bookmarkMapper;
  28. @Resource
  29. private CoinMapper coinMapper;
  30. /**
  31. * insertOrUpdateBookmark
  32. *
  33. * @author lvzhiqiang
  34. * 2025/9/25 16:36
  35. */
  36. @RequestMapping("/insertOrUpdate")
  37. @ResponseBody
  38. public R insertOrUpdate(String id, String title, String url, String tags, String remark, String level1Category, String level2Category, String level3Category, String crudType, String userName) {
  39. if (StringUtils.isEmpty(crudType)) {
  40. throw new ParameterException("crudType为空!");
  41. }
  42. Integer userId;
  43. if (StringUtils.isEmpty(userName)) {
  44. throw new ParameterException("userName为空!");
  45. } else {
  46. JSONObject coinUser = coinMapper.findUserByUsername(userName);
  47. if (coinUser == null) {
  48. throw new ParameterException("用户不存在!");
  49. }
  50. userId = coinUser.getInteger("id");
  51. }
  52. if ("1".equals(crudType)) {
  53. // 新增
  54. if (StringUtils.isEmpty(title) || StringUtils.isEmpty(url)) {
  55. throw new ParameterException("参数为空!");
  56. }
  57. BookmarkInfo bookmarkInfo = new BookmarkInfo();
  58. bookmarkInfo.setTitle(title.trim());
  59. bookmarkInfo.setUrl(url.trim());
  60. bookmarkInfo.setTags(tags);
  61. bookmarkInfo.setRemark(remark);
  62. bookmarkInfo.setAccountId(String.valueOf(userId));
  63. String categoryId;
  64. if (StringUtils.isNotEmpty(level3Category)) {
  65. categoryId = level3Category;
  66. } else if (StringUtils.isNotEmpty(level2Category)) {
  67. categoryId = level2Category;
  68. } else if (StringUtils.isNotEmpty(level1Category)) {
  69. categoryId = level1Category;
  70. } else {
  71. throw new ParameterException("分类为空!");
  72. }
  73. bookmarkInfo.setCategoryId(Integer.valueOf(categoryId));
  74. bookmarkMapper.insertBookmark(bookmarkInfo);
  75. } else if ("2".equals(crudType)) {
  76. // 修改
  77. if (StringUtils.isEmpty(id)) {
  78. throw new ParameterException("id为空!");
  79. }
  80. BookmarkInfo bookmarkInfo = bookmarkMapper.findBookmarkById(Long.valueOf(id));
  81. if (bookmarkInfo == null) {
  82. throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "id 不存在!");
  83. }
  84. if (StringUtils.isNotEmpty(title)) {
  85. bookmarkInfo.setTitle(title);
  86. }
  87. if (StringUtils.isNotEmpty(url)) {
  88. bookmarkInfo.setUrl(url);
  89. }
  90. if (StringUtils.isNotEmpty(tags)) {
  91. bookmarkInfo.setTags(tags);
  92. }
  93. if (StringUtils.isNotEmpty(remark)) {
  94. bookmarkInfo.setRemark(remark);
  95. }
  96. if (StringUtils.isNotEmpty(level3Category)) {
  97. bookmarkInfo.setCategoryId(Integer.valueOf(level3Category));
  98. } else if (StringUtils.isNotEmpty(level2Category)) {
  99. bookmarkInfo.setCategoryId(Integer.valueOf(level2Category));
  100. } else if (StringUtils.isNotEmpty(level1Category)) {
  101. bookmarkInfo.setCategoryId(Integer.valueOf(level1Category));
  102. }
  103. bookmarkMapper.updateBookmark(bookmarkInfo);
  104. }
  105. return R.ok().data("success");
  106. }
  107. @RequestMapping("/delete/{id}")
  108. @ResponseBody
  109. public R delete(@PathVariable Long id) {
  110. if (id == null) {
  111. throw new ParameterException("id为空!");
  112. }
  113. int count = bookmarkMapper.deleteBookmark(id);
  114. if (count > 0) {
  115. return R.ok();
  116. } else {
  117. return R.error().message("删除0条");
  118. }
  119. }
  120. /**
  121. * 获取完整分类树
  122. *
  123. * @return
  124. */
  125. @GetMapping("/categories/tree")
  126. public List<BookmarkCategory> getCategoryTree() {
  127. List<BookmarkCategory> allCategories = bookmarkMapper.selectAllCategories();
  128. return buildCategoryTree(null, allCategories);
  129. }
  130. /**
  131. * 递归构建分类树
  132. *
  133. * @param parentId
  134. * @param allCategories
  135. * @return
  136. */
  137. private List<BookmarkCategory> buildCategoryTree(Long parentId, List<BookmarkCategory> allCategories) {
  138. return allCategories.stream()
  139. .filter(category -> {
  140. if (parentId == null) {
  141. return category.getParentId() == null;
  142. } else {
  143. return parentId.equals(category.getParentId());
  144. }
  145. })
  146. .map(category -> {
  147. // 设置子分类
  148. category.setChildren(buildCategoryTree(category.getId(), allCategories));
  149. // 设置完整路径
  150. if (category.getPath() != null) {
  151. List<String> pathNames = bookmarkMapper.selectCategoryPath(category.getPath());
  152. category.setFullPath(String.join(" > ", pathNames));
  153. }
  154. return category;
  155. })
  156. .sorted(Comparator.comparing(BookmarkCategory::getSort))
  157. .collect(Collectors.toList());
  158. }
  159. /**
  160. * 根据父级ID获取子分类(用于级联下拉框)
  161. *
  162. * @param parentId
  163. * @return
  164. */
  165. @GetMapping("/categories")
  166. public List<JSONObject> getCategories(@RequestParam(required = false) Long parentId) {
  167. return bookmarkMapper.selectCategoriesByParentId(parentId);
  168. }
  169. /**
  170. * 获取分类下的书签
  171. *
  172. * @param categoryId
  173. * @return
  174. */
  175. @GetMapping("/bookmarks")
  176. public List<BookmarkInfo> getBookmarks(@RequestParam Long categoryId) {
  177. return bookmarkMapper.selectBookmarksByCategoryId(categoryId);
  178. }
  179. }