/// <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> /// 修改,进行默认的修改操作。子类如有自定义操作应重载本函数 /// </summary> /// <param name="updateAllFields">为true时,框架会更新当前Entity的全部值,为false时,框架会检查Request.Form里的key,只更新表单提交的字段</param> public virtual void DoEdit(bool updateAllFields = false) { DoEditPrepare(updateAllFields); DC.SaveChanges(); //删除不需要的附件 if (DeletedFileIds != null && DeletedFileIds.Count > 0) { var fp = KnifeVirgo.HttpContext.RequestServices.GetRequiredService <VirgoFileProvider>(); foreach (var item in DeletedFileIds) { fp.DeleteFile(item.ToString(), DC.ReCreate()); } } }
public virtual async Task DoAddAsync() { DoAddPrepare(); //删除不需要的附件 if (DeletedFileIds != null && DeletedFileIds.Count > 0) { var fp = KnifeVirgo.HttpContext.RequestServices.GetRequiredService <VirgoFileProvider>(); foreach (var item in DeletedFileIds) { fp.DeleteFile(item.ToString(), DC.ReCreate()); } } await DC.SaveChangesAsync(); }
/// <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>; foreach (var sub in subs) { fileids.Add(sub.FileId); } f.SetValue(Entity, null); } using (var newdc = DC.ReCreate()) { TModel m = new TModel(); m.SetPropertyValue("ID", Entity.GetID()); newdc.Set <TModel>().Attach(m); newdc.DeleteEntity(m); newdc.SaveChanges(); } foreach (var item in fileids) { FileAttachmentVM ofa = new FileAttachmentVM(); ofa.CopyContext(this); ofa.SetEntityById(item); ofa.DoDelete(); } } catch (Exception e) { MSD.AddModelError("", "数据使用中,无法删除"); } }
public ActionResult Delete(Guid id, FormCollection noUser) { var vm = CreateVM <FrameworkCompanyVM>(id); vm.DoDelete(); if (!ModelState.IsValid) { DC = DC.ReCreate(); vm = CreateVM <FrameworkCompanyVM>(id); return(PartialView(vm)); } else { return(FFResult().CloseDialog().RefreshGrid()); } }
public override void DoEdit(bool updateAllFields = false) { if (Entity.IsInside == false) { if (Entity.Url != null && Entity.Url != "") { if (string.IsNullOrEmpty(Entity.Domain) == true) { if (Entity.Url.ToLower().StartsWith("http://") == false && Entity.Url.ToLower().StartsWith("https://") == false && Entity.Url.StartsWith("@") == false) { Entity.Url = "http://" + Entity.Url; } } else { if (Entity.Url.StartsWith("/") == false) { Entity.Url = "/" + Entity.Url; } } } } else { if (string.IsNullOrEmpty(SelectedModule) == false && Entity.FolderOnly == false) { var modules = Wtm.GlobaInfo.AllModule; var ndc = DC.ReCreate(); var actionsInDB = DC.Set <FrameworkMenu>().AsNoTracking().Where(x => x.ParentId == Entity.ID).ToList(); var mo = modules.Where(x => x.FullName == this.SelectedModule && x.IsApi == true).FirstOrDefault(); Entity.ModuleName = mo.ModuleName; Entity.ClassName = mo.FullName; Entity.MethodName = null; var otherActions = mo.Actions; int order = 1; Entity.Children = new List <FrameworkMenu>(); foreach (var action in otherActions) { if (SelectedActionIDs != null && SelectedActionIDs.Contains(action.MethodName)) { Guid aid = action.ID; var adb = actionsInDB.Where(x => x.Url.ToLower() == action.Url.ToLower()).FirstOrDefault(); if (adb != null) { aid = adb.ID; } FrameworkMenu menu = new FrameworkMenu(); menu.FolderOnly = false; menu.IsPublic = false; menu.Parent = Entity; menu.ShowOnMenu = false; menu.DisplayOrder = order++; menu.Privileges = new List <FunctionPrivilege>(); menu.IsInside = true; menu.Domain = Entity.Domain; menu.PageName = action.ActionDes?.Description ?? action.ActionName; menu.ModuleName = action.Module.ModuleName; menu.ActionName = action.ActionDes?.Description ?? action.ActionName; menu.Url = action.Url; menu.ClassName = action.Module.FullName; menu.MethodName = action.MethodName; menu.ID = aid; Entity.Children.Add(menu); } } } else { Entity.Children = null; Entity.Url = null; } } if (FC.ContainsKey("Entity.Children") == false) { FC.Add("Entity.Children", 0); } base.DoEdit(updateAllFields); List <Guid> guids = new List <Guid>(); guids.Add(Entity.ID); if (Entity.Children != null) { guids.AddRange(Entity.Children?.Select(x => x.ID).ToList()); } AddPrivilege(guids); }
public override void DoEdit(bool updateAllFields = false) { if (Entity.IsInside == false) { if (Entity.Url != null && Entity.Url != "") { if (Entity.DomainId == null) { if (Entity.Url.ToLower().StartsWith("http://") == false && Entity.Url.ToLower().StartsWith("https://") == false) { Entity.Url = "http://" + Entity.Url; } } else { if (Entity.Url.StartsWith("/") == false) { Entity.Url = "/" + Entity.Url; } } } } else { if (string.IsNullOrEmpty(SelectedModule) == true && Entity.FolderOnly == false) { MSD.AddModelError("SelectedModule", "请选择一个模块"); return; } if (string.IsNullOrEmpty(SelectedModule) == false && Entity.FolderOnly == false) { var modules = GlobalServices.GetRequiredService <GlobalData>().AllModule; List <FrameworkAction> otherActions = null; var mainAction = modules.Where(x => x.FullName == this.SelectedModule).SelectMany(x => x.Actions).Where(x => x.MethodName == "Index").SingleOrDefault(); if (mainAction == null && Entity.ShowOnMenu == true) { MSD.AddModelError("Entity.ModuleId", "模块中没有找到Index页面"); return; } if (mainAction == null && Entity.ShowOnMenu == false) { var model = modules.Where(x => x.FullName == this.SelectedModule).FirstOrDefault(); mainAction = new FrameworkAction(); mainAction.Module = model; mainAction.MethodName = "Index"; } var ndc = DC.ReCreate(); var oldIDs = ndc.Set <FrameworkMenu>().Where(x => x.ParentId == Entity.ID).Select(x => x.ID).ToList(); foreach (var oldid in oldIDs) { try { FrameworkMenu fp = new FrameworkMenu { ID = oldid }; ndc.Set <FrameworkMenu>().Attach(fp); ndc.DeleteEntity(fp); } catch { } } ndc.SaveChanges(); Entity.Url = mainAction.Url; Entity.ModuleName = mainAction.Module.ModuleName; Entity.ClassName = mainAction.Module.ClassName; Entity.MethodName = "Index"; otherActions = modules.Where(x => x.FullName == this.SelectedModule).SelectMany(x => x.Actions).Where(x => x.MethodName != "Index").ToList(); int order = 1; foreach (var action in otherActions) { if (SelectedActionIDs != null && SelectedActionIDs.Contains(action.Url)) { FrameworkMenu menu = new FrameworkMenu(); menu.FolderOnly = false; menu.IsPublic = false; menu.Parent = Entity; menu.ShowOnMenu = false; menu.DisplayOrder = order++; menu.Privileges = new List <FunctionPrivilege>(); menu.CreateBy = LoginUserInfo.ITCode; menu.CreateTime = DateTime.Now; menu.IsInside = true; menu.DomainId = Entity.DomainId; menu.PageName = action.ActionName; menu.ModuleName = action.Module.ModuleName; menu.ActionName = action.ActionName; menu.Url = action.Url; Entity.Children.Add(menu); } } } else { Entity.Children = null; Entity.Url = null; } } base.DoEdit(); List <Guid> guids = new List <Guid>(); guids.Add(Entity.ID); if (Entity.Children != null) { guids.AddRange(Entity.Children?.Select(x => x.ID).ToList()); } AddPrivilege(guids); }
public override void DoEdit(bool updateAllFields = false) { List <Guid> guids = new List <Guid>(); if (Entity.IsInside == false) { if (Entity.Url != null && Entity.Url != "") { if (Entity.DomainId == null) { if (Entity.Url.ToLower().StartsWith("http://") == false && Entity.Url.ToLower().StartsWith("https://") == false && Entity.Url.StartsWith("@") == false) { Entity.Url = "http://" + Entity.Url; } } else { if (Entity.Url.StartsWith("/") == false) { Entity.Url = "/" + Entity.Url; } } } } else { if (string.IsNullOrEmpty(SelectedModule) == true && Entity.FolderOnly == false) { MSD.AddModelError("SelectedModule", Program._localizer["SelectModule"]); return; } if (string.IsNullOrEmpty(SelectedModule) == false && Entity.FolderOnly == false) { var modules = GlobalServices.GetRequiredService <GlobalData>().AllModule; var ndc = DC.ReCreate(); var actionsInDB = DC.Set <FrameworkMenu>().AsNoTracking().Where(x => x.ParentId == Entity.ID).ToList(); var mo = modules.Where(x => x.FullName == this.SelectedModule && x.IsApi == true).FirstOrDefault(); Entity.ModuleName = mo.ModuleName; Entity.ClassName = mo.FullName; Entity.MethodName = null; var otherActions = mo.Actions; int order = 1; Entity.Children = new List <FrameworkMenu>(); foreach (var action in otherActions) { if (SelectedActionIDs != null && SelectedActionIDs.Contains(action.MethodName)) { Guid aid = action.ID; var adb = actionsInDB.Where(x => x.Url.ToLower() == action.Url.ToLower()).FirstOrDefault(); if (adb != null) { aid = adb.ID; } else { guids.Add(aid); } FrameworkMenu menu = new FrameworkMenu(); menu.FolderOnly = false; menu.IsPublic = false; menu.Parent = Entity; menu.ShowOnMenu = false; menu.DisplayOrder = order++; menu.Privileges = new List <FunctionPrivilege>(); menu.CreateBy = LoginUserInfo.ITCode; menu.CreateTime = DateTime.Now; menu.IsInside = true; menu.DomainId = Entity.DomainId; menu.PageName = action.ActionDes?.Description ?? action.ActionName; menu.ModuleName = action.Module.ModuleName; menu.ActionName = action.ActionDes?.Description ?? action.ActionName; menu.Url = action.Url; menu.ClassName = action.Module.FullName; menu.MethodName = action.MethodName; menu.ID = aid; Entity.Children.Add(menu); } } } else { Entity.Children = null; Entity.Url = null; } } if (FC.ContainsKey("Entity.Children") == false) { FC.Add("Entity.Children", 0); } base.DoEdit(updateAllFields); AddPrivilege(guids); }
/// <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 void DoEdit(bool updateAllFields = false) { if (Entity.IsInside == false) { Entity.ActionId = null; Entity.ModuleId = null; if (Entity.Url != null && Entity.Url != "") { if (Entity.DomainId == null) { if (Entity.Url.ToLower().StartsWith("http://") == false) { Entity.Url = "http://" + Entity.Url; } } else { if (Entity.Url.StartsWith("/") == false) { Entity.Url = "/" + Entity.Url; } } } } else { if (Entity.ModuleId != null && Entity.FolderOnly == false) { List<FrameworkAction> otherActions = null; var mainAction = DC.Set<FrameworkAction>().Include(x => x.Module.Area).Where(x => x.ModuleId == this.Entity.ModuleId && x.MethodName == "Index").SingleOrDefault(); if (mainAction == null) { MSD.AddModelError("Entity.ModuleId", "模块中没有找到Index页面"); return; } var ndc = DC.ReCreate(); var oldIDs = ndc.Set<FrameworkMenu>().Where(x => x.ParentId == Entity.ID).Select(x => x.ID).ToList(); foreach (var oldid in oldIDs) { try { FrameworkMenu fp = new FrameworkMenu { ID = oldid }; ndc.Set<FrameworkMenu>().Attach(fp); ndc.DeleteEntity(fp); } catch { } } ndc.SaveChanges(); Entity.ActionId = mainAction.ID; Entity.Url = "/" + mainAction.Module.ClassName + "/" + mainAction.MethodName; if (mainAction.Module.Area != null) { Entity.Url = "/" + mainAction.Module.Area.Prefix + Entity.Url; } otherActions = DC.Set<FrameworkAction>().Include(x => x.Module.Area).Where(x => x.ModuleId == this.Entity.ModuleId && x.ID != Entity.ActionId).ToList(); int order = 1; foreach (var action in otherActions) { if (SelectedActionIDs.Contains(action.ID)) { FrameworkMenu menu = new FrameworkMenu(); menu.ActionId = action.ID; menu.ModuleId = action.ModuleId; menu.FolderOnly = false; menu.IsPublic = false; menu.Parent = Entity; menu.ShowOnMenu = false; menu.DisplayOrder = order++; menu.Privileges = new List<FunctionPrivilege>(); menu.CreateBy = LoginUserInfo.ITCode; menu.CreateTime = DateTime.Now; menu.IsInside = true; menu.DomainId = Entity.DomainId; menu.PageName = action.ActionName; menu.ModuleName = action.Module.ModuleName; menu.ActionName = action.ActionName; menu.Url = "/" + action.Module.ClassName + "/" + action.MethodName; if (action.Module.Area != null) { menu.Url = "/" + action.Module.Area.Prefix + menu.Url; } Entity.Children.Add(menu); } } } else { Entity.Children = null; Entity.ModuleId = null; Entity.Url = null; } } base.DoEdit(); List<Guid> guids = new List<Guid>(); guids.Add(Entity.ID); if (Entity.Children != null) { guids.AddRange(Entity.Children?.Select(x => x.ID).ToList()); } AddPrivilege(guids); }