⚡代码生成优化
This commit is contained in:
parent
1316e14664
commit
dc6f41d96e
@ -5,11 +5,6 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class GlobalConstant
|
public class GlobalConstant
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 代码生成常量
|
|
||||||
/// </summary>
|
|
||||||
public static readonly string CodeGenDbConfig;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 管理员权限
|
/// 管理员权限
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -23,7 +23,7 @@ namespace Infrastructure.Model
|
|||||||
public Upload Upload { get; set; }
|
public Upload Upload { get; set; }
|
||||||
public ALIYUN_OSS ALIYUN_OSS { get; set; }
|
public ALIYUN_OSS ALIYUN_OSS { get; set; }
|
||||||
public JwtSettings JwtSettings { get; set; }
|
public JwtSettings JwtSettings { get; set; }
|
||||||
public Gen Gen { get; set; }
|
public CodeGen CodeGen { get; set; }
|
||||||
public List<DbConfigs> DbConfigs { get; set; }
|
public List<DbConfigs> DbConfigs { get; set; }
|
||||||
public DbConfigs CodeGenDbConfig { get; set; }
|
public DbConfigs CodeGenDbConfig { get; set; }
|
||||||
}
|
}
|
||||||
@ -94,13 +94,14 @@ namespace Infrastructure.Model
|
|||||||
public string TokenType { get; set; } = "Bearer";
|
public string TokenType { get; set; } = "Bearer";
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Gen
|
public class CodeGen
|
||||||
{
|
{
|
||||||
public bool ShowApp { get; set; }
|
public bool ShowApp { get; set; }
|
||||||
public bool AutoPre { get; set; }
|
public bool AutoPre { get; set; }
|
||||||
public string VuePath { get; set; }
|
public string VuePath { get; set; }
|
||||||
public string Author { 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; }
|
public CsharpTypeArr CsharpTypeArr { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -132,45 +132,50 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 导入表结构(保存)
|
/// 导入表结构(保存)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="tables"></param>
|
/// <param name="dto"></param>
|
||||||
/// <param name="dbName"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("importTable")]
|
[HttpPost("importTable")]
|
||||||
[Log(Title = "代码生成", BusinessType = BusinessType.IMPORT)]
|
[Log(Title = "代码生成", BusinessType = BusinessType.IMPORT)]
|
||||||
[ActionPermissionFilter(Permission = "tool:gen: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));
|
DbConfigs dbConfig = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
|
||||||
string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
CodeGen codeGen = AppSettings.Get<CodeGen>("codeGen");
|
||||||
|
|
||||||
int result = 0;
|
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();
|
List<OracleSeq> seqs = new();
|
||||||
GenTable genTable = CodeGeneratorTool.InitTable(dbName, HttpContext.GetName(), tableName, tabInfo?.Description);
|
InitTableDto initTableDto = new()
|
||||||
|
{
|
||||||
|
DbName = dto.DbName,
|
||||||
|
UserName = HttpContext.GetName(),
|
||||||
|
TableName = table.Name,
|
||||||
|
Desc = table.Description,
|
||||||
|
CodeGen = codeGen
|
||||||
|
};
|
||||||
|
|
||||||
|
GenTable genTable = CodeGeneratorTool.InitTable(initTableDto);
|
||||||
genTable.TableId = GenTableService.ImportGenTable(genTable);
|
genTable.TableId = GenTableService.ImportGenTable(genTable);
|
||||||
if (dbConfig.DbType == 3)
|
if (dbConfig.DbType == 3)
|
||||||
{
|
{
|
||||||
seqs = _CodeGeneraterService.GetAllOracleSeqs(dbName);
|
seqs = _CodeGeneraterService.GetAllOracleSeqs(table.Name);
|
||||||
}
|
}
|
||||||
if (genTable.TableId > 0)
|
if (genTable.TableId > 0)
|
||||||
{
|
{
|
||||||
//保存列信息
|
//保存列信息
|
||||||
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, tableName);
|
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dto.DbName, table.Name);
|
||||||
List<GenTableColumn> genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos, seqs);
|
List<GenTableColumn> genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos, seqs, codeGen);
|
||||||
GenTableColumnService.DeleteGenTableColumnByTableName(tableName);
|
GenTableColumnService.DeleteGenTableColumnByTableName(table.Name);
|
||||||
GenTableColumnService.InsertGenTableColumn(genTableColumns);
|
GenTableColumnService.InsertGenTableColumn(genTableColumns);
|
||||||
genTable.Columns = genTableColumns;
|
genTable.Columns = genTableColumns;
|
||||||
result++;
|
result++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ToResponse(result);
|
return ToResponse(result);
|
||||||
}
|
}
|
||||||
@ -222,7 +227,7 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
||||||
}
|
}
|
||||||
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
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.DbType = dbConfig.DbType;
|
||||||
dto.GenTable = genTableInfo;
|
dto.GenTable = genTableInfo;
|
||||||
@ -248,7 +253,7 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
|
||||||
}
|
}
|
||||||
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
|
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.DbType = dbConfig.DbType;
|
||||||
dto.GenTable = genTableInfo;
|
dto.GenTable = genTableInfo;
|
||||||
@ -314,8 +319,9 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
if (table == null) { throw new CustomException("同步数据失败,原表结构不存在"); }
|
if (table == null) { throw new CustomException("同步数据失败,原表结构不存在"); }
|
||||||
table.Update_by = HttpContext.GetName();
|
table.Update_by = HttpContext.GetName();
|
||||||
|
|
||||||
|
var codeGen = AppSettings.Get<CodeGen>("codeGen");
|
||||||
List<DbColumnInfo> dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName);
|
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);
|
bool result = GenTableService.SynchDb(tableId, table, dbTableColumns);
|
||||||
return SUCCESS(result);
|
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.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Xml.Linq;
|
||||||
using ZR.CodeGenerator.Model;
|
using ZR.CodeGenerator.Model;
|
||||||
using ZR.Model.System.Generate;
|
using ZR.Model.System.Generate;
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ namespace ZR.CodeGenerator
|
|||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
public static void Generate(GenerateDto dto)
|
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";
|
dto.VueParentPath = dto.VueVersion == 3 ? "ZRAdmin-vue" : "ZR.Vue";
|
||||||
if (!genOptions.VuePath.IsEmpty())
|
if (!genOptions.VuePath.IsEmpty())
|
||||||
{
|
{
|
||||||
@ -346,11 +347,8 @@ namespace ZR.CodeGenerator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="tableName"></param>
|
/// <param name="tableName"></param>
|
||||||
/// <returns></returns>
|
/// <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)
|
if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre)
|
||||||
{
|
{
|
||||||
string[] prefixList = tablePrefix.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
string[] prefixList = tablePrefix.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
||||||
@ -427,32 +425,31 @@ namespace ZR.CodeGenerator
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化表信息
|
/// 初始化表信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dbName"></param>
|
/// <param name="dto"></param>
|
||||||
/// <param name="userName"></param>
|
|
||||||
/// <param name="tableName"></param>
|
|
||||||
/// <param name="desc"></param>
|
|
||||||
/// <returns></returns>
|
/// <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()
|
GenTable genTable = new()
|
||||||
{
|
{
|
||||||
DbName = dbName,
|
DbName = dto.DbName,
|
||||||
BaseNameSpace = "ZR.",//导入默认命名空间前缀
|
BaseNameSpace = "ZR.",//导入默认命名空间前缀
|
||||||
ModuleName = "business",//导入默认模块名
|
ModuleName = dto.CodeGen.ModuleName,//导入默认模块名
|
||||||
ClassName = GetClassName(tableName),
|
ClassName = className,
|
||||||
BusinessName = GetClassName(tableName),
|
BusinessName = className,
|
||||||
FunctionAuthor = AppSettings.GetConfig(GenConstants.Gen_author),
|
FunctionAuthor = dto.CodeGen.Author,
|
||||||
TableName = tableName,
|
TableName = dto.TableName,
|
||||||
TableComment = desc,
|
TableComment = dto.Desc,
|
||||||
FunctionName = desc,
|
FunctionName = dto.Desc,
|
||||||
Create_by = userName,
|
Create_by = dto.UserName,
|
||||||
Options = new Options()
|
Options = new Options()
|
||||||
{
|
{
|
||||||
SortType = "asc",
|
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;
|
return genTable;
|
||||||
}
|
}
|
||||||
@ -463,16 +460,16 @@ namespace ZR.CodeGenerator
|
|||||||
/// <param name="genTable"></param>
|
/// <param name="genTable"></param>
|
||||||
/// <param name="dbColumnInfos"></param>
|
/// <param name="dbColumnInfos"></param>
|
||||||
/// <param name="seqs"></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();
|
OptionsSetting optionsSetting = new();
|
||||||
|
|
||||||
var gen = AppSettings.Get<Gen>("gen");
|
var dbConfig = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
|
||||||
var dbConfig = AppSettings.Get<DbConfigs>("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();
|
List<GenTableColumn> genTableColumns = new();
|
||||||
foreach (var column in dbColumnInfos)
|
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)
|
private static GenTableColumn InitColumnField(GenTable genTable, DbColumnInfo column, List<OracleSeq> seqs, OptionsSetting optionsSetting)
|
||||||
{
|
{
|
||||||
var dataType = column.DataType;
|
var dataType = column.DataType;
|
||||||
if (optionsSetting.Gen.GenDbConfig.DbType == 3)
|
if (optionsSetting.CodeGenDbConfig.DbType == 3)
|
||||||
{
|
{
|
||||||
dataType = column.OracleDataType;
|
dataType = column.OracleDataType;
|
||||||
var seqName = $"SEQ_{genTable.TableName}_{column.DbColumnName}";
|
var seqName = $"SEQ_{genTable.TableName}_{column.DbColumnName}";
|
||||||
@ -507,7 +504,7 @@ namespace ZR.CodeGenerator
|
|||||||
ColumnType = dataType,
|
ColumnType = dataType,
|
||||||
TableId = genTable.TableId,
|
TableId = genTable.TableId,
|
||||||
TableName = genTable.TableName,
|
TableName = genTable.TableName,
|
||||||
CsharpType = GetCSharpDatatype(dataType, optionsSetting.Gen.CsharpTypeArr).ToString(),
|
CsharpType = GetCSharpDatatype(dataType, optionsSetting.CodeGen.CsharpTypeArr).ToString(),
|
||||||
CsharpField = column.DbColumnName.ConvertToPascal("_"),
|
CsharpField = column.DbColumnName.ConvertToPascal("_"),
|
||||||
IsRequired = !column.IsNullable,
|
IsRequired = !column.IsNullable,
|
||||||
IsIncrement = column.IsIdentity,
|
IsIncrement = column.IsIdentity,
|
||||||
|
|||||||
@ -7,9 +7,8 @@ namespace ZR.CodeGenerator
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class GenConstants
|
public class GenConstants
|
||||||
{
|
{
|
||||||
public static string Gen_author = "gen:author";
|
public static string Gen_author = "codeGen:author";
|
||||||
public static string Gen_autoPre = "gen:autoPre";
|
public static string CodeGenDbConfig;
|
||||||
public static string Gen_tablePrefix = "gen:tablePrefix";
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// InputDto输入实体是不包含字段
|
/// InputDto输入实体是不包含字段
|
||||||
|
|||||||
@ -18,7 +18,7 @@ namespace ZR.CodeGenerator.Service
|
|||||||
{
|
{
|
||||||
var db = GetSugarDbContext();
|
var db = GetSugarDbContext();
|
||||||
//Oracle库特殊处理
|
//Oracle库特殊处理
|
||||||
DbConfigs configs = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
|
DbConfigs configs = AppSettings.Get<DbConfigs>(nameof(GenConstants.CodeGenDbConfig));
|
||||||
if (configs.DbType == 3)
|
if (configs.DbType == 3)
|
||||||
{
|
{
|
||||||
return new List<string>() { configs?.DbName };
|
return new List<string>() { configs?.DbName };
|
||||||
|
|||||||
@ -38,10 +38,10 @@ namespace ZR.Model.System.Dto
|
|||||||
public string Sql { get; set; }
|
public string Sql { get; set; }
|
||||||
|
|
||||||
[ExcelColumn(Name = "变更前数据", Width = 30)]
|
[ExcelColumn(Name = "变更前数据", Width = 30)]
|
||||||
public string BeforeData { get; set; }
|
public string BeforeData { get; set; } = string.Empty;
|
||||||
|
|
||||||
[ExcelColumn(Name = "变更后数据", Width = 30)]
|
[ExcelColumn(Name = "变更后数据", Width = 30)]
|
||||||
public string AfterData { get; set; }
|
public string AfterData { get; set; } = string.Empty;
|
||||||
|
|
||||||
[ExcelColumn(Name = "操作用户名")]
|
[ExcelColumn(Name = "操作用户名")]
|
||||||
public string UserName { get; set; }
|
public string UserName { get; set; }
|
||||||
|
|||||||
@ -48,7 +48,7 @@ namespace ZR.ServiceCore.Services
|
|||||||
if (info != null)
|
if (info != null)
|
||||||
{
|
{
|
||||||
info.Columns = GenTableColumnService.GenTableColumns(tableId);
|
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 = Queryable().Where(f => f.TableName == info.SubTableName).First();
|
||||||
info.SubTable.Columns = GenTableColumnService.GenTableColumns(info.SubTable.TableId);
|
info.SubTable.Columns = GenTableColumnService.GenTableColumns(info.SubTable.TableId);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user