1、保存
public void saveOrganization(Organization org, int parentId, String parentPath) {
org.setParentId(parentId);
org.setPath(parentPath + "/" + org.getId());
organizationDao.save(org);
List<Organization> children = organizationDao.getChildrenById(parentId);
for (Organization child : children) {
saveOrganization(child, org.getId(), org.getPath());
}
}
2、变更了上级id,需要更新所有相关组织机构的上级id路径
public void updateParentPath(Organization org, int newParentId, String parentPath) {
String newPath = parentPath + "/" + org.getId();
if (!newPath.equals(org.getPath())) {
org.setParentId(newParentId);
org.setPath(newPath);
organizationDao.update(org);
List<Organization> children = organizationDao.getChildrenById(org.getId());
for (Organization child : children) {
updateParentPath(child, org.getId(), newPath);
}
}
}