You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.4 KiB
C#

using System.Text;
using SqlSugar;
namespace DS.Module.SqlSugar;
/// <summary>
/// sqlsuagr差异帮助类
/// </summary>
public static class SqlSugarDiffUtil
{
/// <summary>
/// 比较两个数据对象的修改内容
/// </summary>
/// <param name="beforeData"></param>
/// <param name="afterData"></param>
/// <returns></returns>
public static DiffLog GetDiff(List<DiffLogTableInfo> beforeData, List<DiffLogTableInfo> afterData)
{
string mianID = null;
if (beforeData != null)
{
var keyCoulumn = beforeData[0].Columns.FirstOrDefault(p => p.IsPrimaryKey == true);
if (keyCoulumn != null)
{
mianID = keyCoulumn.Value.ToString();
}
}
else if (afterData != null)
{
var keyCoulumn = afterData[0].Columns.FirstOrDefault(p => p.IsPrimaryKey == true);
if (keyCoulumn != null)
{
mianID = keyCoulumn.Value.ToString();
}
}
StringBuilder sb = new StringBuilder();
if (beforeData != null && afterData != null)
{
var befroeColumns = beforeData[0].Columns;
var afterCloums = afterData[0].Columns;
foreach (var item in befroeColumns)
{
if (IgnoreColumns.Contains(item.ColumnName))
continue;
var afterItem = afterCloums.FirstOrDefault(p => p.ColumnName == item.ColumnName && !p.Value.Equals(item.Value));
if (afterItem != null)
{
sb.Append($"[字段:{item.ColumnDescription},修改前:{item.Value},修改后:{afterItem.Value}]");
}
}
}
return new DiffLog { Id = mianID, DiffData = sb.ToString() };
}
public class DiffLog
{
/// <summary>
/// 主键Id
/// </summary>
public string Id { get; set; }
/// <summary>
/// 差异数据
/// </summary>
public string DiffData { get; set; }
}
/// <summary>
/// 忽略的字段
/// </summary>
private static readonly List<string> IgnoreColumns = new List<string>()
{
"CreateTime",
4 months ago
"CreateUserName",
"CreateBy",
"UpdateTime",
4 months ago
"UpdateUserName",
"UpdateBy",
4 months ago
"DeleteTime",
"DeleteUserName",
"DeleteBy",
};
}