/// <summary> /// Unit Tree Contruct For Format All Unit Tree From DataBase /// </summary> public UnitTree() { Lib.DataUtility du = new Lib.DataUtility(); DataTable dt = du.getDataTableByText("select * from unit", null); d = new Dictionary <string, Lib.Unit>(); foreach (DataRow row in dt.Rows) { Lib.Unit unit = new Lib.Unit(); unit.Unit_Code = row["unit_code"].ToString(); unit.Parent_Unit_Code = row["parent_unit_code"].ToString(); unit.Unit_Title = row["unit_title"].ToString(); unit.Service_Code = row["service_code"].ToString(); d.Add(unit.Unit_Code, unit); } foreach (KeyValuePair <string, Lib.Unit> item in d) { Lib.Unit u = (Lib.Unit)item.Value; //if (u.Parent_Unit_Code != "") //{ var parent_code = u.Parent_Unit_Code; foreach (KeyValuePair <string, Lib.Unit> parent in d) { if (parent_code == ((Lib.Unit)parent.Value).Unit_Code) { if (((Lib.Unit)parent.Value).ChildUnit == null) { u.Parent_Title = ((Lib.Unit)parent.Value).Unit_Title; ((Lib.Unit)parent.Value).ChildUnit = new Dictionary <string, Lib.Unit>(); ((Lib.Unit)parent.Value).ChildUnit.Add(u.Unit_Code, u); } else { u.Parent_Title = ((Lib.Unit)parent.Value).Unit_Title; ((Lib.Unit)parent.Value).ChildUnit.Add(u.Unit_Code, u); } break; } } //} } }
/// <summary> /// Get Unit Class With Child Unit Array /// </summary> /// <param name="unit_code"></param> /// <returns></returns> public Unit GetUnitWithChild(string unit_code) { foreach (KeyValuePair <string, Unit> item in d) { if (((Unit)item.Value).Unit_Code == unit_code) { _u = (Unit)item.Value; break; } } DataTable dt = new DataTable(); dt.Columns.Add("unit_code"); Lib.Unit unit = _u; // if unit is the biggest 國防部 #region Sql Command And List list.Clear(); // add self data first list.Add(unit.Unit_Code, unit.Unit_Title); unitCodeList.Add(unit.Unit_Code); DataRow firstRow = dt.NewRow(); firstRow[0] = unit.Unit_Code; dt.Rows.Add(firstRow); // then the children unit if (unit.ChildUnit != null && unit.ChildUnit.Count != 0) { foreach (KeyValuePair <string, Lib.Unit> child_1 in unit.ChildUnit) // this should be 陸總部 { Lib.Unit myUnit = child_1.Value as Lib.Unit; list.Add(myUnit.Unit_Code, myUnit.Unit_Title); unitCodeList.Add(myUnit.Unit_Code); DataRow row_1 = dt.NewRow(); row_1[0] = myUnit.Unit_Code; dt.Rows.Add(row_1); if (myUnit.ChildUnit != null && myUnit.ChildUnit.Count != 0) { foreach (KeyValuePair <string, Lib.Unit> child_2 in myUnit.ChildUnit) // this should be 旅級 { Lib.Unit myUnit_2 = child_2.Value as Lib.Unit; list.Add(myUnit_2.Unit_Code, myUnit_2.Unit_Title); unitCodeList.Add(myUnit_2.Unit_Code); DataRow row_2 = dt.NewRow(); row_2[0] = myUnit_2.Unit_Code; dt.Rows.Add(row_2); if (myUnit_2.ChildUnit != null && myUnit_2.ChildUnit.Count != 0) { foreach (KeyValuePair <string, Lib.Unit> child_3 in myUnit_2.ChildUnit) // this should be 營級 { Lib.Unit myUnit_3 = child_3.Value as Lib.Unit; list.Add(myUnit_3.Unit_Code, myUnit_3.Unit_Title); unitCodeList.Add(myUnit_3.Unit_Code); DataRow row_3 = dt.NewRow(); row_3[0] = myUnit_3.Unit_Code; dt.Rows.Add(row_3); if (myUnit_3.ChildUnit != null && myUnit_3.ChildUnit.Count != 0) { foreach (KeyValuePair <string, Lib.Unit> child_4 in myUnit_3.ChildUnit) // this should be 連級 { Lib.Unit myUnit_4 = child_4.Value as Lib.Unit; list.Add(myUnit_4.Unit_Code, myUnit_4.Unit_Title); unitCodeList.Add(myUnit_4.Unit_Code); DataRow row_4 = dt.NewRow(); row_4[0] = myUnit_4.Unit_Code; dt.Rows.Add(row_4); if (myUnit_4.ChildUnit != null && myUnit_4.ChildUnit.Count != 0) { foreach (KeyValuePair <string, Lib.Unit> child_5 in myUnit_4.ChildUnit) { // this should be 排級 Lib.Unit myUnit_5 = child_5.Value as Lib.Unit; list.Add(myUnit_5.Unit_Code, myUnit_5.Unit_Title); unitCodeList.Add(myUnit_5.Unit_Code); DataRow row_5 = dt.NewRow(); row_5[0] = myUnit_5.Unit_Code; dt.Rows.Add(row_5); } } } } } } } } } } SqlCmd = ""; if (list.Count != 0) { foreach (KeyValuePair <string, string> pair in list) { SqlCmd += "'" + pair.Key + "',"; } SqlCmd = SqlCmd.Substring(0, SqlCmd.Length - 1); } _u.SqlCmdText = SqlCmd; _u.ChildUnitList = list; _u.ChildUnitCount = list.Count; _u.ChildUnitCodeTable = dt; _u.ChildUnitCodeArray = unitCodeList.ToArray(); #endregion return(_u); }