public ActionResult Create(DepartmentModel department)
        {
            if (ModelState.IsValid)
            {
                db.Departments.Add(department);
                db.SaveChanges();

                if (!savePath(department))//保存path字段,路径如 0,1,2
                {
                    return View(department);
                }

                return RedirectToAction("Index");
            }

            return View(department);
        }
        private List<DepartmentModel> GetAllChildrenList(DepartmentModel departmentObj, ref List<DepartmentModel> SortList, int depth)
        {
            string s1 = "|__ ";//分隔符

            var list = db.Departments.Where(p => p.ParentId == departmentObj.DepartmentId).OrderBy(o => o.DepartmentId).OrderByDescending(o => o.SortId);

            if (list.Count() == 0)
                return SortList;

            ++depth;
            string s2 = "";
            for (int j = 1; j < depth; j++)
            {
                s2 += " ";//分隔符 全角空格
            }

            foreach (var child in list)
            {
                if (depth > 1)
                    child.name = s2 + s1 + child.name;

                SortList.Add(child);//添加下级的ParentId

                GetAllChildrenList(child, ref SortList, depth);
            }

            return SortList;
        }
        //根据id及parentid保存path,便于数据的检索
        private bool savePath(DepartmentModel department)
        {
            try
            {
                if (department.ParentId == 0)
                {
                    department.Path = "0," + department.DepartmentId + ",";
                }
                else
                {
                    DepartmentModel parentData = db.Departments.Single(d => d.DepartmentId == department.ParentId);
                    department.Path = parentData.Path  + department.DepartmentId + ",";
                }

                db.Entry(department).State = EntityState.Modified;
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return false;
            }

            return true;
        }
        //获取parentId字段的下拉选择集合
        private List<SelectListItem> getParentSelectList(DepartmentModel menu)
        {
            int depth = 1;

            var AllList = new List<SelectListItem>();
            AllList.Add(new SelectListItem { Text = "/", Value = "0" });//添加根parentId

            if (menu == null)
            {
                menu = new DepartmentModel { ParentId = 0 };
            }

            int selectedParentId = menu.ParentId;//传入的记录的上级菜单id,用于设定selectList的默认选中项

            GetChildrenList(menu, AllList, depth, selectedParentId);

            return AllList;
        }
        private List<DepartmentModel> GetChildrenList(DepartmentModel dep, List<SelectListItem> AllList, int depth, int selectedParentId)
        {
            var list = db.Departments.Where(o => o.ParentId == dep.DepartmentId).OrderBy(o => o.DepartmentId).OrderByDescending(o => o.SortId);

            if (depth == 1)//如果是第一层,那应该取parentId为0的
                list = db.Departments.Where(p => p.ParentId == 0).OrderBy(o => o.DepartmentId).OrderByDescending(o => o.SortId);

            if (list.Count() == 0)
                return null;

            ++depth;
            string s2 = "";
            for (int j = 1; j < depth; j++)
            {
                s2 += " ";//分隔符
            }

            foreach (var child in list)
            {
                if (selectedParentId == child.DepartmentId)//如果此菜单是传入菜单的父级
                    AllList.Add(new SelectListItem { Text = s2  + s1 + child.name, Value = child.DepartmentId.ToString(), Selected = true });//添加下级的ParentId
                else
                    AllList.Add(new SelectListItem { Text = s2  + s1 + child.name, Value = child.DepartmentId.ToString() });//添加下级的ParentId

                GetChildrenList(child, AllList, depth, selectedParentId);
            }

            return list.ToList();
        }
        private List<DepartmentModel> GetAllChildrenList(DepartmentModel department, ref List<DepartmentModel> SortList, int depth)
        {
            int userId = int.Parse(Request.Cookies["User"]["UserId"]);

            if (department == null
                && userId == 1)//第一次调用时取第一级,所以parentId = 0,必须是系统管理员才能看到所有部门信息
            {
                department = new DepartmentModel { ParentId = 0 };
            }

            var list = db.Departments.Where(p => p.ParentId == department.DepartmentId).OrderBy(o => o.DepartmentId).OrderByDescending(o => o.SortId);

            if (depth == 1)
            {

                var users = db.Users.Where(u => u.UserId == userId).ToList();
                int id = users[0].DepartmentId;

                if (userId == 1)//系统管理员
                    list = db.Departments.Where(p => p.ParentId == department.DepartmentId).OrderBy(o => o.DepartmentId).OrderByDescending(o => o.SortId);
                else
                    list = db.Departments.Where(p => p.DepartmentId == id).OrderBy(o => o.DepartmentId).OrderByDescending(o => o.SortId);
            }

            if (list.Count() == 0)
                return SortList;

            ++depth;
            string s2 = "";
            for (int j = 2; j < depth; j++)
            {
                s2 += " ";//分隔符 全角空格
            }

            foreach (var child in list)
            {
                if (depth != 2)
                    child.name = s2 + s1 + child.name;

                SortList.Add(child);//添加下级的ParentId

                GetAllChildrenList(child, ref SortList, depth);
            }

            return SortList;
        }
        public ActionResult Edit(DepartmentModel department)
        {
            if (ModelState.IsValid)
            {
                db.Entry(department).State = EntityState.Modified;
                db.SaveChanges();

                if (!savePath(department))//保存path字段,路径如 0,1,2
                {
                    return View(department);
                }

                return RedirectToAction("Index");
            }
            return View(department);
        }