public void UpdatePartial <T>(object entity, Expression <Func <T, object> > columns) where T : class { Type entityType = typeof(T); IEntitySchema es = IEntitySchemaHelper.Get(entityType); IOperationManager opm = new DefaultOperationManager(this._context, this._currentUser); List <string> modifiedPropertys = (from p in columns.Body.Type.GetProperties() select p.Name).ToList <string>(); opm.AUD_OperationCheck(es, entity, EntityOperationEnum.Update); this.UpdateActionUniqueKeyCheck(es, entity, modifiedPropertys); if (es.EntityName == T_AttachmentTable) { this._attachHandler.UpdateAttachment(entity as T_Attachment, columns as Expression <Func <T_Attachment, object> >); } else { this.AutoUpdateSystemLevelCode(es, entity); if (!(!this.UpdateTableVersion(es, entity) || modifiedPropertys.Contains(TableVersion))) { modifiedPropertys.Add(TableVersion); } this.UpdateEntityChangeLog(es, entity, modifiedPropertys); this._context.UpdatePartial(entity, modifiedPropertys); this.SaveAttachmentOwner(es, entity); } }
public void MMDelete(Type entityType, int parentId, string parentEntityName, List <object> childIds) { IOperationManager opm = new DefaultOperationManager(this._context, this._currentUser); IEntitySchema es = IEntitySchemaHelper.Get(entityType); IEntitySchema schema2 = IEntitySchemaHelper.Get(parentEntityName); object entity = this._context.FindById(schema2.EntityType, new object[] { parentId }); if (entity == null) { throw new DeleteException(es, parentId, string.Format("父实体[{0}]记录不存在", parentEntityName)); } opm.AUD_OperationCheck(schema2, entity, EntityOperationEnum.Update); string str = string.Format("{0}s", es.EntityName); string entityName = schema2.MmTables[str]; IEntitySchema schema3 = IEntitySchemaHelper.Get(entityName); using (TransactionScope scope = new TransactionScope()) { foreach (object obj3 in childIds) { string condition = string.Format("{0} = {1} and {2} = {3}", new object[] { es.KeyName, obj3, schema2.KeyName, parentId }); IList list = this._context.Where(schema3.EntityType, condition, new DbParameter[0]); foreach (object obj4 in list) { this._context.Delete((dynamic)obj4); } } scope.Complete(); } }
public void Update(object entity) { IEntitySchema es = IEntitySchemaHelper.Get(entity.GetType()); IOperationManager opm = new DefaultOperationManager(this._context, this._currentUser); List <string> modifiedPropertys = (from p in es.PropertyTypes select p.Key).ToList <string>(); opm.AUD_OperationCheck(es, entity, EntityOperationEnum.Update); this.UpdateActionUniqueKeyCheck(es, entity, modifiedPropertys); if (es.PrivilegeModel() == PrivilegeModel.Persional) { entity.SetPropertyValue("UpdateTime", DateTime.Now, null); entity.SetPropertyValue("UpdateUserId", opm.CurrentUser.User_ID, null); } if (es.EntityName == T_AttachmentTable) { this._attachHandler.UpdateAttachment(entity as T_Attachment); } else { this.AutoUpdateSystemLevelCode(es, entity); this.UpdateTableVersion(es, entity); this.UpdateEntityChangeLog(es, entity, modifiedPropertys); this._context.Update(entity); this.SaveAttachmentOwner(es, entity); } }
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("添加"); } } } }
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); } } } }
public object GetEntity(string entityName, int id) { if (!(string.IsNullOrEmpty(entityName) || (id == 0))) { IEntitySchema schema = IEntitySchemaHelper.Get(entityName); return(this._context.FindById(schema.EntityType, new object[] { id })); } return(null); }
public string GetDisplayValue(long entityId, int objectId) { try { IEntitySchema schema = IEntitySchemaHelper.Get(entityId); object entity = this.GetEntity(schema.EntityName, objectId); return(string.Empty); } catch { return(string.Empty); } }
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); } }
public void MMSave(Type entityType, int parentId, string parentEntityName, List <string> childIds) { IOperationManager opm = new DefaultOperationManager(this._context, this._currentUser); IEntitySchema es = IEntitySchemaHelper.Get(entityType); IEntitySchema schema2 = IEntitySchemaHelper.Get(parentEntityName); object entity = this._context.FindById(schema2.EntityType, new object[] { parentId }); if (entity == null) { throw new DeleteException(es, parentId, string.Format("父实体[{0}]记录不存在", parentEntityName)); } opm.AUD_OperationCheck(schema2, entity, EntityOperationEnum.Update); string str = string.Format("{0}s", es.EntityName); string entityName = schema2.MmTables[str]; IEntitySchema schema3 = IEntitySchemaHelper.Get(entityName); string condition = string.Format("{0} = {1}", schema2.KeyName, parentId); IList list = this._context.Where(schema3.EntityType, condition, new DbParameter[0]); List <int> first = (from o in childIds select o.ToInt()).ToList <int>(); List <int> second = new List <int>(); foreach (object obj3 in list) { int item = (int)obj3.GetPropertyValue(es.KeyName, null); second.Add(item); } List <int> list4 = first.Except <int>(second).ToList <int>(); using (TransactionScope scope = new TransactionScope()) { foreach (int num in list4) { object targetObj = schema3.CreateInstance(); targetObj.SetPropertyValue(schema3.KeyName, this.GetNextIdentity(), null); targetObj.SetPropertyValue(schema2.KeyName, parentId, null); targetObj.SetPropertyValue(es.KeyName, num, null); this._context.Insert(targetObj); } scope.Complete(); } }
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(); } } }
private string GetEntityNameById(long entityId) { return(IEntitySchemaHelper.Get(entityId).EntityName); }