using System; using System.ComponentModel.DataAnnotations; using System.Linq; using HealthMonitor.Util.Common; using HealthMonitor.Util.Entities.Interfaces; namespace HealthMonitor.Util.Entities.Base { public abstract class EntityBase : IEntity { /// /// 获取全局(数据库内)唯一键 /// /// 需要赋予实体键的值,多个实体键赋予的值,用逗号分隔 /// public string GetUniqueKey(string? key = null) { var type = this.GetType(); if(EntityUtils.HasAutoGenerateKey(type)) { return $"{type.Name}_{Guid.NewGuid()}"; } if (!string.IsNullOrWhiteSpace(key)) AssignEntityKey(key); var propertyValues = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0) .Select(e => e.GetValue(this) + ""); string str = $"{type.Name}_{string.Join("_", propertyValues)}"; return str; } /// /// 获取全局(数据库内)唯一键(通过实体的主键创建,而不会创建随机键) /// /// public string GetRequiredUniqueKey() { var type = this.GetType(); var propertyValues = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0) .Select(e => e.GetValue(this) + "").ToList(); if (propertyValues.Count() == 0 || (propertyValues[0] + "") == "0") throw new ArgumentException($"实体{type.Name}主键无效"); string str = $"{type.Name}_{string.Join("_", propertyValues)}"; return str; } /// /// 获取主键(数据表内唯一) /// /// public string GetPrimaryKey() { var type = this.GetType(); var propertyValues = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0) .Select(e => e.GetValue(this) + ""); string str = string.Join("_", propertyValues); return str; } /// /// 为实体的键(标记了KeyAttribute)属性赋值 /// /// public virtual void AssignEntityKey(string key) { var type = this.GetType(); try { var properties = type.GetProperties().Where(e => e.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0); if (!string.IsNullOrWhiteSpace(key)) { //为字段赋值 var values = key.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (properties.Count() == 1 || properties.Count() == values.Count()) { int i = 0; foreach (var p in properties) p.SetValue(this, values[i]); } } } catch (Exception) { } } /// /// 断言实体各个字段值符合要求 /// public void AssertValidate() { var type = this.GetType(); var properties = type.GetProperties(); var requiredProperties = properties.Where(e => e.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0).ToList(); requiredProperties.ForEach(e => { var value = e.GetValue(this); if (value == null) { //throw new ArgumentException($"字段{e.Name}值能为空"); //赋默认值 object? v = e.PropertyType.IsValueType ? Activator.CreateInstance(e.PropertyType) : string.Empty; e.SetValue(this, v); } }); var limitedProperties = properties.Where(e => e.GetCustomAttributes(typeof(StringLengthAttribute), false).Length > 0).ToList(); limitedProperties.ForEach(e => { var attr = (e.GetCustomAttributes(typeof(StringLengthAttribute), false) as StringLengthAttribute[])[0]; int max = attr.MaximumLength; int min = attr.MinimumLength; var strValue = e.GetValue(this) + ""; if (strValue.Length > max || strValue.Length < min) throw new ArgumentException($"字段{e.Name}值长度大于{max},或者小于{min}"); }); } } }