public override bool DoBatchDelete() { using (var trans = DC.BeginTransaction()) { var userorleids = DC.Set <FrameworkUserGroup>().AsNoTracking().Where(y => DC.Set <FrameworkGroup>().CheckIDs(Ids.ToList(), null).Select(x => x.GroupCode).Contains(y.GroupCode)).Select(x => x.ID); foreach (var item in userorleids) { FrameworkUserGroup f = new FrameworkUserGroup { ID = item }; DC.DeleteEntity(f); } DC.SaveChanges(); base.DoBatchDelete(); if (MSD.IsValid == true) { trans.Commit(); } else { trans.Rollback(); } } return(base.DoBatchDelete()); }
/// <summary> /// 物理删除,对于普通的TopBasePoco和Delete操作相同,对于PersistPoco则进行真正的删除。子类如有自定义操作应重载本函数 /// </summary> public virtual void DoRealDelete() { try { List <Guid> fileids = new List <Guid>(); var pros = typeof(TModel).GetProperties(); //如果包含附件,则先删除附件 var fa = pros.Where(x => x.PropertyType == typeof(FileAttachment)).ToList(); foreach (var f in fa) { if (f.GetValue(Entity) is FileAttachment file) { fileids.Add(file.ID); f.SetValue(Entity, null); } } DC.DeleteEntity(Entity); DC.SaveChanges(); foreach (var item in fileids) { FileAttachmentVM ofa = new FileAttachmentVM(); ofa.CopyContext(this); ofa.SetEntityById(item); ofa.DoDelete(); } } catch (Exception) { MSD.AddModelError("", "数据使用中,无法删除"); } }
public override async Task DoDeleteAsync() { List <Guid> oldIDs = null; if (DpType == DpTypeEnum.User) { oldIDs = DC.Set <DataPrivilege>().Where(x => x.UserCode == Entity.UserCode && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } else { oldIDs = DC.Set <DataPrivilege>().Where(x => x.GroupCode == Entity.GroupCode && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } foreach (var oldid in oldIDs) { DataPrivilege dp = new DataPrivilege { ID = oldid }; DC.Set <DataPrivilege>().Attach(dp); DC.DeleteEntity(dp); } await DC.SaveChangesAsync(); if (DpType == DpTypeEnum.User) { await Wtm.RemoveUserCache(Entity.UserCode.ToString()); } else { var userids = DC.Set <FrameworkUserGroup>().Where(x => x.GroupCode == Entity.GroupCode).Select(x => x.UserCode).ToArray(); await Wtm.RemoveUserCache(userids); } }
/// <summary> /// 物理删除,对于普通的TopBasePoco和Delete操作相同,对于PersistPoco则进行真正的删除。子类如有自定义操作应重载本函数 /// </summary> public virtual void DoRealDelete() { try { List <Guid> fileids = new List <Guid>(); var pros = typeof(TModel).GetAllProperties(); //如果包含附件,则先删除附件 var fa = pros.Where(x => x.PropertyType == typeof(FileAttachment) || typeof(TopBasePoco).IsAssignableFrom(x.PropertyType)).ToList(); foreach (var f in fa) { if (f.GetValue(Entity) is FileAttachment file) { fileids.Add(file.ID); } f.SetValue(Entity, null); } var fas = pros.Where(x => typeof(IEnumerable <ISubFile>).IsAssignableFrom(x.PropertyType)).ToList(); foreach (var f in fas) { IEnumerable <ISubFile> subs = f.GetValue(Entity) as IEnumerable <ISubFile>; if (subs == null) { var fullEntity = DC.Set <TModel>().AsQueryable().Include(f.Name).AsNoTracking().CheckID(Entity.ID).FirstOrDefault(); subs = f.GetValue(fullEntity) as IEnumerable <ISubFile>; } if (subs != null) { foreach (var sub in subs) { fileids.Add(sub.FileId); } f.SetValue(Entity, null); } } if (typeof(TModel) != typeof(FileAttachment)) { foreach (var pro in pros) { if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco))) { pro.SetValue(Entity, null); } } } DC.DeleteEntity(Entity); DC.SaveChanges(); var fp = KnifeVirgo.HttpContext.RequestServices.GetRequiredService <VirgoFileProvider>(); foreach (var item in fileids) { fp.DeleteFile(item.ToString(), DC.ReCreate()); } } catch (Exception) { MSD.AddModelError("", CoreProgram.Callerlocalizer["DeleteFailed"]); } }
/// <summary> /// 物理删除,对于普通的TopBasePoco和Delete操作相同,对于PersistPoco则进行真正的删除。子类如有自定义操作应重载本函数 /// </summary> public virtual void DoRealDelete() { try { List <Guid> fileids = new List <Guid>(); var pros = typeof(TModel).GetProperties(); //如果包含附件,则先删除附件 var fa = pros.Where(x => x.PropertyType == typeof(FileAttachment) || typeof(TopBasePoco).IsAssignableFrom(x.PropertyType)).ToList(); foreach (var f in fa) { if (f.GetValue(Entity) is FileAttachment file) { fileids.Add(file.ID); } f.SetValue(Entity, null); } var fas = pros.Where(x => typeof(IEnumerable <ISubFile>).IsAssignableFrom(x.PropertyType)).ToList(); foreach (var f in fas) { var subs = f.GetValue(Entity) as IEnumerable <ISubFile>; if (subs != null) { foreach (var sub in subs) { fileids.Add(sub.FileId); } f.SetValue(Entity, null); } } if (typeof(TModel) != typeof(FileAttachment)) { foreach (var pro in pros) { if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco))) { pro.SetValue(Entity, null); } } } DC.DeleteEntity(Entity); DC.SaveChanges(); foreach (var item in fileids) { FileAttachmentVM ofa = new FileAttachmentVM(); ofa.CopyContext(this); ofa.SetEntityById(item); ofa.DoDelete(); } } catch (Exception) { MSD.AddModelError("", Program._localizer["DeleteFailed"]); } }
public override void DoAdd() { if (SelectedItemsID == null) { return; } List <Guid> oldIDs = null; if (DpType == DpTypeEnum.User) { oldIDs = DC.Set <DataPrivilege>().Where(x => x.UserId == Entity.UserId && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } else { oldIDs = DC.Set <DataPrivilege>().Where(x => x.GroupId == Entity.GroupId && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } foreach (var oldid in oldIDs) { DataPrivilege dp = new DataPrivilege { ID = oldid }; DC.Set <DataPrivilege>().Attach(dp); DC.DeleteEntity(dp); } if (DpType == DpTypeEnum.User) { foreach (var id in SelectedItemsID) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = id; dp.UserId = Entity.UserId; dp.TableName = this.Entity.TableName; dp.DomainId = this.Entity.DomainId; DC.Set <DataPrivilege>().Add(dp); } } else { foreach (var id in SelectedItemsID) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = id; dp.GroupId = Entity.GroupId; dp.TableName = this.Entity.TableName; dp.DomainId = this.Entity.DomainId; DC.Set <DataPrivilege>().Add(dp); } } DC.SaveChanges(); }
public async Task <bool> DoChangeAsync() { List <Guid> AllowedMenuIds = new List <Guid>(); var torem = AllowedMenuIds.Distinct(); foreach (var page in Pages) { if (page.Actions != null) { foreach (var action in page.Actions) { if (AllowedMenuIds.Contains(action) == false) { AllowedMenuIds.Add(action); } } } } var oldIDs = DC.Set <FunctionPrivilege>().Where(x => x.RoleId == Entity.ID).Select(x => x.ID).ToList(); foreach (var oldid in oldIDs) { FunctionPrivilege fp = new FunctionPrivilege { ID = oldid }; DC.Set <FunctionPrivilege>().Attach(fp); DC.DeleteEntity(fp); } foreach (var menuid in AllowedMenuIds) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = Entity.ID; fp.UserId = null; fp.Allowed = true; DC.Set <FunctionPrivilege>().Add(fp); } await DC.SaveChangesAsync(); var userids = DC.Set <FrameworkUserRole>().Where(x => x.RoleId == Entity.ID).Select(x => x.UserId.ToString()).ToArray(); await LoginUserInfo.RemoveUserCache(userids); return(true); }
public bool DoChange() { List <Guid> AllowedMenuIds = new List <Guid>(); var torem = AllowedMenuIds.Distinct(); foreach (var page in Pages) { if (page.Actions != null) { foreach (var action in page.Actions) { if (AllowedMenuIds.Contains(action) == false) { AllowedMenuIds.Add(action); } } } } var oldIDs = DC.Set <FunctionPrivilege>().Where(x => x.RoleId == Entity.ID).Select(x => x.ID).ToList(); foreach (var oldid in oldIDs) { FunctionPrivilege fp = new FunctionPrivilege { ID = oldid }; DC.Set <FunctionPrivilege>().Attach(fp); DC.DeleteEntity(fp); } foreach (var menuid in AllowedMenuIds) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = Entity.ID; fp.UserId = null; fp.Allowed = true; DC.Set <FunctionPrivilege>().Add(fp); } DC.SaveChanges(); return(true); }
public void AddPrivilege(List <Guid> menuids) { var oldIDs = DC.Set <FunctionPrivilege>().Where(x => menuids.Contains(x.MenuItemId)).Select(x => x.ID).ToList(); var admin = DC.Set <FrameworkRole>().Where(x => x.RoleCode == "001").SingleOrDefault(); foreach (var oldid in oldIDs) { try { FunctionPrivilege fp = new FunctionPrivilege { ID = oldid }; DC.Set <FunctionPrivilege>().Attach(fp); DC.DeleteEntity(fp); } catch { } } if (admin != null && SelectedRolesIDs.Contains(admin.ID) == false) { SelectedRolesIDs.Add(admin.ID); } foreach (var menuid in menuids) { if (SelectedRolesIDs != null) { foreach (var id in SelectedRolesIDs) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = id; fp.UserId = null; fp.Allowed = true; DC.Set <FunctionPrivilege>().Add(fp); } } } DC.SaveChanges(); }
public override void DoDelete() { List <Guid> oldIDs = null; if (DpType == DpTypeEnum.User) { oldIDs = DC.Set <DataPrivilege>().Where(x => x.UserId == Entity.UserId && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } else { oldIDs = DC.Set <DataPrivilege>().Where(x => x.GroupId == Entity.GroupId && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } foreach (var oldid in oldIDs) { DataPrivilege dp = new DataPrivilege { ID = oldid }; DC.Set <DataPrivilege>().Attach(dp); DC.DeleteEntity(dp); } DC.SaveChanges(); }
public bool DoChange() { List <Guid> oldIDs = DC.Set <DataPrivilege>().Where(x => x.GroupId == GroupId).Select(x => x.ID).ToList(); foreach (var oldid in oldIDs) { DataPrivilege dp = new DataPrivilege { ID = oldid }; DC.Set <DataPrivilege>().Attach(dp); DC.DeleteEntity(dp); } foreach (var item in DpLists) { if (item.IsAll == true) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = null; dp.GroupId = GroupId; dp.TableName = item.List.Searcher.TableName; dp.DomainId = null; DC.Set <DataPrivilege>().Add(dp); } if (item.IsAll == false) { foreach (var id in item.SelectedIds) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = id; dp.GroupId = GroupId; dp.TableName = item.List.Searcher.TableName; dp.DomainId = null; DC.Set <DataPrivilege>().Add(dp); } } } DC.SaveChanges(); return(true); }
public bool DoChange() { var all = FC.Where(x => x.Key.StartsWith("menu_")).ToList(); List <Guid> AllowedMenuIds = all.Where(x => x.Value.ToString() == "1").Select(x => Guid.Parse(x.Key.Replace("menu_", ""))).ToList(); List <Guid> DeniedMenuIds = all.Where(x => x.Value.ToString() == "2").Select(x => Guid.Parse(x.Key.Replace("menu_", ""))).ToList(); List <Guid> DefaultMenuIds = all.Where(x => x.Value.ToString() == "0").Select(x => Guid.Parse(x.Key.Replace("menu_", ""))).ToList(); var torem = AllowedMenuIds.Concat(DeniedMenuIds).Concat(DefaultMenuIds).Distinct(); var oldIDs = DC.Set <FunctionPrivilege>().Where(x => x.RoleId == Entity.ID && torem.Contains(x.MenuItemId)).Select(x => x.ID).ToList(); foreach (var oldid in oldIDs) { FunctionPrivilege fp = new FunctionPrivilege { ID = oldid }; DC.Set <FunctionPrivilege>().Attach(fp); DC.DeleteEntity(fp); } foreach (var menuid in AllowedMenuIds) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = Entity.ID; fp.UserId = null; fp.Allowed = true; DC.Set <FunctionPrivilege>().Add(fp); } foreach (var menuid in DeniedMenuIds) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = Entity.ID; fp.UserId = null; fp.Allowed = false; DC.Set <FunctionPrivilege>().Add(fp); } DC.SaveChanges(); return(true); }
/// <summary> /// 批量删除,默认对Ids中包含的主键的数据进行删除。子类如果有特殊判断应重载本函数 /// </summary> /// <returns>true代表成功,false代表失败</returns> public virtual bool DoBatchDelete() { bool rv = true; //循环所有数据Id List <string> idsData = Ids.ToList(); var modelType = typeof(TModel); var pros = modelType.GetAllProperties(); //如果包含附件,则先删除附件 List <Guid> fileids = new List <Guid>(); var fa = pros.Where(x => x.PropertyType == typeof(FileAttachment) || typeof(TopBasePoco).IsAssignableFrom(x.PropertyType)).ToList(); var isPersist = modelType.IsSubclassOf(typeof(PersistPoco)); var query = DC.Set <TModel>().AsQueryable(); var fas = pros.Where(x => typeof(IEnumerable <ISubFile>).IsAssignableFrom(x.PropertyType)).ToList(); foreach (var f in fas) { query = query.Include(f.Name); } query = query.AsNoTracking().CheckIDs(idsData); var entityList = query.ToList(); for (int i = 0; i < entityList.Count; i++) { string checkErro = null; //检查是否可以删除,如不能删除则直接跳过 if (CheckIfCanDelete(idsData[i], out checkErro) == false) { ErrorMessage.Add(idsData[i], checkErro); rv = false; break; } //进行删除 try { var Entity = entityList[i]; if (isPersist) { (Entity as PersistPoco).IsValid = false; (Entity as PersistPoco).UpdateTime = DateTime.Now; (Entity as PersistPoco).UpdateBy = LoginUserInfo.ITCode; DC.UpdateProperty(Entity, "IsValid"); DC.UpdateProperty(Entity, "UpdateTime"); DC.UpdateProperty(Entity, "UpdateBy"); } else { foreach (var f in fa) { if (f.PropertyType == typeof(FileAttachment)) { string fidfield = DC.GetFKName2(modelType, f.Name); var fidpro = pros.Where(x => x.Name == fidfield).FirstOrDefault(); var idresult = fidpro.GetValue(Entity); if (idresult != null) { Guid fid = Guid.Empty; if (Guid.TryParse(idresult.ToString(), out fid) == true) { fileids.Add(fid); } } } f.SetValue(Entity, null); } foreach (var f in fas) { var subs = f.GetValue(Entity) as IEnumerable <ISubFile>; if (subs != null) { foreach (var sub in subs) { fileids.Add(sub.FileId); } f.SetValue(Entity, null); } else { } } if (typeof(TModel) != typeof(FileAttachment)) { foreach (var pro in pros) { if (pro.PropertyType.GetTypeInfo().IsSubclassOf(typeof(TopBasePoco))) { pro.SetValue(Entity, null); } } } DC.DeleteEntity(Entity); } } catch (Exception e) { SetExceptionMessage(e, idsData[i]); rv = false; } } //进行数据库的删除操作 if (rv == true) { try { DC.SaveChanges(); var fp = KnifeVirgo.HttpContext.RequestServices.GetRequiredService <VirgoFileProvider>(); foreach (var item in fileids) { fp.DeleteFile(item.ToString(), DC.ReCreate()); } } catch (Exception e) { SetExceptionMessage(e, null); rv = false; } } //如果失败,添加错误信息 if (rv == false) { if (ErrorMessage.Count > 0) { foreach (var id in idsData) { if (!ErrorMessage.ContainsKey(id)) { ErrorMessage.Add(id, Core.CoreProgram.Callerlocalizer?["Sys.Rollback"]); } } } ListVM?.DoSearch(); if (ListVM != null) { foreach (var item in ListVM?.GetEntityList()) { item.BatchError = ErrorMessage.Where(x => x.Key == item.GetID().ToString()).Select(x => x.Value).FirstOrDefault(); } } MSD.AddModelError("", Core.CoreProgram.Callerlocalizer?["Sys.DataCannotDelete"]); } return(rv); }
public override async Task DoEditAsync(bool updateAllFields = false) { if (FC.ContainsKey("Entity.ITCode")) { FC.Remove("Entity.ITCode"); } using (var trans = DC.BeginTransaction()) { if (SelectedRolesCodes != null) { List <Guid> todelete = new List <Guid>(); todelete.AddRange(DC.Set <FrameworkUserRole>().AsNoTracking().Where(x => x.UserCode == Entity.ITCode).Select(x => x.ID)); foreach (var item in todelete) { DC.DeleteEntity(new FrameworkUserRole { ID = item }); } } if (SelectedGroupCodes != null) { List <Guid> todelete = new List <Guid>(); todelete.AddRange(DC.Set <FrameworkUserGroup>().AsNoTracking().Where(x => x.UserCode == Entity.ITCode).Select(x => x.ID)); foreach (var item in todelete) { DC.DeleteEntity(new FrameworkUserGroup { ID = item }); } } if (SelectedRolesCodes != null) { foreach (var rolecode in SelectedRolesCodes) { FrameworkUserRole r = new FrameworkUserRole { RoleCode = rolecode, UserCode = Entity.ITCode }; DC.AddEntity(r); } } if (SelectedGroupCodes != null) { foreach (var groupcode in SelectedGroupCodes) { FrameworkUserGroup g = new FrameworkUserGroup { GroupCode = groupcode, UserCode = Entity.ITCode }; DC.AddEntity(g); } } await base.DoEditAsync(updateAllFields); if (MSD.IsValid) { trans.Commit(); await Wtm.RemoveUserCache(Entity.ID.ToString()); } else { trans.Rollback(); } } }
private void DoEditPrepare(bool updateAllFields) { if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = Entity as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo?.ITCode; } } var pros = typeof(TModel).GetProperties(); #region 更新子表 foreach (var pro in pros) { //找到类型为List<xxx>的字段 if (pro.PropertyType.GenericTypeArguments.Count() > 0) { //获取xxx的类型 var ftype = pro.PropertyType.GenericTypeArguments.First(); //如果xxx继承自TopBasePoco if (ftype.IsSubclassOf(typeof(TopBasePoco))) { //界面传过来的子表数据 if (pro.GetValue(Entity) is IEnumerable <TopBasePoco> list && list.Count() > 0) { //获取外键字段名称 string fkname = DC.GetFKName <TModel>(pro.Name); PropertyInfo[] itemPros = ftype.GetProperties(); bool found = false; foreach (var newitem in list) { var subtype = newitem.GetType(); if (subtype.IsSubclassOf(typeof(BasePoco))) { BasePoco ent = newitem as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo?.ITCode; } } //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.GetID()); found = true; } } } } //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环 if (found == false) { continue; } TModel _entity = null; //打开新的数据库联接,获取数据库中的主表和子表数据 using (var ndc = DC.CreateNew()) { _entity = ndc.Set <TModel>().Include(pro.Name).AsNoTracking().CheckID(Entity.GetID()).FirstOrDefault(); } //比较子表原数据和新数据的区别 IEnumerable <TopBasePoco> toadd = null; IEnumerable <TopBasePoco> toremove = null; IEnumerable <TopBasePoco> data = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; Utils.CheckDifference(data, list, out toremove, out toadd); //设定子表应该更新的字段 List <string> setnames = new List <string>(); foreach (var field in FC.Keys) { if (field.StartsWith("Entity." + pro.Name + "[0].")) { string name = field.Replace("Entity." + pro.Name + "[0].", ""); setnames.Add(name); } } //前台传过来的数据 foreach (var newitem in list) { //数据库中的数据 foreach (var item in data) { //需要更新的数据 if (newitem.GetID().ToString() == item.GetID().ToString()) { dynamic i = newitem; var newitemType = item.GetType(); foreach (var itempro in itemPros) { if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && (updateAllFields == true || setnames.Contains(itempro.Name))) { var notmapped = itempro.GetCustomAttribute <NotMappedAttribute>(); if (itempro.Name != "ID" && notmapped == null && itempro.PropertyType.IsList() == false) { DC.UpdateProperty(i, itempro.Name); } } } if (item.GetType().IsSubclassOf(typeof(BasePoco))) { DC.UpdateProperty(i, "UpdateTime"); DC.UpdateProperty(i, "UpdateBy"); } } } } //需要删除的数据 foreach (var item in toremove) { //如果是PersistPoco,则把IsValid设为false,并不进行物理删除 if (ftype.IsSubclassOf(typeof(PersistPoco))) { (item as PersistPoco).IsValid = false; (item as PersistPoco).UpdateTime = DateTime.Now; (item as PersistPoco).UpdateBy = LoginUserInfo?.ITCode; dynamic i = item; DC.UpdateEntity(i); } else { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } } //需要添加的数据 foreach (var item in toadd) { if (item.GetType().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = item as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } DC.AddEntity(item); } } else if (FC.Keys.Contains("Entity." + pro.Name + ".DONOTUSECLEAR") || (FC.ContainsKey("Entity." + pro.Name) && pro.GetValue(Entity) is IEnumerable <TopBasePoco> list2 && list2?.Count() == 0)) { PropertyInfo[] itemPros = ftype.GetProperties(); var _entity = DC.Set <TModel>().Include(pro.Name).AsNoTracking().CheckID(Entity.GetID()).FirstOrDefault(); if (_entity != null) { IEnumerable <TopBasePoco> removeData = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; //如果是PersistPoco,则把IsValid设为false,并不进行物理删除 if (removeData is IEnumerable <PersistPoco> removePersistPocoData) { foreach (var item in removePersistPocoData) { (item as PersistPoco).IsValid = false; (item as PersistPoco).UpdateTime = DateTime.Now; (item as PersistPoco).UpdateBy = LoginUserInfo?.ITCode; dynamic i = item; DC.UpdateEntity(i); } } else { foreach (var item in removeData) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } } } } } } } #endregion if (updateAllFields == false) { foreach (var field in FC.Keys) { if (field.StartsWith("Entity.") && !field.Contains("[")) { string name = field.Replace("Entity.", ""); try { DC.UpdateProperty(Entity, name); } catch (Exception ea) { } } } if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { try { DC.UpdateProperty(Entity, "UpdateTime"); DC.UpdateProperty(Entity, "UpdateBy"); } catch (Exception) { } } } else { DC.UpdateEntity(Entity); } }
public virtual void UpdateEntityList(bool updateAllFields = false) { if (EntityList != null) { var ftype = EntityList.GetType().GenericTypeArguments.First(); PropertyInfo[] itemPros = ftype.GetProperties(); foreach (var newitem in EntityList) { var subtype = newitem.GetType(); if (subtype.IsSubclassOf(typeof(BasePoco))) { BasePoco ent = newitem as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo?.ITCode; } } //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } } } IEnumerable <TopBasePoco> data = null; //打开新的数据库联接,获取数据库中的主表和子表数据 using (var ndc = DC.CreateNew()) { var ids = EntityList.Select(x => x.GetID().ToString()).ToList(); data = ndc.Set <TModel>().AsNoTracking().Where(ids.GetContainIdExpression <TModel>()).ToList(); } //比较子表原数据和新数据的区别 IEnumerable <TopBasePoco> toadd = null; IEnumerable <TopBasePoco> toremove = null; Utils.CheckDifference(data, EntityList, out toremove, out toadd); //设定子表应该更新的字段 List <string> setnames = new List <string>(); foreach (var field in FC.Keys) { if (field.StartsWith("EntityList[0].")) { string name = field.Replace("EntityList[0].", ""); setnames.Add(name); } } //前台传过来的数据 foreach (var newitem in EntityList) { //数据库中的数据 foreach (var item in data) { //需要更新的数据 if (newitem.GetID().ToString() == item.GetID().ToString()) { dynamic i = newitem; var newitemType = item.GetType(); foreach (var itempro in itemPros) { if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && (updateAllFields == true || setnames.Contains(itempro.Name))) { var notmapped = itempro.GetCustomAttribute <NotMappedAttribute>(); if (itempro.Name != "ID" && notmapped == null && itempro.PropertyType.IsList() == false) { DC.UpdateProperty(i, itempro.Name); } } } if (item.GetType().IsSubclassOf(typeof(BasePoco))) { DC.UpdateProperty(i, "UpdateTime"); DC.UpdateProperty(i, "UpdateBy"); } } } } //需要删除的数据 foreach (var item in toremove) { //如果是PersistPoco,则把IsValid设为false,并不进行物理删除 if (ftype.IsSubclassOf(typeof(PersistPoco))) { (item as PersistPoco).IsValid = false; (item as PersistPoco).UpdateTime = DateTime.Now; (item as PersistPoco).UpdateBy = LoginUserInfo?.ITCode; dynamic i = item; DC.UpdateEntity(i); } else { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } } //需要添加的数据 foreach (var item in toadd) { if (item.GetType().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = item as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } DC.AddEntity(item); } DC.SaveChanges(); } }
public override async Task DoEditAsync(bool updateAllFields = false) { List <Guid> oldIDs = null; if (DpType == DpTypeEnum.User) { oldIDs = DC.Set <DataPrivilege>().Where(x => x.UserId == Entity.UserId && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } else { oldIDs = DC.Set <DataPrivilege>().Where(x => x.GroupId == Entity.GroupId && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } foreach (var oldid in oldIDs) { DataPrivilege dp = new DataPrivilege { ID = oldid }; DC.Set <DataPrivilege>().Attach(dp); DC.DeleteEntity(dp); } if (IsAll == true) { if (DpType == DpTypeEnum.User) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = null; dp.UserId = Entity.UserId; dp.TableName = this.Entity.TableName; dp.DomainId = this.Entity.DomainId; DC.Set <DataPrivilege>().Add(dp); } else { DataPrivilege dp = new DataPrivilege(); dp.RelateId = null; dp.GroupId = Entity.GroupId; dp.TableName = this.Entity.TableName; dp.DomainId = this.Entity.DomainId; DC.Set <DataPrivilege>().Add(dp); } } else { if (SelectedItemsID != null) { if (DpType == DpTypeEnum.User) { foreach (var id in SelectedItemsID) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = id; dp.UserId = Entity.UserId; dp.TableName = this.Entity.TableName; dp.DomainId = this.Entity.DomainId; DC.Set <DataPrivilege>().Add(dp); } } else { foreach (var id in SelectedItemsID) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = id; dp.GroupId = Entity.GroupId; dp.TableName = this.Entity.TableName; dp.DomainId = this.Entity.DomainId; DC.Set <DataPrivilege>().Add(dp); } } } } await DC.SaveChangesAsync(); if (DpType == DpTypeEnum.User) { await LoginUserInfo.RemoveUserCache(Entity.UserId.ToString()); } else { var userids = DC.Set <FrameworkUserGroup>().Where(x => x.GroupId == Entity.GroupId).Select(x => x.UserId.ToString()).ToArray(); await LoginUserInfo.RemoveUserCache(userids); } }
public void AddPrivilege(Guid menuid) { var oldIDs = DC.Set <FunctionPrivilege>().Where(x => x.MenuItemId == menuid).Select(x => x.ID).ToList(); foreach (var oldid in oldIDs) { try { FunctionPrivilege fp = new FunctionPrivilege { ID = oldid }; DC.Set <FunctionPrivilege>().Attach(fp); DC.DeleteEntity(fp); } catch { } } if (SelectedRolesIDs != null) { foreach (var id in SelectedRolesIDs) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = id; fp.UserId = null; fp.Allowed = true; DC.Set <FunctionPrivilege>().Add(fp); } } if (SelectedRolesID2 != null) { foreach (var id in SelectedRolesID2) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = id; fp.UserId = null; fp.Allowed = false; DC.Set <FunctionPrivilege>().Add(fp); } } if (SelectedUsersID != null) { foreach (var id in SelectedUsersID) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = null; fp.UserId = id; fp.Allowed = true; DC.Set <FunctionPrivilege>().Add(fp); } } if (SelectedUsersID2 != null) { foreach (var id in SelectedUsersID2) { FunctionPrivilege fp = new FunctionPrivilege(); fp.MenuItemId = menuid; fp.RoleId = null; fp.UserId = id; fp.Allowed = false; DC.Set <FunctionPrivilege>().Add(fp); } } DC.SaveChanges(); }
/// <summary> /// 批量删除,默认对Ids中包含的主键的数据进行删除。子类如果有特殊判断应重载本函数 /// </summary> /// <returns>true代表成功,false代表失败</returns> public virtual bool DoBatchDelete() { bool rv = true; //循环所有数据Id List <Guid> idsData = new List <Guid>(Ids); for (int i = 0; i < idsData.Count; i++) { string checkErro = null; //检查是否可以删除,如不能删除则直接跳过 if (CheckIfCanDelete(idsData[i], out checkErro) == false) { ErrorMessage.Add(idsData[i], checkErro); rv = false; break; } //进行删除 try { var ctor = typeof(TModel).GetConstructor(Type.EmptyTypes); if (typeof(TModel).IsSubclassOf(typeof(PersistPoco))) { var pp = DC.Set <TModel>().Find(idsData[i]); (pp as PersistPoco).IsValid = false; (pp as PersistPoco).UpdateTime = DateTime.Now; (pp as PersistPoco).UpdateBy = LoginUserInfo.ITCode; DC.UpdateProperty(pp, "IsValid"); DC.UpdateProperty(pp, "UpdateTime"); DC.UpdateProperty(pp, "UpdateBy"); } else { TModel m = ctor.Invoke(null) as TModel; m.ID = idsData[i]; DC.Set <TModel>().Attach(m); DC.DeleteEntity(m); } } catch (Exception e) { SetExceptionMessage(e, idsData[i]); rv = false; } } //进行数据库的删除操作 if (rv == true) { try { DC.SaveChanges(); } catch (Exception e) { SetExceptionMessage(e, null); rv = false; } } //如果失败,添加错误信息 if (rv == false) { if (ErrorMessage.Count > 0) { foreach (var id in idsData) { if (!ErrorMessage.ContainsKey(id)) { ErrorMessage.Add(id, "已回滚"); } } } ListVM?.DoSearch(); if (ListVM != null) { foreach (var item in ListVM?.GetEntityList()) { item.BatchError = ErrorMessage.Where(x => x.Key == item.ID).Select(x => x.Value).FirstOrDefault(); } } MSD.AddModelError("", "数据已被使用,无法删除"); } return(rv); }
/// <summary> /// 修改,进行默认的修改操作。子类如有自定义操作应重载本函数 /// </summary> public virtual void DoEdit(bool updateAllFields = false) { //自动设定修改日期和修改人 if (typeof(TModel).GetTypeInfo().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = Entity as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo?.ITCode; } } var pros = typeof(TModel).GetProperties(); #region 更新子表 foreach (var pro in pros) { //找到类型为List<xxx>的字段 if (pro.PropertyType.GenericTypeArguments.Count() > 0) { //获取xxx的类型 var ftype = pro.PropertyType.GenericTypeArguments.First(); //如果xxx继承自TopBasePoco if (ftype.IsSubclassOf(typeof(TopBasePoco))) { //界面传过来的子表数据 if (pro.GetValue(Entity) is IEnumerable <TopBasePoco> list && list.Count() > 0) { //获取外键字段名称 string fkname = DC.GetFKName <TModel>(pro.Name); PropertyInfo[] itemPros = ftype.GetProperties(); bool found = false; foreach (var newitem in list) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.ID); found = true; } } } } //如果没有找到相应的外建字段,则可能是多对多的关系,或者做了特殊的设定,这种情况框架无法支持,直接退出本次循环 if (found == false) { continue; } //循环页面传过来的子表数据,将关联到TopBasePoco的字段设为null,并且把外键字段的值设定为主表ID foreach (var newitem in list) { var subtype = newitem.GetType(); if (subtype.IsSubclassOf(typeof(BasePoco))) { BasePoco ent = newitem as BasePoco; if (ent.UpdateTime == null) { ent.UpdateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.UpdateBy)) { ent.UpdateBy = LoginUserInfo.ITCode; } } foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(newitem, null); } if (!string.IsNullOrEmpty(fkname)) { if (itempro.Name.ToLower() == fkname.ToLower()) { itempro.SetValue(newitem, Entity.ID); found = true; } } } } TModel _entity = null; //打开新的数据库联接,获取数据库中的主表和子表数据 using (var ndc = DC.CreateNew()) { _entity = ndc.Set <TModel>().Include(pro.Name).AsNoTracking().Where(x => x.ID == Entity.ID).FirstOrDefault(); } //比较子表原数据和新数据的区别 IEnumerable <TopBasePoco> toadd = null; IEnumerable <TopBasePoco> toremove = null; IEnumerable <TopBasePoco> data = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; Utils.CheckDifference(data, list, out toremove, out toadd); //设定子表应该更新的字段 List <string> setnames = new List <string>(); foreach (var field in FC.Keys) { if (field.StartsWith("Entity." + pro.Name + "[0].")) { string name = field.Replace("Entity." + pro.Name + "[0].", ""); if (name != "UpdateTime" && name != "UpdateBy") { setnames.Add(name); } } } //前台传过来的数据 foreach (var newitem in list) { //数据库中的数据 foreach (var item in data) { //需要更新的数据 if (newitem.ID == item.ID) { dynamic i = newitem; var newitemType = item.GetType(); foreach (var itempro in itemPros) { if (!itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco)) && setnames.Contains(itempro.Name)) { if (itempro.Name != "ID") { DC.UpdateProperty(i, itempro.Name); } } } DC.UpdateProperty(i, "UpdateTime"); DC.UpdateProperty(i, "UpdateBy"); } } } //需要删除的数据 foreach (var item in toremove) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } //需要添加的数据 foreach (var item in toadd) { if (item.GetType().IsSubclassOf(typeof(BasePoco))) { BasePoco ent = item as BasePoco; if (ent.CreateTime == null) { ent.CreateTime = DateTime.Now; } if (string.IsNullOrEmpty(ent.CreateBy)) { ent.CreateBy = LoginUserInfo?.ITCode; } } DC.AddEntity(item); } } else if (FC.Keys.Contains("Entity." + pro.Name + ".DONOTUSECLEAR")) { PropertyInfo[] itemPros = ftype.GetProperties(); var _entity = DC.Set <TModel>().Include(pro.Name).AsNoTracking().Where(x => x.ID == Entity.ID).FirstOrDefault(); IEnumerable <TopBasePoco> removeData = _entity.GetType().GetProperty(pro.Name).GetValue(_entity) as IEnumerable <TopBasePoco>; foreach (var item in removeData) { foreach (var itempro in itemPros) { if (itempro.PropertyType.IsSubclassOf(typeof(TopBasePoco))) { itempro.SetValue(item, null); } } dynamic i = item; DC.DeleteEntity(i); } } } } } #endregion if (updateAllFields == false) { foreach (var field in FC.Keys) { if (field.StartsWith("Entity.") && !field.Contains("[")) { string name = field.Replace("Entity.", ""); if (name != "UpdateTime" && name != "UpdateBy") { try { DC.UpdateProperty(Entity, name); } catch (Exception) { } } } } } else { DC.UpdateEntity(Entity); } DC.SaveChanges(); //删除不需要的附件 if (DeletedFileIds != null) { foreach (var item in DeletedFileIds) { FileAttachmentVM ofa = new FileAttachmentVM(); ofa.CopyContext(this); ofa.SetEntityById(item); ofa.DoDelete(); } } }
public override async Task DoAddAsync() { if (SelectedItemsID == null && IsAll == false) { return; } List <Guid> oldIDs = null; if (DpType == DpTypeEnum.User) { oldIDs = DC.Set <DataPrivilege>().Where(x => x.UserCode == Entity.UserCode && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } else { oldIDs = DC.Set <DataPrivilege>().Where(x => x.GroupCode == Entity.GroupCode && x.TableName == this.Entity.TableName).Select(x => x.ID).ToList(); } foreach (var oldid in oldIDs) { DataPrivilege dp = new DataPrivilege { ID = oldid }; DC.Set <DataPrivilege>().Attach(dp); DC.DeleteEntity(dp); } if (DpType == DpTypeEnum.User) { if (IsAll == true) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = null; dp.UserCode = Entity.UserCode; dp.TableName = this.Entity.TableName; DC.Set <DataPrivilege>().Add(dp); } else { foreach (var id in SelectedItemsID) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = id; dp.UserCode = Entity.UserCode; dp.TableName = this.Entity.TableName; DC.Set <DataPrivilege>().Add(dp); } } } else { if (IsAll == true) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = null; dp.GroupCode = Entity.GroupCode; dp.TableName = this.Entity.TableName; DC.Set <DataPrivilege>().Add(dp); } else { foreach (var id in SelectedItemsID) { DataPrivilege dp = new DataPrivilege(); dp.RelateId = id; dp.GroupCode = Entity.GroupCode; dp.TableName = this.Entity.TableName; DC.Set <DataPrivilege>().Add(dp); } } } await DC.SaveChangesAsync(); if (DpType == DpTypeEnum.User) { await Wtm.RemoveUserCache(Entity.UserCode); } else { var userids = DC.Set <FrameworkUserGroup>().Where(x => x.GroupCode == Entity.GroupCode).Select(x => x.UserCode).ToArray(); await Wtm.RemoveUserCache(userids); } }
public virtual void DoDelete() { DC.DeleteEntity(DC.GetSet <TModel>(), Entity); DC.Commit(); }