Пример #1
0
        public int CreateLog()
        {
            var logList      = new List <AuditLog>();
            var notifies     = new List <Notification>();
            var addedEntries = _context.ChangeTracker.Entries().Where(e => e.State == EntityState.Added).ToList();

            if (addedEntries.Count > 0)
            {
                _context.SaveChanges();
                foreach (var entry in addedEntries)
                {
                    var audit = WriteLog.Create(entry, 1, typeof(TEntity));
                    if (audit == null)
                    {
                        continue;
                    }
                    logList.Add(audit);
                    var notify = Notification.ActionNotifyForGroup(entry, 1, audit.Id, typeof(TEntity));
                    if (notify == null)
                    {
                        continue;
                    }
                    notifies.Add(notify);
                }
            }

            var modifiedEntries = _context.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified).ToList();

            if (modifiedEntries.Count > 0)
            {
                foreach (var entry in modifiedEntries)
                {
                    var properties = typeof(TEntity).GetProperties()
                                     .Where(property =>
                                            property != null && Attribute.IsDefined(property, typeof(IgnoreUpdateAttribute)))
                                     .Select(p => p.Name);
                    foreach (var property in properties)
                    {
                        entry.Property(property).IsModified = false;
                    }

                    var audit = WriteLog.Create(entry, 2, typeof(TEntity));
                    if (audit == null)
                    {
                        continue;
                    }
                    logList.Add(audit);

                    var notify = Notification.ActionNotifyForGroup(entry, 1, audit.Id, typeof(TEntity));
                    if (notify == null)
                    {
                        continue;
                    }
                    notifies.Add(notify);
                }
            }

            var deleteEntries = _context.ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted).ToList();

            if (deleteEntries.Count > 0)
            {
                foreach (var entry in deleteEntries)
                {
                    var audit = WriteLog.Create(entry, 3, typeof(TEntity));
                    if (audit == null)
                    {
                        continue;
                    }
                    logList.Add(audit);

                    var notify = Notification.ActionNotifyForGroup(entry, 1, audit.Id, typeof(TEntity));
                    if (notify == null)
                    {
                        continue;
                    }
                    notifies.Add(notify);
                }
            }

            if (addedEntries.Count <= 0 && modifiedEntries.Count <= 0 && deleteEntries.Count <= 0)
            {
                return(0);
            }

            LogNotifyList(logList, notifies);

            return(1);
        }