public void Update(UpdateDepartment updateDepartment)
 {
     if (updateDepartment.ID == Guid.Empty)
     {
         Add(updateDepartment);
     }
     else
     {
         Edit(updateDepartment);
     }
 }
 public ActionResult Edit(UpdateDepartment updateDepartment)
 {
     try
     {
         // TODO: Add update logic here
         var departmentServices = ServiceLocator.Instance.GetService<IDepartmentServices>();
         if (updateDepartment.Name.Null())
         {
             var failure = departmentServices.GetResult();
             return Json(failure);
         }
         departmentServices.Update(updateDepartment);
         var result = departmentServices.GetResult();
         return Json(result);
     }
     catch
     {
         return View();
     }
 }
        private void Edit(UpdateDepartment updateDepartment)
        {
            using (var accountContext = new DefaultContext())
            {
                var department = accountContext.Departments.SingleOrDefault(x => x.ID == updateDepartment.ID);
                if (department.Null())
                {
                    return;
                }
                department = updateDepartment.UpdateShowDepartment(department);
                accountContext.ModifiedModel(department);
                var workCard =
                    accountContext.WorkCards.FirstOrDefault(x => x.Owner && x.DepartmentId == updateDepartment.ID);

                if (!workCard.Null())
                {
                    if (workCard.AccountId != updateDepartment.ID)
                    {
                        workCard.Owner = false;
                        accountContext.ModifiedModel(workCard);
                    }
                }
                var newWorkCard =
                        accountContext.WorkCards.FirstOrDefault(x => x.AccountId == updateDepartment.OwnerId && x.DepartmentId == updateDepartment.ID);
                if (!newWorkCard.Null())
                {
                    newWorkCard.Owner = true;
                    accountContext.ModifiedModel(newWorkCard);
                }



                accountContext.SaveChanges();
                Flag = true;
            }
        }
 private void Add(UpdateDepartment updateDepartment)
 {
     using (var accountContext = new DefaultContext())
     {
         var department = accountContext.Departments.SingleOrDefault(x => x.Name == updateDepartment.Name);
         if (!department.Null())
         {
             Message = "创建部门已经存在";
             return;
         }
         department = updateDepartment.CreateShowDepartment();
         accountContext.Departments.Add(department);
         accountContext.SaveChanges();
         Flag = true;
     }
 }