树形文件夹创建、递归删除、锁定

创建文件夹需要的字段

文件夹类:

//文件夹id
private String dirId;

//文件夹名称
private String dirName;

//文件夹上级id
private String parentId;

//菜单类型名称
private String categoryName;

//创建文件或者文件夹的项目uuid
private String projectUuid;

//是否被锁定
private boolean locked;

//创建用户
private Account creator;

目录类:

//目录节点id
private String treeId;

//目录名
private String treename;

//目录父节点id
private String parentId;

//目录类型,是文件还是目录
private DataTreeType dataTreeType;

//子节点对象
private List<DirTree> children;

//子文件
private List<Assets> assetsList;

//是否被锁定
private boolean locked;

//返回类型
public enum DataTreeType {
    DIR,
    FILE;
}

功能性代码impl

@Autowired
private FolderMapper folderMapper;
@Autowired
private AssetsMapper assetsMapper;
@Autowired
private AssetsVersionMapper assetsVersionMapper;
@Autowired
private ProjectMapper projectMapper;

/**
 *@Author: mahongfei
 *@description: 将文件夹信息添加到数据库
 */
@Override
public void addFolder(Folder folder) {
    if (StringUtil.isBlank(folder.getDirId())) {
        throw new IllegalStateException("文件夹id为空");
	}
	if (!folder.getDirId().matches("^[0-9]*$")){
	    throw new IllegalStateException("文件夹id只能是数字");
    }
	if (StringUtil.isBlank(folder.getDirName())) {
        throw new IllegalStateException("文件夹名称为空");
	}
	if (StringUtil.isBlank(folder.getParentId())) {
        folder.setParentId("0");
	}
	if (folder.getDirId().equals("1") && !folder.getParentId().equals("0")) {
	    throw new IllegalStateException("根目录父类id应该为0");
    }
	if (StringUtil.isBlank(folder.getCategoryName())) {
        throw new IllegalStateException("菜单分类名称为空");
	}
	if (StringUtil.isBlank(folder.getProjectUuid())) {
        throw new IllegalStateException("项目id为空");
	}

    try {
        folder.setUuid(IdUtil.UUID());
        folder.setCreator(SessionHolder.currentAccount());
        folderMapper.insert(folder);
    } catch (IllegalStateException e) {
        logger.error(folder + e.getLocalizedMessage(), e);
        throw new IllegalStateException("文件夹添加失败");
    }catch (DuplicateKeyException e) {
        throw new IllegalArgumentException("文件夹名称不能重复");
	}
}

/**
 *@Author: mahongfei
 *@description: 文件夹列表
 */
@Override
public List<Object> listAll(String projectUuid) {
	if (StringUtil.isBlank(projectUuid)) {
		throw new IllegalArgumentException("项目uuid为空!");
	}

	List<Object> list = new ArrayList<>();
	List<String> types = new ArrayList<>();
    types.add(AssetsType.FACT);
    types.add(AssetsType.CONSTANT);
    types.add(AssetsType.GUIDED_RULE);
    types.add(AssetsType.RULE_TABLE);
    types.add(AssetsType.RULE_TREE);
    types.add(AssetsType.SCORECARD);
    types.add(AssetsType.RULE_FLOW);
	for (String categoryName : types) {
	    Map<String, Object> map = new HashMap<>();
        List<DirTree> dirTreeList = new ArrayList<>();
        map.put("type", categoryName);
        List<String> dirIdList = folderMapper.listDirId(projectUuid, categoryName);
        if (!dirIdList.isEmpty()) {
            String lastdirId = dirIdList.get(dirIdList.size()-1);
            dirTreeList.addAll(getchildrenDataTree(1, lastdirId, projectUuid, categoryName, "0"));
        }

        List<Assets> assetsList = assetsMapper.selectAssertByParentId("0", projectUuid, categoryName,
                 SessionHolder.hasAdminPermission(), SessionHolder.currentAccount().getId());
        if (!assetsList.isEmpty()) {
            DirTree dirTree = new DirTree();
            dirTree.setDataTreeType(DirTree.DataTreeType.FILE);
            dirTree.setAssetsList(assetsList);
            dirTreeList.add(dirTree);
        }
        map.put("list", dirTreeList);
        list.add(map);
    }
	return list;
}

/**
 *@Author: mahongfei
 *@description: 获取子目录
 */
