feat(recycle-bin): 实现回收站功能
- 在数据库中添加逻辑删除字段和相关索引- 新增回收站相关实体类和接口 - 实现回收站列表查询、项目恢复、永久删除和清空回收站等功能 - 前端集成回收站接口,支持回收站页面操作
This commit is contained in:
@@ -3,6 +3,7 @@ package com.test.bijihoudaun.config;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -10,11 +11,12 @@ import org.springframework.context.annotation.Configuration;
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 添加分页插件
|
||||
* 添加分页插件和逻辑删除插件
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
// 分页插件
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQLITE));
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.TrashItemVo;
|
||||
import com.test.bijihoudaun.service.TrashService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/trash")
|
||||
@Tag(name = "回收站管理")
|
||||
public class TrashController {
|
||||
|
||||
@Autowired
|
||||
private TrashService trashService;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "获取回收站列表")
|
||||
public R<List<TrashItemVo>> getTrashItems() {
|
||||
return R.success(trashService.getTrashItems());
|
||||
}
|
||||
|
||||
@PostMapping("/restore/{type}/{id}")
|
||||
@Operation(summary = "恢复项目")
|
||||
public R<Void> restoreItem(@PathVariable String type, @PathVariable String id) {
|
||||
trashService.restoreItem(id, type);
|
||||
return R.success();
|
||||
}
|
||||
|
||||
@DeleteMapping("/permanently/{type}/{id}")
|
||||
@Operation(summary = "永久删除项目")
|
||||
public R<Void> permanentlyDeleteItem(@PathVariable String type, @PathVariable String id) {
|
||||
trashService.permanentlyDeleteItem(id, type);
|
||||
return R.success();
|
||||
}
|
||||
|
||||
@DeleteMapping("/clean")
|
||||
@Operation(summary = "清空回收站")
|
||||
public R<Void> cleanTrash() {
|
||||
trashService.cleanTrash();
|
||||
return R.success();
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,19 @@ package com.test.bijihoudaun.entity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Schema(name = "分组实体")
|
||||
@TableName("grouping")
|
||||
public class Grouping {
|
||||
public class Grouping implements Serializable {
|
||||
@Schema(description = "分组id",implementation = Long.class)
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段
|
||||
@@ -24,4 +28,14 @@ public class Grouping {
|
||||
|
||||
@Schema(description = "分组名称",implementation = String.class)
|
||||
private String grouping;
|
||||
|
||||
@Schema(description = "是否删除 0-未删除 1-已删除", implementation = Integer.class)
|
||||
@TableLogic
|
||||
private Integer isDeleted;
|
||||
|
||||
@Schema(description = "删除时间", implementation = Date.class)
|
||||
private Date deletedAt;
|
||||
|
||||
@Schema(description = "删除人ID", implementation = Long.class)
|
||||
private Long deletedBy;
|
||||
}
|
||||
|
||||
@@ -3,17 +3,19 @@ package com.test.bijihoudaun.entity;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Schema(name = "文本实体")
|
||||
@TableName("markdown_file")
|
||||
public class MarkdownFile {
|
||||
public class MarkdownFile implements Serializable {
|
||||
@Schema(description = "文本id",implementation = Long.class)
|
||||
@TableId(type = IdType.AUTO)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段
|
||||
@@ -33,4 +35,14 @@ public class MarkdownFile {
|
||||
private Date createdAt;
|
||||
@Schema(description = "更新时间",implementation = Date.class)
|
||||
private Date updatedAt;
|
||||
|
||||
@Schema(description = "是否删除 0-未删除 1-已删除", implementation = Integer.class)
|
||||
@TableLogic
|
||||
private Integer isDeleted;
|
||||
|
||||
@Schema(description = "删除时间", implementation = Date.class)
|
||||
private Date deletedAt;
|
||||
|
||||
@Schema(description = "删除人ID", implementation = Long.class)
|
||||
private Long deletedBy;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.test.bijihoudaun.entity;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Schema(name = "回收站项目视图对象")
|
||||
public class TrashItemVo {
|
||||
|
||||
@Schema(description = "项目ID")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "项目名称(笔记标题或分组名称)")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "项目类型(note 或 group)")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "删除时间")
|
||||
private Date deletedAt;
|
||||
|
||||
@Schema(description = "删除者ID")
|
||||
private String deletedBy;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.test.bijihoudaun.service;
|
||||
|
||||
import com.test.bijihoudaun.entity.TrashItemVo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TrashService {
|
||||
|
||||
/**
|
||||
* 获取回收站中的所有项目
|
||||
* @return 回收站项目列表
|
||||
*/
|
||||
List<TrashItemVo> getTrashItems();
|
||||
|
||||
/**
|
||||
* 恢复指定的回收站项目
|
||||
* @param id 项目ID
|
||||
* @param type 项目类型 ("note" 或 "group")
|
||||
*/
|
||||
void restoreItem(String id, String type);
|
||||
|
||||
/**
|
||||
* 永久删除指定的回收站项目
|
||||
* @param id 项目ID
|
||||
* @param type 项目类型 ("note" 或 "group")
|
||||
*/
|
||||
void permanentlyDeleteItem(String id, String type);
|
||||
|
||||
/**
|
||||
* 清空回收站
|
||||
*/
|
||||
void cleanTrash();
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.test.bijihoudaun.entity.Grouping;
|
||||
import com.test.bijihoudaun.entity.MarkdownFile;
|
||||
import com.test.bijihoudaun.entity.TrashItemVo;
|
||||
import com.test.bijihoudaun.mapper.GroupingMapper;
|
||||
import com.test.bijihoudaun.mapper.MarkdownFileMapper;
|
||||
import com.test.bijihoudaun.service.TrashService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
public class TrashServiceImpl implements TrashService {
|
||||
|
||||
@Autowired
|
||||
private MarkdownFileMapper markdownFileMapper;
|
||||
|
||||
@Autowired
|
||||
private GroupingMapper groupingMapper;
|
||||
|
||||
@Override
|
||||
public List<TrashItemVo> getTrashItems() {
|
||||
// 查询已删除的笔记
|
||||
List<TrashItemVo> deletedNotes = markdownFileMapper.selectList(new QueryWrapper<MarkdownFile>().eq("is_deleted", 1))
|
||||
.stream()
|
||||
.map(file -> {
|
||||
TrashItemVo vo = new TrashItemVo();
|
||||
vo.setId(String.valueOf(file.getId()));
|
||||
vo.setName(file.getTitle());
|
||||
vo.setType("note");
|
||||
vo.setDeletedAt(file.getDeletedAt());
|
||||
vo.setDeletedBy(String.valueOf(file.getDeletedBy()));
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询已删除的分组
|
||||
List<TrashItemVo> deletedGroups = groupingMapper.selectList(new QueryWrapper<Grouping>().eq("is_deleted", 1))
|
||||
.stream()
|
||||
.map(group -> {
|
||||
TrashItemVo vo = new TrashItemVo();
|
||||
vo.setId(String.valueOf(group.getId()));
|
||||
vo.setName(group.getGrouping());
|
||||
vo.setType("group");
|
||||
vo.setDeletedAt(group.getDeletedAt());
|
||||
vo.setDeletedBy(String.valueOf(group.getDeletedBy()));
|
||||
return vo;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 合并并返回
|
||||
return Stream.concat(deletedNotes.stream(), deletedGroups.stream()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void restoreItem(String id, String type) {
|
||||
if ("note".equals(type)) {
|
||||
MarkdownFile file = new MarkdownFile();
|
||||
file.setId(Long.parseLong(id));
|
||||
file.setIsDeleted(0);
|
||||
file.setDeletedAt(null);
|
||||
file.setDeletedBy(null);
|
||||
markdownFileMapper.updateById(file);
|
||||
} else if ("group".equals(type)) {
|
||||
Grouping group = new Grouping();
|
||||
group.setId(Long.parseLong(id));
|
||||
group.setIsDeleted(0);
|
||||
group.setDeletedAt(null);
|
||||
group.setDeletedBy(null);
|
||||
groupingMapper.updateById(group);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void permanentlyDeleteItem(String id, String type) {
|
||||
if ("note".equals(type)) {
|
||||
markdownFileMapper.deleteById(Long.parseLong(id));
|
||||
} else if ("group".equals(type)) {
|
||||
// 删除分组时,也删除其下的所有笔记
|
||||
groupingMapper.deleteById(Long.parseLong(id));
|
||||
markdownFileMapper.delete(new QueryWrapper<MarkdownFile>().eq("grouping_id", id));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void cleanTrash() {
|
||||
markdownFileMapper.delete(new QueryWrapper<MarkdownFile>().eq("is_deleted", 1));
|
||||
groupingMapper.delete(new QueryWrapper<Grouping>().eq("is_deleted", 1));
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: org.sqlite.JDBC
|
||||
# url: jdbc:sqlite:C:\it\houtaigunli\biji\mydatabase.db
|
||||
url: jdbc:sqlite:C:\KAIFA\2\mydatabase.db
|
||||
url: jdbc:sqlite:C:\it\houtaigunli\biji\mydatabase.db
|
||||
# url: jdbc:sqlite:C:\KAIFA\2\mydatabase.db
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: none
|
||||
|
||||
@@ -29,6 +29,11 @@ mybatis-plus:
|
||||
mapper-locations: classpath:mapper/*.xml
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
global-config:
|
||||
db-config:
|
||||
logic-delete-field: isDeleted # 全局逻辑删除的实体字段名
|
||||
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
|
||||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
||||
|
||||
# JWT 配置
|
||||
jwt:
|
||||
|
||||
Reference in New Issue
Block a user