构建链条节点,丰富信息

This commit is contained in:
2026-05-19 19:12:13 +08:00
parent 1313269710
commit 28c57666d7
3 changed files with 44 additions and 10 deletions

View File

@@ -56,7 +56,7 @@ public class LbDepartmentUser implements Serializable {
private String industry;
@TableField("org_chain")
@Schema(description = "上级链条(从直接上级到根上级,逗号分隔的用户ID")
@Schema(description = "上级链条(从直接上级到根上级,节点内 id,name,phone 逗号分隔,节点间 -> 连接")
private String orgChain;
@TableField("join_date")

View File

@@ -513,8 +513,11 @@ public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMap
}
}
private static final String ORG_CHAIN_NODE_SEPARATOR = "->";
/**
* 沿 lb_user.pid 向上追溯,构建从直接上级到根上级的用户ID链条。
* 沿 lb_user.pid 向上追溯,构建从直接上级到根上级的链条。
* 格式id,name,phone->id,name,phone->...(节点内逗号分隔,节点间箭头连接)
*/
private String buildOrgChain(Long userId, Map<Long, LbUser> userById) {
LbUser current = userById.get(userId);
@@ -522,21 +525,52 @@ public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMap
return null;
}
List<String> chain = new ArrayList<>();
List<String> nodes = new ArrayList<>();
Set<Long> visited = new HashSet<>();
Long pid = current.getPid();
while (pid != null && pid > 0) {
if (!visited.add(pid)) {
break;
}
chain.add(String.valueOf(pid));
LbUser parent = userById.get(pid);
if (parent == null) {
break;
Long currentPid = pid;
LbUser parent = userById.get(currentPid);
String name;
String phone;
if (parent != null) {
name = sanitizeOrgChainField(resolveDisplayName(parent));
phone = sanitizeOrgChainField(parent.getMobile());
pid = parent.getPid();
} else {
name = "";
phone = "";
pid = null;
}
pid = parent.getPid();
nodes.add(formatOrgChainNode(currentPid, name, phone));
}
return chain.isEmpty() ? null : String.join(",", chain);
return nodes.isEmpty() ? null : String.join(ORG_CHAIN_NODE_SEPARATOR, nodes);
}
/**
* 节点格式id有 name/phone 时才追加逗号,避免末尾出现多余的 , 或 ,,
*/
private static String formatOrgChainNode(Long currentPid, String name, String phone) {
StringBuilder sb = new StringBuilder(String.valueOf(currentPid));
if (!name.isEmpty()) {
sb.append(',').append(name);
}
if (!phone.isEmpty()) {
sb.append(',').append(phone);
}
return sb.toString();
}
private static String sanitizeOrgChainField(String value) {
if (value == null) {
return "";
}
return value.trim()
.replace(",", "")
.replace(ORG_CHAIN_NODE_SEPARATOR, "");
}
private String resolveDisplayName(LbUser user) {

View File

@@ -1,2 +1,2 @@
ALTER TABLE `lb_department_user`
ADD COLUMN `org_chain` VARCHAR(2000) DEFAULT NULL COMMENT '上级链条(从直接上级到根上级,逗号分隔的用户ID' AFTER `industry`;
ADD COLUMN `org_chain` VARCHAR(2000) DEFAULT NULL COMMENT '上级链条(从直接上级到根上级,节点内 id,name,phone 逗号分隔,节点间 -> 连接' AFTER `industry`;