Files
tnb.server/system/Tnb.Systems.Entitys/Model/System/DataBase/DbEntityInfo.cs

125 lines
2.9 KiB
C#

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;
}
}