|
@@ -0,0 +1,317 @@
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Data.Entity.Infrastructure;
|
|
|
|
+using EMIS.Entities.Log;
|
|
|
|
+using EMIS.Utility.FormValidate;
|
|
|
|
+using Bowin.Common.Data;
|
|
|
|
+using System.Web;
|
|
|
|
+using System.Data;
|
|
|
|
+using System.Linq.Expressions;
|
|
|
|
+
|
|
|
|
+namespace EMIS.Utility.Log
|
|
|
|
+{
|
|
|
|
+ public class DbLog
|
|
|
|
+ {
|
|
|
|
+ public static List<Log_Operate> GetBatchUpdateLog<T>(IList<T> entities, System.Data.Entity.DbContext ctx) where T : class
|
|
|
|
+ {
|
|
|
|
+ var result = new List<Log_Operate>();
|
|
|
|
+ var principal = CustomPrincipal.Current;
|
|
|
|
+ var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
|
|
|
|
+
|
|
|
|
+ var tableKey = EMIS.Entities.TableKeyDictionary.GetKeyName<T>();
|
|
|
|
+ ParameterExpression keyParamter = Expression.Parameter(typeof(DbEntityEntry<T>), "w");
|
|
|
|
+ MemberExpression keyEntryExpression = Expression.Property(keyParamter, "Entity");
|
|
|
|
+ MemberExpression keyExpression = Expression.Property(keyEntryExpression, tableKey);
|
|
|
|
+ var entryList = ctx.ChangeTracker.Entries<T>().ToList();
|
|
|
|
+ var originalValues = entities.Select(x => new {
|
|
|
|
+ OriginalValues = entryList.FirstOrDefault(
|
|
|
|
+ Expression.Lambda<Func<DbEntityEntry<T>, bool>>(
|
|
|
|
+ Expression.Equal(keyExpression, Expression.Constant((Guid)typeof(T).GetProperty(tableKey).GetValue(x, null)))
|
|
|
|
+ , keyParamter).Compile()).OriginalValues,
|
|
|
|
+ CurrentValues = x
|
|
|
|
+ }).ToList();
|
|
|
|
+
|
|
|
|
+ foreach (var updateChange in originalValues)
|
|
|
|
+ {
|
|
|
|
+ var log = new Log_Operate
|
|
|
|
+ {
|
|
|
|
+ OperateID = Guid.NewGuid(),
|
|
|
|
+ UserID = operateUserID,
|
|
|
|
+ IP = ApplicationClientHelper.GetIP(),
|
|
|
|
+ TableName = updateChange.CurrentValues.GetType().Name,
|
|
|
|
+ SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
|
|
|
|
+ OperateTime = DateTime.Now
|
|
|
|
+ };
|
|
|
|
+ log.Operate = "修改";
|
|
|
|
+ log.Detail = "修改前内容(" + GatherValues(updateChange.OriginalValues) + "),";
|
|
|
|
+ log.Detail += "修改后内容(" + GatherValues(updateChange.CurrentValues) + ")";
|
|
|
|
+
|
|
|
|
+ result.Add(log);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<Log_Operate> GetBulkInsertLog<T>(IList<T> entities)
|
|
|
|
+ {
|
|
|
|
+ var table = entities.ToTable();
|
|
|
|
+ return GetBulkInsertLog(table, typeof(T).Name);
|
|
|
|
+ }
|
|
|
|
+ public static List<Log_Operate> GetBulkInsertLog(DataTable table, string tableName)
|
|
|
|
+ {
|
|
|
|
+ var result = new List<Log_Operate>();
|
|
|
|
+ var principal = CustomPrincipal.Current;
|
|
|
|
+ var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
|
|
|
|
+ var myIP = ApplicationClientHelper.GetIP();
|
|
|
|
+
|
|
|
|
+ foreach (DataRow row in table.Rows)
|
|
|
|
+ {
|
|
|
|
+ var value = "详细内容(";
|
|
|
|
+ for (int i = 0; i < table.Columns.Count; i++)
|
|
|
|
+ {
|
|
|
|
+ string columnName = table.Columns[i].ColumnName;
|
|
|
|
+ value += columnName + ":";
|
|
|
|
+ value += row[columnName].ToString() + "|";
|
|
|
|
+ }
|
|
|
|
+ if (value.EndsWith("|"))
|
|
|
|
+ {
|
|
|
|
+ value = value.TrimEnd('|');
|
|
|
|
+ }
|
|
|
|
+ value += ")";
|
|
|
|
+
|
|
|
|
+ Log_Operate operate = new Log_Operate()
|
|
|
|
+ {
|
|
|
|
+ OperateID = Guid.NewGuid(),
|
|
|
|
+ UserID = operateUserID,
|
|
|
|
+ IP = myIP,
|
|
|
|
+ TableName = tableName,
|
|
|
|
+ SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
|
|
|
|
+ OperateTime = DateTime.Now,
|
|
|
|
+ Operate = "新增",
|
|
|
|
+ Detail = value
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ result.Add(operate);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<Log_Operate> GetDeleteLog<T>(IList<T> entities)
|
|
|
|
+ {
|
|
|
|
+ var table = entities.ToTable();
|
|
|
|
+ return GetDeleteLog(table, typeof(T).Name);
|
|
|
|
+ }
|
|
|
|
+ public static List<Log_Operate> GetDeleteLog(DataTable table, string tableName)
|
|
|
|
+ {
|
|
|
|
+ var result = new List<Log_Operate>();
|
|
|
|
+ var principal = CustomPrincipal.Current;
|
|
|
|
+ var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
|
|
|
|
+ var myIP = ApplicationClientHelper.GetIP();
|
|
|
|
+
|
|
|
|
+ foreach (DataRow row in table.Rows)
|
|
|
|
+ {
|
|
|
|
+ var value = "删除内容(";
|
|
|
|
+ for (int i = 0; i < table.Columns.Count; i++)
|
|
|
|
+ {
|
|
|
|
+ string columnName = table.Columns[i].ColumnName;
|
|
|
|
+ value += columnName + ":";
|
|
|
|
+ value += row[columnName].ToString() + "|";
|
|
|
|
+ }
|
|
|
|
+ if (value.EndsWith("|"))
|
|
|
|
+ {
|
|
|
|
+ value = value.TrimEnd('|');
|
|
|
|
+ }
|
|
|
|
+ value += ")";
|
|
|
|
+
|
|
|
|
+ Log_Operate operate = new Log_Operate()
|
|
|
|
+ {
|
|
|
|
+ OperateID = Guid.NewGuid(),
|
|
|
|
+ UserID = operateUserID,
|
|
|
|
+ IP = myIP,
|
|
|
|
+ TableName = tableName,
|
|
|
|
+ SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
|
|
|
|
+ OperateTime = DateTime.Now,
|
|
|
|
+ Operate = "删除",
|
|
|
|
+ Detail = value
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ result.Add(operate);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<Log_Operate> GetLog(DbChangeTracker changeTracker)
|
|
|
|
+ {
|
|
|
|
+ var result = new List<Log_Operate>();
|
|
|
|
+ var entryList = changeTracker.Entries().ToList();
|
|
|
|
+ var principal = CustomPrincipal.Current;
|
|
|
|
+
|
|
|
|
+ entryList.ForEach(x => AddLog(x, result, principal));
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<Log_Operate> GetRelationshipLog(System.Data.Entity.DbContext ctx)
|
|
|
|
+ {
|
|
|
|
+ var result = new List<Log_Operate>();
|
|
|
|
+ var principal = CustomPrincipal.Current;
|
|
|
|
+ var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
|
|
|
|
+ var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
|
|
|
|
+ var deleteChanges = objectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Deleted);
|
|
|
|
+ var addChanges = objectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added);
|
|
|
|
+ var myIP = ApplicationClientHelper.GetIP();
|
|
|
|
+
|
|
|
|
+ foreach(var deleteChange in deleteChanges)
|
|
|
|
+ {
|
|
|
|
+ if (deleteChange.IsRelationship)
|
|
|
|
+ {
|
|
|
|
+ var log = new Log_Operate
|
|
|
|
+ {
|
|
|
|
+ OperateID = Guid.NewGuid(),
|
|
|
|
+ UserID = operateUserID,
|
|
|
|
+ IP = myIP,
|
|
|
|
+ TableName = deleteChange.EntitySet.Name,
|
|
|
|
+ SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
|
|
|
|
+ OperateTime = DateTime.Now,
|
|
|
|
+ Operate = "删除",
|
|
|
|
+ Detail = "删除内容(" + GatherValues(deleteChange.OriginalValues) + ")"
|
|
|
|
+ };
|
|
|
|
+ result.Add(log);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ foreach (var addChange in addChanges)
|
|
|
|
+ {
|
|
|
|
+ if (addChange.IsRelationship)
|
|
|
|
+ {
|
|
|
|
+ var log = new Log_Operate
|
|
|
|
+ {
|
|
|
|
+ OperateID = Guid.NewGuid(),
|
|
|
|
+ UserID = operateUserID,
|
|
|
|
+ IP = myIP,
|
|
|
|
+ TableName = addChange.EntitySet.Name,
|
|
|
|
+ SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
|
|
|
|
+ OperateTime = DateTime.Now,
|
|
|
|
+ Operate = "新增",
|
|
|
|
+ Detail = "详细内容(" + GatherRelationValues(addChange.CurrentValues, objectContext) + ")"
|
|
|
|
+ };
|
|
|
|
+ result.Add(log);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void AddLog(DbEntityEntry entry, IList<Log_Operate> logList, CustomPrincipal principal)
|
|
|
|
+ {
|
|
|
|
+ var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
|
|
|
|
+ var log = new Log_Operate {
|
|
|
|
+ OperateID = Guid.NewGuid(),
|
|
|
|
+ UserID = operateUserID,
|
|
|
|
+ IP = ApplicationClientHelper.GetIP(),
|
|
|
|
+ TableName = entry.Entity.GetType().Name,
|
|
|
|
+ SourceUrl = (HttpContext.Current == null) ? null : (HttpContext.Current.Request.UrlReferrer != null ? HttpContext.Current.Request.UrlReferrer.OriginalString : HttpContext.Current.Request.RawUrl),
|
|
|
|
+ OperateTime = DateTime.Now
|
|
|
|
+ };
|
|
|
|
+ switch (entry.State)
|
|
|
|
+ {
|
|
|
|
+ case System.Data.Entity.EntityState.Added:
|
|
|
|
+ log.Operate = "新增";
|
|
|
|
+ log.Detail = "详细内容(" + GatherValues(entry.CurrentValues) + ")";
|
|
|
|
+ break;
|
|
|
|
+ case System.Data.Entity.EntityState.Modified:
|
|
|
|
+ log.Operate = "修改";
|
|
|
|
+ log.Detail = "修改前内容(" + GatherValues(entry.OriginalValues) + "),";
|
|
|
|
+ log.Detail += "修改后内容(" + GatherValues(entry.CurrentValues) + ")";
|
|
|
|
+ break;
|
|
|
|
+ case System.Data.Entity.EntityState.Deleted:
|
|
|
|
+ log.Operate = "删除";
|
|
|
|
+ log.Detail = "删除内容(" + GatherValues(entry.OriginalValues) + ")";
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ logList.Add(log);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static string GatherRelationValues(System.Data.Common.DbDataRecord values,
|
|
|
|
+ System.Data.Entity.Core.Objects.ObjectContext context)
|
|
|
|
+ {
|
|
|
|
+ var result = "";
|
|
|
|
+ for (int i = 0; i < values.FieldCount; i ++ )
|
|
|
|
+ {
|
|
|
|
+ var entry = (System.Data.Entity.Core.EntityKey)values.GetValue(i);
|
|
|
|
+ result += entry.EntitySetName + ":";
|
|
|
|
+ var entity = context.GetObjectByKey(entry);
|
|
|
|
+ var entityType = entity.GetType();
|
|
|
|
+ var propertyName = entry.GetEntitySet(context.MetadataWorkspace).ElementType.KeyMembers[0].Name;
|
|
|
|
+ var property = entityType.GetProperty(propertyName);
|
|
|
|
+ result += property.GetValue(entity, null) + "|";
|
|
|
|
+ //foreach (var property in entityType.GetProperties())
|
|
|
|
+ //{
|
|
|
|
+ // result += property.Name + ":" + property.GetValue(entity, null) + "|";
|
|
|
|
+ //}
|
|
|
|
+ }
|
|
|
|
+ if (result.EndsWith("|"))
|
|
|
|
+ {
|
|
|
|
+ result = result.TrimEnd('|');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static string GatherValues(System.Data.Common.DbDataRecord values)
|
|
|
|
+ {
|
|
|
|
+ var result = "";
|
|
|
|
+ for (int i = 0; i < values.FieldCount; i ++ )
|
|
|
|
+ {
|
|
|
|
+ var entry = (System.Data.Entity.Core.EntityKey)values.GetValue(i);
|
|
|
|
+ result += entry.EntitySetName + ":" + entry.EntityKeyValues[0].Value.ToString() + "|";
|
|
|
|
+ }
|
|
|
|
+ if (result.EndsWith("|"))
|
|
|
|
+ {
|
|
|
|
+ result = result.TrimEnd('|');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static string GatherValues(DbPropertyValues values)
|
|
|
|
+ {
|
|
|
|
+ var result = "";
|
|
|
|
+ foreach (var propertyName in values.PropertyNames)
|
|
|
|
+ {
|
|
|
|
+ result += propertyName + ":" + values[propertyName] + "|";
|
|
|
|
+ }
|
|
|
|
+ if (result.EndsWith("|"))
|
|
|
|
+ {
|
|
|
|
+ result = result.TrimEnd('|');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static string GatherValues<T>(T entity)
|
|
|
|
+ {
|
|
|
|
+ var result = "";
|
|
|
|
+ var properties = typeof(T).GetProperties();
|
|
|
|
+ foreach (var property in properties)
|
|
|
|
+ {
|
|
|
|
+ if (property.PropertyType.IsPrimitive || property.PropertyType.IsValueType || property.PropertyType.Name.StartsWith("String"))
|
|
|
|
+ {
|
|
|
|
+ result += property.Name + ":" + (property.GetValue(entity, null) ?? "").ToString() + "|";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (result.EndsWith("|"))
|
|
|
|
+ {
|
|
|
|
+ result = result.TrimEnd('|');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|