From dc6f41d96e4272ba4a6192988714d768bb8e22fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Sat, 9 Dec 2023 07:42:05 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/GlobalConstant.cs | 5 -- Infrastructure/Model/OptionsSetting.cs | 7 +- .../System/CodeGeneratorController.cs | 66 ++++++++++--------- ZR.Admin.WebApi/codeGen.json | 14 ++++ ZR.CodeGenerator/CodeGeneratorTool.cs | 55 ++++++++-------- ZR.CodeGenerator/GenConstants.cs | 5 +- .../Service/CodeGeneraterService.cs | 2 +- ZR.ServiceCore/Model/Dto/SqlDiffLogDto.cs | 4 +- ZR.ServiceCore/Services/GenTableService.cs | 2 +- 9 files changed, 86 insertions(+), 74 deletions(-) create mode 100644 ZR.Admin.WebApi/codeGen.json diff --git a/Infrastructure/GlobalConstant.cs b/Infrastructure/GlobalConstant.cs index ff72abe..e9a07f8 100644 --- a/Infrastructure/GlobalConstant.cs +++ b/Infrastructure/GlobalConstant.cs @@ -5,11 +5,6 @@ /// public class GlobalConstant { - /// - /// 代码生成常量 - /// - public static readonly string CodeGenDbConfig; - /// /// 管理员权限 /// diff --git a/Infrastructure/Model/OptionsSetting.cs b/Infrastructure/Model/OptionsSetting.cs index 552b781..890fe52 100644 --- a/Infrastructure/Model/OptionsSetting.cs +++ b/Infrastructure/Model/OptionsSetting.cs @@ -23,7 +23,7 @@ namespace Infrastructure.Model public Upload Upload { get; set; } public ALIYUN_OSS ALIYUN_OSS { get; set; } public JwtSettings JwtSettings { get; set; } - public Gen Gen { get; set; } + public CodeGen CodeGen { get; set; } public List DbConfigs { get; set; } public DbConfigs CodeGenDbConfig { get; set; } } @@ -94,13 +94,14 @@ namespace Infrastructure.Model public string TokenType { get; set; } = "Bearer"; } - public class Gen + public class CodeGen { public bool ShowApp { get; set; } public bool AutoPre { get; set; } public string VuePath { get; set; } public string Author { get; set; } - public DbConfigs GenDbConfig { get; set; } + public string TablePrefix { get; set; } + public string ModuleName { get; set; } public CsharpTypeArr CsharpTypeArr { get; set; } } diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs index ca047ae..7ca442f 100644 --- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs @@ -132,43 +132,48 @@ namespace ZR.Admin.WebApi.Controllers /// /// 导入表结构(保存) /// - /// - /// + /// /// [HttpPost("importTable")] [Log(Title = "代码生成", BusinessType = BusinessType.IMPORT)] [ActionPermissionFilter(Permission = "tool:gen:import")] - public IActionResult ImportTableSave(string tables, string dbName) + public IActionResult ImportTableSave([FromBody] ImportCodeGenTableDto dto) { - if (string.IsNullOrEmpty(tables)) + if (string.IsNullOrEmpty(dto.DbName) || dto.Tables == null) { - throw new CustomException("表不能为空"); + throw new CustomException("表或数据库不能为空"); } - DbConfigs dbConfig = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); - string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries); + DbConfigs dbConfig = AppSettings.Get(nameof(GenConstants.CodeGenDbConfig)); + CodeGen codeGen = AppSettings.Get("codeGen"); + int result = 0; - foreach (var tableName in tableNames) + foreach (var table in dto.Tables) { - var tabInfo = _CodeGeneraterService.GetTableInfo(dbName, tableName); - if (tabInfo != null) + List seqs = new(); + InitTableDto initTableDto = new() { - List seqs = new(); - GenTable genTable = CodeGeneratorTool.InitTable(dbName, HttpContext.GetName(), tableName, tabInfo?.Description); - genTable.TableId = GenTableService.ImportGenTable(genTable); - if (dbConfig.DbType == 3) - { - seqs = _CodeGeneraterService.GetAllOracleSeqs(dbName); - } - if (genTable.TableId > 0) - { - //保存列信息 - List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, tableName); - List genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos, seqs); - GenTableColumnService.DeleteGenTableColumnByTableName(tableName); - GenTableColumnService.InsertGenTableColumn(genTableColumns); - genTable.Columns = genTableColumns; - result++; - } + DbName = dto.DbName, + UserName = HttpContext.GetName(), + TableName = table.Name, + Desc = table.Description, + CodeGen = codeGen + }; + + GenTable genTable = CodeGeneratorTool.InitTable(initTableDto); + genTable.TableId = GenTableService.ImportGenTable(genTable); + if (dbConfig.DbType == 3) + { + seqs = _CodeGeneraterService.GetAllOracleSeqs(table.Name); + } + if (genTable.TableId > 0) + { + //保存列信息 + List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dto.DbName, table.Name); + List genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos, seqs, codeGen); + GenTableColumnService.DeleteGenTableColumnByTableName(table.Name); + GenTableColumnService.InsertGenTableColumn(genTableColumns); + genTable.Columns = genTableColumns; + result++; } } @@ -222,7 +227,7 @@ namespace ZR.Admin.WebApi.Controllers throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); - var dbConfig = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); + var dbConfig = AppSettings.Get(nameof(GenConstants.CodeGenDbConfig)); dto.DbType = dbConfig.DbType; dto.GenTable = genTableInfo; @@ -248,7 +253,7 @@ namespace ZR.Admin.WebApi.Controllers throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); - var dbConfig = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); + var dbConfig = AppSettings.Get(nameof(GenConstants.CodeGenDbConfig)); dto.DbType = dbConfig.DbType; dto.GenTable = genTableInfo; @@ -314,8 +319,9 @@ namespace ZR.Admin.WebApi.Controllers if (table == null) { throw new CustomException("同步数据失败,原表结构不存在"); } table.Update_by = HttpContext.GetName(); + var codeGen = AppSettings.Get("codeGen"); List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName); - List dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos); + List dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos, codeGen: codeGen); bool result = GenTableService.SynchDb(tableId, table, dbTableColumns); return SUCCESS(result); diff --git a/ZR.Admin.WebApi/codeGen.json b/ZR.Admin.WebApi/codeGen.json new file mode 100644 index 0000000..4ec1c60 --- /dev/null +++ b/ZR.Admin.WebApi/codeGen.json @@ -0,0 +1,14 @@ +{ + "CodeGen": { + "csharpTypeArr": { + "string": [ "varchar", "nvarchar", "text", "longtext" ], + "int": [ "int", "integer", "smallint", "int4", "int8", "int2" ], + "long": [ "bigint", "number" ], + "float": [ "numeric", "real", "float" ], + "decimal": [ "money", "decimal", "smallmoney" ], + "dateTime": [ "date", "datetime", "datetime2", "smalldatetime", "timestamp" ], + "byte": [ "tinyint" ], + "bool": [ "bit" ] + } + } +} diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index 138c008..214c2eb 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Xml.Linq; using ZR.CodeGenerator.Model; using ZR.Model.System.Generate; @@ -27,7 +28,7 @@ namespace ZR.CodeGenerator /// public static void Generate(GenerateDto dto) { - var genOptions = AppSettings.Get("gen"); + var genOptions = AppSettings.Get("codeGen"); dto.VueParentPath = dto.VueVersion == 3 ? "ZRAdmin-vue" : "ZR.Vue"; if (!genOptions.VuePath.IsEmpty()) { @@ -346,11 +347,8 @@ namespace ZR.CodeGenerator /// /// /// - public static string GetClassName(string tableName) + public static string GetClassName(string tableName, bool autoRemovePre, string tablePrefix) { - bool autoRemovePre = AppSettings.GetAppConfig(GenConstants.Gen_autoPre, false); - string tablePrefix = AppSettings.GetAppConfig(GenConstants.Gen_tablePrefix); - if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre) { string[] prefixList = tablePrefix.Split(",", StringSplitOptions.RemoveEmptyEntries); @@ -427,32 +425,31 @@ namespace ZR.CodeGenerator /// /// 初始化表信息 /// - /// - /// - /// - /// + /// /// - public static GenTable InitTable(string dbName, string userName, string tableName, string desc) + public static GenTable InitTable(InitTableDto dto) { + var className = GetClassName(dto.TableName, dto.CodeGen.AutoPre, dto.CodeGen.TablePrefix); + GenTable genTable = new() { - DbName = dbName, + DbName = dto.DbName, BaseNameSpace = "ZR.",//导入默认命名空间前缀 - ModuleName = "business",//导入默认模块名 - ClassName = GetClassName(tableName), - BusinessName = GetClassName(tableName), - FunctionAuthor = AppSettings.GetConfig(GenConstants.Gen_author), - TableName = tableName, - TableComment = desc, - FunctionName = desc, - Create_by = userName, + ModuleName = dto.CodeGen.ModuleName,//导入默认模块名 + ClassName = className, + BusinessName = className, + FunctionAuthor = dto.CodeGen.Author, + TableName = dto.TableName, + TableComment = dto.Desc, + FunctionName = dto.Desc, + Create_by = dto.UserName, Options = new Options() { SortType = "asc", - CheckedBtn = new int[] { 1, 2, 3 } + CheckedBtn = new int[] { 1, 2, 3 }, + PermissionPrefix = className.ToLower() } }; - genTable.Options.PermissionPrefix = $"{genTable.ClassName.ToLower()}";//权限 return genTable; } @@ -463,16 +460,16 @@ namespace ZR.CodeGenerator /// /// /// - public static List InitGenTableColumn(GenTable genTable, List dbColumnInfos, List seqs = null) + /// + public static List InitGenTableColumn(GenTable genTable, List dbColumnInfos, List seqs = null, CodeGen codeGen = null) { OptionsSetting optionsSetting = new(); - var gen = AppSettings.Get("gen"); - var dbConfig = AppSettings.Get("CodeGenDbConfig"); + var dbConfig = AppSettings.Get(nameof(GenConstants.CodeGenDbConfig)); + + optionsSetting.CodeGenDbConfig = dbConfig ?? throw new CustomException("代码生成节点数据配置异常"); ; + optionsSetting.CodeGen = codeGen ?? throw new CustomException("代码生成节点配置异常"); - optionsSetting.CodeGenDbConfig = dbConfig; - optionsSetting.Gen = gen ?? throw new CustomException("代码生成节点配置异常"); - optionsSetting.Gen.GenDbConfig = dbConfig ?? throw new CustomException("代码生成节点数据配置异常"); List genTableColumns = new(); foreach (var column in dbColumnInfos) { @@ -492,7 +489,7 @@ namespace ZR.CodeGenerator private static GenTableColumn InitColumnField(GenTable genTable, DbColumnInfo column, List seqs, OptionsSetting optionsSetting) { var dataType = column.DataType; - if (optionsSetting.Gen.GenDbConfig.DbType == 3) + if (optionsSetting.CodeGenDbConfig.DbType == 3) { dataType = column.OracleDataType; var seqName = $"SEQ_{genTable.TableName}_{column.DbColumnName}"; @@ -507,7 +504,7 @@ namespace ZR.CodeGenerator ColumnType = dataType, TableId = genTable.TableId, TableName = genTable.TableName, - CsharpType = GetCSharpDatatype(dataType, optionsSetting.Gen.CsharpTypeArr).ToString(), + CsharpType = GetCSharpDatatype(dataType, optionsSetting.CodeGen.CsharpTypeArr).ToString(), CsharpField = column.DbColumnName.ConvertToPascal("_"), IsRequired = !column.IsNullable, IsIncrement = column.IsIdentity, diff --git a/ZR.CodeGenerator/GenConstants.cs b/ZR.CodeGenerator/GenConstants.cs index 772cb2c..cc733dd 100644 --- a/ZR.CodeGenerator/GenConstants.cs +++ b/ZR.CodeGenerator/GenConstants.cs @@ -7,9 +7,8 @@ namespace ZR.CodeGenerator /// public class GenConstants { - public static string Gen_author = "gen:author"; - public static string Gen_autoPre = "gen:autoPre"; - public static string Gen_tablePrefix = "gen:tablePrefix"; + public static string Gen_author = "codeGen:author"; + public static string CodeGenDbConfig; /// /// InputDto输入实体是不包含字段 diff --git a/ZR.CodeGenerator/Service/CodeGeneraterService.cs b/ZR.CodeGenerator/Service/CodeGeneraterService.cs index 101a18e..9a21db3 100644 --- a/ZR.CodeGenerator/Service/CodeGeneraterService.cs +++ b/ZR.CodeGenerator/Service/CodeGeneraterService.cs @@ -18,7 +18,7 @@ namespace ZR.CodeGenerator.Service { var db = GetSugarDbContext(); //Oracle库特殊处理 - DbConfigs configs = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); + DbConfigs configs = AppSettings.Get(nameof(GenConstants.CodeGenDbConfig)); if (configs.DbType == 3) { return new List() { configs?.DbName }; diff --git a/ZR.ServiceCore/Model/Dto/SqlDiffLogDto.cs b/ZR.ServiceCore/Model/Dto/SqlDiffLogDto.cs index a0c1ea5..b90d6c2 100644 --- a/ZR.ServiceCore/Model/Dto/SqlDiffLogDto.cs +++ b/ZR.ServiceCore/Model/Dto/SqlDiffLogDto.cs @@ -38,10 +38,10 @@ namespace ZR.Model.System.Dto public string Sql { get; set; } [ExcelColumn(Name = "变更前数据", Width = 30)] - public string BeforeData { get; set; } + public string BeforeData { get; set; } = string.Empty; [ExcelColumn(Name = "变更后数据", Width = 30)] - public string AfterData { get; set; } + public string AfterData { get; set; } = string.Empty; [ExcelColumn(Name = "操作用户名")] public string UserName { get; set; } diff --git a/ZR.ServiceCore/Services/GenTableService.cs b/ZR.ServiceCore/Services/GenTableService.cs index 40df692..4484cd2 100644 --- a/ZR.ServiceCore/Services/GenTableService.cs +++ b/ZR.ServiceCore/Services/GenTableService.cs @@ -48,7 +48,7 @@ namespace ZR.ServiceCore.Services if (info != null) { info.Columns = GenTableColumnService.GenTableColumns(tableId); - if (!info.SubTableName.IsEmpty() && info.SubTable != null) + if (!info.SubTableName.IsEmpty()) { info.SubTable = Queryable().Where(f => f.TableName == info.SubTableName).First(); info.SubTable.Columns = GenTableColumnService.GenTableColumns(info.SubTable.TableId);