文件删除时并非真正的删除,将文件状态置为RECYCLE
状态,如果是目录,将其中的文件置为DEL
状态,目录则置为RECYCLE
,回收站查询时只查询RECYCLE
状态的文件和目录。
接口信息
定义Mapper
找到 FileInfoMapper 接口,在其中添加如下方法定义,批量更新文件的删除和回收状态
FileInfoMapperInteger 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
void removeFile2RecycleBatch(String userId, String ids);
|
在 UserInfoServiceImpl 中实现该方法
FileInfoServiceImpl
@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()) { FileInfo delFileInfo = new FileInfo(); delFileInfo.setDeleted(FileDelFlagEnums.DEL.getFlag()); baseMapper.updateFileDelFlagBatch(delFileInfo, userId, delFilePidList, null, FileDelFlagEnums.USING.getFlag()); } 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); }
|