| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package top.lvzhiqiang.controller;
- import com.alibaba.fastjson.JSONObject;
- import org.springframework.web.bind.annotation.*;
- import top.lvzhiqiang.dto.R;
- import top.lvzhiqiang.entity.BookmarkCategory;
- import top.lvzhiqiang.entity.BookmarkInfo;
- import top.lvzhiqiang.enumeration.ResultCodeEnum;
- import top.lvzhiqiang.exception.BusinessException;
- import top.lvzhiqiang.exception.ParameterException;
- import top.lvzhiqiang.mapper.BookmarkMapper;
- import top.lvzhiqiang.mapper.CoinMapper;
- import top.lvzhiqiang.util.StringUtils;
- 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;
- @Resource
- private CoinMapper coinMapper;
- /**
- * insertOrUpdateBookmark
- *
- * @author lvzhiqiang
- * 2025/9/25 16:36
- */
- @RequestMapping("/insertOrUpdate")
- @ResponseBody
- public R insertOrUpdate(String id, String title, String url, String tags, String remark, String level1Category, String level2Category, String level3Category, String crudType, String userName) {
- if (StringUtils.isEmpty(crudType)) {
- throw new ParameterException("crudType为空!");
- }
- Integer userId;
- if (StringUtils.isEmpty(userName)) {
- throw new ParameterException("userName为空!");
- } else {
- JSONObject coinUser = coinMapper.findUserByUsername(userName);
- if (coinUser == null) {
- throw new ParameterException("用户不存在!");
- }
- userId = coinUser.getInteger("id");
- }
- if ("1".equals(crudType)) {
- // 新增
- if (StringUtils.isEmpty(title) || StringUtils.isEmpty(url)) {
- throw new ParameterException("参数为空!");
- }
- BookmarkInfo bookmarkInfo = new BookmarkInfo();
- bookmarkInfo.setTitle(title.trim());
- bookmarkInfo.setUrl(url.trim());
- bookmarkInfo.setTags(tags);
- bookmarkInfo.setRemark(remark);
- bookmarkInfo.setAccountId(String.valueOf(userId));
- String categoryId;
- if (StringUtils.isNotEmpty(level3Category)) {
- categoryId = level3Category;
- } else if (StringUtils.isNotEmpty(level2Category)) {
- categoryId = level2Category;
- } else if (StringUtils.isNotEmpty(level1Category)) {
- categoryId = level1Category;
- } else {
- throw new ParameterException("分类为空!");
- }
- bookmarkInfo.setCategoryId(Integer.valueOf(categoryId));
- bookmarkMapper.insertBookmark(bookmarkInfo);
- } else if ("2".equals(crudType)) {
- // 修改
- if (StringUtils.isEmpty(id)) {
- throw new ParameterException("id为空!");
- }
- BookmarkInfo bookmarkInfo = bookmarkMapper.findBookmarkById(Long.valueOf(id));
- if (bookmarkInfo == null) {
- throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "id 不存在!");
- }
- if (StringUtils.isNotEmpty(title)) {
- bookmarkInfo.setTitle(title);
- }
- if (StringUtils.isNotEmpty(url)) {
- bookmarkInfo.setUrl(url);
- }
- if (StringUtils.isNotEmpty(tags)) {
- bookmarkInfo.setTags(tags);
- }
- if (StringUtils.isNotEmpty(remark)) {
- bookmarkInfo.setRemark(remark);
- }
- if (StringUtils.isNotEmpty(level3Category)) {
- bookmarkInfo.setCategoryId(Integer.valueOf(level3Category));
- } else if (StringUtils.isNotEmpty(level2Category)) {
- bookmarkInfo.setCategoryId(Integer.valueOf(level2Category));
- } else if (StringUtils.isNotEmpty(level1Category)) {
- bookmarkInfo.setCategoryId(Integer.valueOf(level1Category));
- }
- bookmarkMapper.updateBookmark(bookmarkInfo);
- }
- return R.ok().data("success");
- }
- @RequestMapping("/delete/{id}")
- @ResponseBody
- public R delete(@PathVariable Long id) {
- if (id == null) {
- throw new ParameterException("id为空!");
- }
- int count = bookmarkMapper.deleteBookmark(id);
- if (count > 0) {
- return R.ok();
- } else {
- return R.error().message("删除0条");
- }
- }
- /**
- * 获取完整分类树
- *
- * @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);
- }
- }
|