优化组织机构带用户加载树算法

This commit is contained in:
wy
2022-01-13 20:04:08 +08:00
parent bd71d5126b
commit 870976f6cf
2 changed files with 25 additions and 21 deletions

View File

@@ -264,8 +264,8 @@ public class OfficeController extends BaseController {
/** /**
* 获取机构树结构数据 * 获取机构树结构数据
* @param excludeCode 排除的ID * @param excludeCode 排除的ID
* @param parentCode 上级Code * @param parentCode 设置父级编码返回一级
* @param isAll 是否显示所有机构true不进行权限过滤 * @param isAll 是否显示所有机构true不进行权限过滤
* @param officeTypes 机构类型1省级公司2市级公司3部门 * @param officeTypes 机构类型1省级公司2市级公司3部门
* @param companyCode 仅查询公司下的机构 * @param companyCode 仅查询公司下的机构
@@ -281,7 +281,7 @@ public class OfficeController extends BaseController {
@ResponseBody @ResponseBody
public List<Map<String, Object>> treeData(String excludeCode, String parentCode, Boolean isAll, public List<Map<String, Object>> treeData(String excludeCode, String parentCode, Boolean isAll,
String officeTypes, String companyCode, String isShowCode, String isShowFullName, String officeTypes, String companyCode, String isShowCode, String isShowFullName,
String isLoadUser, String postCode, String roleCode, String ctrlPermi) { String isLoadUser, String userIdPrefix, String postCode, String roleCode, String ctrlPermi) {
List<Map<String, Object>> mapList = ListUtils.newArrayList(); List<Map<String, Object>> mapList = ListUtils.newArrayList();
Office where = new Office(); Office where = new Office();
where.setStatus(Office.STATUS_NORMAL); where.setStatus(Office.STATUS_NORMAL);
@@ -297,6 +297,7 @@ public class OfficeController extends BaseController {
if (StringUtils.isNotBlank(officeTypes)){ if (StringUtils.isNotBlank(officeTypes)){
where.setOfficeType_in(officeTypes.split(",")); where.setOfficeType_in(officeTypes.split(","));
} }
List<String> idList = ListUtils.newArrayList();
List<Office> list = officeService.findList(where); List<Office> list = officeService.findList(where);
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
Office e = list.get(i); Office e = list.get(i);
@@ -313,6 +314,7 @@ public class OfficeController extends BaseController {
continue; continue;
} }
} }
idList.add(e.getId());
Map<String, Object> map = MapUtils.newHashMap(); Map<String, Object> map = MapUtils.newHashMap();
map.put("id", e.getId()); map.put("id", e.getId());
map.put("pId", e.getParentCode()); map.put("pId", e.getParentCode());
@@ -323,23 +325,21 @@ public class OfficeController extends BaseController {
map.put("code", e.getViewCode()); map.put("code", e.getViewCode());
map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getViewCode(), name)); map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getViewCode(), name));
map.put("title", e.getFullName()); map.put("title", e.getFullName());
// 如果需要加载用户,则处理用户数据 // 返回是否是父节点,如果需要加载用户,则全部都是父节点,来加载用户数据
if (StringUtils.inString(isLoadUser, "true", "lazy")) { map.put("isParent", !e.getIsTreeLeaf() || StringUtils.inString(isLoadUser, "true", "lazy"));
map.put("isParent", true);
// 一次性后台加载用户,若数据量比较大,建议使用懒加载
if (StringUtils.equals(isLoadUser, "true")) {
List<Map<String, Object>> userList =
empUserController.treeData("u_", e.getOfficeCode(), e.getOfficeCode(),
companyCode, postCode, roleCode, isAll, isShowCode, ctrlPermi);
mapList.addAll(userList);
}
}
mapList.add(map); mapList.add(map);
} }
// 一次性后台加载用户,若数据量比较大,建议使用懒加载
if (StringUtils.equals(isLoadUser, "true") && idList.size() > 0) {
List<Map<String, Object>> userList =
empUserController.treeData(userIdPrefix, idList.toArray(new String[idList.size()]),
companyCode, postCode, roleCode, isAll, isShowCode, ctrlPermi);
mapList.addAll(userList);
}
// 懒加载用户,点击叶子节点的时候再去加载部门(懒加载无法回显,数据量大时,建议使用 listselect 实现列表选择用户) // 懒加载用户,点击叶子节点的时候再去加载部门(懒加载无法回显,数据量大时,建议使用 listselect 实现列表选择用户)
if (StringUtils.inString(isLoadUser, "lazy") && StringUtils.isNotBlank(parentCode)) { if (StringUtils.inString(isLoadUser, "lazy") && StringUtils.isNotBlank(parentCode)) {
List<Map<String, Object>> userList = List<Map<String, Object>> userList =
empUserController.treeData("u_", parentCode, parentCode, empUserController.treeData(userIdPrefix, new String[]{parentCode},
companyCode, postCode, roleCode, isAll, isShowCode, ctrlPermi); companyCode, postCode, roleCode, isAll, isShowCode, ctrlPermi);
mapList.addAll(userList); mapList.addAll(userList);
} }

View File

@@ -33,6 +33,7 @@ import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.config.Global; import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page; import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils; import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils; import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.shiro.realm.AuthorizingRealm; import com.jeesite.common.shiro.realm.AuthorizingRealm;
import com.jeesite.common.utils.excel.ExcelExport; import com.jeesite.common.utils.excel.ExcelExport;
@@ -381,7 +382,6 @@ public class EmpUserController extends BaseController {
/** /**
* 根据机构查询用户树格式 * 根据机构查询用户树格式
* @param idPrefix id前缀默认 u_ * @param idPrefix id前缀默认 u_
* @param pId 父级编码,默认 0
* @param officeCode 机构Code * @param officeCode 机构Code
* @param companyCode 公司Code * @param companyCode 公司Code
* @param postCode 岗位Code * @param postCode 岗位Code
@@ -393,13 +393,17 @@ public class EmpUserController extends BaseController {
@RequiresPermissions("user") @RequiresPermissions("user")
@RequestMapping(value = "treeData") @RequestMapping(value = "treeData")
@ResponseBody @ResponseBody
public List<Map<String, Object>> treeData(String idPrefix, String pId, public List<Map<String, Object>> treeData(String idPrefix,
String officeCode, String companyCode, String postCode, String roleCode, String[] officeCode, String companyCode, String postCode, String roleCode,
Boolean isAll, String isShowCode, String ctrlPermi) { Boolean isAll, String isShowCode, String ctrlPermi) {
List<Map<String, Object>> mapList = ListUtils.newArrayList(); List<Map<String, Object>> mapList = ListUtils.newArrayList();
EmpUser empUser = new EmpUser(); EmpUser empUser = new EmpUser();
Employee employee = empUser.getEmployee(); Employee employee = empUser.getEmployee();
employee.getOffice().setOfficeCode(officeCode); if (officeCode != null && officeCode.length == 1) {
employee.getOffice().setOfficeCode(officeCode[0]);
}else {
employee.getOffice().setId_in(officeCode);
}
employee.getOffice().setIsQueryChildren(false); employee.getOffice().setIsQueryChildren(false);
employee.getCompany().setCompanyCode(companyCode); employee.getCompany().setCompanyCode(companyCode);
employee.getCompany().setIsQueryChildren(false); employee.getCompany().setIsQueryChildren(false);
@@ -414,8 +418,8 @@ public class EmpUserController extends BaseController {
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
EmpUser e = list.get(i); EmpUser e = list.get(i);
Map<String, Object> map = MapUtils.newHashMap(); Map<String, Object> map = MapUtils.newHashMap();
map.put("id", StringUtils.defaultIfBlank(idPrefix, "u_") + e.getId()); map.put("id", ObjectUtils.defaultIfNull(idPrefix, "u_") + e.getId());
map.put("pId", StringUtils.defaultIfBlank(pId, "0")); map.put("pId", StringUtils.defaultIfBlank(e.getEmployee().getOffice().getOfficeCode(), "0"));
map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getLoginCode(), e.getUserName())); map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getLoginCode(), e.getUserName()));
mapList.add(map); mapList.add(map);
} }