using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using WePlatform.BaseInfo; using WePlatform.Configuration; using WePlatform.EF; namespace WePlatform.SeedData { public class DefaultFunctionsCreator { private readonly WePlatformDbContext _context; public DefaultFunctionsCreator(WePlatformDbContext context) { _context = context; LangStr = "\r\n\r\n"; PermissionStr = ""; LangList = new List(); } public void Create() { _context.Database.ExecuteSqlCommand("TRUNCATE TABLE [dbo].[Sys_Functions]"); CreateFunctions("HTSystem", "后台管理系统", "0", FunctionTypeDefinition.Catalog, "Index", "Home", "", "fas fa-home", 0, "", "", false, "Pages", 0); Resource(); EngineModel(); WePlayer(); OpenPlatform(); System(); Write(); } #region 应急信息资源库 public void Resource() { CreateCatalog("ResourceMg", "HTSystem", "应急信息资源库云服务", 20, "fa fa-cogs"); CreateMenu("SceneMg", "ResourceMg", "情景库管理", 1, "SceneInfo", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("SceneMg", "情景", "Scene"); CreateBtn_Other("TemplateMg", "SceneMg", "下载Excel模板", 4, "fa fa-download", "_btnExportTemplate", "Scene/ExportTemplate"); CreateBtn_Other("ExportMg", "SceneMg", "导出情景", 5, "iconfont icon-download", "_btnExport", "Scene/Export"); CreateBtn_Other("ImportMg", "SceneMg", "导入情景", 6, "iconfont icon-upload", "_btnImport", "Scene/Import"); CreateMenu("BehaviorMg", "ResourceMg", "行为规则库管理", 2, "BehaviorInfo", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("BehaviorMg", "行为", "Behavior"); //CreateMenu("GuideMg", "应急知识引导库管理", "ResourceMg", 3, "Guide", "ResourceLib", "", "fa fa-cog"); //CreateBtn_Curd("GuideMg", "知识引导", "Guide"); GuideInfo(3); CreateMenu("EnvironReMg", "ResourceMg", "场景渲染资源库管理", 4, "EnvironResourceInfo", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("EnvironReMg", "资源", "EnvironResource"); BasicInfo(5); } private void GuideInfo(int sort) { CreateCatalog("GuideMg", "ResourceMg", "应急知识信息库", sort, "fa fa-cogs"); CreateMenu("PlanMg", "GuideMg", "应急预案库管理", 1, "Plan", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("PlanMg", "预案", "EmergencyPlan"); CreateMenu_NotShow("PlanContentMg", "GuideMg", "应急预案内容维护",2, "fa fa-cog","",""); CreateBtn_Curd("PlanContentMg", "预案", "EmergencyPlan"); CreateBtn_Other("SetUseMg", "PlanContentMg","设为当前",5, "fa fa-cog"); CreateMenu("KnowledgeMg", "GuideMg", "知识库管理", 3, "Knowledge", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("KnowledgeMg", "知识", "Knowledge"); CreateMenu("PromptMg", "GuideMg", "引导库管理", 4, "Prompt", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("PromptMg", "引导", "Prompt"); CreateMenu("SupplyMg", "GuideMg", "应急物资库管理", 5, "Supply", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("SupplyMg", "物资", "Supply"); } private void BasicInfo(int sort) { CreateCatalog("BasicMg", "ResourceMg", "基础信息库", sort, "fa fa-cogs"); CreateMenu("SceneCategoryMg", "BasicMg", "场景类别管理", 1, "SceneCategory", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("SceneCategoryMg", "场景类别", "SceneCategory"); CreateMenu("BehaviorRoleMg", "BasicMg", "行为角色管理", 2, "BehaviorRole", "ResourceLib", "", "fa fa-cog"); CreateBtn_Curd("BehaviorRoleMg", "行为角色", "BehaviorRole"); } #endregion #region 智能化推演模式支撑 private void EngineModel() { CreateCatalog("EngineModelMg", "HTSystem", "智能化推演模型服务", 25, "fa fa-cogs"); CreateMenu("EngineMg", "EngineModelMg", "推演引擎管理", 1, "Engine", "EngineModel", "", "fa fa-cog"); CreateBtn_Curd("EngineMg", "引擎", "Engine"); CreateMenu("ModelMg", "EngineModelMg", "推演模型管理", 2, "Model", "EngineModel", "", "fa fa-cog"); CreateBtn_Curd("ModelMg", "模型", "EngineModel"); CreateMenu("EvalModelMg", "EngineModelMg", "评估模型管理", 3, "EvalModel", "EngineModel", "", "fa fa-cog"); CreateBtn_Curd("EvalModelMg", "模型", "EvalModel"); CreateMenu("EvalTargetMg", "EngineModelMg", "评估指标管理", 4, "EvalTarget", "EngineModel", "", "fa fa-cog"); CreateBtn_Curd("EvalTargetMg", "指标", "EvalTarget"); CreateMenu("EvalReportTemplateMg", "EngineModelMg", "评估报告模板管理", 5, "ReportTemplate", "EngineModel", "", "fa fa-cog"); CreateBtn_Curd("EvalReportTemplateMg", "模板", "EvalReportTemplate"); //CreateMenu("EngineComponentMg", "EngineModelMg", "引擎事务组件管理", 4, "Component", "EngineModel", "", "fa fa-cog"); //CreateBtn_Curd("EngineComponentMg", "组件", "EngineComponent"); } #endregion #region 一站式演练策划 private void WePlayer() { CreateCatalog("WePlayerMg", "HTSystem", "一站式演练策划服务", 30, "fa fa-cogs"); CreateMenu("FlowMg", "WePlayerMg", "演练情景流策划", 1, "SceneFlow", "WePlayer", "", "fa fa-cog"); CreateBtn_Curd("FlowMg", "情景流", "SceneFlow"); CreateBtn_Other("Copy", "FlowMg", "拷贝情景流", 4,"fa fa-copy","", "SceneFlow/Copy"); //CreateBtn_Other("Solidify", "FlowMg", "固化情景流", 5,"fa fa-copy","", "SceneFlow/Solidify"); CreateMenu("PackageMg", "WePlayerMg", "演练方案包策划", 2, "Package", "WePlayer", "", "fa fa-cog"); CreateBtn_Curd("PackageMg", "方案包", "Package"); CreateBtn_Other("Copy","PackageMg", "拷贝方案包", 4,"fa fa-copy", "", "Package/Copy"); CreateBtn_Other("Solidify", "PackageMg", "固化方案包", 5,"fa fa-copy","", "Package/Solidify"); CreateBtn_NotShow("Detail", "PackageMg", "固化方案包详情", 6,"fa fa-list","None", "Package/Detail"); } #endregion #region 开放性平台基础服务 private void OpenPlatform() { CreateCatalog("OpenPlatformMg", "HTSystem", "开放性平台基础服务", 35, "fa fa-cogs"); CreateMenu("OpenApiMg", "OpenPlatformMg", "平台开放性接口", 1, "OpenApi", "OpenPlatform", "", "fa fa-cog"); CreateBtn_Query("OpenApiMg",0); //CreateMenu("SubApiMg", "OpenPlatformMg", "接口注册订阅管理", 2, "SubscriptionApi", "OpenPlatform", "", "fa fa-cog"); //CreateBtn_Query("SubApiMg", 0); //CreateBtn_Other("Audit","SubApiMg", "审核订阅",1,"fa fa-audit"); } #endregion #region SYSTEM private void System() { CreateCatalog("SystemMg", "HTSystem", "系统信息维护", 100, "fa fa-cogs"); CreateMenu("TenantMg", "SystemMg", "租户信息管理", 0, "SysTenants", "BaseSystem", "host"); CreateBtn_Curd("TenantMg", "租户", "Tenants"); CreateMenu("RoleMg", "SystemMg", "角色信息管理", 1, "SysRoles", "BaseSystem", "", "fa fa-users"); CreateBtn_Curd("RoleMg", "角色", "Roles"); CreateBtn_Other("Auth", "RoleMg", "角色授权", 4, "fa fa-cog", null, "Roles/Auth"); CreateMenu("UserMg", "SystemMg", "用户信息管理", 2, "SysUsers", "BaseSystem", "", "fa fa-user"); CreateBtn_Curd("UserMg", "用户", "Users"); CreateBtn_Other("Auth", "UserMg", "用户授权", 4, "fa fa-cog", null, "Users/Auth"); CreateBtn_Other("ResetPassword", "UserMg", "重置密码", 5, "fa fa-lock", "btnResetPwd", "Users/ResetPassword"); CreateBtn_Other("ResetLock", "UserMg", "解除锁定", 5, "fa fa-unlock", "btnResetLock", "Users/ResetLock"); CreateMenu("StateMg", "SystemMg", "系统字典信息管理", 3, "SysStates", "BaseSystem", "", "fa fa-book"); CreateBtn_Curd("StateMg", "字典", "States"); CreateMenu("SettingMg", "SystemMg", "系统配置信息管理", 4, "SysSettings", "BaseSystem", "", "fa fa-cog"); //CreateBtn_Curd("SettingMg", "配置", "Settings"); CreateBtn_Query("SettingMg", 0); CreateBtn_Update("配置", "SettingMg", 2, "Settings"); CreateMenu("HelpMg", "SystemMg", "系统帮助信息管理", 5, "SysHelps", "BaseSystem", "", "fa fa-question"); CreateBtn_Curd("HelpMg", "帮助", "Helps"); CreateMenu("FunctionMg", "SystemMg", "功能菜单管理", 6, "SysFunctions", "BaseSystem", "", "fa fa-server"); CreateBtn_Curd("FunctionMg", "菜单", "Functions"); CreateBtn_Other("MoveUp", "FunctionMg", "上移菜单", 4, "fa fa-arrow-up", "btnMoveUp", "Functions/MoveUp"); CreateBtn_Other("MoveDown", "FunctionMg", "下移菜单", 5, "fa fa-arrow-down", "btnMoveDown", "Functions/MoveDown"); CreateBtn_Other("Refresh", "FunctionMg", "刷新菜单", 6, "fa fa-retweet", "btnRefresh", "Functions/Refresh"); CreateMenu("LogMg", "SystemMg", "系统日志管理", 7, "SysLogs", "BaseSystem", "", "fa fa-list-alt"); CreateBtn_Query("LogMg", 0); CreateMenu("UserHelpInfo", "HTSystem", "系统帮助信息", 101, "Index", "Help", "", "fa fa-question"); CreateMenu("UserSysSetting", "HTSystem", "用户配置", 102, "", "", icon: "fa fa-cogs", isShow: false); CreateBtn_Other("LoginImage", "UserSysSetting", "配置登陆页背景", 1, "fa fa-cog", "_Admin_btnLoginImage", "Settings/LoginImage", false); CreateBtn_Other("HomeImage", "UserSysSetting", "配置主页背景", 2, "fa fa-cog", "_Admin_btnHomeImage", "Settings/HomeImage", false); CreateBtn_Other("RefreshCache", "UserSysSetting", "刷新缓存", 3, "fa fa-recycle", "_Admin_btnCache", "Settings/CacheRefresh", false); CreateBtn_Other("RefreshLang", "UserSysSetting", "刷新语言包", 4, "fa fa-recycle", "_Admin_btnLang", "Settings/LangRefresh", false); } #endregion #region CREATE /// ///创建增删改查按钮 /// /// /// /// {WePlatformConsts.ApiAppUrl}{url}/Create 如果url与parentNo不同需指定 private void CreateBtn_Curd(string parentNo, string funName, string url = null) { CreateBtn_Query(parentNo, 0); CreateBtn_Create(funName, parentNo, 1, url); CreateBtn_Update(funName, parentNo, 2, url); CreateBtn_Delete(funName, parentNo, 3, url); } /// ///创建不是增删改查的按钮 /// private void CreateBtn_Other(string funNo, string parentNo, string funName, int sort, string icon = "far fa-plus-square", string script = null, string url = null, bool isShow = true, bool isApiUrl = true) { script = script ?? $"btn{funNo}"; url = url ?? $"{parentNo}/{funNo}"; url = isApiUrl ? $"{IwbConsts.ApiAppUrl}{url}" : url; if (isShow) { CreateBtn(funNo, parentNo, funName, sort, url, icon, script); } else { CreateBtn_NotShow(funNo, parentNo, funName, sort, icon, script, url); } } /// /// 创建目录 /// private void CreateCatalog(string funNo, string parentNo, string funName, int sort, string icon = "fa fa-indent", string s = "", string c = "", bool auth = true, bool isShow = true) { if (isShow) { CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Catalog, "", "", "", icon, sort, c, s, auth); } else { CreateCatalog_NotShow(funNo, parentNo, funName, sort, icon, c, s, auth); } } /// /// 创建菜单 /// private void CreateMenu(string funNo, string parentNo, string funName, int sort, string action, string controller, string s = "", string icon = "fa fa-bullseye", string c = "", string url = null, bool auth = true, bool isShow = true) { url = url ?? $"{controller}/{action}"; if (isShow) { CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Menu, action, controller, url, icon, sort, c, s, auth); } else { CreateMenu_NotShow(funNo, parentNo, funName, sort, icon, action, controller, url, c, s, auth); } } /// /// 创建查询按钮 /// private void CreateBtn_Query(string parentNo, int sort) { CreateBtn_NotShow("Query", parentNo, "查看页面", sort, "fa fa-eye", "btnQuery", ""); } /// /// 创建添加按钮 /// private void CreateBtn_Create(string funName, string parentNo, int sort, string url = null) { url = url ?? parentNo; CreateBtn("Create", parentNo, $"添加{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Create", "far fa-plus-square", "_btnCreate"); } /// /// 创建修改按钮 /// private void CreateBtn_Update(string funName, string parentNo, int sort, string url = null) { url = url ?? parentNo; CreateBtn("Update", parentNo, $"修改{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Update", "far fa-edit", "btnUpdate"); } /// /// 创建删除按钮 /// private void CreateBtn_Delete(string funName, string parentNo, int sort, string url = null) { url = url ?? parentNo; CreateBtn("Delete", parentNo, $"删除{funName}", sort, $"{IwbConsts.ApiAppUrl}{url}/Delete", "far fa-minus-square", "btnDelete"); } /// /// 创建按钮 /// private void CreateBtn(string funNo, string parentNo, string funName, int sort, string url, string icon, string s, string c = "btn btn-default", string action = "", string controller = "", bool auth = true) { CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.Button, action, controller, url, icon, sort, c, s, auth); } /// /// 创建不显示的目录 /// private void CreateCatalog_NotShow(string funNo, string parentNo, string funName, int sort, string icon, string c = "", string s = "", bool auth = true) { CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, "", "", "", icon, sort, c, s, auth); } /// /// 创建不显示的菜单 /// private void CreateMenu_NotShow(string funNo, string parentNo, string funName, int sort, string icon, string action, string controller, string url = "", string c = "", string s = "", bool auth = true) { CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, action, controller, url, icon, sort, c, s, auth); } /// /// 创建不显示的按钮 /// private void CreateBtn_NotShow(string funNo, string parentNo, string funName, int sort, string icon, string s, string url, string c = "btn btn-default", string action = "", string controller = "", bool auth = true) { CreateFunctions(funNo, funName, parentNo, FunctionTypeDefinition.NotShow, action, controller, url, icon, sort, c, s, auth); } /// /// 创建功能菜单 /// private void CreateFunctions(string funNo, string funName, string parentNo, int funType, string action, string controller, string url, string icon, int sort, string @class, string script, bool auth = true, string path = "", int? depth = null) { var fun = _context.SysFunctions.FirstOrDefault(e => e.FunctionNo == funNo && e.ParentNo == parentNo); if (fun == null) { string permissionName = path.Replace(",", "."); if (string.IsNullOrEmpty(path)) { var parentFun = _context.SysFunctions.FirstOrDefault(f => f.FunctionNo == parentNo); if (parentFun != null) { //path = parentFun.FunctionPath + "," + funNo; permissionName = parentFun.PermissionName + "." + funNo; depth = depth ?? parentFun.Depth + 1; } else { throw new Exception($"未查询到父菜单【{funName}-No:{funNo}-ParentNo:{parentNo}】"); } } url = url ?? controller + "/" + action; fun = new SysFunction { FunctionNo = funNo, ParentNo = parentNo, FunctionName = funName, PermissionName = permissionName, //FunctionPath = path, FunctionType = funType, Controller = controller, Action = action, Icon = icon, Sort = sort, Depth = depth ?? 0, Class = @class, Script = script, Url = url, NeedAuth = auth }; _context.SysFunctions.Add(fun); _context.SaveChanges(); } var permName = fun.PermissionName.Replace(".", ""); PermissionStr += $" public const string {permName} = \"{fun.PermissionName}\";\r\n"; if (!LangList.Contains(permName)) { LangList.Add(permName); LangStr += $" \r\n"; } } #endregion #region WRITE private List LangList { get; set; } private string LangStr { get; set; } private string PermissionStr { get; set; } private void Write() { WritePermission(); WriteLang(); } private void WritePermission() { if (string.IsNullOrEmpty(PermissionStr)) { return; } try { string content = "namespace WePlatform.Authorization\r\n"; content += "{\r\n"; content += " public static class PermissionNames\r\n"; content += " {\r\n"; content += PermissionStr; content += " }\r\n"; content += "}\r\n"; var filePath = Path.Combine(GetBasePath(), "WePlatform.Core\\Authorization\\PermissionNames.cs"); var fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite);//清空文件内容 fs.Close(); using (FileStream fr = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) { fr.Seek(0, SeekOrigin.Begin); byte[] bytes = Encoding.UTF8.GetBytes(content); fr.Write(bytes, 0, bytes.Length); fr.Flush(); } } catch (Exception e) { throw (new Exception("Permission:" + e.Message)); } } private void WriteLang() { if (string.IsNullOrEmpty(LangStr)) { return; } //LangStr = $"\r\n{LangStr}\r\n\r\n"; LangStr += "\r\n"; try { var filePath = Path.Combine(GetBasePath(), "WePlatform.Web\\Localization\\SourceFiles\\WePlatform-zh-Hans.xml"); string content; using (FileStream fr = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { fr.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[fr.Length]; fr.Read(bytes, 0, bytes.Length); content = Encoding.UTF8.GetString(bytes); //Regex regex = new Regex("(.*)"); //string content2 = regex.Replace(content, LangStr); string first = ""; string second = ""; if (!string.IsNullOrEmpty(content) && content.Contains(first) && content.Contains(second)) { var start = content.IndexOf(first, 0, StringComparison.Ordinal) + first.Length; var end = content.IndexOf(second, start, StringComparison.Ordinal); string temp = content.Substring(start, end - start); content = content.Replace(temp, LangStr); } else { throw new Exception("Lang:语言配置信息没有生成!"); } } if (!string.IsNullOrEmpty(content)) { var fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite);//清空文件内容 fs.Close(); using (FileStream fr = new FileStream(filePath, FileMode.Append, FileAccess.Write)) { byte[] newBytes = Encoding.UTF8.GetBytes(content); fr.Seek(0, SeekOrigin.Begin); fr.Write(newBytes, 0, newBytes.Length); fr.Flush(); } } } catch (Exception e) { throw (new Exception("Lang:" + e.Message)); } } private string GetBasePath() { var basePath = AppDomain.CurrentDomain.BaseDirectory; //string basePath = path.Contains("\\We.Platform") // ? AppDomain.CurrentDomain.BaseDirectory.Substring(0, // path.LastIndexOf("\\We.Platform", StringComparison.Ordinal)) // : Path.Combine(AppDomain.CurrentDomain.BaseDirectory.Substring(0, // path.LastIndexOf("\\WisdomExercisePlatform", StringComparison.Ordinal)), "We.Platform"); string path= Path.Combine(basePath.Substring(0,basePath.LastIndexOf("\\EngineRefactor", StringComparison.Ordinal)), "EngineRefactor\\We.Platform"); return path; } #endregion } }