|
|
@@ -359,6 +359,77 @@ public class EsmInsCompanyServiceImpl extends ServiceImpl<EsmInsCompanyMapper, E
|
|
|
public String getTopCompanyId(String companyId) {
|
|
|
return baseMapper.getTopCompanyId(companyId);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有保险公司的全路径名称
|
|
|
+ * Key: 公司ID
|
|
|
+ * Value: 总公司/分公司/支公司
|
|
|
+ */
|
|
|
+ public Map<String, String> getAllCompanyHierarchyPaths() {
|
|
|
+ log.info("获取所有保险公司的全路径名称");
|
|
|
+ // 1. 查询所有未删除的保险公司(一次性加载,避免循环查库)
|
|
|
+ List<EsmInsCompany> allCompanies = baseMapper.selectList(null);
|
|
|
+ allCompanies = allCompanies.stream().filter(action->action.getIsDelete()==0).toList();
|
|
|
+
|
|
|
+ // 2. 将列表转换为 Map,以便通过 ID 快速查找对象 (ID -> Company Object)
|
|
|
+ Map<String, EsmInsCompany> companyMap = allCompanies.stream()
|
|
|
+ .collect(Collectors.toMap(EsmInsCompany::getId, c -> c));
|
|
|
+
|
|
|
+ // 3. 结果缓存 Map (ID -> "A/B/C" Path)
|
|
|
+ // 使用 ConcurrentHashMap 或者普通 HashMap 均可,视并发需求而定
|
|
|
+ Map<String, String> pathCache = new HashMap<>();
|
|
|
+
|
|
|
+ // 4. 遍历所有公司,构建路径
|
|
|
+ for (EsmInsCompany company : allCompanies) {
|
|
|
+ buildPath(company.getId(), companyMap, pathCache);
|
|
|
+ }
|
|
|
+ log.info("构建得到的公司的层级结构如下: [{}]", JSONUtil.toJsonStr(pathCache));
|
|
|
+ return pathCache;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归构建单个公司的路径(带缓存优化,防止重复计算)
|
|
|
+ */
|
|
|
+ private String buildPath(String currentId, Map<String, EsmInsCompany> companyMap, Map<String, String> pathCache) {
|
|
|
+ // 如果已经计算过,直接返回(记忆化搜索,防止重复递归)
|
|
|
+ if (pathCache.containsKey(currentId)) {
|
|
|
+ return pathCache.get(currentId);
|
|
|
+ }
|
|
|
+
|
|
|
+ EsmInsCompany currentCompany = companyMap.get(currentId);
|
|
|
+ if (currentCompany == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取父级 ID
|
|
|
+ String parentId = currentCompany.getParentId();
|
|
|
+
|
|
|
+ String fullPath;
|
|
|
+
|
|
|
+ // 判断是否有父级 (根据数据库设计,根节点的 parent_id 通常为 0 或 null)
|
|
|
+ if (parentId == null || parentId.equals("0")) {
|
|
|
+ // 如果是根节点,路径就是它自己的名字
|
|
|
+ //fullPath = currentCompany.getName();
|
|
|
+ fullPath = currentCompany.getNameSimple();
|
|
|
+ } else {
|
|
|
+ // 如果有父级,递归获取父级的路径,然后拼接当前名字
|
|
|
+ // 注意:这里递归调用 buildPath,利用缓存避免死循环和重复计算
|
|
|
+ String parentPath = buildPath(parentId, companyMap, pathCache);
|
|
|
+
|
|
|
+ if (parentPath.isEmpty()) {
|
|
|
+ //fullPath = currentCompany.getName();
|
|
|
+ fullPath = currentCompany.getNameSimple();
|
|
|
+ } else {
|
|
|
+ //fullPath = parentPath + "/" + currentCompany.getName();
|
|
|
+ fullPath = parentPath + "/" + currentCompany.getNameSimple();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 存入缓存
|
|
|
+ pathCache.put(currentId, fullPath);
|
|
|
+ return fullPath;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|