⚡代码生成优化
This commit is contained in:
parent
1316e14664
commit
dc6f41d96e
@ -5,11 +5,6 @@
|
||||
/// </summary>
|
||||
public class GlobalConstant
|
||||
{
|
||||
/// <summary>
|
||||
/// 代码生成常量
|
||||
/// </summary>
|
||||
public static readonly string CodeGenDbConfig;
|
||||
|
||||
/// <summary>
|
||||
/// 管理员权限
|
||||
/// </summary>
|
||||
|
||||
@ -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> 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; }
|
||||
}
|
||||
|
||||
|
||||
@ -132,43 +132,48 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// <summary>
|
||||
/// 导入表结构(保存)
|
||||
/// </summary>
|
||||
/// <param name="tables"></param>
|
||||
/// <param name="dbName"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[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<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
|
||||
string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
DbConfigs dbConfig = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
|
||||
CodeGen codeGen = AppSettings.Get<CodeGen>("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<OracleSeq> seqs = new();
|
||||
InitTableDto initTableDto = new()
|
||||
{
|
||||
List<OracleSeq> 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<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, tableName);
|
||||
List<GenTableColumn> 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<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dto.DbName, table.Name);
|
||||
List<GenTableColumn> 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<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
|
||||
var dbConfig = AppSettings.Get<DbConfigs>(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<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
|
||||
var dbConfig = AppSettings.Get<DbConfigs>(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>("codeGen");
|
||||
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName);
|
||||
List<GenTableColumn> dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos);
|
||||
List<GenTableColumn> dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos, codeGen: codeGen);
|
||||
|
||||
bool result = GenTableService.SynchDb(tableId, table, dbTableColumns);
|
||||
return SUCCESS(result);
|
||||
|
||||
14
ZR.Admin.WebApi/codeGen.json
Normal file
14
ZR.Admin.WebApi/codeGen.json
Normal file
@ -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" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
/// <param name="dto"></param>
|
||||
public static void Generate(GenerateDto dto)
|
||||
{
|
||||
var genOptions = AppSettings.Get<Gen>("gen");
|
||||
var genOptions = AppSettings.Get<CodeGen>("codeGen");
|
||||
dto.VueParentPath = dto.VueVersion == 3 ? "ZRAdmin-vue" : "ZR.Vue";
|
||||
if (!genOptions.VuePath.IsEmpty())
|
||||
{
|
||||
@ -346,11 +347,8 @@ namespace ZR.CodeGenerator
|
||||
/// </summary>
|
||||
/// <param name="tableName"></param>
|
||||
/// <returns></returns>
|
||||
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<string>(GenConstants.Gen_tablePrefix);
|
||||
|
||||
if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre)
|
||||
{
|
||||
string[] prefixList = tablePrefix.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
||||
@ -427,32 +425,31 @@ namespace ZR.CodeGenerator
|
||||
/// <summary>
|
||||
/// 初始化表信息
|
||||
/// </summary>
|
||||
/// <param name="dbName"></param>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="tableName"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
/// <param name="genTable"></param>
|
||||
/// <param name="dbColumnInfos"></param>
|
||||
/// <param name="seqs"></param>
|
||||
public static List<GenTableColumn> InitGenTableColumn(GenTable genTable, List<DbColumnInfo> dbColumnInfos, List<OracleSeq> seqs = null)
|
||||
/// <param name="codeGen"></param>
|
||||
public static List<GenTableColumn> InitGenTableColumn(GenTable genTable, List<DbColumnInfo> dbColumnInfos, List<OracleSeq> seqs = null, CodeGen codeGen = null)
|
||||
{
|
||||
OptionsSetting optionsSetting = new();
|
||||
|
||||
var gen = AppSettings.Get<Gen>("gen");
|
||||
var dbConfig = AppSettings.Get<DbConfigs>("CodeGenDbConfig");
|
||||
var dbConfig = AppSettings.Get<DbConfigs>(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<GenTableColumn> genTableColumns = new();
|
||||
foreach (var column in dbColumnInfos)
|
||||
{
|
||||
@ -492,7 +489,7 @@ namespace ZR.CodeGenerator
|
||||
private static GenTableColumn InitColumnField(GenTable genTable, DbColumnInfo column, List<OracleSeq> 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,
|
||||
|
||||
@ -7,9 +7,8 @@ namespace ZR.CodeGenerator
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// InputDto输入实体是不包含字段
|
||||
|
||||
@ -18,7 +18,7 @@ namespace ZR.CodeGenerator.Service
|
||||
{
|
||||
var db = GetSugarDbContext();
|
||||
//Oracle库特殊处理
|
||||
DbConfigs configs = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
|
||||
DbConfigs configs = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
|
||||
if (configs.DbType == 3)
|
||||
{
|
||||
return new List<string>() { configs?.DbName };
|
||||
|
||||
@ -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; }
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user