构建链条节点,丰富信息
This commit is contained in:
@@ -56,7 +56,7 @@ public class LbDepartmentUser implements Serializable {
|
|||||||
private String industry;
|
private String industry;
|
||||||
|
|
||||||
@TableField("org_chain")
|
@TableField("org_chain")
|
||||||
@Schema(description = "上级链条(从直接上级到根上级,逗号分隔的用户ID)")
|
@Schema(description = "上级链条(从直接上级到根上级,节点内 id,name,phone 逗号分隔,节点间 -> 连接)")
|
||||||
private String orgChain;
|
private String orgChain;
|
||||||
|
|
||||||
@TableField("join_date")
|
@TableField("join_date")
|
||||||
|
|||||||
@@ -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) {
|
private String buildOrgChain(Long userId, Map<Long, LbUser> userById) {
|
||||||
LbUser current = userById.get(userId);
|
LbUser current = userById.get(userId);
|
||||||
@@ -522,21 +525,52 @@ public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMap
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> chain = new ArrayList<>();
|
List<String> nodes = new ArrayList<>();
|
||||||
Set<Long> visited = new HashSet<>();
|
Set<Long> visited = new HashSet<>();
|
||||||
Long pid = current.getPid();
|
Long pid = current.getPid();
|
||||||
while (pid != null && pid > 0) {
|
while (pid != null && pid > 0) {
|
||||||
if (!visited.add(pid)) {
|
if (!visited.add(pid)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
chain.add(String.valueOf(pid));
|
Long currentPid = pid;
|
||||||
LbUser parent = userById.get(pid);
|
LbUser parent = userById.get(currentPid);
|
||||||
if (parent == null) {
|
String name;
|
||||||
break;
|
String phone;
|
||||||
}
|
if (parent != null) {
|
||||||
|
name = sanitizeOrgChainField(resolveDisplayName(parent));
|
||||||
|
phone = sanitizeOrgChainField(parent.getMobile());
|
||||||
pid = parent.getPid();
|
pid = parent.getPid();
|
||||||
|
} else {
|
||||||
|
name = "";
|
||||||
|
phone = "";
|
||||||
|
pid = null;
|
||||||
}
|
}
|
||||||
return chain.isEmpty() ? null : String.join(",", chain);
|
nodes.add(formatOrgChainNode(currentPid, name, phone));
|
||||||
|
}
|
||||||
|
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) {
|
private String resolveDisplayName(LbUser user) {
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
ALTER TABLE `lb_department_user`
|
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`;
|
||||||
|
|||||||
Reference in New Issue
Block a user