使用razor模板生成实体文件
This commit is contained in:
@@ -44,6 +44,25 @@ public class SystemMapper : IRegister
|
||||
.Map(dest => dest.IsPrimarykey, src => src.primaryKey)
|
||||
.Map(dest => dest.IsNullable, src => src.allowNull == 1)
|
||||
.Map(dest => dest.DefaultValue, src => src.defaults);
|
||||
config.ForType<DbTableInfo, DbEntityInfo>()
|
||||
.Map(dest => dest.tableName, src => src.Name)
|
||||
.Map(dest => dest.descrip, src => src.Description);
|
||||
config.ForType<DbColumnInfo, DbEntityPropInfo>()
|
||||
.Map(dest => dest.colName, src => src.DbColumnName)
|
||||
.Map(dest => dest.dataType, src => src.DataType)
|
||||
.Map(dest => dest.propName, src => src.PropertyName)
|
||||
.Map(dest => dest.propType, src => src.PropertyType)
|
||||
.Map(dest => dest.length, src => src.Length)
|
||||
.Map(dest => dest.descrip, src => src.ColumnDescription)
|
||||
.Map(dest => dest.defaultValue, src => src.DefaultValue)
|
||||
.Map(dest => dest.allowNull, src => src.IsNullable)
|
||||
.Map(dest => dest.identity, src => src.IsIdentity)
|
||||
.Map(dest => dest.primaryKey, src => src.IsPrimarykey)
|
||||
.Map(dest => dest.isArray, src => src.IsArray)
|
||||
.Map(dest => dest.isJson, src => src.IsJson)
|
||||
.Map(dest => dest.value, src => src.Value)
|
||||
.Map(dest => dest.scale, src => src.Scale)
|
||||
.Map(dest => dest.digits, src => src.DecimalDigits);
|
||||
//config.ForType<DynamicDbTableModel, DbTableModel>()
|
||||
// .Map(dest => dest.table, src => src.F_TABLE)
|
||||
// .Map(dest => dest.tableName, src => src.F_TABLENAME)
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
using JNPF.DependencyInjection;
|
||||
using SqlSugar;
|
||||
|
||||
namespace JNPF.Systems.Entitys.Dto.Database;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库表列表输出.
|
||||
/// </summary>
|
||||
[SuppressSniffer]
|
||||
public class DatabaseTableInfo
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
DbTableInfo table { get; set; }
|
||||
|
||||
List<DbColumnInfo> columns { get; set; }
|
||||
|
||||
public string clsName { get; set; } = string.Empty;
|
||||
|
||||
public string nsName { get; set; } = "Tnb.Entities";
|
||||
|
||||
public string pkType { get; set; } = "string";
|
||||
|
||||
public string pkName { get; set; } = "id";
|
||||
|
||||
public string GetColumnDefaultValue(string colName)
|
||||
{
|
||||
return "default";
|
||||
}
|
||||
public string GetColumnCsType(string colName)
|
||||
{
|
||||
return "default";
|
||||
}
|
||||
public string GetColumnCsName(string colName)
|
||||
{
|
||||
return "default";
|
||||
}
|
||||
public string GetColumnAttrib(string colName)
|
||||
{
|
||||
return "default";
|
||||
}
|
||||
}
|
||||
124
system/Tnb.Systems.Entitys/Model/System/DataBase/DbEntityInfo.cs
Normal file
124
system/Tnb.Systems.Entitys/Model/System/DataBase/DbEntityInfo.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using JNPF.DependencyInjection;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using SqlSugar;
|
||||
|
||||
namespace JNPF.Systems.Entitys.Dto.Database;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库表映射实体信息.
|
||||
/// </summary>
|
||||
[SuppressSniffer]
|
||||
public class DbEntityInfo
|
||||
{
|
||||
public string tableName { get; set; } = string.Empty;
|
||||
|
||||
public string descrip { get; set; } = string.Empty;
|
||||
|
||||
public string clsName { get; set; } = string.Empty;
|
||||
|
||||
public string nsName { get; set; } = "Tnb.Entities";
|
||||
|
||||
public string pkType { get; set; } = "string";
|
||||
|
||||
public string pkName { get; set; } = "id";
|
||||
|
||||
public List<DbEntityPropInfo> columns { get; set; } = new List<DbEntityPropInfo>();
|
||||
public List<string> ignoreCols { get; set; } = new List<string>();
|
||||
|
||||
public string GetInherit()
|
||||
{
|
||||
return pkName == "id" ? $"BaseEntity<{pkType}>" : "IEntity";
|
||||
}
|
||||
public string GetConstruct()
|
||||
{
|
||||
if (pkName == "id")
|
||||
{
|
||||
string str = pkType switch
|
||||
{
|
||||
"string" => "id = SnowflakeIdHelper.NextId();",
|
||||
"long" => "id = YitIdHelper.NextId();",
|
||||
_ => string.Empty
|
||||
};
|
||||
return str;
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据库表映射实体属性信息.
|
||||
/// </summary>
|
||||
[SuppressSniffer]
|
||||
public class DbEntityPropInfo
|
||||
{
|
||||
public string colName { get; set; }
|
||||
|
||||
public string dataType { get; set; }
|
||||
|
||||
public string csType { get; set; }
|
||||
|
||||
public string propName { get; set; }
|
||||
|
||||
public Type propType { get; set; }
|
||||
|
||||
public int length { get; set; }
|
||||
|
||||
public string descrip { get; set; }
|
||||
|
||||
public string defaultValue { get; set; }
|
||||
|
||||
public bool allowNull { get; set; }
|
||||
|
||||
public bool identity { get; set; }
|
||||
|
||||
public bool primaryKey { get; set; }
|
||||
|
||||
public bool isArray { get; set; }
|
||||
|
||||
public bool isJson { get; set; }
|
||||
|
||||
public object value { get; set; }
|
||||
|
||||
public int digits { get; set; }
|
||||
|
||||
public int scale { get; set; }
|
||||
|
||||
public string GetPropType()
|
||||
{
|
||||
string propType = csType;
|
||||
if (allowNull)
|
||||
propType = csType + "?";
|
||||
return propType;
|
||||
}
|
||||
public virtual string GetPropAttr()
|
||||
{
|
||||
List<string> sugarAttrs = new List<string>();
|
||||
if (primaryKey)
|
||||
sugarAttrs.Add("IsPrimaryKey = true");
|
||||
if (identity)
|
||||
sugarAttrs.Add("IsIdentity = true");
|
||||
//if (allowNull)
|
||||
// sugarAttrs.Add("IsNullable = true");
|
||||
if (colName != propName)
|
||||
sugarAttrs.Add($"ColumnName = \"{colName}\"");
|
||||
if (sugarAttrs.Count > 0)
|
||||
return $" [SugarColumn({string.Join(", ", sugarAttrs)})]\n";
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
public string GetDefaultValue()
|
||||
{
|
||||
if (allowNull) return string.Empty;
|
||||
|
||||
string str = csType switch
|
||||
{
|
||||
"string" => " = string.Empty;",
|
||||
"DateTime" => " = DateTime.Now;",
|
||||
_ => string.Empty
|
||||
};
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,7 @@ using JNPF.Systems.Entitys.Dto.Database;
|
||||
using JNPF.Systems.Entitys.Model.DataBase;
|
||||
using JNPF.Systems.Entitys.System;
|
||||
using JNPF.Systems.Interfaces.System;
|
||||
using JNPF.ViewEngine;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -51,6 +52,12 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
/// </summary>
|
||||
private readonly IDbLinkService _dbLinkService;
|
||||
|
||||
/// <summary>
|
||||
/// 视图引擎.
|
||||
/// </summary>
|
||||
private readonly IViewEngine _viewEngine;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文件服务.
|
||||
/// </summary>
|
||||
@@ -74,13 +81,15 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
IDbLinkService dbLinkService,
|
||||
IFileManager fileManager,
|
||||
IDataBaseManager dataBaseManager,
|
||||
IUserManager userManager)
|
||||
IUserManager userManager,
|
||||
IViewEngine viewEngine)
|
||||
{
|
||||
_repository = repository;
|
||||
_dbLinkService = dbLinkService;
|
||||
_fileManager = fileManager;
|
||||
_dataBaseManager = dataBaseManager;
|
||||
_userManager = userManager;
|
||||
_viewEngine = viewEngine;
|
||||
}
|
||||
|
||||
#region GET
|
||||
@@ -386,17 +395,47 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
var link = await _dbLinkService.GetInfo(linkId);
|
||||
sugarClient = _dataBaseManager.ChangeDataBase(link);
|
||||
}
|
||||
foreach (var item in sugarClient.DbMaintenance.GetTableInfoList().Where(t => t.Name == input.TableName))
|
||||
{
|
||||
var entityName = string.Join("", input.TableName.Split('_').Select(a => a.ToPascalCase()));
|
||||
sugarClient.MappingTables.Add(entityName, item.Name);
|
||||
//foreach (var col in sugarClient.DbMaintenance.GetColumnInfosByTableName(item.Name))
|
||||
//{
|
||||
// //var colName = CustomFormatName(col.DbColumnName);
|
||||
// sugarClient.MappingColumns.Add(col.DbColumnName /*类的属性首字母大写*/, col.DbColumnName, entityName);
|
||||
//}
|
||||
}
|
||||
List<DbEntityInfo> entities = new();
|
||||
Dictionary<string, string> nsMapper = GetNsMapper();
|
||||
foreach (var tbl in sugarClient.DbMaintenance.GetTableInfoList().Where(t => t.Name == input.TableName))
|
||||
{
|
||||
var entityName = string.Join("", tbl.Name.Split('_').Select(a => a.ToPascalCase()));
|
||||
sugarClient.MappingTables.Add(entityName, tbl.Name);
|
||||
DbEntityInfo model = new() { tableName = tbl.Name, descrip = tbl.Description, clsName = entityName };
|
||||
var key = tbl.Name.Split('_')[0] + "_";
|
||||
if (nsMapper.ContainsKey(key)) model.nsName = nsMapper[key];
|
||||
foreach (var field in sugarClient.DbMaintenance.GetColumnInfosByTableName(tbl.Name))
|
||||
{
|
||||
DbEntityPropInfo col = field.Adapt<DbEntityPropInfo>();
|
||||
col.csType = sugarClient.Ado.DbBind.GetPropertyTypeName(field.DataType);
|
||||
col.propName = field.DbColumnName;
|
||||
model.columns.Add(col);
|
||||
}
|
||||
var primaryKey = model.columns.FirstOrDefault(a => a.primaryKey);
|
||||
if (primaryKey != null)
|
||||
{
|
||||
model.pkType = primaryKey.csType;
|
||||
model.pkName = primaryKey.propName;
|
||||
}
|
||||
if (model.pkName == "id") model.ignoreCols.Add(model.pkName);
|
||||
entities.Add(model);
|
||||
}
|
||||
var templatePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "Template", "Entity.vue.vm");
|
||||
string tContent = File.ReadAllText(templatePath);
|
||||
foreach (var item in entities)
|
||||
{
|
||||
var tResult = _viewEngine.RunCompileFromCached(tContent, item, builderAction: builder =>
|
||||
{
|
||||
builder.AddUsing("System.Collections.Generic");
|
||||
builder.AddAssemblyReferenceByName("System.Collections");
|
||||
});
|
||||
var dir = Path.Combine(FileVariable.GenerateCodePath, item.nsName);
|
||||
Directory.CreateDirectory(dir);
|
||||
File.WriteAllText(Path.Combine(dir, item.clsName + ".cs"), tResult);
|
||||
|
||||
return tResult;
|
||||
}
|
||||
|
||||
var prefix = input.TableName.Split('_')[0];
|
||||
var nsName = nsMapper.ContainsKey(prefix) ? nsMapper[prefix] : "Tnb.Entities";
|
||||
var dict = GenerateEntityConfig(sugarClient.DbFirst.Where(input.TableName)).ToClassStringList(nsName);
|
||||
@@ -664,8 +703,8 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
var txt = tpl.Replace("{PropertyType}", type).Replace("{PropertyName}", col.DbColumnName).Replace("{SugarColumn}", sugarColumnStr);
|
||||
if (col.DbColumnName == "id")
|
||||
{
|
||||
if(type == "string") txt = txt.TrimEnd('\n', '\r') + " = SnowflakeIdHelper.NextId();\r\n";
|
||||
else if(type == "long") txt = txt.TrimEnd('\n', '\r') + " = YitIdHelper.NextId();\r\n";
|
||||
if (type == "string") txt = txt.TrimEnd('\n', '\r') + " = SnowflakeIdHelper.NextId();\r\n";
|
||||
else if (type == "long") txt = txt.TrimEnd('\n', '\r') + " = YitIdHelper.NextId();\r\n";
|
||||
}
|
||||
else if (type == "string") txt = txt.TrimEnd('\n', '\r') + " = string.Empty;\r\n";
|
||||
return txt;
|
||||
|
||||
Reference in New Issue
Block a user