using DBUtility; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.Script.Serialization; namespace ReportTool.Handler { /// <summary> /// Tree 的摘要说明 /// </summary> public class Tree2 : IHttpHandler { public DataProvider dataprovider = new DataProvider(); public Tree2() { dataprovider.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString2"].ConnectionString; } public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; GetTreeJson(context); } private void GetTreeJson(HttpContext context) { DataTable dt = new DataTable(); var sql = @" select OUID,ParentOUID,OUCode,OUName,OUShortName from tbl_Base_OUInfo where LEN(oucode) in(5,10,15,20) order by LEN(oucode),OrderNo,ParentOUID"; dt = dataprovider.FillDataTable(sql, CommandType.Text); string json = GetArrayJSON(dt); context.Response.Write(json.Replace(""OUName"", ""text"").Replace(""OUID"", ""id"")); } private static string GetArrayJSON(DataTable dt) { var o = DatatableToTreeData(dt); return new JavaScriptSerializer().Serialize(o); } private static object DatatableToTreeData(DataTable dtConvert) { var list = DatatableToHash(dtConvert); var r = new List<Hashtable>(); Hashtable root = new Hashtable(); //中国石油 //root = list.Find(p => p["OUID"].ToString() == "00000000-0000-0000-0000-000000000000"); //集团公司 root = list.Find(p => p["OUID"].ToString() == "CCB93E1A-CC22-4120-B99F-62E941CC6E4A".ToLower()); TreeData(list, root, "CCB93E1A-CC22-4120-B99F-62E941CC6E4A".ToLower()); r.Add(root); return r; } private static Hashtable TreeData(List<Hashtable> list, Hashtable r, string pouid) { var list1 = list.FindAll(p => p["ParentOUID"].ToString() == pouid); if (list1.Count > 0) { //var pitem = r as Hashtable; if (!r.ContainsKey("children")) r["children"] = new List<Hashtable>(); var children = r["children"] as List<Hashtable>; foreach (var item in list1) { TreeData(list, item, item["OUID"].ToString()); children.Add(item); } } return r; } private static List<Hashtable> DatatableToHash(DataTable dtConvert) { var list = new List<Hashtable>(); int j; foreach (DataRow dr in dtConvert.Rows) { var item = new Hashtable(); j = 0; foreach (DataColumn dc in dtConvert.Columns) { var name = dc.ColumnName; var value = dr[j]; item[name] = value; j++; } list.Add(item); } return list; } public bool IsReusable { get { return false; } } } }