public OpResult SaveOrUpdate(Plans plan) { if (plan.Id.IsNullOrEmpty()) { plan.Id = Guid.NewGuid().ToString("N"); plan.CreateDT = DateTime.Now; plan.CreateUID = CurrentUser.UID; plan.Status = 375; PlanRepository.Add(plan, false); } else { var source = PlanRepository.GetQuery(o => o.Id == plan.Id).Include(o => o.Attachments).Include(o => o.Replys).FirstOrDefault(); plan.ToCopyProperty(source, new List <string>() { "CreateDT", "CreateUID", "Status" }); if (plan.Attachments != null) { source.Attachments.AddRange(plan.Attachments); } if (plan.Replys != null) { source.Replys.AddRange(plan.Replys); } } if (plan.Attachments != null) { string path = ""; string fullPath = FileHelper.SaveAttachPath(ref path); for (var i = plan.Attachments.Count - 1; i >= 0; i--) { var attach = plan.Attachments[i]; if (!attach.Bytes.Any()) { plan.Attachments.Remove(attach); continue; } attach.CreateDT = DateTime.Now; attach.CreateUID = CurrentUser.UID; attach.ItemId = plan.Id; attach.NewName = Guid.NewGuid().ToString("N") + "." + attach.ExtName; attach.Path = path; using (var fs = new FileStream(fullPath + attach.NewName, FileMode.OpenOrCreate)) { fs.Write(attach.Bytes, 0, attach.Bytes.Length); fs.Close(); } } } if (plan.Replys != null) { plan.Replys.Each(o => { o.CreateDT = DateTime.Now; o.Creater = CurrentUser.FullName; o.CreateUID = CurrentUser.UID; o.PlanId = plan.Id; }); } PlanRepository.SaveChanges(); return(OpResult.Success()); }