using KGIS.Framework.DBOperator; using KGIS.Framework.Maps; using KGIS.Framework.Platform; using KGIS.Framework.Platform.Interface; using KGIS.Framework.Utils; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Kingo.Plugin.DataDictionary.Helper { public class DicAPI : IDicHelper { public List AllDic { get; set; } private Dictionary DicTypes = new Dictionary(); //FileSystemWatcher watcher = null; /// /// 获取行政区字典数据 /// /// 行政级别(1:县、2:镇、3:村) //List selectNode = new List(); public BasicDataModel[] GetXZQDic(int pLevel) { List result = new List(); if (pLevel == 1) { IRDBHelper helper = null; DataTable dtData = null; try { if ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) == null) { result = null; } string strConn = SysConfigsOprator.GetDBConnectionByName("MDBOledbConnection"); strConn = string.Format(strConn, SysAppPath.GetDataBaseRoot() + "System.mdb"); if (string.IsNullOrWhiteSpace(strConn)) { result = null; } helper = RDBFactory.CreateDbHelper(strConn, DatabaseType.MSAccess); string strSQL = string.Format("SELECT * FROM {0} where XZQ ='{1}'", "XZQ", ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) as ProjectInfo).CODE); dtData = helper.ExecuteDatatable("dtData", strSQL, true); if (dtData != null) { foreach (DataRow row in dtData.Rows) { BasicDataModel node = new BasicDataModel(); node.Name = row["XZQMC"] is DBNull ? "" : row["XZQMC"].ToString(); node.Code = row["XZQ"].ToString(); node.DisplayName = string.Format("({0}){1}", node.Code, node.Name); node.Attribures = new Dictionary(); foreach (DataColumn col in dtData.Columns) { node.Attribures.Add(col.ColumnName, row[col]); } result.Add(node); } } return result.ToArray(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (dtData != null) { dtData.Clear(); dtData.Dispose(); } if (helper != null) { helper.DisConnect(); } } } else if (pLevel == 2) { IRDBHelper helper = null; DataTable dtData = null; try { if ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) == null) { result = null; } string strConn = SysConfigsOprator.GetDBConnectionByName("MDBOledbConnection"); strConn = string.Format(strConn, ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) as ProjectInfo).ProjDir + "PrjDB.gdb"); if (string.IsNullOrWhiteSpace(strConn)) { result = null; } helper = RDBFactory.CreateDbHelper(strConn, DatabaseType.FileGeodatabase); string strSQL = string.Format("SELECT * FROM {0}", "XZQ"); dtData = helper.ExecuteDatatable("dtData", strSQL, true); if (dtData != null) { foreach (DataRow row in dtData.Rows) { BasicDataModel node = new BasicDataModel(); node.Name = row["XZQMC"] is DBNull ? "" : row["XZQMC"].ToString(); node.Code = row["XZQDM"].ToString(); node.DisplayName = string.Format("({0}){1}", node.Code, node.Name); node.Attribures = new Dictionary(); foreach (DataColumn col in dtData.Columns) { node.Attribures.Add(col.ColumnName, row[col]); } //GetSubXZQDic(); result.Add(node); } } return result.ToArray(); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (dtData != null) { dtData.Clear(); dtData.Dispose(); } if (helper != null) { helper.DisConnect(); } } } else { } return null; } public string GetDicTypeID(DicTypeEnum pDicType, bool GetNewDic = false) { string result = string.Empty; DataTable dtDicTypeID = null; try { if (dtDicTypeID == null || GetNewDic) { //foreach (var item in DicTypes) //{ // if (item.Value == pDicType.ToString()) // { // return item.Key; // } //} //string result = string.Empty; IRDBHelper rdbHelper = null; try { string dbPath = ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) as ProjectInfo).GetDicDataPath(); if (string.IsNullOrWhiteSpace(dbPath)) { LogAPI.Debug("GetDicDataPath 获取失败"); //dbPath = System.IO.Path.Combine(SysAppPath.GetCurrentAppPath() + "\\字典", "dic.mdb"); //if (!File.Exists(dbPath)) // return string.Empty; } //else //{ // #region 监测工作目录下的文件 // if (watcher == null) // { // watcher = new FileSystemWatcher(); // watcher.Path = ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) as ProjectInfo).ProjDir; // watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // watcher.IncludeSubdirectories = true; // watcher.Filter = "dic.mdb"; // watcher.Changed += new FileSystemEventHandler(OnChanged); // watcher.EnableRaisingEvents = true; // } // #endregion //} //if (DicTypes.Count > 0) //{ // foreach (var item in DicTypes) // { // if (item.Value == pDicType.ToString()) // { // return item.Key; // } // } //} rdbHelper = RDBFactory.CreateDbHelper("Data Source=" + dbPath, DatabaseType.SQLite); string strSQL = "select ID,ALIASNAME from Sys_DicManage ";//WHERE ALIASNAME = '" + pDicType + "'"; dtDicTypeID = rdbHelper.ExecuteDatatable("DicType", strSQL, true); //if (dt != null && dt.Rows.Count > 0) //{ // if (dt.Rows[0][0] != null && !(dt.Rows[0][0] is DBNull)) // result = dt.Rows[0][0].ToString(); //} } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (rdbHelper != null) { rdbHelper.DisConnect(); } } } if (dtDicTypeID != null && dtDicTypeID.Rows.Count > 0) { foreach (DataRow item in dtDicTypeID.Rows) { if (item[0] != null && !(item[0] is DBNull)) { if (item[1].ToString() == pDicType.ToString()) result = item[0].ToString(); if (DicTypes.ContainsKey(item[0].ToString())) continue; DicTypes.Add(item[0].ToString(), item[1].ToString()); //return item[0].ToString(); } } //DataRow[] rows = dtDicTypeID.Select(string.Format("ALIASNAME='{0}'", pDicType)); //if (rows != null && rows.Length > 0) //{ // if (rows[0][0] != null && !(rows[0][0] is DBNull)) // { // return rows[0][0].ToString(); // } //} } return result; } catch (Exception ex) { throw ex; } finally { if (dtDicTypeID != null) { dtDicTypeID.Clear(); dtDicTypeID.Dispose(); } } } private void OnChanged(object source, FileSystemEventArgs e) { var watcher = source as FileSystemWatcher; if (watcher != null) { if (DicTypes.Count > 0) DicTypes.Clear(); } } /// /// 避免access由于独占情况,造成报错 /// /// /// /// 王欢 2018-11-16 public DataTable GetDicTypeID(List pDicType, bool GetNewDic = false) { DataTable dtDicTypeID = null; IRDBHelper rdbHelper = null; try { string dbPath = ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) as ProjectInfo).GetDicDataPath(); if (string.IsNullOrWhiteSpace(dbPath)) { rdbHelper = RDBFactory.CreateDbHelper("Data Source=" + dbPath, DatabaseType.SQLite); StringBuilder sb = new StringBuilder("select ID,ALIASNAME from Sys_DicManage WHERE ALIASNAME in ( "); foreach (DicTypeEnum item in pDicType) { sb.Append("'" + item + "',"); } sb.Remove(sb.Length - 1, 1); sb.Append(")"); dtDicTypeID = rdbHelper.ExecuteDatatable("DicType", sb.ToString(), true); } } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (rdbHelper != null) rdbHelper.DisConnect(); } return dtDicTypeID; } /// /// 根据类型获取字典项集合 /// /// /// public List GetDic(DicTypeEnum pDicType, bool IsReset = false) { List result = null; try { //if (Env.Instance.DicList == null || Env.Instance.DicList.Count <= 0) //{ // Env.Instance.DicList = CommonAPI.GetAllDic(); //} if (AllDic == null) AllDic = new List(); if (AllDic.Count == 0 || IsReset) { AllDic = DicHelper.GetAllDic(); } if (AllDic.Count == 0) { result = new List(); return result; } string s = GetDicTypeID(pDicType); if (string.IsNullOrWhiteSpace(s)) return result; //List tempDic = DicHelper.GetAllDic(); //if (tempDic == null) // return result; List dicList = AllDic.FindAll(f => f.OWNERDIC == s && (f.PID == 0 || f.PID == null)); foreach (DataDicTionary item in dicList) { if (result == null) result = new List(); item.SubDic = GetSubDic(item.ID, AllDic); result.Add(item); //List list = GetSubDic(item.ID); //if (list != null) //{ // result.AddRange(list); //} } } catch (Exception ex) { throw ex; } return result; } /// /// (定制化)根据类型获取字典项集合 /// edit by cmj 2018/5/7 /// /// /// public List GetDlbmDic(DicTypeEnum pDicType, bool IsReset = false) { List result = null; try { //if (Env.Instance.DicList == null || Env.Instance.DicList.Count <= 0) //{ // Env.Instance.DicList = CommonAPI.GetAllDic(); //} string s = GetDicTypeID(pDicType); if (string.IsNullOrWhiteSpace(s)) return result; List tempDic = DicHelper.GetAllDic(); if (tempDic == null) return result; List dicList = tempDic.OrderBy(f => f.CODE).ToList().FindAll(f => f.OWNERDIC == s); foreach (DataDicTionary item in dicList) { if (result == null) result = new List(); //修改人:李忠盼 修改时间:20180917 地类编码增加一级类湿地(00),而以00开头的地类编码只有湿地本身,一级类不应出现在绘制属性继承框内 //int i = dicList.Where(x => x.CODE.StartsWith(item.CODE)).ToList().Count; //if (i > 1 && item.CODE.Length < 3) //2019-01-30 白明雅 存在没有子类的特殊情况,这种需要添加 //查询有几个二级类 int i = dicList.Where(x => x.PID == item.ID).ToList().Count; if (item.CODE.Length < 3 && i > 0) continue; item.SubDic = GetSubDic(item.ID, tempDic); result.Add(item); } } catch (Exception ex) { throw ex; } return result; } public DataDicTionary GetSingleDlbmDic(DicTypeEnum pDicType, bool IsReset = false) { DataDicTionary result = new DataDicTionary(); try { string s = GetDicTypeID(pDicType); if (string.IsNullOrWhiteSpace(s)) return result; List tempDic = DicHelper.GetAllDic(); if (tempDic == null) return result; List dicList = tempDic.OrderBy(f => f.CODE).ToList().FindAll(f => f.OWNERDIC == s && f.CODE.Length == 6); foreach (DataDicTionary item in dicList) { result = item; } } catch (Exception ex) { throw ex; } return result; } public List GetNoGroupDic(DicTypeEnum pDicType, List dicList = null) { List result = null; try { //if (IsReset || Env.Instance.DicList == null || Env.Instance.DicList.Count == 0) //{ // Env.Instance.DicList = CommonAPI.GetAllDic(); //} //if (Env.Instance.DicList == null || Env.Instance.DicList.Count == 0) //{ // Env.Instance.DicList = CommonAPI.GetAllDicForSystem(); //} string s = GetDicTypeID(pDicType); if (string.IsNullOrWhiteSpace(s)) return result; List tempDic = dicList; if (dicList == null || dicList.Count == 0) tempDic = DicHelper.GetAllDic(); if (tempDic == null) return result; foreach (DataDicTionary item in tempDic) { if (item.OWNERDIC == s) { if (result == null) { result = new List(); result.Add(new DataDicTionary() { CODE = "", NAME = "", DisplayName = "" }); } //item.SubDic = GetSubDic(item.ID); result.Add(item); } } } catch (Exception ex) { throw ex; } return result; } public List GetAllDic() { DataTable dt = null; List result = new List(); IRDBHelper rdbHelper = null; try { if ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) != null) { string dbPath = ((MapsManager.Instance.CurrProjectInfo as ProjectInfo) as ProjectInfo).GetDicDataPath(); if (!string.IsNullOrWhiteSpace(dbPath)) { rdbHelper = RDBFactory.CreateDbHelper("Data Source=" + dbPath, DatabaseType.SQLite); string strSQL = "select * from Sys_DicDetail"; dt = rdbHelper.ExecuteDatatable("Dic", strSQL, true); if (dt != null) { //Bug-12137 霍岩 2018-11-14 使用CODE字段按顺序排序 result = KGIS.Framework.Utils.Utility.TBToList.ToList(dt).OrderBy(x => x.CODE).ToList(); result.ForEach(x => x.DisplayName = x.CODE + "-" + x.NAME); } } } } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (rdbHelper != null) { rdbHelper.DisConnect(); } if (dt != null) { dt.Clear(); dt.Dispose(); } } return result; } /// /// 结果去重 /// /// /// /// /// 白明雅 2019-04-13 public List GetNoGroupDic2(DicTypeEnum pDicType, List dicList = null, bool isRepeat = true, bool IsSupply = false, char SupplyValue = '\u0000') { List result = null; try { string s = GetDicTypeID(pDicType); if (string.IsNullOrWhiteSpace(s)) return result; List tempDic = dicList; if (dicList == null || dicList.Count == 0) tempDic = DicHelper.GetAllDic(); if (tempDic == null) return result; if (IsSupply && SupplyValue != '\u0000') { foreach (DataDicTionary item in tempDic) { if (string.IsNullOrWhiteSpace(item.CODE)) continue; item.CODE = item.CODE.PadRight(19, SupplyValue); } } foreach (DataDicTionary item in tempDic) { if (item.OWNERDIC == s) { if (result == null) { result = new List(); result.Add(new DataDicTionary() { CODE = "", NAME = "", DisplayName = "" }); } if (isRepeat) { if (result.Where(x => x.NAME == item.NAME && x.CODE == item.CODE).Count() < 1) { result.Add(item); } } else result.Add(item); } } } catch (Exception ex) { throw ex; } return result; } public DataTable GetDic(List lstDicType) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Type")); dt.Columns.Add(new DataColumn("Code")); dt.Columns.Add(new DataColumn("Name")); try { //if (Env.Instance.DicList == null || Env.Instance.DicList.Count <= 0) //{ // Env.Instance.DicList = CommonAPI.GetAllDic(); //} List tempDic = DicHelper.GetAllDic(); if (tempDic == null) return dt; foreach (DicTypeEnum pDicType in lstDicType) { string s = GetDicTypeID(pDicType); List dicList = tempDic.FindAll(f => f.OWNERDIC == s && (f.PID == 0 || f.PID == null)); foreach (DataDicTionary item in dicList) { DataRow drAdd = dt.NewRow(); drAdd["Type"] = pDicType.ToString(); if (pDicType == DicTypeEnum.CZCJGKYD) { drAdd["Type"] = DicTypeEnum.DLBM.ToString(); } drAdd["Code"] = item.CODE; drAdd["Name"] = item.NAME; dt.Rows.Add(drAdd); GetSubDic(item.ID, dt, drAdd["Type"].ToString()); } } } catch (Exception ex) { throw ex; } return dt; } #region 王欢 2018-11-16 对getdic重新编写 优化代码避免频繁打开数据库 /// /// 对getdic重新编写 优化代码避免频繁打开数据库 /// /// /// public DataTable resetGetDic(List lstDicType) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Type")); dt.Columns.Add(new DataColumn("Code")); dt.Columns.Add(new DataColumn("Name")); try { List tempDic = DicHelper.GetAllDic(); if (tempDic == null) return dt; DataTable GetDicdt = GetDicTypeID(lstDicType); if (GetDicdt == null) { return dt; } foreach (DataRow dtrow in GetDicdt.Rows) { string s = dtrow["ID"].ToString(); List dicList = tempDic.FindAll(f => f.OWNERDIC == s && (f.PID == 0 || f.PID == null)); foreach (DataDicTionary item in dicList) { DataRow drAdd = dt.NewRow(); drAdd["Type"] = dtrow["ALIASNAME"].ToString(); if (dtrow["ALIASNAME"].ToString().ToUpper() == "CZCJGKYD") { drAdd["Type"] = DicTypeEnum.DLBM.ToString(); } drAdd["Code"] = item.CODE; drAdd["Name"] = item.NAME; dt.Rows.Add(drAdd); GetSubDic(tempDic, item.ID, dt, drAdd["Type"].ToString()); } } } catch (Exception ex) { throw ex; } return dt; } #endregion private void GetSubDic(List tempDic, int PID, DataTable dt, string type) { List list = tempDic.FindAll(e => e.PID == PID); if (list != null) { foreach (DataDicTionary item in list) { DataRow drAdd = dt.NewRow(); drAdd["Type"] = type; drAdd["Code"] = item.CODE; drAdd["Name"] = item.NAME; dt.Rows.Add(drAdd); GetSubDic(tempDic, item.ID, dt, type); } } } private void GetSubDic(int PID, DataTable dt, string type) { List tempDic = DicHelper.GetAllDic(); if (tempDic == null) return; List list = tempDic.FindAll(e => e.PID == PID); if (list != null) { foreach (DataDicTionary item in list) { DataRow drAdd = dt.NewRow(); drAdd["Type"] = type; drAdd["Code"] = item.CODE; drAdd["Name"] = item.NAME; dt.Rows.Add(drAdd); GetSubDic(item.ID, dt, type); } } } private List GetSubDic(int PID, List pDicList) { List result = null; //if (Env.Instance.DicList == null || Env.Instance.DicList.Count <= 0) //{ // Env.Instance.DicList = CommonAPI.GetAllDic(); //} //List tempDic = CommonAPI.GetAllDic(); if (pDicList == null) return result; List list = pDicList.FindAll(e => e.PID == PID); if (list != null) { foreach (DataDicTionary item in list) { if (result == null) result = new List(); item.SubDic = GetSubDic(item.ID, pDicList); result.Add(item); //List subList = GetSubDic(item.ID); //if (subList != null) //{ // result.AddRange(subList); //} } } return result; } } }