Пример #1
0
 private void AutoUpdateSystemLevelCode(IEntitySchema es, object entity)
 {
     if (!string.IsNullOrEmpty(es.TreeRelationFieldName))
     {
         object oldEntity = this.GetOldEntity(es, entity);
         object obj3      = entity.GetPropertyValue(es.TreeRelationFieldName, null);
         object obj4      = oldEntity.GetPropertyValue(es.TreeRelationFieldName, null);
         if (obj3 != obj4)
         {
             string      str       = this.AutoSaveSystemLevelCode(es, entity);
             object      keyValue  = es.GetKeyValue(entity);
             string      str2      = Convert.ToString(oldEntity.GetPropertyValue(SystemLevelCode, null));
             DbParameter parameter = this._context.CreateParameter();
             parameter.ParameterName = this._context.AddPrefixToParameterName(SystemLevelCode);
             parameter.Value         = string.Format("{0}%", str2);
             string condition = string.Format("{0} like {1} and {2} != {3}", new object[] { SystemLevelCode, parameter.ParameterName, es.KeyName, keyValue });
             IList  list      = this._context.Where(es.EntityType, condition, new DbParameter[] { parameter });
             foreach (object obj6 in list)
             {
                 string str4 = Convert.ToString(obj6.GetPropertyValue(SystemLevelCode, null));
                 string str5 = str + str4.Substring(str2.Length);
                 obj6.SetPropertyValue(SystemLevelCode, str5, null);
                 this._context.Update(obj6);
             }
         }
     }
 }
Пример #2
0
 private void UpdateEntityChangeLog(IEntitySchema es, object entity, List <string> modifiedColumns)
 {
     if (es.IsHistory)
     {
         int          num = Convert.ToInt32(es.GetKeyValue(entity));
         SysChangeLog log = new SysChangeLog {
             LogId        = this.GetNextIdentity(),
             EntityName   = es.EntityName,
             ChangeTime   = new DateTime?(DateTime.Now),
             ChangeUserId = new int?(this._currentUser.User_ID),
             ObjectId     = new int?(num)
         };
         this._context.Insert(log);
         object oldEntity = this.GetOldEntity(es, entity);
         foreach (string str in modifiedColumns)
         {
             object obj3 = oldEntity.GetPropertyValue(str, null);
             object obj4 = entity.GetPropertyValue(str, null);
             if ((((obj3 != null) && (obj4 == null)) || ((obj3 == null) && (obj4 != null))) || (((obj3 != null) && (obj4 != null)) && !obj3.Equals(obj4)))
             {
                 SysChangeLogDetail detail = new SysChangeLogDetail {
                     DetailId      = this.GetNextIdentity(),
                     LogId         = new long?(log.LogId),
                     CurrentValue  = Convert.ToString(obj4),
                     OriginalValue = Convert.ToString(obj3),
                     DataType      = new int?(es.PropertyTypeEnums[str]),
                     FieldName     = str
                 };
                 this._context.Insert(detail);
             }
         }
     }
 }
Пример #3
0
 private void IfSetNull(string sourceEntity, ReferencedObject refObj, object refValue, Dictionary <string, DeleteInfo> setNullEntitys, Action <string, object, object> action)
 {
     if (refObj.DeleteFlag == DeleteFlag.SetNull)
     {
         string        name   = refObj.EntityType.Name;
         IEntitySchema schema = IEntitySchemaHelper.Get(name);
         IList         list   = this.FindReferenceds(name, refObj, refValue);
         foreach (object obj2 in list)
         {
             object     keyValue = schema.GetKeyValue(obj2);
             DeleteInfo info     = new DeleteInfo {
                 Id           = keyValue,
                 EntitySchema = schema,
                 RefObject    = refObj
             };
             Console.WriteLine(string.Format("找到置为空的数据:{0}", info.Key));
             if (setNullEntitys.ContainsKey(info.Key))
             {
                 Console.WriteLine("已存在,不添加");
             }
             else
             {
                 setNullEntitys.Add(info.Key, info);
                 Console.WriteLine("添加");
             }
         }
     }
 }