public List<DirTree> getchildrenDataTree(int firstDirId,
                                         String dirLastId,
                                         String projectUuid,
                                         String categoryName,
                                         String dirParentId) {
    List<DirTree> result = new ArrayList<>();
    String dirId = String.valueOf(firstDirId);
    List<Folder> folderList = folderMapper.listAllByDirId(dirId, projectUuid, categoryName, dirParentId);
    for (Folder folder : folderList) {
        DirTree dirTree = new DirTree();
        dirTree.setTreeId(folder.getDirId());
        dirTree.setUuid(folder.getUuid());
        dirTree.setParentId(folder.getParentId());
        dirTree.setTreename(folder.getDirName());
        dirTree.setLocked(folder.isLocked());
        dirTree.setDataTreeType(DirTree.DataTreeType.DIR);
        dirParentId = dirTree.getUuid();
        List<DirTree> childrenDir = new ArrayList<>();
        if (firstDirId != Integer.parseInt(dirLastId)) {
            childrenDir = getchildrenDataTree(firstDirId + 1, dirLastId, projectUuid, categoryName, dirParentId);
        }

        List<DirTree> childrenDataSet = getChildrenData(dirTree, projectUuid, categoryName);
        List<DirTree> children = new ArrayList<>();
        if (!childrenDir.isEmpty()) {
            children.addAll(childrenDir);
        }
        if (!childrenDataSet.isEmpty()) {
            children.addAll(childrenDataSet);
        }
        dirTree.setChildren(children);
        result.add(dirTree);
    }

    return result;
}

/**
 *@Author: mahongfei
 *@description: 获取目录树下的数据集
 */
public List<DirTree> getChildrenData(DirTree dirTree, String projectUuid, String categoryName) {
    //获取指定目录节点下所有文件
    List<Assets> assetsList = assetsMapper.selectAssertByParentId(dirTree.getUuid(), projectUuid, categoryName,
                SessionHolder.hasAdminPermission(), SessionHolder.currentAccount().getId());
    List<DirTree> dirAssetsList = new ArrayList<>();
    if (!assetsList.isEmpty()) {
        DirTree child = new DirTree();
        child.setDataTreeType(DirTree.DataTreeType.FILE);
        child.setAssetsList(assetsList);
        dirAssetsList.add(child);
    }
    return dirAssetsList;
}

/**
 *@Author: mahongfei
 *@description: 编辑文件夹
 */
 @Override
public void editFolder(Folder folder) {
    if (StringUtil.isBlank(folder.getUuid())) {
        throw new IllegalArgumentException("uuid不能为空");
    }
    if (StringUtil.isBlank(folder.getDirName()) || folder.getDirName().length() > 20) {
        throw new IllegalArgumentException("名称不能为空并且长度不能超过20个字符");
    }

    try {
         Folder folder1 = folderMapper.selectByUuid(folder.getUuid());
         if (null != folder1 && !folder1.isLocked()) {
             folderMapper.update(folder);
         } else {
             throw new IllegalArgumentException("uuid不存在或者被锁定");
         }
    } catch (DuplicateKeyException e) {
        throw new IllegalArgumentException("名称不能重复");
    } catch (Exception e) {
        logger.error(folder + "\n" + e.getLocalizedMessage(), e);
        throw new IllegalArgumentException("修改失败");
    }
}

/**
 *@Author: mahongfei
 *@description: 检查文件夹是否有锁定
 */
