using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace RoadFlow.Platform { public class Dictionary { private RoadFlow.Data.Interface.IDictionary dataDictionary; private static string cacheKey = RoadFlow.Utility.Keys.CacheKeys.Dictionary.ToString(); public Dictionary() { this.dataDictionary = Data.Factory.Factory.GetDictionary(); } /// /// 新增 /// public int Add(RoadFlow.Data.Model.Dictionary model) { return dataDictionary.Add(model); } /// /// 更新 /// public int Update(RoadFlow.Data.Model.Dictionary model) { return dataDictionary.Update(model); } /// /// 查询所有记录 /// public List GetAll(bool fromCache=false) { if (!fromCache) { return dataDictionary.GetAll(); } else { object obj = RoadFlow.Cache.IO.Opation.Get(cacheKey); if (obj != null && obj is List) { return obj as List; } else { var list = dataDictionary.GetAll(); RoadFlow.Cache.IO.Opation.Set(cacheKey, list); return list; } } } /// /// 查询单条记录 /// public RoadFlow.Data.Model.Dictionary Get(Guid id, bool fromCache = false) { return fromCache ? GetAll(true).Find(p => p.ID == id) : dataDictionary.Get(id); } /// /// 删除 /// public int Delete(Guid id) { return dataDictionary.Delete(id); } /// /// 查询记录条数 /// public long GetCount() { return dataDictionary.GetCount(); } /// /// 根据父ID查询一条记录 /// public RoadFlow.Data.Model.Dictionary GetRoot(Guid id) { return dataDictionary.GetRoot(id); } /// /// 查询根记录 /// public RoadFlow.Data.Model.Dictionary GetRoot() { return dataDictionary.GetRoot(); } /// /// 查询下级记录 /// public List GetChilds(Guid id, bool fromCache=false) { return fromCache ? getChildsByIDFromCache(id) : dataDictionary.GetChilds(id); } /// /// 查询下级记录 /// public List GetChilds(string code, bool fromCache=false) { return code.IsNullOrEmpty() ? new List() : fromCache ? getChildsByCodeFromCache(code) : dataDictionary.GetChilds(code.Trim()); } private List getChildsByCodeFromCache(string code) { var list = GetAll(true); var dict = list.Find(p => string.Compare(p.Code, code, true) == 0); return dict == null ? new List() : list.FindAll(p => p.ParentID == dict.ID).OrderBy(p=>p.Sort).ToList(); } private List getChildsByIDFromCache(Guid id) { var list = GetAll(true); return list.FindAll(p => p.ParentID == id).OrderBy(p=>p.Sort).ToList(); } /// /// 得到所有下级 /// /// /// 是否使用缓存 /// public List GetAllChilds(string code, bool fromCache) { if (code.IsNullOrEmpty()) return new List(); var dict = GetByCode(code, fromCache); if (dict == null) return new List(); return GetAllChilds(dict.ID, fromCache); } /// /// 得到所有下级 /// /// /// 是否使用缓存 /// public List GetAllChilds(Guid id, bool fromCache=false) { List list = new List(); addChilds(list, id, fromCache); return list; } private void addChilds(List list, Guid id, bool fromCache=false) { var childs = fromCache ? getChildsByIDFromCache(id) : GetChilds(id); foreach (var child in childs) { list.Add(child); addChilds(list, child.ID, fromCache); } } /// /// 得到一个项的所有下级项ID字符串 /// /// /// 是否包含自己 /// public string GetAllChildsIDString(Guid id, bool isSelf = true) { StringBuilder sb = new StringBuilder(); if (isSelf) { sb.Append(id); sb.Append(","); } var childs = GetAllChilds(id, true); foreach (var child in childs) { sb.Append(child.ID); sb.Append(","); } return sb.ToString().TrimEnd(','); } /// /// 查询上级记录 /// public RoadFlow.Data.Model.Dictionary GetParent(Guid id) { return dataDictionary.GetParent(id); } /// /// 是否包含下级记录 /// public bool HasChilds(Guid id) { return dataDictionary.HasChilds(id); } /// /// 得到最大排序 /// public int GetMaxSort(Guid id) { return dataDictionary.GetMaxSort(id); } /// /// 更新排序 /// public int UpdateSort(Guid id, int sort) { return dataDictionary.UpdateSort(id, sort); } /// /// 根据代码查询一条记录 /// /// /// 是否使用缓存 /// public RoadFlow.Data.Model.Dictionary GetByCode(string code, bool fromCache=false) { return code.IsNullOrEmpty() ? null : fromCache ? GetAll(true).Find(p => string.Compare(p.Code, code, true) == 0) : dataDictionary.GetByCode(code.Trim()); } /// /// 下拉选项时以哪个字段作为值字段 /// public enum OptionValueField { ID, Title, Code, Value, Other, Note } /// /// 根据ID得到选项 /// /// /// /// public string GetOptionsByID(Guid id, OptionValueField valueField = OptionValueField.Value, string value = "") { var childs = GetAllChilds(id, true); StringBuilder options = new StringBuilder(childs.Count * 100); StringBuilder space = new StringBuilder(); foreach (var child in childs) { space.Clear(); int parentCount = getParentCount(childs, child); for (int i = 0; i < parentCount-1; i++) { space.Append("  "); } if (parentCount > 0) { space.Append("┝"); } string value1 = getOptionsValue(valueField, child); options.AppendFormat("" + parentCount, value1, value1 == value ? " selected=\"selected\"" : "", space.ToString(), child.Title); } return options.ToString(); } /// /// 得到一个字典项的上级节点数 /// /// /// /// private int getParentCount(List dictList, RoadFlow.Data.Model.Dictionary dict) { int parent = 0; RoadFlow.Data.Model.Dictionary parentDict = dictList.Find(p => p.ID == dict.ParentID); while (parentDict != null) { parentDict = dictList.Find(p => p.ID == parentDict.ParentID); parent++; } return parent; } /// /// 根据代码得到选项 /// /// /// /// public string GetOptionsByCode(string code, OptionValueField valueField = OptionValueField.Value, string value="") { return GetOptionsByID(GetIDByCode(code), valueField, value); } /// /// 根据ID得到单选项 /// /// /// 名称 /// /// /// 其它属性 /// public string GetRadiosByID(Guid id, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "") { var childs = GetChilds(id, true); return getRadios(childs, name, valueField, value, attr); } /// /// 根据代码得到单选项 /// /// /// 名称 /// /// /// 其它属性 /// public string GetRadiosByCode(string code, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr="") { if (code.IsNullOrEmpty()) return ""; var childs = GetChilds(code.Trim(), true); return getRadios(childs, name, valueField, value, attr); } private string getRadios(List childs, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "") { StringBuilder options = new StringBuilder(childs.Count * 100); foreach (var child in childs) { string value1 = getOptionsValue(valueField, child); options.Append(""); options.AppendFormat("", name, child.ID.ToString("N"), child.Title); } return options.ToString(); } /// /// 根据ID得到多选项 /// /// /// 名称 /// /// /// 其它属性 /// public string GetCheckboxsByID(Guid id, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "") { var childs = GetChilds(id, true); return getCheckboxs(childs, name, valueField, value, attr); } /// /// 根据代码得到多选项 /// /// /// 名称 /// /// /// 其它属性 /// public string GetCheckboxsByCode(string code, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "") { if (code.IsNullOrEmpty()) return ""; var childs = GetChilds(code.Trim(), true); return getCheckboxs(childs, name, valueField, value, attr); } private string getCheckboxs(List childs, string name, OptionValueField valueField = OptionValueField.Value, string value = "", string attr = "") { StringBuilder options = new StringBuilder(childs.Count * 100); foreach (var child in childs) { string value1 = getOptionsValue(valueField, child); options.Append(""); options.AppendFormat("", name, child.ID.ToString("N"), child.Title); } return options.ToString(); } private string getOptionsValue(OptionValueField valueField, RoadFlow.Data.Model.Dictionary dict) { string value = string.Empty; switch (valueField) { case OptionValueField.Code: value = dict.Code; break; case OptionValueField.ID: value = dict.ID.ToString(); break; case OptionValueField.Note: value = dict.Note; break; case OptionValueField.Other: value = dict.Other; break; case OptionValueField.Title: value = dict.Title; break; case OptionValueField.Value: value = dict.Value; break; } return value; } /// /// 刷新字典缓存 /// public void RefreshCache() { RoadFlow.Cache.IO.Opation.Set(cacheKey, GetAll()); } /// /// 检查代码是否存在 /// /// /// /// public bool HasCode(string code, string id="") { if (code.IsNullOrEmpty()) { return false; } var dict = GetByCode(code.Trim()); Guid gid; if (dict == null) { return false; } else { if (id.IsGuid(out gid) && dict.ID == gid) { return false; } else { return true; } } } /// /// 删除一个字典及其所有下级 /// /// /// public int DeleteAndAllChilds(Guid id) { int i = 0; var childs = GetAllChilds(id); foreach (var child in childs) { Delete(child.ID); i++; } Delete(id); i++; return i; } /// /// 得到标题 /// /// /// public string GetTitle(Guid id) { var dict = Get(id, true); return dict == null ? "" : dict.Title; } /// /// 得到标题 /// /// /// public string GetTitle(string code) { if (code.IsNullOrEmpty()) return ""; var dict = GetByCode(code.Trim(), true); return dict == null ? "" : dict.Title; } /// /// 得到标题 /// /// /// /// public string GetTitle(string code, string value) { if (code.IsNullOrEmpty()) return ""; var childs = getChildsByCodeFromCache(code.Trim()); var child = childs.Find(p => p.Value == value); return child == null ? "" : child.Title; } /// /// 根据代码得到ID /// /// /// public Guid GetIDByCode(string code) { var dict = GetByCode(code, true); return dict == null ? Guid.Empty : dict.ID; } /// /// 得到combox表格html /// /// /// /// /// public string GetComboxTableHtmlByID(string id, RoadFlow.Platform.Dictionary.OptionValueField valueField, string defaultValue) { if (!id.IsGuid()) { return ""; } var dicts = GetChilds(id.ToGuid()); StringBuilder html = new StringBuilder(2000); html.Append(""); foreach (var dict in dicts) { html.Append(""); html.AppendFormat(""); html.Append(""); html.Append(""); html.Append(""); } html.Append(""); html.Append("
标题备注其它
", dict.ID, dict.ID.ToString().Equals(defaultValue, StringComparison.CurrentCultureIgnoreCase) ? " selected=\"selected\"" : ""); html.Append(dict.Title); html.Append(""); html.Append(dict.Note); html.Append(""); html.Append(dict.Other); html.Append("
"); return html.ToString(); } } }