Пример #4
0
 private void IfCascadeDelete(string sourceEntity, ReferencedObject refObj, object refValue, Dictionary <string, DeleteInfo> deleteEntitys, Action <string, object, object> action)
 {
     if (refObj.DeleteFlag == DeleteFlag.CascadeDelete)
     {
         string        name   = refObj.EntityType.Name;
         IEntitySchema schema = IEntitySchemaHelper.Get(name);
         IList         list   = this.FindReferenceds(name, refObj, refValue);
         foreach (object obj2 in list)
         {
             object     keyValue = schema.GetKeyValue(obj2);
             DeleteInfo info     = new DeleteInfo {
                 Id           = keyValue,
                 EntitySchema = schema,
                 RefObject    = refObj
             };
             Console.WriteLine(string.Format("找到需要删除的数据:{0}", info.Key));
             if (deleteEntitys["current_delete_entity"].Equals(info))
             {
                 Console.WriteLine("父对象,不添加");
             }
             else if (deleteEntitys.ContainsKey(info.Key))
             {
                 Console.WriteLine("已存在,不添加");
             }
             else
             {
                 deleteEntitys.Add(info.Key, info);
                 Console.WriteLine("添加");
                 action(name, obj2, keyValue);
             }
         }
     }
 }
Пример #5
0
        private string AutoSaveSystemLevelCode(IEntitySchema es, object entity)
        {
            string str = null;

            if (!string.IsNullOrEmpty(es.TreeRelationFieldName))
            {
                object keyValue = es.GetKeyValue(entity);
                object obj3     = entity.GetPropertyValue(es.TreeRelationFieldName, null);
                if (obj3 != null)
                {
                    object obj5 = this._context.FindById(es.EntityType, new object[] { obj3 }).GetPropertyValue(SystemLevelCode, null);
                    str = string.Format("{0}{1}-", obj5, keyValue);
                    entity.SetPropertyValue(SystemLevelCode, str, null);
                    return(str);
                }
                str = string.Format("{0}-", keyValue);
                entity.SetPropertyValue(SystemLevelCode, str, null);
            }
            return(str);
        }
Пример #6
0
        public void Save(object entity)
        {
            IEntitySchema     es  = IEntitySchemaHelper.Get(entity.GetType());
            IOperationManager opm = new DefaultOperationManager(this._context, this._currentUser);

            if (Convert.ToInt32(es.GetKeyValue(entity)) <= 0)
            {
                int nextIdentity = this.GetNextIdentity();
                entity.SetPropertyValue(es.KeyName, nextIdentity, null);
            }
            if (es.RequiredLevel() != RequireLevelEnum.PlatForm)
            {
                entity.SetPropertyValue("CreateTime", DateTime.Now, null);
                entity.SetPropertyValue("CreateUserId", opm.CurrentUser.User_ID, null);
                object obj2 = entity.GetPropertyValue("OwnerId", null);
                if ((obj2 == null) || (Convert.ToInt32(obj2) <= 0))
                {
                    entity.SetPropertyValue("OwnerId", opm.CurrentUser.User_ID, null);
                }
                object obj3 = entity.GetPropertyValue("State", null);
                if ((obj3 == null) || (Convert.ToInt32(obj3) <= 0))
                {
                    entity.SetPropertyValue("State", 0, null);
                }
                entity.SetPropertyValue("StateDetail", 0, null);
            }
            this.SetDefaultValues(es, entity);
            opm.AUD_OperationCheck(es, entity, EntityOperationEnum.Add);
            if (es.EntityName == T_AttachmentTable)
            {
                this._attachHandler.SaveAttachment(entity as T_Attachment);
            }
            else
            {
                this.SaveActionUniqueKeyCheck(es, entity);
                this.AutoSaveSystemLevelCode(es, entity);
                this.UpdateTableVersion(es, entity);
                this._context.Insert(entity);
                this.SaveAttachmentOwner(es, entity);
            }
        }
