// GET: Manage/Branch/Create
 public ActionResult Create(Guid? id)
 {
     var branchServices = ServiceLocator.Instance.GetService<IBranchServices>();
     var branchList = branchServices.GetFirstBranches();
     ViewBag.Branchs = new HtmlString(branchList.ToJson());
     var branchs = new UpdateBranch();
     if (id.HasValue)
     {
         branchs = branchServices.GetBranches(id.Value);
     }
     ViewBag.UpdateBranch = new HtmlString(branchs.ToJson());
     return PartialView();
 }
 public ActionResult Edit(UpdateBranch branch)
 {
     try
     {
         // TODO: Add update logic here
         var branchServices = ServiceLocator.Instance.GetService<IBranchServices>();
         if (branch.Title.Null())
         {
             var failure = branchServices.GetResult();
             return Json(failure);
         }
         branchServices.Update(branch);
         var result = branchServices.GetResult();
         return Json(result);
     }
     catch
     {
         return View();
     }
 }
 public UpdateBranch GetBranches(Guid id)
 {
     var branch = new Branch();
     if (id != Guid.Empty)
     {
         using (var context = new DefaultContext())
         {
             branch = context.Branches.AsNoTracking().FirstOrDefault(x => x.ID == id);
             if (branch.Null())
             {
                 return new UpdateBranch();
             }
         }
     }
     var updateBranch = new UpdateBranch();
     updateBranch.ID = branch.ID;
     updateBranch.Title = branch.Title;
     updateBranch.Enble = branch.Enble;
     updateBranch.Level = branch.Level;
     updateBranch.PreBranch = branch.PreBranch;
     updateBranch.Logo = branch.Logo;
     updateBranch.AreaName = branch.AreaName;
     updateBranch.ActionName = branch.ActionName;
     updateBranch.Description = branch.Description;
     updateBranch.SortID = branch.SortID;
     return updateBranch;
 }
 private void Edit(UpdateBranch updateBranch)
 {
     using (var context = new DefaultContext())
     {
         var editBranch = context.Branches.FirstOrDefault(x => x.ID == updateBranch.ID);
         if (editBranch.Null())
         {
             Message = "修改菜单不存在";
             return;
         }
         editBranch.Title = updateBranch.Title;
         editBranch.Level = updateBranch.Level;
         editBranch.PreBranch = updateBranch.PreBranch;
         editBranch.SortID = updateBranch.SortID;
         editBranch.Logo = updateBranch.Logo;
         editBranch.ActionName = updateBranch.ActionName;
         editBranch.AreaName = updateBranch.AreaName;
         editBranch.Description = updateBranch.Description;
         context.ModifiedModel(editBranch);
         context.SaveChanges();
     }
 }
 private void Add(UpdateBranch updateBranch)
 {
     var branch = new Branch();
     branch.ID = Guid.NewGuid();
     branch.Title = updateBranch.Title;
     branch.Enble = true;
     branch.Level = updateBranch.Level;
     branch.PreBranch = updateBranch.PreBranch;
     branch.SortID = updateBranch.SortID;
     branch.Logo = updateBranch.Logo;
     branch.ActionName = updateBranch.ActionName;
     branch.AreaName = updateBranch.AreaName;
     branch.Description = updateBranch.Description;
     using (var context = new DefaultContext())
     {
         branch.SortID = context.Branches.Max(x => x.SortID) + 1;
         context.Branches.Add(branch);
         context.SaveChanges();
     }
 }
 public void Update(UpdateBranch updateBranch)
 {
     Branch parentBranch = null;
     if (updateBranch.Level == 2 && updateBranch.PreBranch != Guid.Empty)
     {
         using (var context = new DefaultContext())
         {
             parentBranch = context.Branches.AsNoTracking().FirstOrDefault(x => x.ID == updateBranch.PreBranch);
         }
     }
     if (parentBranch.Null())
     {
         updateBranch.Level = 1;
         updateBranch.PreBranch = Guid.Empty;
     }
     else
     {
         updateBranch.AreaName = parentBranch.AreaName;
         updateBranch.Logo = string.Empty;
     }
     if (updateBranch.ID == Guid.Empty)
     {
         Add(updateBranch);
     }
     else
     {
         Edit(updateBranch);
     }
     Flag = true;
 }