Example #1
0
        public async Task <int> UpdateAsync(T entity, string where, params Expression <Func <T, object> >[] fields)
        {
            int updateCount = 0;

            #region PreExecute
            var watch = StartMonitor();
            //重新构建where
            where = BuildWhere(where);

            //处理IModifyRecord
            if (fields != null && fields.Length > 0 && tableInfo.IsIModifyRecord)
            {
                var fieldList = fields.ToList();
                Expression <Func <T, object> > expression = a => (a as IModifyRecord).ModifyTime;
                fieldList.Add(expression as Expression <Func <T, object> >);
                expression = a => (a as IModifyRecord).ModifyUserId;
                fieldList.Add(expression as Expression <Func <T, object> >);
                expression = a => (a as IModifyRecord).ModifyUserName;
                fieldList.Add(expression as Expression <Func <T, object> >);
                expression = a => (a as IModifyRecord).RecordHash;
                fieldList.Add(expression as Expression <Func <T, object> >);

                var baseEntity = entity as IModifyRecord;
                baseEntity.ModifyTime = DateTime.Now;
                if (this.AccessUser != null)
                {
                    baseEntity.ModifyUserId   = this.AccessUser.UserId;
                    baseEntity.ModifyUserName = this.AccessUser.UserName;
                }
                baseEntity.RecordHash = EntityUtility.GetEntityHash <T>(entity, true);
                fields = fieldList.Distinct(new FieldExpressionComparer <T>()).ToArray();
            }
            #endregion

            #region Executing
            updateCount = await OnUpdateAsync(entity, where, fields);

            #endregion

            #region Executed
            Monitoring("Update", "数据更新", string.Format("要更新的数据为[{0}],过滤条件为[{1}],要更新的字段为[{2}]", entity.ToJson(), where, fields.ToJson()), watch);
            #endregion

            return(updateCount);
        }
Example #2
0
        public async Task <int> InsertAsync(params T[] entities)
        {
            int insertCount = 0;

            #region PreExecute
            var watch = StartMonitor();
            if (entities == null || entities.Length == 0)
            {
                return(0);
            }
            if (entities != null && entities.Length > 0 && (tableInfo.IsICreateRecord || tableInfo.IsIModifyRecord))
            {
                foreach (T entity in entities)
                {
                    DateTime now = DateTime.Now;
                    if (tableInfo.IsICreateRecord)
                    {
                        var insertEntity = (ICreateRecord)entity;
                        insertEntity.CreateTime     = now;
                        insertEntity.CreateUserId   = AccessUser.UserId;
                        insertEntity.CreateUserName = AccessUser.UserName;
                    }
                    if (tableInfo.IsIModifyRecord)
                    {
                        var modifyEntity = (IModifyRecord)entity;
                        modifyEntity.ModifyTime     = now;
                        modifyEntity.ModifyUserId   = AccessUser.UserId;
                        modifyEntity.ModifyUserName = AccessUser.UserName;
                        modifyEntity.RecordHash     = EntityUtility.GetEntityHash <T>(entity);
                    }
                }
            }
            #endregion

            #region Executing
            insertCount = await OnInsertAsync(entities);

            #endregion

            #region Executed
            Monitoring("Insert", "插入一条或多条数据", entities.ToJson(), watch);
            #endregion

            return(insertCount);
        }
Example #3
0
 /// <summary>
 /// 处理RecordHash的更新
 /// </summary>
 /// <param name="list"></param>
 private void ProcessRecordHash(params T[] entities)
 {
     if (entities != null && tableInfo.IsIModifyRecord)
     {
         entities.AsParallel().ForAll(entity =>
         {
             if (entity == null)
             {
                 return;
             }
             var modifyEntity = ((IModifyRecord)entity);
             if (modifyEntity.RecordHash == null || modifyEntity.RecordHash.Length <= 32)
             {
                 modifyEntity.RecordHash = EntityUtility.GetEntityHash(entity);
                 TaskQueue.UpdateRecordHash <T>(entity, this);
             }
         });
     }
 }
Example #4
0
        public static void UpdateRecordHash <T>(T entity, IRepository <T> repository)
            where T : class, IEntity
        {
            if (entity == null || repository == null)
            {
                return;
            }
            var task = new Task(() =>
            {
                var tableInfo = TableInfo.GetTableInfo <T>();
                if (entity != null && repository != null && tableInfo.IsIModifyRecord)
                {
                    if (tableInfo.PKList == null || tableInfo.PKList.Count == 0)
                    {
                        //没有主键则不进行更新,以免出错
                        //这里进行记录日志预警
                        return;
                    }
                    var dataDict = EntityUtility.GetEntityDict <T>(entity);
                    if (dataDict == null || dataDict.Count == 0)
                    {
                        return;
                    }
                    string where = "1=1";
                    foreach (var pk in tableInfo.PKList)
                    {
                        if (dataDict.ContainsKey(pk) == false)
                        {
                            return;
                        }
                        where += string.Format(" AND {0}='{1}'", pk, dataDict[pk]);
                    }
                    string hash  = EntityUtility.GetEntityHash <T>(entity);
                    string sql   = string.Format("UPDATE {0} SET RecordHash='{1}' WHERE {2}", tableInfo.TableName, hash, where);
                    var repoType = repository.GetType();
                    var newRepo  = Assembly.GetAssembly(repoType).CreateInstance(repoType.FullName, true, BindingFlags.Public | BindingFlags.Instance, null, new object[] { repository.ConnectionStr }, CultureInfo.CurrentCulture, null) as IRepository <T>;
                    newRepo.ExecuteNonQueryAsync(sql);
                }
            });

            _queue.Enqueue(task);
        }