public IHttpActionResult MultDelete(int[] ids) { var query = Dbctx.GgcmsArticles.Where(x => ids.Contains(x.Id)); foreach (GgcmsArticles item in query.ToList()) { //删除关联附件 var attalist = Dbctx.GgcmsAttachments.Where(x => x.Articles_Id == item.Id); Dbctx.GgcmsAttachments.RemoveRange(attalist); //删除关联模型数据 var category = dbHelper.Categories(Math.Abs(item.Category_Id)); if (category != null) { if (category.ExtModelId > 0) { ExtendModule.Delete(item.Id, category.ExtModelId); } updateArticleNumber(item.Category_Id, -1); } //删除关联分页 Dbctx.GgcmsArticlePages.RemoveRange(Dbctx.GgcmsArticlePages.Where(x => x.Article_Id == item.Id)); } Dbctx.GgcmsArticles.RemoveRange(query); int c = Dbctx.SaveChanges(); ClearCache(); CacheHelper.RemoveAllCache(CacheTypeNames.Categorys); return(Ok(c)); }
// DELETE: api/GgcmsCategories/5 public IHttpActionResult Delete(int id) { GgcmsArticles info = Dbctx.GgcmsArticles.Find(id); if (info == null) { return(BadRequest("信息不存在")); } //删除关联模型数据 var category = dbHelper.Categories(Math.Abs(info.Category_Id)); if (category != null) { if (category.ExtModelId > 0) { ExtendModule.Delete(info.Id, category.ExtModelId); } updateArticleNumber(info.Category_Id, -1); } Dbctx.GgcmsArticles.Remove(info); //删除关联附件 Dbctx.GgcmsAttachments.RemoveRange(Dbctx.GgcmsAttachments.Where(x => x.Articles_Id == info.Id)); //删除关联分页 Dbctx.GgcmsArticlePages.RemoveRange(Dbctx.GgcmsArticlePages.Where(x => x.Article_Id == info.Id)); Dbctx.SaveChanges(); ClearCache(); CacheHelper.RemoveAllCache(CacheTypeNames.Categorys); return(Ok(info)); }
// PUT: api/GgcmsCategories/5 public IHttpActionResult Edit(GgcmsArticles info) { if (Dbctx.GgcmsArticles.Where(x => x.Id == info.Id).Count() == 0) { return(BadRequest("信息不存在")); } //Dbctx.GgcmsArticles.Attach(info); //Dbctx.Entry(info).Property("goods_name").IsModified = true; var ent = Dbctx.Entry(info); ent.State = EntityState.Modified; info.pagesCount = info.pages.Where(x => x.state != EntityState.Deleted).Count() + 1; UpFileClass.FileSave(info, info.files.FindAll(x => x.fileType != 3)); var old = Dbctx.GgcmsArticles.Find(info.Id); if (old.Category_Id != info.Category_Id) { updateArticleNumber(info.Category_Id, 1); updateArticleNumber(old.Category_Id, -1); CacheHelper.RemoveAllCache(CacheTypeNames.Categorys); } var list = Dbctx.GgcmsAttachments.Where(x => x.Articles_Id == info.Id).ToList(); //附件 foreach (GgcmsAttachments attach in list) { var item = info.attachments.Find(x => x.Id == attach.Id); if (item == null) { Dbctx.GgcmsAttachments.Remove(attach); } else { attach.AttaUrl = item.AttaUrl; attach.AttaTitle = item.AttaTitle; attach.Describe = item.Describe; attach.CreateTime = DateTime.Now; attach.RealName = item.RealName; } } foreach (GgcmsAttachments attach in info.attachments) { if (attach.Id == 0) { attach.Articles_Id = info.Id; attach.CreateTime = DateTime.Now; Dbctx.GgcmsAttachments.Add(attach); } } if (info.ModuleInfo != null) { info.ExtModelId = info.ModuleInfo.Id; if (old.ExtModelId > 0 && info.ExtModelId != old.ExtModelId) { ExtendModule.Delete(info.Id, old.ExtModelId); } foreach (var file in info.files.FindAll(x => x.fileType == 3)) { foreach (var item in info.ModuleInfo.Columns) { if (item.ColName == file.propertyName) { item.Value = UpFileClass.FileSave(file.filePath.ToString(), item.Value.ToString(), (int)file.fileType); } } } ExtendModule.SaveData(info.Id, info.ModuleInfo); } else if (old.ExtModelId > 0) { ExtendModule.Delete(info.Id, old.ExtModelId); } //分页保存 if (info.pages.Count > 0) { foreach (var page in info.pages) { UpFileClass.FileSave(page, page.files); if (page.state == EntityState.Added) { page.Article_Id = info.Id; Dbctx.GgcmsArticlePages.Add(page); } else if (page.state == EntityState.Modified) { var pageEnt = Dbctx.Entry(page); pageEnt.State = EntityState.Modified; } else if (page.state == EntityState.Deleted) { var p = Dbctx.GgcmsArticlePages.Find(page.Id); if (p != null) { Dbctx.GgcmsArticlePages.Remove(p); } } //只更新分页信息 else if (page.state == EntityState.Unchanged) { Dbctx.GgcmsArticlePages.Attach(page); Dbctx.Entry(info).Property("OrderId").IsModified = true; } } } Dbctx.SaveChanges(); ClearCache(); return(Ok(info)); }