Files
tnb.server/visualdev/Tnb.Vengine/Domain/Vmodel.cs
2024-04-11 17:31:32 +08:00

322 lines
9.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using JNPF.Common.Contracts;
using JNPF.Common.Core.Manager;
using JNPF.Common.Extension;
using Mapster;
using SqlSugar;
using Tnb.Core;
using Yitter.IdGenerator;
namespace Tnb.Vengine.Domain;
/// <summary>
/// 视图模型
/// </summary>
[SugarTable("sys_vmodel")]
public partial class Vmodel : Entity
{
private static string[] SOFT_DELETED = new string[] { "deleted", "isDeleted", "softDeleted" };
private static string[] AUTO_FILL_PROP = new string[] { "createTime", "createId", "updateTime", "modifyTime", "updateId", "modifyId" };
#region Properties
/// <summary>
/// 主键标识
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public string id { get; set; } = YitIdHelper.NextId().ToString();
/// <summary>
/// 模块代码
/// </summary>
[SugarColumn(ColumnName = "area_code", Length = DbConsts.LengthS)]
public string areaCode { get; set; } = "edp";
/// <summary>
/// 视图代码
/// </summary>
[SugarColumn(ColumnName = "vm_code", IsNullable = false, Length = DbConsts.LengthM)]
public string vmCode { get; set; } = string.Empty;
/// <summary>
/// 视图名称
/// </summary>
[SugarColumn(ColumnName = "vm_name", IsNullable = false, Length = DbConsts.LengthM)]
public string vmName { get; set; } = string.Empty;
/// <summary>
/// 数据库连接
/// </summary>
[SugarColumn(ColumnName = "db_code", Length = DbConsts.LengthS)]
public string? dbCode { get; set; }
/// <summary>
/// 主表名称
/// </summary>
[SugarColumn(ColumnName = "table_name", IsNullable = false, Length = DbConsts.LengthS)]
public string tableName { get; set; } = string.Empty;
/// <summary>
/// 表字段属性
/// </summary>
[SugarColumn(ColumnName = "db_props", IsNullable = false, IsJson = true)]
public List<VmDbProp> dbProps { get; set; } = new List<VmDbProp>();
/// <summary>
/// 导航属性
/// </summary>
[SugarColumn(ColumnName = "nav_props", IsNullable = true, IsJson = true)]
public List<VmNavProp> navProps { get; set; } = new List<VmNavProp>();
/// <summary>
/// 计算属性
/// </summary>
[SugarColumn(ColumnName = "cal_props", IsNullable = true, IsJson = true)]
public List<VmCalProp> calProps { get; set; } = new List<VmCalProp>();
/// <summary>
/// 排序
/// </summary>
[SugarColumn(ColumnName = "ordinal", IsNullable = false)]
public int ordinal { get; set; }
/// <summary>
/// 是否软删除
/// </summary>
[SugarColumn(ColumnName = "soft_delete", IsNullable = false)]
public short softDelete { get; set; }
/// <summary>
/// 是否激活
/// </summary>
[SugarColumn(ColumnName = "enabled", IsNullable = false)]
public short enabled { get; set; } = 1;
/// <summary>
/// 是否删除
/// </summary>
[SugarColumn(ColumnName = "deleted", IsNullable = false)]
public short deleted { get; set; } = 0;
/// <summary>
/// 描述
/// </summary>
[SugarColumn(ColumnName = "descrip", Length = DbConsts.LengthL)]
public string? descrip { get; set; }
/// <summary>
/// 创建时间
/// </summary>
[SugarColumn(ColumnName = "create_time", IsNullable = false)]
public DateTime createTime { get; set; } = DateTime.Now;
/// <summary>
/// 创建人
/// </summary>
[SugarColumn(ColumnName = "create_id", Length = DbConsts.LengthS)]
public string? createId { get; set; }
/// <summary>
/// 修改时间
/// </summary>
[SugarColumn(ColumnName = "modify_time", Length = DbConsts.LengthS)]
public DateTime? modifyTime { get; set; }
/// <summary>
/// 修改人
/// </summary>
[SugarColumn(ColumnName = "modify_id", Length = DbConsts.LengthS)]
public string? modifyId { get; set; }
[SugarColumn(IsIgnore = true)]
public string fullCode => areaCode + "/" + vmCode;
/// <summary>
/// 主键
/// </summary>
public override object[] GetKeys()
{
return new object[] { id };
}
#endregion Properties
private Dictionary<string, VmDbProp>? _mapProps = null;
/// <summary>
/// 通过实体创建模型
/// </summary>
/// <param name="tpEntity"></param>
/// <param name="dbCode"></param>
/// <returns></returns>
public static Vmodel CreateByEntity(Type tpEntity, string? dbCode = null)
{
Vmodel model = new() { dbCode = dbCode, vmCode = tpEntity.Name };
SugarTable? sugarTableAttr = tpEntity.GetCustomAttribute<SugarTable>();
if (sugarTableAttr != null)
{
model.tableName = sugarTableAttr.TableName;
model.vmName = sugarTableAttr.TableDescription;
}
if (string.IsNullOrEmpty(model.tableName))
{
model.tableName = tpEntity.GetCustomAttribute<TableAttribute>()?.Name ?? tpEntity.Name;
}
if (string.IsNullOrEmpty(model.vmName))
{
model.vmName = tpEntity.GetCustomAttribute<DisplayAttribute>()?.Name ?? tpEntity.GetCustomAttribute<DescriptionAttribute>()?.Description ?? model.vmCode;
}
PropertyInfo[] props = tpEntity.GetProperties(BindingFlags.Public);
int n = 1;
foreach (PropertyInfo p in props)
{
VmDbProp prop = new();
SugarColumn? sugarColumn = p.GetCustomAttribute<SugarColumn>();
if (sugarColumn != null)
{
prop = sugarColumn.Adapt<VmDbProp>();
}
prop.code = p.Name;
prop.ordinal = n++;
model.dbProps.Add(prop);
}
return model;
}
/// <summary>
/// 获取模型的主键属性
/// </summary>
/// <returns></returns>
public VmDbProp GetPrimary()
{
var key = dbProps.FirstOrDefault(a => a.pkey);
ThrowIf.IsNull(key, $"模型({fullCode})没有定义主键属性");
return key;
}
/// <summary>
/// 获取模型的软删除属性
/// </summary>
/// <returns></returns>
public VmDbProp? GetSoftDeleted()
{
return dbProps.FirstOrDefault(a => SOFT_DELETED.Contains(a.code));
}
/// <summary>
/// 根据属性名获取字段名
/// </summary>
/// <param name="propCode"></param>
/// <returns></returns>
public string? PropToFieldCode(string propCode)
{
_mapProps ??= dbProps.ToDictionary(a => a.code);
return _mapProps.GetOrDefault(propCode)?.field;
}
/// <summary>
/// 字段代码转换为属性代码
/// </summary>
/// <param name="input"></param>
/// <param name="ignoreNotMapped"></param>
/// <returns></returns>
public VmDbProp? GetDbProp(string propCode)
{
_mapProps ??= dbProps.ToDictionary(a => a.code);
return _mapProps.GetOrDefault(propCode);
}
/// <summary>
/// 获取默认对象
/// </summary>
/// <returns></returns>
public DObject GetDefaultDObject()
{
DObject obj = new();
foreach (VmDbProp p in dbProps)
{
obj.Add(p.code, p.GetDefaultValue()!);
}
return obj;
}
/// <summary>
/// 获取属性字符串
/// </summary>
/// <param name="isCreate"></param>
/// <returns></returns>
public string GetPropStr(bool includePkey, bool includeAutoProp)
{
var strs = new List<string>();
foreach (var col in dbProps.OrderBy(a => a.ordinal))
{
if (!includeAutoProp && AUTO_FILL_PROP.Contains(col.code)) continue;
if (!includePkey && col.pkey) continue;
strs.Add(col.GetCsType() + " " + col.code);
}
return string.Join(", ", strs);
}
/// <summary>
/// 转换为待新增的实体对象
/// </summary>
/// <returns></returns>
public DObject ToCreateEntity(DObject input, IUserManager user)
{
DObject obj = new();
foreach (VmDbProp p in dbProps)
{
if (input.ContainsKey(p.code))
{
obj.Add(p.field, input[p.code]);
}
//当提交的数据与内置规则有重复时采用内置规则如果要优先采用提交的数据这里要使用else if
if ((p.pkey && p.code == "id") || p.defValue == "@snowid")
{
obj[p.field] = YitIdHelper.NextId().ToString();
}
else if (p.csType == "DateTime" && (p.code == "createTime" || p.defValue == "@createTime"))
{
obj[p.field] = DateTime.Now;
}
else if (p.csType == "string" && (p.code == "createId" || p.defValue == "@createId"))
{
obj[p.field] = user.UserId;
}
else if (obj.GetOrDefault(p.field) == null && (p.required || !string.IsNullOrEmpty(p.defValue)))
{
obj[p.field] = p.GetDefaultValue()!;
}
}
return obj;
}
/// <summary>
/// 转换为待修改的实体对象
/// </summary>
/// <returns></returns>
public DObject ToUpdateEntity(DObject input, IUserManager user)
{
DObject obj = new();
foreach (VmDbProp p in dbProps)
{
if (input.ContainsKey(p.code))
{
obj.Add(p.field, input[p.code]);
}
//当提交的数据与内置规则有重复时采用内置规则如果要优先采用提交的数据这里要使用else if
if (p.csType == "DateTime" && (p.code == "updateTime" || p.code == "modifyTime" || p.defValue == "@updateTime"))
{
obj[p.field] = DateTime.Now;
}
else if (p.csType == "string" && (p.code == "updateId" || p.code == "modifyId" || p.defValue == "@updateId"))
{
obj[p.field] = user.UserId;
}
}
return obj;
}
}