private OralConsultationItem UpdateOralConsultation(OralConsultationItem item)
        {
            OralConsultation entity = new OralConsultation
            {
                OralConsultationID = item.OralConsultationID,
                //ResidentID = (int)item.ResidentID,
                IssueID = (int)item.IssueID,
                OralConsultationDate    = item.OralConsultationDate,
                InvocationTypeID        = item.InvocationTypeID,
                TargetGroupID           = item.TargetGroupID,
                ProblemDescription      = item.ProblemDescription,
                ConsultationDescription = item.ConsultationDescription,
                UserID     = item.UserID,
                ChangeDate = DateTime.Now
            };

            db.OralConsultations.Attach(entity);
            db.Entry(entity).State = EntityState.Modified;

            this.UpdateConsultant(item, entity);
            this.UpdateOrganization(item, entity);
            this.UpdateRight(item, entity);
            this.UpdatePermission(item, entity);

            db.SaveChanges();

            item.OralConsultationID = entity.OralConsultationID;

            return(item);
        }
        private bool InsertConsultant(OralConsultationItem item, OralConsultation entity)
        {
            foreach (var itm in db.TmpOralConsultationConsultants.Where(p => p.GUID == item.GUID))
            {
                OralConsultationConsultant dbItem = new OralConsultationConsultant
                {
                    OralConsultation             = entity,
                    ConsultantID                 = itm.ConsultantID,
                    OralConsultationID           = itm.OralConsultationID,
                    OralConsultationConsultantID = itm.OralConsultationConsultantID
                };
                db.OralConsultationConsultants.Add(dbItem);
            }

            List <int> deletionTempItems = new List <int>();

            foreach (var itm3 in db.TmpOralConsultationConsultants.Where(p => p.GUID == item.GUID))
            {
                deletionTempItems.Add(itm3.ID);
            }
            foreach (int ID in deletionTempItems)
            {
                db.TmpOralConsultationConsultants.Remove(db.TmpOralConsultationConsultants.Find(ID));
            }
            return(true);
        }
        private OralConsultationItem InsertOralConsultation(OralConsultationItem item)
        {
            OralConsultation entity = new OralConsultation
            {
                OralConsultationID = item.OralConsultationID,
                //ResidentID = (int)item.ResidentID,
                IssueID = (int)item.IssueID,
                OralConsultationDate    = item.OralConsultationDate,
                InvocationTypeID        = item.InvocationTypeID,
                TargetGroupID           = item.TargetGroupID,
                ProblemDescription      = item.ProblemDescription,
                ConsultationDescription = item.ConsultationDescription,
                UserID     = item.UserID,
                ChangeDate = DateTime.Now,
                OwnerID    = item.UserID
            };

            db.OralConsultations.Add(entity);

            this.InsertConsultant(item, entity);
            this.InsertOrganization(item, entity);
            this.InsertRight(item, entity);
            this.InsertPermission(item, entity);

            db.SaveChanges();

            item.OralConsultationID = entity.OralConsultationID;

            return(item);
        }
        private bool UpdatePermission(OralConsultationItem item, OralConsultation entity)
        {
            List <int> existingItems = new List <int>();

            foreach (var itm in db.TmpOralConsultationPermissions.Where(p => p.GUID == item.GUID))
            {
                existingItems.Add(itm.OralConsultationPermissionID);
                OralConsultationPermission dbItem = new OralConsultationPermission
                {
                    OralConsultation             = entity,
                    UserID                       = itm.UserID,
                    IsChange                     = itm.IsChange,
                    IsRead                       = itm.IsRead,
                    OralConsultationID           = itm.OralConsultationID,
                    OralConsultationPermissionID = itm.OralConsultationPermissionID
                };

                if (itm.OralConsultationPermissionID == 0)
                {
                    db.OralConsultationPermissions.Add(dbItem);
                }
                else
                {
                    db.OralConsultationPermissions.Attach(dbItem);
                    db.Entry(dbItem).State = EntityState.Modified;
                }
            }

            List <int> deletedItems = new List <int>();

            foreach (var itm2 in db.OralConsultationPermissions.Where(p => p.OralConsultationID == item.OralConsultationID))
            {
                deletedItems.Add(itm2.OralConsultationPermissionID);
            }

            List <int> common = deletedItems.Except(existingItems).ToList();

            foreach (int deletionID in common)
            {
                db.OralConsultationPermissions.Remove(db.OralConsultationPermissions.Find(deletionID));
            }

            List <int> deletionTempItems = new List <int>();

            foreach (var itm3 in db.TmpOralConsultationPermissions.Where(p => p.GUID == item.GUID))
            {
                deletionTempItems.Add(itm3.ID);
            }
            foreach (int ID in deletionTempItems)
            {
                db.TmpOralConsultationPermissions.Remove(db.TmpOralConsultationPermissions.Find(ID));
            }
            return(true);
        }