|
- 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;
- }
-
-
-
-
-
- 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)
- {
-
-
-
- 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}");
- });
- }
- }
- }
|