package com.product.org.admin.service; import com.alibaba.fastjson.JSONArray; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.product.admin.service.OrganizationCacheService; import com.product.common.collect.SetUtils; import com.product.common.lang.StringUtils; import com.product.core.cache.DataPoolCacheImpl; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.module.sys.entity.SystemUser; import com.product.org.admin.config.CmnConst; import com.product.org.admin.config.SystemCode; import com.product.org.admin.service.idel.IOrganizationService; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; import java.util.Set; /** * Copyright © 6c * * @Date: 2021-03-18 14:45 * @Author: 6c * @Description: */ @Component public class OrganizationService extends AbstractBaseService implements IOrganizationService { @Autowired public BaseDao baseDao; /** * 获取组织架构 * * @param fse 传入的fse参数 * @return JSONArray */ @Override public JSONArray getOrganization(FieldSetEntity fse) { boolean showCurUserFlag = "1".equals(fse.getString("is_show_cur_user_info")); Integer typeValue = fse.getInteger("type"); int type; if (typeValue != null) { type = typeValue; } else { throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText() + ":organization-type"); } Map> resultMap = Maps.newLinkedHashMap(); perfectData(fse, type, showCurUserFlag); getOrganizationInfo(resultMap, fse, type); DataTableEntity aimDte = new DataTableEntity(); dealSpMap2Dte(aimDte, resultMap, CmnConst.COMPANYS); dealSpMap2Dte(aimDte, resultMap, CmnConst.DEPTS); dealSpMap2Dte(aimDte, resultMap, CmnConst.POSTS); dealSpMap2Dte(aimDte, resultMap, CmnConst.STAFFS); return BaseUtil.dataTableToTreeData(aimDte, "sid", "pid", null, false); } /** * 完善数据-自下而上 * * @param fse 参数fse * @param type 类型:1-公司,2-公司-部门,3-公司-部门-岗位,4-公司-部门-岗位-人员,5-公司-部门-人员 */ private void perfectData(FieldSetEntity fse, int type, boolean showCurUserFlag) { SystemUser curUser = SpringMVCContextHolder.getCurrentUser(); String curUUID; Set perfectCompanySet = Sets.newHashSet(); Set perfectDeptSet = Sets.newHashSet(); Set perfectPostSet = Sets.newHashSet(); String companyUUIDs = fse.getString("org_level_uuids"); Set companyUUIDSet = SetUtils.string2Set(companyUUIDs, ","); String deptUUIDs = fse.getString("dept_uuids"); Set deptUUIDSet = SetUtils.string2Set(deptUUIDs, ","); String postUUIDs = fse.getString("post_uuids"); Set postUUIDSet = SetUtils.string2Set(postUUIDs, ","); String userIDs = fse.getString("user_ids"); Set userIDSet = SetUtils.string2Set(userIDs, ","); if (showCurUserFlag) { curUUID = curUser.getOrg_level_uuid(); if (!StringUtils.isEmpty(curUUID) && !companyUUIDSet.contains(curUUID)) { companyUUIDSet.add(curUUID); perfectCompanySet.add(curUUID); } curUUID = curUser.getDept_uuid(); if (!StringUtils.isEmpty(curUUID) && !deptUUIDSet.contains(curUUID)) { deptUUIDSet.add(curUUID); perfectDeptSet.add(curUUID); } curUUID = curUser.getPost(); if (!StringUtils.isEmpty(curUUID) && !postUUIDSet.contains(curUUID)) { postUUIDSet.add(curUUID); perfectPostSet.add(curUUID); } int userID = curUser.getUser_id(); curUUID = userID > 0 ? String.valueOf(userID) : ""; if (!StringUtils.isEmpty(curUUID) && !postUUIDSet.contains(curUUID)) { userIDSet.add(curUUID); } } if (type == 5) { // 公司-部门-人员 perfectDeptSet.addAll(perfectData(deptUUIDSet, userIDSet, "部门-人员", "dept_uuid")); fse.setValue("perfect_dept", perfectDeptSet); } if (type == 4) { // 公司-部门-岗位-人员 perfectPostSet.addAll(perfectData(postUUIDSet, userIDSet, "岗位-人员", "job_post_uuid")); fse.setValue("perfect_post", perfectPostSet); } if (type >= 3) { // 公司-部门-岗位 perfectDeptSet.addAll(perfectData(deptUUIDSet, postUUIDSet, "部门-岗位", "dept_uuid")); fse.setValue("perfect_dept", perfectDeptSet); } if (type >= 2) { // 公司-部门 perfectCompanySet.addAll(perfectData(companyUUIDSet, deptUUIDSet, "公司-部门", "org_level_uuid")); fse.setValue("perfect_company", perfectCompanySet); } fse.setValue("org_level_uuids", SetUtils.set2String(companyUUIDSet, ",")); fse.setValue("dept_uuids", SetUtils.set2String(deptUUIDSet, ",")); fse.setValue("post_uuids", SetUtils.set2String(postUUIDSet, ",")); fse.setValue("user_ids", SetUtils.set2String(userIDSet, ",")); } /** * 完善数据-自下而上-通用 * * @param parentSet 父类UUIDSet * @param childSet 子类UUIDSet * @param cacheName 缓存名称 * @param parentFieldName 子类关联父类字段 */ private Set perfectData(Set parentSet, Set childSet, String cacheName, String parentFieldName) { DataTableEntity tempDte = DataPoolCacheImpl.getInstance().getCacheData(cacheName); if ("部门-人员".equals(cacheName) || "岗位-人员".equals(cacheName)) { tempDte = OrganizationCacheService.getUserData(); } else if ("部门-岗位".equals(cacheName)) { tempDte = OrganizationCacheService.getPostData(); } else if ("公司-部门".equals(cacheName)) { tempDte = OrganizationCacheService.getOrgData("2"); } FieldSetEntity tempFse; String parentUUID; Set perfectSet = Sets.newHashSet(); for (int i = 0; i < tempDte.getRows(); i++) { tempFse = tempDte.getFieldSetEntity(i); if (childSet.contains(tempFse.getUUID())) { parentUUID = tempFse.getString(parentFieldName); if (!parentSet.contains(parentUUID)) { parentSet.add(parentUUID); perfectSet.add(parentUUID); } } } return perfectSet; } /** * 获取组织架构信息 * * @param resultMap 结果map * @param fse 参数fse * @param type 类型:1-公司,2-公司-部门,3-公司-部门-岗位,4-公司-部门-岗位-人员,5-公司-部门-人员 */ public DataTableEntity getOrganizationInfo(Map> resultMap, FieldSetEntity fse, int type) { String companyUUIDs = fse.getString("org_level_uuids"); String deptUUIDs = fse.getString("dept_uuids"); String postUUIDs = fse.getString("post_uuids"); String userIDs = fse.getString("user_ids"); Set perfectCompanySet = fse.getValue("perfect_company") == null ? null : (Set) fse.getValue("perfect_company"); Set perfectDeptSet = fse.getValue("perfect_dept") == null ? null : (Set) fse.getValue("perfect_dept"); Set perfectPostSet = fse.getValue("perfect_post") == null ? null : (Set) fse.getValue("perfect_post"); DataTableEntity dte = new DataTableEntity(); boolean isAll = StringUtils.isEmpty(companyUUIDs) && StringUtils.isEmpty(deptUUIDs) && StringUtils.isEmpty(postUUIDs) && StringUtils.isEmpty(userIDs); if (type == 1) { // 公司 dte = getCompany(resultMap, companyUUIDs, isAll); } else if (type == 2) { // 公司-部门 dte = getDept(resultMap, companyUUIDs, perfectCompanySet, deptUUIDs, isAll); } else if (type == 3) { // 公司-部门-岗位 dte = getPost(resultMap, companyUUIDs, perfectCompanySet, deptUUIDs, perfectDeptSet, postUUIDs, isAll); } else if (type == 4) { // 公司-部门-岗位-人员 dte = getStaff(resultMap, companyUUIDs, perfectCompanySet, deptUUIDs, perfectDeptSet, postUUIDs, perfectPostSet, userIDs, isAll); } else if (type == 5) { // 公司-部门-人员 dte = getStaff(resultMap, companyUUIDs, perfectCompanySet, deptUUIDs, perfectDeptSet, userIDs, isAll); } return dte; } /** * 公司 * * @param resultMap 结果map * @param uuids 指定的公司uuids * @param isAll 是否全部 * @return */ private DataTableEntity getCompany(Map> resultMap, String uuids, boolean isAll) { SystemUser curUser = SpringMVCContextHolder.getCurrentUser(); String curClientUUID; if (curUser.getUserType() == 1) { // 超管,看所有客户 DataTableEntity clientDte = DataPoolCacheImpl.getInstance().getCacheData("客户信息"); curClientUUID = clientDte.getUuidsToString(); isAll = true; } else { curClientUUID = curUser.getClient_uuid(); if (curUser.getUserType() == 4) { if (!StringUtils.isEmpty(curClientUUID)) { curClientUUID += ","; } curClientUUID += curUser.getClientUuid() == null ? "" : curUser.getClientUuid(); } } String aimUUIDs = uuids; DataTableEntity companyDte = DataPoolCacheImpl.getInstance().getCacheData("所有公司信息"); DataTableEntity aimDte = new DataTableEntity(); Map showFieldMap = Maps.newHashMap(); showFieldMap.put("value", "uuid"); showFieldMap.put("label", "org_level_name"); showFieldMap.put("pid", "org_level_code_parent"); showFieldMap.put("sid", "org_level_code"); List list = Lists.newArrayList(); FieldSetEntity tempFse; Map companyMap = resultMap.computeIfAbsent(CmnConst.COMPANYS, k -> Maps.newLinkedHashMap()); for (int i = 0; i < companyDte.getRows(); i++) { tempFse = companyDte.getFieldSetEntity(i); if (( ( ( !isAll && StringUtils.headAndTailJointAimSign(aimUUIDs, ",").contains(StringUtils.headAndTailJointAimSign(tempFse.getUUID(), ",")) ) || isAll ) && StringUtils.headAndTailJointAimSign(curClientUUID, ",").contains(StringUtils.headAndTailJointAimSign(tempFse.getString(CmnConst.CLIENT_UUID), ",")) ) || checkIsSub(list, tempFse.getString("org_level_code_parent"))) { list.add(tempFse.getString("org_level_code")); tempFse = spDeal(tempFse, showFieldMap, 1); aimDte.addFieldSetEntity(tempFse); companyMap.put(tempFse.getString("value"), tempFse); } } return aimDte; } /** * 提取map中的数据转换为dte * * @param dte * @param map * @param sign */ private void dealSpMap2Dte(DataTableEntity dte, Map> map, String sign) { Map sourceMap = map.get(sign); if (sourceMap == null) { return; } sourceMap.forEach((k, v) -> { dte.addFieldSetEntity(v); }); } /** * 公司-部门 * * @param resultMap 结果map * @param companyUUIDs 指定的公司uuids * @param perfectCompanySet 完善公司的uuid集合 * @param deptUUIDs 指定的部门uuids * @param iaAll 是否全部 * @return */ private DataTableEntity getDept(Map> resultMap, String companyUUIDs, Set perfectCompanySet, String deptUUIDs, boolean iaAll) { DataTableEntity companyDte = getCompany(resultMap, companyUUIDs, iaAll); Map showFieldMap = Maps.newHashMap(); showFieldMap.put("value", "uuid"); showFieldMap.put("label", "org_level_name"); showFieldMap.put("pid", "org_level_code_parent"); showFieldMap.put("sid", "org_level_code"); return getOrganization(resultMap, CmnConst.DEPTS, companyDte, deptUUIDs, perfectCompanySet, "公司-部门", showFieldMap, true, 2); } /** * 公司-部门-岗位 * * @param resultMap 结果map * @param companyUUIDs 指定的公司uuids * @param perfectCompanySet 完善公司的uuid集合 * @param deptUUIDs 指定的部门uuids * @param perfectDeptSet 完善部门的uuid集合 * @param postUUIDs 指定的岗位uuids * @param iaAll 是否全部 * @return */ private DataTableEntity getPost(Map> resultMap, String companyUUIDs, Set perfectCompanySet, String deptUUIDs, Set perfectDeptSet, String postUUIDs, boolean iaAll) { DataTableEntity deptDte = getDept(resultMap, companyUUIDs, perfectCompanySet, deptUUIDs, iaAll); Map showFieldMap = Maps.newHashMap(); showFieldMap.put("value", "uuid"); showFieldMap.put("label", "job_post_name"); showFieldMap.put("pid", "org_level_code"); showFieldMap.put("sid", "uuid"); return getOrganization(resultMap, CmnConst.POSTS, deptDte, postUUIDs, perfectDeptSet, "部门-岗位", showFieldMap, 3); } /** * 公司-部门-岗位-人员 * * @param resultMap 结果map * @param companyUUIDs 指定的公司uuids * @param perfectCompanySet 完善公司的uuid集合 * @param deptUUIDs 指定的部门uuids * @param perfectDeptSet 完善部门的uuid集合 * @param postUUIDs 指定的岗位uuids * @param perfectPostSet 完善岗位的uuid集合 * @param userIDs 指定的人员id * @param iaAll 是否全部 * @return */ private DataTableEntity getStaff(Map> resultMap, String companyUUIDs, Set perfectCompanySet, String deptUUIDs, Set perfectDeptSet, String postUUIDs, Set perfectPostSet, String userIDs, boolean iaAll) { DataTableEntity postDte = getPost(resultMap, companyUUIDs, perfectCompanySet, deptUUIDs, perfectDeptSet, postUUIDs, iaAll); Map showFieldMap = Maps.newHashMap(); showFieldMap.put("value", "uuid"); showFieldMap.put("label", "show_name"); showFieldMap.put("pid", "job_post_uuid"); return getOrganization(resultMap, CmnConst.STAFFS, postDte, userIDs, perfectPostSet, "岗位-人员", showFieldMap, 4); } /** * 公司-部门-人员 * * @param resultMap 结果map * @param companyUUIDs 指定的公司uuids * @param perfectCompanySet 完善公司的uuid集合 * @param deptUUIDs 指定的部门uuids * @param perfectDeptSet 完善部门的uuid集合 * @param userIDs 指定的人员id * @param iaAll 是否全部 * @return */ private DataTableEntity getStaff(Map> resultMap, String companyUUIDs, Set perfectCompanySet, String deptUUIDs, Set perfectDeptSet, String userIDs, boolean iaAll) { DataTableEntity deptDte = getDept(resultMap, companyUUIDs, perfectCompanySet, deptUUIDs, iaAll); Map showFieldMap = Maps.newHashMap(); showFieldMap.put("value", "user_id"); showFieldMap.put("label", "user_name"); showFieldMap.put("pid", "org_level_code"); return getOrganization(resultMap, CmnConst.STAFFS, deptDte, userIDs, perfectDeptSet, "部门-人员", showFieldMap, 4); } /** * 获取组织架构-通用 * * @param parentDte 父类数据集合 * @param uuids 允许查看的uuids,逗号分割 * @param parentPerfectSet 完善的父类uuid集合 * @param cacheName 缓存名称 * @param showFieldMap 展示的字段,原名=别名 * @param existsLeader 是否存在上下级关系,目前仅用于公司部门表 * @param type 1-公司,2-部门,3-岗位,4-人员 * @return */ private DataTableEntity getOrganization(Map> resultMap, String sign, DataTableEntity parentDte, String uuids, Set parentPerfectSet, String cacheName, Map showFieldMap, boolean existsLeader, int type) { String aimUUIDs = uuids; DataTableEntity aimDte = new DataTableEntity(); aimDte.addFieldSetEntity(parentDte); FieldSetEntity parentFse; DataTableEntity childDte; FieldSetEntity childFse; List list = Lists.newArrayList(); String parentFieldName = null; String childFieldName = null; for (Map.Entry entry : showFieldMap.entrySet()) { if ("pid".equalsIgnoreCase(entry.getKey())) { parentFieldName = entry.getValue(); } if ("sid".equalsIgnoreCase(entry.getKey())) { childFieldName = entry.getValue(); } } if (StringUtils.isEmpty(parentFieldName) || StringUtils.isEmpty(childFieldName)) { existsLeader = false; } Map aimMap = resultMap.computeIfAbsent(sign, k -> Maps.newLinkedHashMap()); DataTableEntity aimChildDte; FieldSetEntity tempFse; String parentValue; for (int i = 0; i < parentDte.getRows(); i++) { parentFse = parentDte.getFieldSetEntity(i); parentValue = parentFse.getString("value"); if ("部门-人员".equals(cacheName)) { childDte = OrganizationCacheService.getUserDataByDept(parentValue); } else if ("岗位-人员".equals(cacheName)) { childDte = OrganizationCacheService.getUserDataByPost(parentValue); } else if ("部门-岗位".equals(cacheName)) { childDte = OrganizationCacheService.getPostDataByDept(parentValue); } else if ("公司-部门".equals(cacheName)) { childDte = OrganizationCacheService.getDeptDataByCompany(parentValue); } else { childDte = DataPoolCacheImpl.getInstance().getCacheData(cacheName, new String[]{parentValue}); } if (childDte == null || childDte.getRows() == 0) { continue; } if (!parentPerfectSet.contains(parentValue)) { aimChildDte = spDeal(childDte, showFieldMap, type); } else { aimChildDte = new DataTableEntity(); for (int j = 0; j < childDte.getRows(); j++) { childFse = childDte.getFieldSetEntity(j); if (StringUtils.headAndTailJointAimSign(aimUUIDs, ",").contains(StringUtils.headAndTailJointAimSign(childFse.getUUID(), ",")) || (existsLeader && checkIsSub(list, childFse.getString(parentFieldName)))) { list.add(childFse.getString(childFieldName)); aimChildDte.addFieldSetEntity(spDeal(childFse, showFieldMap, type)); } } } aimDte.addFieldSetEntity(aimChildDte); for (int j = 0; j < aimChildDte.getRows(); j++) { tempFse = aimChildDte.getFieldSetEntity(j); aimMap.put(tempFse.getString("value"), tempFse); } } return aimDte; } private DataTableEntity getOrganization(Map> resultMap, String sign, DataTableEntity parentDte, String uuids, Set parentPerfectSet, String cacheName, Map showFieldMap, int type) { return getOrganization(resultMap, sign, parentDte, uuids, parentPerfectSet, cacheName, showFieldMap, false, type); } /** * 筛选并替换指定字段名称-fse * * @param fse * @param showFieldMap * @param type * @return */ private FieldSetEntity spDeal(FieldSetEntity fse, Map showFieldMap, int type) { FieldSetEntity aimFse = new FieldSetEntity(); aimFse.setTableName(fse.getTableName()); String fieldName; String replaceFieldName; for (Map.Entry entry : showFieldMap.entrySet()) { fieldName = entry.getValue(); replaceFieldName = entry.getKey(); if (!StringUtils.isEmpty(replaceFieldName)) { aimFse.setValue(replaceFieldName, fse.getString(fieldName)); } else { aimFse.setValue(fieldName, fse.getString(fieldName)); } aimFse.setValue("type", type); } return aimFse; } /** * 筛选并替换指定字段名称-dte * * @param dte * @param showFieldMap * @return */ private DataTableEntity spDeal(DataTableEntity dte, Map showFieldMap, int type) { DataTableEntity aimDte = new DataTableEntity(); if (dte == null || dte.getRows() == 0) { return aimDte; } for (int i = 0; i < dte.getRows(); i++) { aimDte.addFieldSetEntity(spDeal(dte.getFieldSetEntity(i), showFieldMap, type)); } return aimDte; } /** * 检查是否为子类(目前用于公司部门表) * * @param list * @param content * @return */ private boolean checkIsSub(List list, String content) { if (StringUtils.isEmpty(content)) { return false; } boolean flag = false; for (String str : list) { if (content.startsWith(str)) { flag = true; break; } } return flag; } }