///////////////////////////////////////////////////////////////////////////////// // 宁波拓通e智造平台 ToTong Next Builder // // https://git.tuotong-tech.com/tnb/tnb.server // ///////////////////////////////////////////////////////////////////////////////// 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; /// /// 视图模型 /// [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 /// /// 主键标识 /// [SugarColumn(IsPrimaryKey = true)] public string id { get; set; } = YitIdHelper.NextId().ToString(); /// /// 模块代码 /// [SugarColumn(ColumnName = "area_code", Length = DbConsts.LengthS)] public string areaCode { get; set; } = "edp"; /// /// 视图代码 /// [SugarColumn(ColumnName = "vm_code", IsNullable = false, Length = DbConsts.LengthM)] public string vmCode { get; set; } = string.Empty; /// /// 视图名称 /// [SugarColumn(ColumnName = "vm_name", IsNullable = false, Length = DbConsts.LengthM)] public string vmName { get; set; } = string.Empty; /// /// 数据库连接 /// [SugarColumn(ColumnName = "db_code", Length = DbConsts.LengthS)] public string? dbCode { get; set; } /// /// 主表名称 /// [SugarColumn(ColumnName = "table_name", IsNullable = false, Length = DbConsts.LengthS)] public string tableName { get; set; } = string.Empty; /// /// 表字段属性 /// [SugarColumn(ColumnName = "db_props", IsNullable = false, IsJson = true)] public List dbProps { get; set; } = new List(); /// /// 导航属性 /// [SugarColumn(ColumnName = "nav_props", IsNullable = true, IsJson = true)] public List navProps { get; set; } = new List(); /// /// 计算属性 /// [SugarColumn(ColumnName = "cal_props", IsNullable = true, IsJson = true)] public List calProps { get; set; } = new List(); /// /// 排序 /// [SugarColumn(ColumnName = "ordinal", IsNullable = false)] public int ordinal { get; set; } /// /// 是否软删除 /// [SugarColumn(ColumnName = "soft_delete", IsNullable = false)] public short softDelete { get; set; } /// /// 是否激活 /// [SugarColumn(ColumnName = "enabled", IsNullable = false)] public short enabled { get; set; } = 1; /// /// 是否删除 /// [SugarColumn(ColumnName = "deleted", IsNullable = false)] public short deleted { get; set; } = 0; /// /// 描述 /// [SugarColumn(ColumnName = "descrip", Length = DbConsts.LengthL)] public string? descrip { get; set; } /// /// 创建时间 /// [SugarColumn(ColumnName = "create_time", IsNullable = false)] public DateTime createTime { get; set; } = DateTime.Now; /// /// 创建人 /// [SugarColumn(ColumnName = "create_id", Length = DbConsts.LengthS)] public string? createId { get; set; } /// /// 修改时间 /// [SugarColumn(ColumnName = "modify_time", Length = DbConsts.LengthS)] public DateTime? modifyTime { get; set; } /// /// 修改人 /// [SugarColumn(ColumnName = "modify_id", Length = DbConsts.LengthS)] public string? modifyId { get; set; } [SugarColumn(IsIgnore = true)] public string fullCode => areaCode + "/" + vmCode; /// /// 主键 /// public override object[] GetKeys() { return new object[] { id }; } #endregion Properties private Dictionary? _mapProps = null; /// /// 通过实体创建模型 /// /// /// /// public static Vmodel CreateByEntity(Type tpEntity, string? dbCode = null) { Vmodel model = new() { dbCode = dbCode, vmCode = tpEntity.Name }; SugarTable? sugarTableAttr = tpEntity.GetCustomAttribute(); if (sugarTableAttr != null) { model.tableName = sugarTableAttr.TableName; model.vmName = sugarTableAttr.TableDescription; } if (string.IsNullOrEmpty(model.tableName)) { model.tableName = tpEntity.GetCustomAttribute()?.Name ?? tpEntity.Name; } if (string.IsNullOrEmpty(model.vmName)) { model.vmName = tpEntity.GetCustomAttribute()?.Name ?? tpEntity.GetCustomAttribute()?.Description ?? model.vmCode; } PropertyInfo[] props = tpEntity.GetProperties(BindingFlags.Public); int n = 1; foreach (PropertyInfo p in props) { VmDbProp prop = new(); SugarColumn? sugarColumn = p.GetCustomAttribute(); if (sugarColumn != null) { prop = sugarColumn.Adapt(); } prop.code = p.Name; prop.ordinal = n++; model.dbProps.Add(prop); } return model; } /// /// 获取模型的主键属性 /// /// public VmDbProp GetPrimary() { var key = dbProps.FirstOrDefault(a => a.pkey); ThrowIf.IsNull(key, $"模型({fullCode})没有定义主键属性"); return key; } /// /// 获取模型的软删除属性 /// /// public VmDbProp? GetSoftDeleted() { return dbProps.FirstOrDefault(a => SOFT_DELETED.Contains(a.code)); } /// /// 根据属性名获取字段名 /// /// /// public string? PropToFieldCode(string propCode) { _mapProps ??= dbProps.ToDictionary(a => a.code); return _mapProps.GetOrDefault(propCode)?.field; } /// /// 字段代码转换为属性代码 /// /// /// /// public VmDbProp? GetDbProp(string propCode) { _mapProps ??= dbProps.ToDictionary(a => a.code); return _mapProps.GetOrDefault(propCode); } /// /// 获取默认对象 /// /// public DObject GetDefaultDObject() { DObject obj = new(); foreach (VmDbProp p in dbProps) { obj.Add(p.code, p.GetDefaultValue()!); } return obj; } /// /// 获取属性字符串 /// /// /// public string GetPropStr(bool includePkey, bool includeAutoProp) { var strs = new List(); 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); } /// /// 转换为待新增的实体对象 /// /// 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; } /// /// 转换为待修改的实体对象 /// /// 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; } }