/// <summary> /// 创建SQL实体文件,指定表名 /// </summary> public void CreateClassFilesByTableNames(SqlSugarClient db, string fileDirectory, string nameSpace, params string[] tableNames) { var isLog = db.IsEnableLogEvent; db.IsEnableLogEvent = false; string sql = SqlSugarTool.GetCreateClassSql(null); var tables = db.GetDataTable(sql); if (!FileSugar.IsExistDirectory(fileDirectory)) { FileSugar.CreateDirectory(fileDirectory); } if (tables != null && tables.Rows.Count > 0) { foreach (DataRow dr in tables.Rows) { string tableName = dr["name"].ToString().ToLower(); if (tableNames.Any(it => it.ToLower() == tableName)) { var currentTable = db.GetDataTable(string.Format(SqlSugarTool.GetSelectTopSql(), GetTableNameWithSchema(db, tableName).GetTranslationSqlName())); var tableColumns = GetTableColumns(db, tableName); string className = db.GetClassTypeByTableName(tableName); var classCode = DataTableToClass(currentTable, className, nameSpace, tableColumns); FileSugar.CreateFile(fileDirectory.TrimEnd('\\') + "\\" + className + ".cs", classCode, Encoding.UTF8); } } } db.IsEnableLogEvent = isLog; }
/// <summary> /// 生成controller /// </summary> private void BuildController(string controllerDir, string controllerPath, string controllerDomainPath, XDocument doc) { //写入控制器 if (!FileSugar.IsExistFile(controllerPath)) { FileSugar.CreateDirectory(controllerDir); ContainFileToCsproj(@"Areas\{0}\Controllers\{1}App\{1}Controller.cs".ToFormat(_data.AreaName, _data.ControllerName), _solutionDir, _projectName, doc); } //获取controller.cshtml的Model BuildControllerModel controllerModel = new BuildControllerModel() { areaName = _data.AreaName, controllerName = _data.ControllerName, actionName = _data.ActionName, siteName = ConfigSugar.GetAppString("siteName") }; var apiList = _db.Queryable <SysBest_DataApi>().ToList();//获取所有api int addressId = _data.Id; int languageId = _languageId; var eleList = _db.Queryable <SysBest_Element>().Where(it => it.AddressId == addressId && it.LanguageId == languageId).ToList(); var eleApidList = eleList.Where(it => it.ApiId != null && it.ApiId > 0).Select(it => it.ApiId).Distinct().ToList();//获取所有元素 //获取元素表里面的数据接口 controllerModel.apiList = apiList.Join(eleApidList, api => api.Id, ele => ele, (api, ele) => api).ToList(); //获取属性表里面的数据接口 controllerModel.apiList.AddRange(GetDataApiWithEleEventByEleId(eleList)); controllerModel.apiList = controllerModel.apiList.Distinct().ToList(); string controllerCode = GetTemplateCodeByName("controller.cshtml", controllerModel); FileSugar.WriteFile(controllerPath, controllerCode); //写入Domain if (!FileSugar.IsExistFile(controllerDomainPath)) { var domainCode = GetTemplateCodeByName("controller_domain.cshtml", new { controllerName = _data.ControllerName, areaName = _data.AreaName }); FileSugar.WriteFile(controllerDomainPath, domainCode); ContainFileToCsproj(@"Areas\{0}\Controllers\{1}App\Domain\{1}Domain.cs".ToFormat(_data.AreaName, _data.ControllerName), _solutionDir, _projectName, doc); } }
/// <summary> /// 生成area /// </summary> private void BuildArea(string areaDir, string viewsDir, string controllerDomainDir, string areaRegistrationPath, string configPath, XDocument doc) { //创建area if (!FileSugar.IsExistDirectory(areaDir)) { //创建目录 FileSugar.CreateDirectory(areaDir); //创建area FileSugar.CreateDirectory(viewsDir); //创建views FileSugar.CreateDirectory(controllerDomainDir); //创建文件 //创建文件 var areaRegistrationCode = GetTemplateCodeByName("area_gobal.cshtml", new { projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, area = _data.AreaName }); FileSugar.WriteFile(areaRegistrationPath, areaRegistrationCode); ContainFileToCsproj(@"Areas\{0}\{0}AreaRegistration.cs".ToFormat(_data.AreaName), _solutionDir, _projectName, doc); ContainConfigToCsproj(@"Areas\{0}\Views\Web.config".ToFormat(_data.AreaName), _solutionDir, _projectName, doc); } var webConfigCode = GetTemplateCodeByName("area_webConfig.cshtml", new { }); FileSugar.WriteFile(configPath, webConfigCode); }