public ResponseInfoModel EditDetail([FromBody] UpdateWildlifeContentInput input) { ResponseInfoModel json = new ResponseInfoModel() { Success = 1, Result = new object() }; try { CheckModelState(); if (!_wildlifeContentService.Edit(input)) { json.Success = 0; json.Result = LocalizationConst.UpdateFail; } else { _logService.Insert(new Log() { ActionContent = LocalizationConst.Update, SourceType = _moduleName, SourceID = input.ID, LogTime = DateTime.Now, LogUserID = input.EditUser, LogIPAddress = IPHelper.GetIPAddress, }); } } catch (Exception e) { DisposeUserFriendlyException(e, ref json, "api/wildlifemanager/editdetail", LocalizationConst.UpdateFail); } return(json); }
public bool Edit(UpdateWildlifeContentInput input) { var wildlifeContent = db.WildlifeContents.Find(input.ID); if (wildlifeContent == null) { throw new UserFriendlyException(LocalizationConst.NoExist); } wildlifeContent = input.MapTo(wildlifeContent); wildlifeContent.EditIP = IPHelper.GetIPAddress; wildlifeContent.EditTime = DateTime.Now; db.Entry <WildlifeContent>(wildlifeContent).State = EntityState.Modified; var attachs = db.ArticleAttaches.Where(a => a.ArticleGuid == wildlifeContent.FileID && a.ModuleType == (int)AttachTypesEnum.动植物管理详细附件); //为空全删除 if (input.Attachs == null || !input.Attachs.Any()) { if (attachs.Any()) { db.ArticleAttaches.RemoveRange(attachs); } } else { List <int> selectIds = input.Attachs.Select(a => a.ID).ToList(); List <int> nowIDs = attachs.Select(a => a.ID).ToList(); List <int> deleteIDs = nowIDs.Except(selectIds).ToList(); if (deleteIDs.Any()) { var deleteList = db.ArticleAttaches.Where(a => deleteIDs.Contains(a.ID)); if (deleteList.Any()) { db.ArticleAttaches.RemoveRange(deleteList); } } var aticlemax = db.ArticleAttaches.Where( a => a.ArticleGuid == wildlifeContent.FileID && a.ModuleType == (int)AttachTypesEnum.动植物管理详细附件) .Select(a => a.AttachIndex); int index = aticlemax.Any() ? aticlemax.Max() + 1 : 1; foreach (var attach in input.Attachs.Where(a => a.ID == 0)) { db.ArticleAttaches.Add(new ArticleAttach() { HashValue = attach.HashValue, ArticleGuid = wildlifeContent.FileID, AttachName = attach.AttachName, AttachNewName = attach.AttachNewName, AttachUrl = attach.AttachUrl, AttachFormat = attach.AttachFormat, AttachIndex = index++, AttachBytes = attach.AttachBytes, AttachType = attach.AttachType, ModuleType = (int)AttachTypesEnum.动植物管理详细附件, CreateTime = DateTime.Now, CreateUser = input.EditUser, CreateIP = IPHelper.GetIPAddress }); } } return(db.SaveChanges() > 0); }