using Models; using Models.ModelItems; using Services.Cache; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace Services.Manager { public class AppServer { /// /// 查询 app 不查询禁用的权限 /// /// /// /// #region 单个App权限信息 private static dynamic GetAppInfo(int id,int IsValid = 10,bool reauthorities = true) { ApplicationDomain app = Cache.CacheHelp.cacheSysApp.FirstOrDefault(x=>x.Id == id); if(app == null) return null; List autho = Cache.CacheHelp.cacheSysAppAutho.Where(x => x.AppId == id).ToList(); List authorities = new List(); if(reauthorities) authorities = Cache.CacheHelp.cacheSysAutho.Where(x=> autho.FirstOrDefault(y=>y.AuthorityId == x.Id)!=null && x.IsValid != IsValid).ToList(); return new { data = authorities, AppName = app.AppName, AppId = app.Id, Desc = app.Desc, app.WebSite, app.AppType, app.Version, app.ReleaseDate, app.Icon, app.DowSum }; } #endregion #region 所有App权限信息 /// /// 所有正常App权限信息 查询所有就默认值即可 查询正常的权限 输入1 查询关闭的权限 输入 0 /// /// /// 是否返回权限信息 /// public static List GetAppInfo( int appid = 0, int appIsValid = 0 ,int IsValid = 0,bool reauthorities = true) { List res = new List(); foreach (var app in CacheHelp.cacheSysApp.Where(x =>( x.IsValid == appIsValid || appIsValid == -1) && (appid == x.Id || appid == 0)).ToList()) { res.Add(GetAppInfo(app.Id, IsValid,reauthorities)); } return res; } #endregion #region 查询App名是否存 public static bool CheckName(string name) { return Cache.CacheHelp.cacheSysApp.FirstOrDefault(x=>x.AppName == name && x.IsValid == 0) == null; } #endregion #region 添加App public static int? AddApp(string name, string Desc = "", string uid = "", string Version = "1.0", string appicon = null, DateTime ReleaseDate = default, string WebSite = "",int AppType =0) { if (CacheHelp.cacheSysApp.FirstOrDefault(u => u.AppName == name && u.IsValid == 0) != null) return null; ApplicationDomain app = new ApplicationDomain { AppName = name, Desc = Desc, CreatedBy = uid, ReleaseDate = ReleaseDate, WebSite = WebSite, Version = Version, AppType = AppType, CreateTime = DateTime.Now, Icon = appicon }; int id= SqlSugarBase.Db.Insertable(app).ExecuteReturnIdentity(); DbLogServer.WriteDbLog($"添加了应用{app.AppName}({id})"); CacheHelp.Removesys(CacheHelp.syskey.sysAppsListKey); return id; } #endregion #region 删除应用 public static bool DelApp(int appid,int IsValid, string uid = null) { try { ApplicationDomain app = SqlSugarBase.Db.Queryable().First(x => x.Id == appid); if (app == null) return false; app.IsValid = IsValid; app.CreatedBy = uid; SqlSugarBase.Db.Updateable(app).ExecuteCommand(); if (IsValid!=0) DbLogServer.WriteDbLog($"删除了应用{app.AppName}({app.Id})",2); else DbLogServer.WriteDbLog($"恢复了应用{app.AppName}({app.Id})", 1); CacheHelp.Removesys(CacheHelp.syskey.sysAppsListKey); return true; } catch (Exception ex) { LogHelp.WriteExceptionLog(ex); return false; } finally { CacheHelp.Removesys(CacheHelp.syskey.Appversions); } } #endregion #region 检测权限名 public static bool Checkautho(int AppId, string AuthoNmae) { var au = CacheHelp.cacheView_AppAutho.FirstOrDefault(x => x.AppId == AppId && x.AuthorityName == AuthoNmae); if (au == null) return true; else return false; } #endregion #region 添加权限 public static int? Addautho(int AppId, string AuthoNmae, string Desc = "", string uid = null, string authoStatusTypeId = null) { try { dynamic au = CacheHelp.cacheView_AppAutho.FirstOrDefault(x => x.AppId == AppId && x.AuthorityName == AuthoNmae); if (au != null) return null; au = CacheHelp.cacheSysApp.FirstOrDefault(x => x.Id == AppId); if (au == null) return null; Authority authority = new Authority { AuthorityName = AuthoNmae, Desc = Desc,CreatedBy = uid, AuthoStatusTypeId = authoStatusTypeId }; int id= SqlSugarBase.Db.Insertable(new Authority { AuthorityName = AuthoNmae, Desc = Desc, CreatedBy = uid, AuthoStatusTypeId = authoStatusTypeId }).ExecuteReturnIdentity(); SqlSugarBase.Db.Insertable(new AppAutho() { AppId = AppId, AuthorityId = id, CreatedBy = uid }).ExecuteCommand(); DbLogServer.WriteDbLog($"给应用{au.AppName}({au.Id})添加了权限'{AuthoNmae}({id})'",0); CacheHelp.Removesys(CacheHelp.syskey.sysView_AppAuthoListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAuthorityListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAppAuthoListKey); return id; } catch (Exception ex) { LogHelp.WriteExceptionLog(ex); return null; } finally { CacheHelp.Removesys(CacheHelp.syskey.Appversions); } } #endregion #region 删除权限 public static bool Delautho(int authoId, int status = 0, string uid = null) { var au = CacheHelp.cacheSysAutho.FirstOrDefault(x=>x.Id == authoId); var app = CacheHelp.cacheView_AppAutho.FirstOrDefault(x=>x.AuthorityId == authoId); if (au == null || app == null) return false; var autho = SqlSugarBase.Db.Queryable().First(x => x.Id == authoId); autho.IsValid = status; SqlSugarBase.Db.Updateable(autho).ExecuteCommand(); if(status==1) DbLogServer.WriteDbLog($"禁用了应用{app.AppName}({app.AppId})权限{au.AuthorityName}({au.Id}).",2); else DbLogServer.WriteDbLog( $"恢复了应用{app.AppName}({app.AppId})权限{au.AuthorityName}({au.Id}).", 2); CacheHelp.Removesys(CacheHelp.syskey.sysView_AppAuthoListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAuthorityListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAppAuthoListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAppAuthoListKey); return true; } #endregion #region 编辑权限 public static bool Editautho(Authority authority, string uid) { try { if (authority.Id <= 0) return false; var auc = CacheHelp.cacheSysAutho.FirstOrDefault(x => x.Id == authority.Id); var app = CacheHelp.cacheView_AppAutho.FirstOrDefault(x => x.AuthorityId == authority.Id); if (auc == null || app == null) return false; var au = SqlSugarBase.Db.Queryable().First(x => x.Id == authority.Id); au.Desc = authority.Desc ?? au.Desc; au.AuthoStatusTypeId = authority.AuthoStatusTypeId ?? au.AuthoStatusTypeId; au.AuthorityName = authority.AuthorityName ?? au.AuthorityName; SqlSugarBase.Db.Updateable(au).ExecuteCommand(); DbLogServer.WriteDbLog( $"修改了{app.AuthorityName} ({app.AppId})权限{au.AuthorityName}({au.Id}).修改后为:" + JsonConvert.SerializeObject(au), 1); CacheHelp.Removesys(CacheHelp.syskey.sysView_AppAuthoListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAuthorityListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAppAuthoListKey); CacheHelp.Removesys(CacheHelp.syskey.sysAppAuthoListKey); return true; } catch (Exception ex) { LogHelp.WriteExceptionLog(ex); return false; } finally { CacheHelp.Removesys(CacheHelp.syskey.Appversions); } } #endregion #region 下载量添加 public static bool AddDown( int AppId,int sum = 1) { try { var app = SqlSugarBase.Db.Queryable().First(x => x.Id == AppId); app.DowSum+=sum; return SqlSugarBase.Db.Updateable(app).ExecuteCommand()>0; } catch (Exception ex) { LogHelp.WriteExceptionLog(ex); return false; }finally { CacheHelp.Removesys( CacheHelp.syskey.Appversions); CacheHelp.Removesys( CacheHelp.syskey.sysAppsListKey); } } #endregion } }