package org.jeecg.modules.doc.service.impl; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.RandomUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import org.apache.commons.lang3.StringUtils; import org.jeecg.common.constant.CacheConstant; import org.jeecg.common.util.DateUtils; import org.jeecg.modules.doc.component.DocFileDealComp; import org.jeecg.modules.doc.component.FileConstant; import org.jeecg.modules.doc.component.PathPermissionComp; import org.jeecg.modules.doc.constant.Constant; import org.jeecg.modules.doc.entity.DocFilePath; import org.jeecg.modules.doc.entity.RecoveryFile; import org.jeecg.modules.doc.mapper.DocFilePathMapper; import org.jeecg.modules.doc.mapper.RecoveryFileMapper; import org.jeecg.modules.doc.service.IDocFilePathService; import org.jeecg.modules.doc.service.IPathPermissionService; import org.jeecg.modules.doc.vo.QiwenFile; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.Executor; import java.util.concurrent.Executors; /** * @Description: doc_file_path * @Author: jeecg-boot * @Date: 2022-07-04 * @Version: V1.0 */ @Service public class DocFilePathServiceImpl extends ServiceImpl implements IDocFilePathService { @Resource RecoveryFileMapper recoveryFileMapper; @Autowired DocFileDealComp docFileDealComp; @Autowired private IPathPermissionService pathPermissionService; @Autowired private PathPermissionComp pathPermissionComp; public static Executor executor = Executors.newFixedThreadPool(20); @Override public void initDocFilePath() { DocFilePath filePath = new DocFilePath(); filePath.setPathStatus(0); baseMapper.update(filePath, new LambdaQueryWrapper().ne(DocFilePath::getPathStatus,0)); } @Override public List listFiles(Set pathIds) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if (pathIds != null && pathIds.size()>0) { queryWrapper.in(DocFilePath::getPathId, pathIds); } queryWrapper.eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE) .orderByAsc(DocFilePath::getFilePath) .orderByAsc(DocFilePath::getFileName); return baseMapper.selectList(queryWrapper); } /** * * @param docFilePath 接收权限分配时模糊查询的文件名参数 * @param pathIds 有访问权限的文件id集合 * @return */ @Override public List listDirTree(DocFilePath docFilePath, Set pathIds) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.and(i -> { i.apply("{0} = {1}", 1,2); if (pathIds != null && pathIds.size()>0) { i.or().in(DocFilePath::getPathId, pathIds); } }); if (docFilePath != null && StringUtils.isNotBlank(docFilePath.getFileName())) { queryWrapper.like(DocFilePath::getFileName, docFilePath.getFileName()); } queryWrapper.eq(DocFilePath::getIsDir,Constant.IS_DIR) .eq(DocFilePath::getDeleteFlag,Constant.DEL_FALSE) //.apply("{0} = {0}",UUID.randomUUID().toString()) .orderByAsc(DocFilePath::getFilePath) .orderByAsc(DocFilePath::getFileName); return baseMapper.selectList(queryWrapper); } @Override public void deleteUserFile(String userFileId, String sessionUserId) { DocFilePath userFile = baseMapper.selectById(userFileId); String uuid = UUID.randomUUID().toString(); if (userFile.getIsDir() == Constant.IS_DIR) { // 是目录,修改当前目录状态,后修改所有子文件删除状态 LambdaUpdateWrapper userFileLambdaUpdateWrapper = new LambdaUpdateWrapper(); userFileLambdaUpdateWrapper.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize)) .set(DocFilePath::getDeleteBatchNum, uuid) .set(DocFilePath::getDeleteTime, DateUtils.getDate()) .eq(DocFilePath::getPathId, userFileId); baseMapper.update(null, userFileLambdaUpdateWrapper); String filePath = new QiwenFile(userFile.getFilePath(), userFile.getFileName(), true).getPath(); updateFileDeleteStateByFilePath(filePath, uuid, sessionUserId); } else { // 是文件 DocFilePath userFileTemp = baseMapper.selectById(userFileId); LambdaUpdateWrapper userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>(); userFileLambdaUpdateWrapper.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(1, FileConstant.deleteFileRandomSize)) .set(DocFilePath::getDeleteTime, DateUtils.getDate()) .set(DocFilePath::getDeleteBatchNum, uuid) .eq(DocFilePath::getPathId, userFileTemp.getPathId()); baseMapper.update(null, userFileLambdaUpdateWrapper); } RecoveryFile recoveryFile = new RecoveryFile(); recoveryFile.setPathId(userFileId); recoveryFile.setDeleteTime(DateUtils.getDate()); recoveryFile.setDeleteBatchNum(uuid); recoveryFileMapper.insert(recoveryFile); } private void updateFileDeleteStateByFilePath(String filePath, String deleteBatchNum, String userId) { // executor.execute(() -> { // List fileList = selectUserFileByLikeRightFilePath(filePath); // for (int i = 0; i < fileList.size(); i++) { // DocFilePath userFileTemp = fileList.get(i); // //标记删除标志 // LambdaUpdateWrapper userFileLambdaUpdateWrapper1 = new LambdaUpdateWrapper<>(); // userFileLambdaUpdateWrapper1.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize)) // .set(DocFilePath::getDeleteTime, DateUtils.getDate()) // .set(DocFilePath::getDeleteBatchNum, deleteBatchNum) // .eq(DocFilePath::getPathId, userFileTemp.getPathId()) // .eq(DocFilePath::getDeleteFlag, 0); // baseMapper.update(null, userFileLambdaUpdateWrapper1); // // } // }); LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper<>(); lambdaUpdateWrapper.set(DocFilePath::getDeleteFlag, RandomUtil.randomInt(FileConstant.deleteFileRandomSize)) .set(DocFilePath::getDeleteTime, DateUtils.getDate()) .set(DocFilePath::getDeleteBatchNum, deleteBatchNum) .likeRight(DocFilePath::getFilePath,filePath) .eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE); baseMapper.update(null, lambdaUpdateWrapper); } @Override public List selectUserFileByLikeRightFilePath(String filePath) { return baseMapper.selectUserFileByLikeRightFilePath(filePath); } @Override public List selectUserFileByNameAndPath(String fileName, String filePath, String userId) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(DocFilePath::getFileName, fileName) .eq(DocFilePath::getFilePath, filePath) .eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE); return baseMapper.selectList(lambdaQueryWrapper); } @Override public void userFileCopy(String userFileId, String newfilePath) { DocFilePath userFile = baseMapper.selectById(userFileId); String oldfilePath = userFile.getFilePath(); String fileName = userFile.getFileName(); userFile.setFilePath(newfilePath); userFile.setPathId(IdUtil.getSnowflakeNextIdStr()); DocFilePath parent = docFileDealComp.getParent(newfilePath); userFile.setParentId(parent.getPathId()); if (userFile.getIsDir() == Constant.IS_NOT_DIR) { String repeatFileName = docFileDealComp.getRepeatFileName(userFile, userFile.getFilePath()); userFile.setFileName(repeatFileName); } baseMapper.insert(userFile); oldfilePath = new QiwenFile(oldfilePath, fileName, true).getPath(); newfilePath = new QiwenFile(newfilePath, fileName, true).getPath(); if (userFile.isDirectory()) { //为null说明是目录,则需要移动子目录 List subUserFileList = baseMapper.selectUserFileByLikeRightFilePath(oldfilePath); Map> subMap = new HashMap<>(); List childList = null; for (DocFilePath oldDfp: subUserFileList ) { childList = subMap.get(oldDfp.getParentId()); if(childList == null) { childList = new ArrayList<>(); } childList.add(oldDfp); subMap.put(oldDfp.getParentId(), childList); } copyChildren(subMap, userFileId, userFile.getPathId(),newfilePath,oldfilePath); // for (DocFilePath newUserFile : subUserFileList) { // newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(oldfilePath, newfilePath)); // newUserFile.setPathId(IdUtil.getSnowflakeNextIdStr()); // if (!newUserFile.isFile()) { // String repeatFileName = docFileDealComp.getRepeatFileName(newUserFile, newUserFile.getFilePath()); // newUserFile.setFileName(repeatFileName); // } // baseMapper.insert(newUserFile); // } } } private void copyChildren(Map> subMap, String oldParentId, String newParentId, String newfilePath, String oldfilePath) { List filePaths = subMap.get(oldParentId); if (filePaths == null) return; for (DocFilePath newUserFile: filePaths) { String pathId = newUserFile.getPathId(); newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(oldfilePath.replace("(","\\(").replace(")","\\)"), newfilePath)); newUserFile.setPathId(IdUtil.getSnowflakeNextIdStr()); newUserFile.setParentId(newParentId); if (!newUserFile.isFile()) { String repeatFileName = docFileDealComp.getRepeatFileName(newUserFile, newUserFile.getFilePath()); newUserFile.setFileName(repeatFileName); } baseMapper.insert(newUserFile); if (newUserFile.isDirectory()) { copyChildren(subMap, pathId, newUserFile.getPathId(),newfilePath,oldfilePath ); } } } @Override public void updateFilepathByUserFileId(String userFileId, String newfilePath) { DocFilePath userFile = baseMapper.selectById(userFileId); String oldfilePath = userFile.getFilePath(); String fileName = userFile.getFileName(); DocFilePath parent = docFileDealComp.getParent(newfilePath); userFile.setParentId(parent.getPathId()); userFile.setFilePath(newfilePath); if (userFile.getIsDir() == Constant.IS_NOT_DIR) { String repeatFileName = docFileDealComp.getRepeatFileName(userFile, userFile.getFilePath()); userFile.setFileName(repeatFileName); } baseMapper.updateById(userFile); //移动子目录 oldfilePath = new QiwenFile(oldfilePath, fileName, true).getPath(); newfilePath = new QiwenFile(newfilePath, fileName, true).getPath(); if (userFile.isDirectory()) { //为空说明是目录,则需要移动子目录 List list = selectUserFileByLikeRightFilePath(oldfilePath); for (DocFilePath newUserFile : list) { newUserFile.setFilePath(newUserFile.getFilePath().replaceFirst(oldfilePath.replace("(","\\(").replace(")","\\)"), newfilePath)); if (newUserFile.getIsDir() == Constant.IS_NOT_DIR) { String repeatFileName = docFileDealComp.getRepeatFileName(newUserFile, newUserFile.getFilePath()); newUserFile.setFileName(repeatFileName); } baseMapper.updateById(newUserFile); } } } @Override public boolean selectPermissionByPath(String pathId, Map manage) { if (pathId == null ) return false; DocFilePath docFilePath = baseMapper.selectById(pathId); // // Map> pathPermission = pathPermissionComp.getPathPermission(); // // // Map manage = pathPermission.get(Constant.MANAGE); //Map managePrem = pathPermissionComp.getSubPremMap(manage); QiwenFile qf = null; DocFilePath dfp = null; if (manage.containsKey(pathId)) { return true; } for (String str : manage.keySet() ) { dfp = manage.get(str); if (dfp == null) continue; try { qf = new QiwenFile(dfp.getFilePath(),dfp.getFileName(), true); } catch (Exception e) { System.out.println(); System.out.println(e.getMessage()); } if (docFilePath != null && docFilePath.getFilePath().startsWith(qf.getPath())) { return true; } } // // LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); // LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); // wrapper.eq(PathPermission::getPathId, pathId).eq(PathPermission::getUserId, loginUser.getId()).eq(PathPermission::getManage, Constant.HAVE); // List list = pathPermissionService.list(wrapper); // if (list!=null && list.size()>0) { // return true; // } return false; } @Override public List> querySubCount(String[] fileIds) { return baseMapper.querySubCount(fileIds); } @Override public List selectSameUserFile(String fileName, String filePath, String extendName) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(DocFilePath::getFileName, fileName) .eq(DocFilePath::getFilePath, filePath) .eq(DocFilePath::getExtendName, extendName) .eq(DocFilePath::getDeleteFlag, "0"); return baseMapper.selectList(lambdaQueryWrapper); } @Override @Cacheable(value = CacheConstant.DOC_FILE_PATH_CACHE,key = "#parentFilePath+':'+#fileName", unless = "#result == null || #result.size() < 1") public List getDocFilePathsByPathName(String parentFilePath, String fileName) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(DocFilePath::getFilePath, parentFilePath) .eq(DocFilePath::getFileName, fileName) .eq(DocFilePath::getDeleteFlag, Constant.DEL_FALSE) .eq(DocFilePath::getIsDir, Constant.IS_DIR); List userFileList = baseMapper.selectList(lambdaQueryWrapper); return userFileList; } }