using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace RoadFlow.Platform { public class AppLibrary { private string cacheKey = RoadFlow.Utility.Keys.CacheKeys.AppLibrary.ToString(); private RoadFlow.Data.Interface.IAppLibrary dataAppLibrary; public AppLibrary() { this.dataAppLibrary = Data.Factory.Factory.GetAppLibrary(); } /// /// 新增 /// public int Add(RoadFlow.Data.Model.AppLibrary model) { return dataAppLibrary.Add(model); } /// /// 更新 /// public int Update(RoadFlow.Data.Model.AppLibrary model) { return dataAppLibrary.Update(model); } /// /// 查询所有记录 /// public List GetAll(bool fromCache=false) { if (!fromCache) { return dataAppLibrary.GetAll(); } else { object obj = RoadFlow.Cache.IO.Opation.Get(cacheKey); if (obj != null) { return obj as List; } else { var list = dataAppLibrary.GetAll(); RoadFlow.Cache.IO.Opation.Set(cacheKey, list); return list; } } } /// /// 查询单条记录 /// public RoadFlow.Data.Model.AppLibrary Get(string id, bool fromCache=false) { if (!fromCache) { return dataAppLibrary.Get(id); } else { var all = GetAll(true); var app = all.Find(p => p.ID == id); return app == null ? dataAppLibrary.Get(id) : app; } } /// /// 清除缓存 /// public void ClearCache() { RoadFlow.Cache.IO.Opation.Remove(cacheKey); } /// /// 删除 /// public int Delete(Guid id) { return dataAppLibrary.Delete(id); } /// /// 查询记录条数 /// public long GetCount() { return dataAppLibrary.GetCount(); } /// /// 查询一个类别下所有记录 /// public List GetAllByType(Guid type) { if (type.IsEmptyGuid()) { return new List(); } return dataAppLibrary.GetAllByType(GetAllChildsIDString(type)).OrderBy(p=>p.Title).ToList(); } /// /// 删除记录 /// public int Delete(string[] idArray) { return dataAppLibrary.Delete(idArray); } /// /// 删除记录 /// public int Delete(string idstring) { return idstring.IsNullOrEmpty() ? 0 : dataAppLibrary.Delete(idstring.Split(',')); } /// /// 得到类型选择项 /// /// public string GetTypeOptions(string value="") { return new Dictionary().GetOptionsByCode("AppLibraryTypes", Dictionary.OptionValueField.ID, value); } /// /// 得到下级ID字符串 /// /// /// public string GetAllChildsIDString(Guid id, bool isSelf = true) { return new Dictionary().GetAllChildsIDString(id, true); } /// /// 得到一个类型选择项 /// /// 程序类型 /// /// public string GetAppsOptions(Guid type, string value = "") { if (type.IsEmptyGuid()) return ""; var apps = GetAllByType(type); StringBuilder options = new StringBuilder(); foreach (var app in apps) { options.AppendFormat("", app.ID, string.Compare(app.ID.ToString(), value, true) == 0 ? "selected=\"selected\"" : "", app.Title ); } return options.ToString(); } /// /// 根据ID得到类型 /// /// /// public string GetTypeByID(string id) { var app = Get(id); return app == null ? "" : app.Type.ToString(); } /// /// 根据代码查询一条记录 /// public RoadFlow.Data.Model.AppLibrary GetByCode(string code) { return code.IsNullOrEmpty() ? null : dataAppLibrary.GetByCode(code.Trim()); } /// /// 得到流程运行时地址 /// /// /// public string GetFlowRunAddress(RoadFlow.Data.Model.AppLibrary app, string query="") { StringBuilder sb = new StringBuilder(); if (app.Params.IsNullOrEmpty()) { if (!app.Address.Contains("?")) { sb.Append(app.Address); sb.Append("?1=1"); } } else { if (app.Address.Contains("?")) { sb.Append(app.Address); sb.Append("&"); sb.Append(app.Params.TrimStart('?').TrimStart('&')); } else { sb.Append(app.Address); sb.Append("?"); sb.Append(app.Params.TrimStart('?').TrimStart('&')); } } if (!query.IsNullOrEmpty()) { sb.Append("&"); sb.Append(query.TrimStart('?').TrimStart('&')); } return sb.ToString(); } /// /// 更新应用程序库使用人员缓存 /// /// /// public List UpdateUseMemberCache(string appid) { string key = RoadFlow.Utility.Keys.CacheKeys.AppLibraryUseMember.ToString(); var obj = RoadFlow.Cache.IO.Opation.Get(key); Dictionary> dict; if (obj != null && obj is Dictionary>) { dict = obj as Dictionary>; } else { dict = new Dictionary>(); } var app = new AppLibrary().Get(appid); if (app == null) { return new List(); } if (dict.ContainsKey(appid)) { if (app.UseMember.IsNullOrEmpty()) { dict.Remove(appid); return new List(); } else { var userIDs = new Organize().GetAllUsersIdList(app.UseMember); dict[appid] = userIDs; return userIDs; } } else if(!app.UseMember.IsNullOrEmpty()) { var userIDs = new Organize().GetAllUsersIdList(app.UseMember); dict.Add(appid, userIDs); return userIDs; } return new List(); } /// /// 得到一个应用程序库的使用人员 /// /// /// public List GetUseMemberCache(string appid) { string key = RoadFlow.Utility.Keys.CacheKeys.AppLibraryUseMember.ToString(); var obj = RoadFlow.Cache.IO.Opation.Get(key); if (obj != null && obj is Dictionary>) { var dict = obj as Dictionary>; if (dict.ContainsKey(appid)) { return dict[appid]; } } var app = new AppLibrary().Get(appid); if (app == null || app.UseMember.IsNullOrEmpty()) { return new List(); } return UpdateUseMemberCache(appid); } /// /// 清除应用程序库的使用人员缓存 /// public void ClearUseMemberCache() { string key = RoadFlow.Utility.Keys.CacheKeys.AppLibraryUseMember.ToString(); Cache.IO.Opation.Remove(key); } } }