Пример #7
0
        public void Delete(object entity)
        {
            IEntitySchema     es   = IEntitySchemaHelper.Get(entity.GetType());
            IOperationManager opm  = new DefaultOperationManager(this._context, this._currentUser);
            object            obj2 = entity.GetPropertyValue(es.KeyName, null);

            opm.AUD_OperationCheck(es, entity, EntityOperationEnum.Delete);
            if (es.EntityName == T_AttachmentTable)
            {
                this._attachHandler.DeleteAttachment(entity as T_Attachment);
            }
            else
            {
                Dictionary <string, DeleteInfo> deleteEntitys = new Dictionary <string, DeleteInfo>();
                DeleteInfo info = new DeleteInfo {
                    Id           = obj2,
                    EntitySchema = es
                };
                deleteEntitys.Add("current_delete_entity", info);
                Dictionary <string, DeleteInfo> setNullEntitys = new Dictionary <string, DeleteInfo>();
                List <string> mmDeleteSqls             = new List <string>();
                Action <string, object, object> action = null;
                action = delegate(string entityname, object delEntity, object refValue) {
                    IEntitySchema schema = IEntitySchemaHelper.Get(entityname);
                    foreach (ReferencedObject refObj in from t in schema.ReferencedObjectList
                             where t.ReferenceType == ReferenceType.OneMore
                             orderby t.DeleteFlag
                             select t)
                    {
                        this.IfNotAllowed(entityname, refObj, refValue);
                        this.IfCascadeDelete(entityname, refObj, refValue, deleteEntitys, action);
                        this.IfSetNull(entityname, refObj, refValue, setNullEntitys, action);
                    }
                    string format = "DELETE FROM {0} WHERE {1} = {2}";
                    foreach (ReferencedObject refObj in from t in schema.ReferencedObjectList
                             where t.ReferenceType == ReferenceType.MoreMore
                             orderby t.DeleteFlag
                             select t)
                    {
                        string item = string.Format(format, schema.MmTables[refObj.ReferenceField], schema.KeyName, schema.GetKeyValue(delEntity));
                        mmDeleteSqls.Add(item);
                    }
                };
                action(es.EntityName, entity, obj2);
                using (TransactionScope scope = new TransactionScope())
                {
                    foreach (string str in mmDeleteSqls)
                    {
                        if (!string.IsNullOrEmpty(str))
                        {
                            this._context.ExecuteNonQuery(str, new DbParameter[0]);
                        }
                    }
                    foreach (IGrouping <string, DeleteInfo> grouping in from x in setNullEntitys.Values group x by x.Group)
                    {
                        Console.WriteLine("更新 " + grouping.Key);
                        this.SetEntityFiledNull(grouping.ToList <DeleteInfo>());
                    }
                    deleteEntitys.Remove("current_delete_entity");
                    foreach (IGrouping <string, DeleteInfo> grouping in from x in deleteEntitys.Values group x by x.Group)
                    {
                        Console.WriteLine("删除 " + grouping.Key);
                        this.DeleteEntity(grouping.ToList <DeleteInfo>());
                    }
                    this.DeleteRoleFunction(info, false);
                    this._context.Delete(entity);
                    scope.Complete();
                }
            }
        }
Пример #8
0
        private bool UpdateActionUniqueKeyCheck(IEntitySchema es, object entity, List <string> modifiedPropertys)
        {
            Dictionary <string, List <string> > uniqueKeyDict = es.UniqueKeyDict;

            if (uniqueKeyDict.Keys.Count > 0)
            {
                foreach (KeyValuePair <string, List <string> > pair in uniqueKeyDict)
                {
                    if (pair.Value.Count == 0)
                    {
                        throw new ApplicationException(string.Format("[{0}]配置了唯一键,但未配置唯一键字段", es.EntityName));
                    }
                    object             oldEntity = this.GetOldEntity(es, entity);
                    List <string>      values    = new List <string>();
                    List <DbParameter> list2     = new List <DbParameter>();
                    string             str       = this._context.AddPrefixToParameterName(es.KeyName);
                    values.Add(string.Format("{0} != {1}", es.KeyName, str));
                    DbParameter item = this._context.CreateParameter();
                    item.ParameterName = str;
                    item.Value         = es.GetKeyValue(entity);
                    list2.Add(item);
                    foreach (string str2 in pair.Value)
                    {
                        object obj3 = null;
                        if (modifiedPropertys.Contains(str2))
                        {
                            obj3 = entity.GetPropertyValue(str2, null);
                        }
                        else
                        {
                            obj3 = oldEntity.GetPropertyValue(str2, null);
                        }
                        if (str2 != es.KeyName)
                        {
                            if (obj3 != null)
                            {
                                string str3 = this._context.AddPrefixToParameterName(str2);
                                values.Add(string.Format("{0} = {1}", str2, str3));
                                DbParameter parameter2 = this._context.CreateParameter();
                                parameter2.ParameterName = str3;
                                parameter2.Value         = obj3;
                                list2.Add(parameter2);
                            }
                            else
                            {
                                values.Add(string.Format("{0} is Null", str2));
                            }
                        }
                    }
                    string str4 = string.Join(" and ", values);
                    string sql  = string.Format("select * from {0} where {1}", es.EntityName, str4);
                    if (this._context.ExecuteDataTable(sql, list2.ToArray()).Rows.Count != 0)
                    {
                        string str6 = string.Empty;
                        foreach (string str7 in pair.Value)
                        {
                            str6 = string.Format("{0} {1}:{2}", str6, str7, entity.GetPropertyValue(str7, null));
                        }
                        throw new ApplicationException(string.Format("更新违反唯一键约束 {0} ", str6));
                    }
                }
            }
            return(true);
        }