日期:2014-05-16 浏览次数:20559 次
/** * 通过上级代号获取下级机构,加载机构树 * @param parentid 上级代号 * @return */ public DTO getProductOrganTree(DTO p_dto) throws Exception { DTO r_dto = new DTO(); List list = null; String productid = (String) p_dto.get("productid"); String userid = (String) p_dto.get("userid"); list=(List)this.userDAO.queryhashmap("select * from v_organ order by parentid"); Map<String, List> parentidMap = new HashMap(); Map<String, String> containMap = new HashMap(); String parentid = ""; String lastparentid = ""; List listorgan = null; for(Iterator it=list.iterator(); it.hasNext();) { Map map = (Map) it.next(); parentid = map.get("parentid").toString(); if(parentid==null||"".equals(parentid)) parentid = "0"; if(!lastparentid.equals(parentid)) { if(listorgan!=null) parentidMap.put(lastparentid, listorgan); listorgan = new ArrayList(); lastparentid = parentid; } listorgan.add(map); } parentidMap.put(lastparentid, listorgan); list=(List)this.userDAO.queryhashmap("select * from v_pou where userid='" + userid + "' and productid='" + productid + "'"); for(Iterator it=list.iterator(); it.hasNext();) { Map map = (Map) it.next(); containMap.put(map.get("organid").toString(), ""); } String json = getTreeNodeJson(parentidMap,containMap,"0"); r_dto.put("result",json); return r_dto; } /** * 通过parentidMap 叠带树 * @param parentidMap * @param parentid * @return */ private String getTreeNodeJson(Map parentidMap,Map containMap,String parentid){ StringBuilder builder = new StringBuilder("["); List list = (List) parentidMap.get(parentid); for (int i=0; i < list.size(); ++i) { HashMap map = (HashMap)list.get(i); String id=map.get("id")==null ?"":map.get("id").toString().trim(); String text=map.get("text")==null ?"":map.get("text").toString().trim(); String iconcls="foler"; if (builder.length() > 1) builder.append(","); builder.append("{\"id\":\"").append(id).append("\"") .append(",\"text\":\"").append(text).append("\"") .append(",\"cls\":\"").append(iconcls).append("\""); if(containMap.containsKey(id)) { builder.append(",\"checked\":").append(true); } else { builder.append(",\"checked\":").append(false); } ////////////////递规子类 start if(parentidMap.containsKey(id)) { builder.append(",\"children\":"); builder.append(getTreeNodeJson(parentidMap,containMap,id)); } else builder.append(",\"leaf\":true"); ////////////////递规子类 end builder.append("}"); } builder.append("]"); return builder.toString(); }