zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.shlanbao.tzsc.pms.sys.org.controller;
 
import java.util.ArrayList;
import java.util.List;
 
import com.shlanbao.tzsc.pms.sys.org.beans.OrgBean;
/**
 * 组织机构HTML标签拼接
 * @author Leejean
 * @create 2014年8月27日下午12:38:06
 */
public class OrgHtmlUtil {
    public static String getHtml(List<OrgBean> orgs){
        OrgBean root=OrgHtmlUtil.getRoot(orgs);
        if(root==null){
            return "";
        }
        String html="<li id='"+root.getId()+"' pid='"+root.getPid()+"'>";
        html+=root.getName();
        html+=getSonsHtml(orgs,root.getId());
        
        return (html+"</li>").replace("<ul></ul>", "");
    }
    private static String getSonsHtml(List<OrgBean> orgs, String id) {
        String html="<ul>";
        for (OrgBean orgBean : OrgHtmlUtil.getSons(orgs, id)) {
            html+="<li id='"+orgBean.getId()+"' pid='"+orgBean.getPid()+"'>";
            html+=orgBean.getName();
            html+=OrgHtmlUtil.getSonsHtml(orgs, orgBean.getId());
            html+="</li>";
        }
        return html+"</ul>";
    }
    private static OrgBean getRoot(List<OrgBean> orgs){
        for (OrgBean orgBean : orgs) {
            if(orgBean.getPid()==null){
                return orgBean;
            }
        }
        return null;
    }
    private static List<OrgBean> getSons(List<OrgBean> orgs,String id){
        List<OrgBean> orgBeans=new ArrayList<OrgBean>();
        for (OrgBean orgBean : orgs) {
            if(orgBean.getPid()!=null&&orgBean.getPid().equals(id)){
                orgBeans.add(orgBean);
            }
        }
        return orgBeans;
        
    }
}