public boolean deleteFolderCheck(String uuid) {
    Folder folder = folderMapper.selectByUuid(uuid);
    if (deleteDirCheck(uuid)){
        if (deleteAssetsCheck(uuid)) {
            if (folder.isLocked()) {
                logger.error("文件夹:" + folder.getUuid() +"被锁定不能删除");
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
    return true;
}

/**
 *@Author: mahongfei
 *@description: 遍历子目录文件夹是否有锁定
 */
public boolean deleteDirCheck(String parentId) {
    boolean a = true;
    List<Folder> folderList = folderMapper.selectByParentId(parentId);
    if (!folderList.isEmpty()) {
        for (Folder folder : folderList) {
            if (folder.isLocked()) {
                a = false;
            } else {
                parentId = folder.getUuid();
                a = deleteDirCheck(parentId);
            }
            if (!deleteAssetsCheck(folder.getUuid())) {
                a = false;
            }
            if (!a) {
                break;
            }
         }
    }
    return a;
}

/**
 *@Author: mahongfei
 *@description: 遍历文件是否有锁定
 */
public boolean deleteAssetsCheck(String uuid) {
    boolean a = true;
    List<Assets> assetsList = assetsMapper.selectByParentId(uuid);
    if (!assetsList.isEmpty()) {
        for (Assets assets : assetsList) {
            String assetUuid = assets.getUuid();
            if (assets.isLocked()) {
                logger.error("文件:" + assetUuid +"被锁定不能删除");
                a = false;
                break;
            }
        }
    }
    return a;
}


/**
 *@Author: mahongfei
 *@description: 删除文件夹
 */
public void deleteFolder(String uuid) {
    if (StringUtil.isBlank(uuid)) {
        throw new IllegalArgumentException("uuid不能为空");
    }

    if (deleteFolderCheck(uuid)) {
        try {
            if (deleteDir(uuid)) {
                if (deleteAssets(uuid)) {
                    folderMapper.delete(new Folder(uuid));
                }
            }
        } catch (Exception e) {
            logger.error(uuid + "\n" + e.getLocalizedMessage(), e);
            throw new IllegalStateException("删除失败");
        }
    } else{
        throw new IllegalStateException("文件夹或者文件被锁定不能删除!");
    }
}

/**
 *@Author: mahongfei
 *@description: 删除目录中的文件
 */
public boolean deleteAssets(String uuid) {
    List<Assets> assetsList = assetsMapper.selectByParentId(uuid);
    if (!assetsList.isEmpty()) {
        for (Assets assets : assetsList) {
            String assetUuid = assets.getUuid();
            try {
                assetsMapper.deleteByDirParentId(uuid);
                //同步删除文件的版本记录
                assetsVersionMapper.deleteAllByAsset(assetUuid);
                projectMapper.updateFileNum(assets.getProjectUuid(), -1);
            } catch (Exception e) {
                logger.error(assetUuid + e.getLocalizedMessage(), e);
                throw new IllegalStateException("删除资源文件失败");
            }
        }
    }
    return true;
}

/**
 *@Author: mahongfei
 *@description: 删除子目录
 */
public boolean deleteDir(String parentId) {
    List<Folder> folderList = folderMapper.selectByParentId(parentId);
    if (!folderList.isEmpty()) {
        for (Folder folder1 : folderList) {
            deleteAssets(folder1.getUuid());
            if (deleteAssets(folder1.getUuid())) {
                try {
                    folderMapper.delete(new Folder(folder1.getUuid()));
                } catch (Exception e) {
                    logger.error(folder1.getUuid() + "\n" + e.getLocalizedMessage(), e);
                    throw new IllegalStateException("删除子目录失败");
                }
            }
            String parentId1 = folder1.getUuid();
            List<Folder> folderList1 = folderMapper.selectByParentId(parentId1);
            if (null != folderList1 && !folderList1.isEmpty()) {
                deleteDir(parentId1);
            }
        }
    }
    return true;
}

/**
 *@Author: mahongfei
 *@description: 锁定/解锁文件夹
 */
@Override
public Result lockFolder(String uuid, Boolean isLock) {
    if (StringUtil.isBlank(uuid)) {
        throw new IllegalArgumentException("uuid不能为空");
    }
    if (isLock == null) {
        throw new IllegalArgumentException("isLock不能为空");
    }

    try {
        Account account = SessionHolder.currentAccount();
        //测试模拟已登录用户
       /* Account account = new Account();
        account.setUuid("446eed1aa587442490b079479a7028a7");
        account.setAdmin(true);*/
        if (!account.isAdmin()) {
            Folder lockInfo = folderMapper.selectLockInfo(uuid);
            if (lockInfo == null) {
                return Result.newError().withMsg("查询信息为空");
            }
            if (!lockInfo.getCreator().getUuid().equals(account.getUuid())) {
                return Result.newError().withMsg("只有文件夹的创建人和管理员才可以锁定和解锁文件夹!");
            }
        }
        folderMapper.updateLock(uuid, isLock);
        return Result.newSuccess();
    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(),"文件夹:" + uuid + "锁操作出错");
        throw new IllegalStateException("操作失败");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43812065/article/details/89848436