anjoy8 / anjoy8/Blog.Core

加了一个ModelMapHelper类

Aberta
#341 2 comentários 0 reações 0 responsáveis Ver no GitHub
很棒的提议 欢迎提交PR,给社区一起做贡献~~
Linguagem predominante
C#
Estrelas
5.3k
Forks
1.4k
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace SEP.Common
{
///
/// 对象转换
///
public class ModelMapHelper
{
///
/// 合并数据 把origin的数据合并到obj中
///
/// 原类型
/// 目标类型
/// 原对象
/// 目标对象
/// 是否允许null或空值
public static void GetModelMerge(T1 origin, T2 obj, bool allowEmployee = true)
{
var OriginPropertyList = GetPropertyValue(origin);
if (obj != null)
{
Dictionary propertyValue = new Dictionary();
Type type = obj.GetType();
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (PropertyInfo property in propertyInfos)
{
string value = "";
if (OriginPropertyList.TryGetValue(property.Name, out value))
{
if (!property.PropertyType.IsGenericType)
{
//非泛型
if (!string.IsNullOrEmpty(value) || allowEmployee)
property.SetValue(obj, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, property.PropertyType), null);
}
else
{
//泛型Nullable<>
Type genericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
if (!string.IsNullOrEmpty(value) || allowEmployee)
property.SetValue(obj, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType)), null);
}
}
}
}

}
}

///
/// 获取对象的属性和值
///
/// 对象
/// 返回属性与值一一对应的字典
public static Dictionary GetPropertyValue(T obj)
{
if (obj != null)
{
Dictionary propertyValue = new Dictionary();
Type type = obj.GetType();
PropertyInfo[] propertyInfos = type.GetProperties();

foreach (PropertyInfo item in propertyInfos)
{
switch (item.PropertyType.FullName)
{
case "System.Int32":
propertyValue.Add(item.Name, (item.GetValue(obj, null) == null ? "" : item.GetValue(obj, null)).ObjToInt().ToString());
break;
case "System.Decimal":
propertyValue.Add(item.Name, (item.GetValue(obj, null) == null ? "" : item.GetValue(obj, null)).ObjToDecimal().ToString());
break;
default:
propertyValue.Add(item.Name, (item.GetValue(obj, null) == null ? "" : item.GetValue(obj, null)).ToString());
break;
}
}

return propertyValue;
}
return null;
}

///
/// 获取对象的主键列表
///
/// 对象
/// 返回主键列表
public static List GetPropertyPrimaryKeys(T obj)
{
if (obj != null)
{
List list = new List();
Type type = obj.GetType();
PropertyInfo[] propertyInfos = type.GetProperties();

foreach (PropertyInfo item in propertyInfos)
{
if (item.CustomAttributes != null)
{
foreach (var attr in item.CustomAttributes)
{
foreach (var info in attr.NamedArguments)
{
if (info.MemberName == "IsPrimaryKey"
&& (bool)info.TypedValue.Value
&& !list.Contains(item.Name))
list.Add(item.Name);
}
}
}
}

return list;
}
return null;
}

///
/// 比较2个对象,不同对象则只比较相同字段
///
///
///
///
///
///
///
///
public static string CompareModel(T1 origin, T2 obj, AppEnum.OperationType oType, string parts = "", bool hasPrimaryKey = true) where T1 : class, new() where T2 : class, new()
{
if (origin == null)
origin = new T1();
if (obj == null)
obj = new T2();

if (!string.IsNullOrEmpty(parts))
parts = "," + parts;
StringBuilder diffColumns = new StringBuilder(); //差异数据
var OriginPropertyList = GetPropertyValue(origin);
var objPropertyList = GetPropertyValue(obj);

var primaryKeysList = GetPropertyPrimaryKeys(origin);
StringBuilder diffKeyColumns = new StringBuilder();
foreach (var originItem in OriginPropertyList)
{
if (
(oType == AppEnum.OperationType.Update ||
oType == AppEnum.OperationType.Add) &&
(
originItem.Key.StartsWith("Update") ||
originItem.Key.StartsWith("Create") ||
originItem.Key.EndsWith("Code")
)) //更新时 不需要记录创建信息
{
continue;
}

string value = "";
if (objPropertyList.TryGetValue(originItem.Key, out value))
{
if (primaryKeysList.Contains(originItem.Key))
{
if (RegexExpressHelper.IsValidDemical(value) && value.ObjToDecimal() > 0)
diffKeyColumns.AppendLine("\"主键[" + originItem.Key + "]\":\"值[" + value + "]\"");
}
if (originItem.Value != value)
{
bool isSameNum = false;
if (RegexExpressHelper.IsValidDemical(originItem.Value) && RegexExpressHelper.IsValidDemical(value))
{
if (originItem.Value.ObjToDecimal() == value.ObjToDecimal())
isSameNum = true;
}
if (!isSameNum)
{
if (oType == AppEnum.OperationType.Add)
{
if (diffColumns.Length == 0)
diffColumns.AppendLine("\"" + originItem.Key + "\":\"新增值[" + value + "]\"");
else
diffColumns.AppendLine(",\"" + originItem.Key + "\":\"新增值[" + value + "]\"");
}
else if (oType == AppEnum.OperationType.Update)
{
if (diffColumns.Length == 0)
diffColumns.AppendLine("\"" + originItem.Key + "\":\"由[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]修改为[" + (string.IsNullOrEmpty(value) ? "空值" : value) + "]\"");
else
diffColumns.AppendLine(",\"" + originItem.Key + "\":\"由[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]修改为[" + (string.IsNullOrEmpty(value) ? "空值" : value) + "]\"");
}
else
{
if (diffColumns.Length == 0)
diffColumns.AppendLine("\"" + originItem.Key + "\":\"值[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]\"");
else
diffColumns.AppendLine(",\"" + originItem.Key + "\":\"值[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]\"");
}
}
}
}
}
if (diffColumns.Length > 0)
{
if (diffKeyColumns.Length > 0)
return "{" + diffKeyColumns.ToString() + "," + diffColumns.ToString() + parts + "}";
else
return "{" + diffColumns.ToString() + parts + "}";
}
else
return "";
}

