文件删除时并非真正的删除,将文件状态置为RECYCLE状态,如果是目录,将其中的文件置为DEL状态,目录则置为RECYCLE,回收站查询时只查询RECYCLE状态的文件和目录。

接口信息

路径地址 http://localhost:7090/api/file/delFile/{ids}
请求方式 GET
请求参数 ids
返回结果

定义Mapper

找到 FileInfoMapper 接口,在其中添加如下方法定义,批量更新文件的删除和回收状态

FileInfoMapper
Integer updateFileDelFlagBatch(@Param("bean") FileInfo fileInfo,
@Param("userId") String userId,
@Param("pidList") List<String> delFilePidList,
@Param("idList") List<String> idList,
@Param("oldDelFlag") Integer oldDelFlag);

找到 FileInfoMapper.xml 文件,实现上述方法

FileInfoMapper.xml
<update id="updateFileDelFlagBatch">
UPDATE file_info
<set>
<if test="bean.filePid != null">
file_pid = #{bean.filePid},
</if>
<if test="bean.recoveryTime != null">
recovery_time = #{bean.recoveryTime},
</if>
<if test="bean.deleted != null">
deleted = #{bean.deleted},
</if>
<if test="bean.updateTime != null">
update_time = #{bean.updateTime}
</if>
</set>
WHERE user_id = #{userId}
<if test="pidList != null">
and file_pid in(<foreach collection="pidList" separator="," item="item">#{item}</foreach>)
</if>
<if test="idList != null">
and id in(<foreach collection="idList" separator="," item="item">#{item}</foreach>)
</if>
<if test="oldDelFlag != null">
and deleted = #{oldDelFlag}
</if>
</update>

定义Service

找到 FileInfoService 接口,在其中添加:

FileInfoService
/**
* 将文件移入回收站
*
* @param userId 用户ID
* @param ids 文件IDS,逗号分隔
*/
void removeFile2RecycleBatch(String userId, String ids);

在 UserInfoServiceImpl 中实现该方法

FileInfoServiceImpl
/**
* 将文件移入回收站
*
* @param userId 用户ID
* @param ids 文件IDS,逗号分隔
*/
@Override
public void removeFile2RecycleBatch(String userId, String ids) {
// 查询文件是否已经在回收站
LambdaQueryWrapper<FileInfo> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(FileInfo::getUserId, userId)
.eq(FileInfo::getDeleted, FileDelFlagEnums.USING.getFlag())
.in(StringUtils.isNotEmpty(ids), FileInfo::getId, Arrays.asList(ids.split(",")));
List<FileInfo> dbFileList = list(wrapper);
if (dbFileList.isEmpty()) {
return;
}
// 递归删除文件
ArrayList<String> delFilePidList = new ArrayList<>();
for (FileInfo fileInfo : dbFileList) {
findAllSubFolderList(delFilePidList, userId, fileInfo.getId(), FileDelFlagEnums.USING.getFlag());
}
if (!delFilePidList.isEmpty()) {
// 目录下的文件,非选中文件直接删除,即deleted置为2
FileInfo delFileInfo = new FileInfo();
delFileInfo.setDeleted(FileDelFlagEnums.DEL.getFlag());
// 根据Pid删除文件,即删除目录下的所有文件
baseMapper.updateFileDelFlagBatch(delFileInfo, userId, delFilePidList, null, FileDelFlagEnums.USING.getFlag());
}
// 将选中的文件更新为回收站,即deleted置为1
List<String> recIds = Arrays.asList(ids.split(","));
FileInfo recFileInfo = new FileInfo();
recFileInfo.setDeleted(FileDelFlagEnums.RECYCLE.getFlag());
recFileInfo.setRecoveryTime(LocalDateTime.now());
baseMapper.updateFileDelFlagBatch(recFileInfo, userId, null, recIds, FileDelFlagEnums.USING.getFlag());
}

// 递归查找当前目录下的子目录
public void findAllSubFolderList(List<String> idList, String userId, String id, Integer delFlag) {
idList.add(id);
// 查找当前目录下的所有子目录
LambdaQueryWrapper<FileInfo> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(FileInfo::getUserId, userId)
.eq(FileInfo::getFilePid, id)
.eq(FileInfo::getDeleted, delFlag)
.eq(FileInfo::getFolderType, FileFolderTypeEnums.FOLDER.getType());
List<FileInfo> fileInfoList = list(wrapper);
for (FileInfo fileInfo : fileInfoList) {
findAllSubFolderList(idList, userId, fileInfo.getId(), delFlag);
}
}

定义Controller

在 FileInfoController 下定义删除文件方法,将文件移入回收站,不释放使用空间。

FileInfoController
// 删除文件
@DeleteMapping("/delFile/{ids}")
public void delFile(HttpSession session, @PathVariable("ids") @NotEmpty String ids) {
SessionWebUserVO user = (SessionWebUserVO) session.getAttribute(Constants.SESSION_KEY);
// 需要校验登陆状态
fileInfoService.removeFile2RecycleBatch(user.getId(), ids);
}