diff options
| author | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 17:17:46 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 17:17:46 +0000 |
| commit | 0440c722a221b8068bbf388c1c0c51f0faff0451 (patch) | |
| tree | 169cbf90c50ff7961db82ecb606c50c2a45a1688 /Gestor.Model/Gestor.Model.Domain.Generic/DomainBase.cs | |
| parent | 225aa1499e37faf9d38257caabbadc68d78b427e (diff) | |
| download | gestor-0440c722a221b8068bbf388c1c0c51f0faff0451.tar.gz gestor-0440c722a221b8068bbf388c1c0c51f0faff0451.zip | |
Diffstat (limited to 'Gestor.Model/Gestor.Model.Domain.Generic/DomainBase.cs')
| -rw-r--r-- | Gestor.Model/Gestor.Model.Domain.Generic/DomainBase.cs | 307 |
1 files changed, 307 insertions, 0 deletions
diff --git a/Gestor.Model/Gestor.Model.Domain.Generic/DomainBase.cs b/Gestor.Model/Gestor.Model.Domain.Generic/DomainBase.cs new file mode 100644 index 0000000..e4a9edf --- /dev/null +++ b/Gestor.Model/Gestor.Model.Domain.Generic/DomainBase.cs @@ -0,0 +1,307 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using Gestor.Model.Attributes; + +namespace Gestor.Model.Domain.Generic; + +public abstract class DomainBase : ICloneable +{ + private List<FieldInformation> _originalValues = new List<FieldInformation>(); + + public long Id { get; set; } + + public void Initialize() + { + _originalValues = GetProperties(); + } + + public List<FieldInformation> GetProperties(object obj) + { + return (from x in obj.GetType().GetProperties() + where !HasValidate(x.GetValue(obj)) && GetLogAttribute(x).GetValueOrDefault() + select x into key + select new FieldInformation + { + Name = key.Name, + Description = GetDescriptionAttribute(key), + Value = key.GetValue(obj) + }).ToList(); + } + + public List<FieldInformation> GetProperties() + { + List<FieldInformation> dictionary = new List<FieldInformation>(); + GetType().GetProperties().ToList().ForEach(delegate(PropertyInfo x) + { + if (!(x.Name == "ValidationEvent")) + { + bool? logAttribute = GetLogAttribute(x); + if (!logAttribute.HasValue || logAttribute != false) + { + object value = x.GetValue(this); + if (!HasValidate(value)) + { + FieldInformation property = GetProperty(x, value, logAttribute); + if (property != null) + { + dictionary.Add(property); + } + } + else + { + dictionary.AddRange(GetPropertiesChield(value, GetDescriptionAttribute(x), logAttribute, defaultInfo: true)); + } + } + } + }); + return dictionary; + } + + public FieldInformation GetProperty(PropertyInfo property, dynamic value, bool? log, string description = null) + { + if (!log.HasValue || log == false) + { + return null; + } + return new FieldInformation + { + Name = property.Name, + Description = (description ?? GetDescriptionAttribute(property)), + Value = value + }; + } + + private List<FieldInformation> GetPropertiesChield(dynamic obj, string description, bool? log, bool defaultInfo = false) + { + List<FieldInformation> list = new List<FieldInformation>(); + if (obj == null) + { + return list; + } + dynamic properties = obj.GetType().GetProperties(); + foreach (dynamic item in properties) + { + if ((item.Name == "ValidationEvent") || ((defaultInfo && !GetDefaultAttribute(item)) ? true : false)) + { + continue; + } + log = GetLogAttribute(item); + if (log.HasValue && log == false) + { + continue; + } + dynamic value = item.GetValue(obj); + if (value != null && HasValidate(value)) + { + list.AddRange(GetPropertiesChield(value, GetDescriptionAttribute(item), log, true)); + continue; + } + dynamic property = GetProperty(item, value, log, description); + if (property != null) + { + list.Add(property); + } + } + return list; + } + + private bool? GetLogAttribute(PropertyInfo info) + { + object[] customAttributes = info.GetCustomAttributes(typeof(LogAttribute), inherit: true); + if (customAttributes.Length == 0) + { + return null; + } + return ((LogAttribute)customAttributes[0]).Description; + } + + private bool GetDefaultAttribute(PropertyInfo info) + { + return info.GetCustomAttributes(typeof(NameAttribute), inherit: true).Length != 0; + } + + private string GetDescriptionAttribute(PropertyInfo info) + { + if (info == null) + { + return ""; + } + object[] customAttributes = info.GetCustomAttributes(typeof(DescriptionAttribute), inherit: true); + if (customAttributes.Length == 0) + { + return info.Name.ToUpper(); + } + return ((DescriptionAttribute)customAttributes[0]).Description; + } + + public bool HasChange() + { + return !GetProperties().All(_originalValues.Contains); + } + + public bool HasChange(string property) + { + IEnumerable<FieldInformation> @object = _originalValues.Where((FieldInformation x) => x.Name == property); + return !GetProperties().All(@object.Contains<FieldInformation>); + } + + public object Clone() + { + return MemberwiseClone(); + } + + public static void Copy<TU, T>(TU dest, T source) + { + List<PropertyInfo> list = (from x in typeof(T).GetProperties() + where x.CanRead + select x).ToList(); + List<PropertyInfo> source2 = (from x in typeof(TU).GetProperties() + where x.CanWrite + select x).ToList(); + foreach (PropertyInfo sourceProp in list) + { + if (source2.Any((PropertyInfo x) => x.Name == sourceProp.Name)) + { + source2.First((PropertyInfo x) => x.Name == sourceProp.Name).SetValue(dest, sourceProp.GetValue(source, null), null); + } + } + } + + public List<ValorOriginal> GetValorOriginal() + { + return (from x in GetProperties() + select new ValorOriginal + { + Campo = x.Name, + Descricao = x.Description, + ValorAtual = ((!((x.Value != null) ? true : false)) ? null : ((!(x.Value.GetType() is DateTime)) ? x.Value?.ToString() : x.Value?.ToString("G"))) + }).ToList(); + } + + public List<Diferenca> Compare() + { + List<FieldInformation> current = GetProperties(); + List<Diferenca> difference = new List<Diferenca>(); + PropertyInfo[] properties = GetType().GetProperties(); + _originalValues.ToList().ForEach(delegate(FieldInformation x) + { + FieldInformation fieldInformation = current.FirstOrDefault((FieldInformation k) => k.Description == x.Description); + if (!((fieldInformation?.Value == null && x.Value == null) ? true : false)) + { + PropertyInfo propertyInfo = properties.FirstOrDefault((PropertyInfo p) => p.Name == x.Name); + bool flag = propertyInfo != null && propertyInfo.GetCustomAttributes(typeof(ForceLogAttribute), inherit: true).Length != 0; + if (fieldInformation?.Value == null && x.Value != null && !string.IsNullOrWhiteSpace(x.Value.ToString())) + { + difference.Add(new Diferenca + { + Campo = x.Name, + Descricao = x.Description, + ValorAnterior = x.Value.ToString(), + ValorAtual = null + }); + } + else if (fieldInformation.Value != null && x.Value == null && !string.IsNullOrWhiteSpace(fieldInformation.Value.ToString())) + { + difference.Add(new Diferenca + { + Campo = x.Name, + Descricao = x.Description, + ValorAnterior = null, + ValorAtual = fieldInformation.Value.ToString() + }); + } + else if (!((fieldInformation.Value.ToString() == x.Value.ToString() && !flag) ? true : false)) + { + difference.Add((x.Value.GetType() is DateTime) ? new Diferenca + { + Campo = x.Name, + Descricao = x.Description, + ValorAnterior = x.Value?.ToString("G"), + ValorAtual = fieldInformation.Value?.ToString("G") + } : new Diferenca + { + Campo = x.Name, + Descricao = x.Description, + ValorAnterior = x.Value?.ToString(), + ValorAtual = fieldInformation.Value?.ToString() + }); + } + } + }); + return difference; + } + + public List<Diferenca> Compare(List<ValorOriginal> valoresOriginais) + { + List<FieldInformation> current = GetProperties(); + List<Diferenca> difference = new List<Diferenca>(); + PropertyInfo[] properties = GetType().GetProperties(); + valoresOriginais.ForEach(delegate(ValorOriginal x) + { + FieldInformation fieldInformation = current.First((FieldInformation k) => k.Name == x.Campo); + if (!((fieldInformation.Value == null && x.ValorAtual == null) ? true : false)) + { + PropertyInfo propertyInfo = properties.FirstOrDefault((PropertyInfo p) => p.Name == x.Campo); + bool flag = propertyInfo != null && propertyInfo.GetCustomAttributes(typeof(ForceLogAttribute), inherit: true).Length != 0; + if (fieldInformation.Value == null && x.ValorAtual != null && !string.IsNullOrWhiteSpace(x.ValorAtual)) + { + difference.Add(new Diferenca + { + Campo = x.Campo, + Descricao = fieldInformation.Description, + ValorAnterior = x.ValorAtual, + ValorAtual = null + }); + } + else if (fieldInformation.Value != null && x.ValorAtual == null && !string.IsNullOrWhiteSpace(fieldInformation.Value.ToString())) + { + difference.Add(new Diferenca + { + Campo = x.Campo, + Descricao = fieldInformation.Description, + ValorAnterior = null, + ValorAtual = fieldInformation.Value.ToString() + }); + } + else if (!((fieldInformation.Value != null && fieldInformation.Value.ToString() == x.ValorAtual && !flag) ? true : false)) + { + difference.Add((fieldInformation.Value?.GetType() is DateTime) ? new Diferenca + { + Campo = x.Campo, + Descricao = x.Descricao, + ValorAnterior = x.ValorAtual, + ValorAtual = fieldInformation.Value?.ToString("G") + } : new Diferenca + { + Campo = x.Campo, + Descricao = x.Descricao, + ValorAnterior = x.ValorAtual?.ToString(), + ValorAtual = fieldInformation.Value?.ToString() + }); + } + } + }); + return difference; + } + + public bool HasValidate(object objectToCheck) + { + if (objectToCheck == null) + { + return false; + } + return objectToCheck.GetType().GetProperty("ValidationEvent") != null; + } + + public bool HasProperty(object objectToCheck, string propertyName) + { + if (objectToCheck == null) + { + return false; + } + return objectToCheck.GetType().GetProperty(propertyName) != null; + } +} |