Feat: [后台]:完成节点编辑,节点拖拽功能

This commit is contained in:
fanxb 2019-07-18 17:06:22 +08:00
parent d426f93080
commit 5009007966
5 changed files with 116 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package com.fanxb.bookmark.business.bookmark.controller;
import com.fanxb.bookmark.business.bookmark.entity.BatchDeleteBody;
import com.fanxb.bookmark.business.bookmark.entity.MoveNodeBody;
import com.fanxb.bookmark.business.bookmark.service.BookmarkService;
import com.fanxb.bookmark.common.entity.Bookmark;
import com.fanxb.bookmark.common.entity.Result;
@ -94,4 +95,18 @@ public class BookmarkController {
return Result.success(null);
}
/**
* Description: 移动一个节点
*
* @param body body
* @return com.fanxb.bookmark.common.entity.Result
* @author fanxb
* @date 2019/7/18 10:54
*/
@PostMapping("/moveNode")
public Result moveNode(@RequestBody MoveNodeBody body) {
bookmarkService.moveNode(UserContextHolder.get().getUserId(), body);
return Result.success(null);
}
}

View File

@ -93,9 +93,45 @@ public interface BookmarkDao {
/**
* Description: 编辑书签
*
* @param bookmark bookmark
* @author fanxb
* @date 2019/7/17 14:49
* @param bookmark bookmark
*/
void editBookmark(Bookmark bookmark);
/**
* Description: path下sort>=某个值的sort自增1
*
* @param userId userId
* @param path path
* @param sort sort
* @author fanxb
* @date 2019/7/18 10:13
*/
void sortPlus(@Param("userId") int userId, @Param("path") String path, @Param("sort") int sort);
/**
* Description: 更新某个路径下所有子节点的路径信息
*
* @param userId userId
* @param oldPath oldPath
* @param newPath newPath
* @author fanxb
* @date 2019/7/18 10:35
*/
void updateChildrenPath(@Param("userId") int userId, @Param("oldPath") String oldPath, @Param("newPath") String newPath);
/**
* Description: 更新某个书签的pathsort
*
* @param userId userId
* @param bookmarkId bookmarkId
* @param path path
* @param sort sort
* @author fanxb
* @date 2019/7/18 10:37
*/
void updatePathAndSort(@Param("userId") int userId, @Param("bookmarkId") int bookmarkId, @Param("path") String path, @Param("sort") int sort);
}

View File

@ -0,0 +1,21 @@
package com.fanxb.bookmark.business.bookmark.entity;
import lombok.Data;
/**
* 类功能简述 移动节点表单
* 类功能详述
*
* @author fanxb
* @date 2019/7/18 10:04
*/
@Data
public class MoveNodeBody {
int bookmarkId;
String sourcePath;
String targetPath;
/**
* sort -1表示移动到某个文件夹下放到最后
*/
int sort;
}

View File

@ -1,6 +1,7 @@
package com.fanxb.bookmark.business.bookmark.service;
import com.fanxb.bookmark.business.bookmark.dao.BookmarkDao;
import com.fanxb.bookmark.business.bookmark.entity.MoveNodeBody;
import com.fanxb.bookmark.common.entity.Bookmark;
import com.fanxb.bookmark.common.exception.CustomException;
import com.fanxb.bookmark.common.util.UserContextHolder;
@ -95,7 +96,9 @@ public class BookmarkService {
bookmarkDao.deleteUserFolder(userId, item);
bookmarkIdList.add(item);
}
bookmarkDao.deleteUserBookmark(userId, bookmarkIdList);
if (bookmarkIdList.size() > 0) {
bookmarkDao.deleteUserBookmark(userId, bookmarkIdList);
}
}
/**
@ -124,12 +127,12 @@ public class BookmarkService {
/**
* Description: 编辑某个用户的某个书签
*
* @param userId userId
* @param bookmark bookmark
* @author fanxb
* @date 2019/7/17 14:42
* @param userId userId
* @param bookmark bookmark
*/
public void updateOne(int userId,Bookmark bookmark){
public void updateOne(int userId, Bookmark bookmark) {
bookmark.setUserId(userId);
bookmarkDao.editBookmark(bookmark);
}
@ -193,4 +196,22 @@ public class BookmarkService {
}
}
@Transactional(rollbackFor = Exception.class)
public void moveNode(int userId, MoveNodeBody body) {
if (body.getSort() == -1) {
Integer max = bookmarkDao.selectMaxSort(userId, body.getTargetPath());
body.setSort(max == null ? 1 : max);
} else {
//更新目标节点的sort
bookmarkDao.sortPlus(userId, body.getTargetPath(), body.getSort());
}
//如果目标位置和当前位置不在一个层级中需要更新子节点的path
if (!body.getTargetPath().equals(body.getSourcePath())) {
bookmarkDao.updateChildrenPath(userId, body.getSourcePath() + "." + body.getBookmarkId()
, body.getTargetPath() + "." + body.getBookmarkId());
}
//更新被移动节点的path和sort
bookmarkDao.updatePathAndSort(userId, body.getBookmarkId(), body.getTargetPath(), body.getSort());
}
}

View File

@ -81,5 +81,23 @@
where bookmarkId = #{bookmarkId} and userId = #{userId}
</update>
<update id="sortPlus">
update bookmark
set sort = sort + 1
where userId = #{userId} and path = #{path} and sort &gt;= #{sort}
</update>
<update id="updateChildrenPath">
update bookmark
set path = replace(path, #{oldPath}, #{newPath})
where userId = #{userId} and path like concat(#{oldPath}, "%")
</update>
<update id="updatePathAndSort">
update bookmark
set path = #{path}, sort = #{sort}
where userId = #{userId} and bookmarkId = #{bookmarkId}
</update>
</mapper>