public static string CompareModelMultiple(List origin, List obj, AppEnum.OperationType oType) where T1 : class, new() where T2 : class, new()
{
if (origin == null && obj == null)
return null;

if (origin == null || origin.Count == 0)
{
origin = new List();
for (int i = 0; i < obj.Count; i++)
origin.Add(new T1());
}
if (obj == null || obj.Count == 0)
{
obj = new List();
for (int i = 0; i < origin.Count; i++)
obj.Add(new T2());
}

StringBuilder diffColumnsList = new StringBuilder(); //差异数据
for (int i = 0; i < origin.Count; i++)
{
StringBuilder diffColumns = new StringBuilder();
var OriginPropertyList = GetPropertyValue(origin[i]);
var objPropertyList = GetPropertyValue(obj[i]);
foreach (var originItem in OriginPropertyList)
{
if (oType == AppEnum.OperationType.Add && (
originItem.Key.Equals("CreateTime") ||
originItem.Key.Equals("CreateUser") ||
originItem.Key.Equals("CreateUserName") ||
originItem.Key.EndsWith("Code")
))
{
continue;
}
if (oType == AppEnum.OperationType.Update && (
originItem.Key.Equals("UpdateTime") ||
originItem.Key.Equals("UpdateUser") ||
originItem.Key.Equals("UpdateUserName") ||
originItem.Key.EndsWith("Code")
))
{
continue;
}

string value = "";
if (objPropertyList.TryGetValue(originItem.Key, out value))
{
if (originItem.Value != value)
{
bool isSameNum = false;
if (RegexExpressHelper.IsValidDemical(originItem.Value) && RegexExpressHelper.IsValidDemical(value))
{
if (originItem.Value.ObjToDecimal() == value.ObjToDecimal())
isSameNum = true;
}
if (!isSameNum)
{
if (oType == AppEnum.OperationType.Add)
{
if (diffColumns.Length == 0)
diffColumns.AppendLine("\"" + originItem.Key + "\":\"新增值[" + value + "]\"");
else
diffColumns.AppendLine(",\"" + originItem.Key + "\":\"新增值[" + value + "]\"");
}
else if (oType == AppEnum.OperationType.Update)
{
if (diffColumns.Length == 0)
diffColumns.AppendLine("\"" + originItem.Key + "\":\"由[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]修改为[" + (string.IsNullOrEmpty(value) ? "空值" : value) + "]\"");
else
diffColumns.AppendLine(",\"" + originItem.Key + "\":\"由[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]修改为[" + (string.IsNullOrEmpty(value) ? "空值" : value) + "]\"");
}
else
{
if (diffColumns.Length == 0)
diffColumns.AppendLine("\"" + originItem.Key + "\":\"值[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]\"");
else
diffColumns.AppendLine(",\"" + originItem.Key + "\":\"值[" + (string.IsNullOrEmpty(originItem.Value) ? "空值" : originItem.Value) + "]\"");
}
}
}
}
}
if (diffColumns.Length > 0)
{
//数据前后添加{} 或者 ,{}
if (diffColumnsList.Length > 0)
diffColumns.Insert(0, ",{");
else
diffColumns.Insert(0, "{");
diffColumns.Append("}");
}
diffColumnsList.Append(diffColumns);
}

if (diffColumnsList.Length > 0)
return "[" + diffColumnsList.ToString() + "]";
else
return "";
}

///
/// 获取枚举值上的Description特性的说明
///
/// 枚举类型
/// 枚举值
/// 特性的说明
public static string GetEnumDescription(T obj)
{
var type = obj.GetType();
FieldInfo field = type.GetField(Enum.GetName(type, obj));
DescriptionAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (descAttr == null)
{
return string.Empty;
}
return descAttr.Description;
}

}
}

```

使用方法:
新增或更新数据,我这样处理:
![image](https://user-images.githubusercontent.com/3362813/228997813-cef11426-6c02-403e-bf02-8038f3af287c.png)

记录日志,这样处理:
![image](https://user-images.githubusercontent.com/3362813/228997983-941ed98b-e33e-4986-9767-b4c8c878778d.png)

前台解析日志:
![image](https://user-images.githubusercontent.com/3362813/228998970-9cbf63a7-2ba2-42cb-b605-4d54a6310d10.png)

![image](https://user-images.githubusercontent.com/3362813/228999272-f9d7588e-2f3b-4ee7-bc95-5312af5d4e19.png)

枚举类型根据自己需要定义:
```
///
/// 操作类型
///
public enum OperationType
{
[Description("新建资料")]
Add = 0,
[Description("更新资料")]
Update = 1,
[Description("删除资料")]
Delete = 2,
[Description("登录")]
Login = 3
}

```

Guia de contribuição

Nenhum guia de contribuição indexado para este repositório

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.