/// <summary>
        /// 递归构建层次结构menu树结构
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="allButtons"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private EasyUiTreeResult BuildTreeByLoop(SysMenu menu, IEnumerable<SysButton> allButtons, EasyUiTreeResult result)
        {
            if (menu.Children == null)
            {
                return GetButtonsByMenu(allButtons, menu);
            }

            List<EasyUiTreeResult> ts = new List<EasyUiTreeResult>();
            foreach (var m in menu.Children)
            {
                EasyUiTreeResult t = new EasyUiTreeResult()
                    {
                        id = m.SysId,
                        text = m.MenuName,
                        link = m.MenuLink,
                        recordStatus = m.RecordStatus,
                        @checked = m.isCheck,
                        visible = m.IsVisible != null && ((PrivilegeOperation)(m.IsVisible.Value)) == PrivilegeOperation.Enable ? "启用" : "禁用"

                    };
                ts.Add(BuildTreeByLoop(m, allButtons, t));
            }
            result.children = ts.ToArray();

            return result;
        }
        private IEnumerable<EasyUiTreeResult> BuildAllTreeButton(IEnumerable<SysButton> lstSource)
        {
            var lstResult = new List<EasyUiTreeResult>();
            if (lstSource == null)
            {
                return lstResult;
            }

            foreach (var s in lstSource)
            {
                var model = new EasyUiTreeResult()
                {
                    id = s.SysId,
                    link = s.BtnFunction,
                    order = s.BtnOrder,
                    text = s.BtnName,
                    iconCls = s.BtnIcon,
                    visible = s.IsVisible == 1 ? "启用" : "禁用"
                };
                lstResult.Add(model);
            }
            return lstResult;
        }
        private IEnumerable<EasyUiTreeResult> BuildAllTreeMenu(IEnumerable<SysMenu> lstSource)
        {
            var lstResult = new List<EasyUiTreeResult>();

            foreach (var s in lstSource)
            {
                var model = new EasyUiTreeResult()
                                {
                                    id = s.SysId,
                                    link = s.MenuLink,
                                    order = s.MenuOrder,
                                    text = s.MenuName,
                                    iconCls = s.MenuIcon,
                                    buttons = BuildAllTreeButton(s.Buttons).ToArray(),
                                    visible = s.IsVisible != null && ((PrivilegeOperation)(s.IsVisible.Value)) == PrivilegeOperation.Enable ? "启用" : "禁用"
                                };
                if (s.Children != null && s.Children.Any())
                {
                    model.children = this.BuildAllTreeMenu(s.Children).ToArray();
                }
                lstResult.Add(model);
            }

            return lstResult;
        }
        /// <summary>
        /// 根据用户构建角色列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<EasyUiTreeResult> GetUserRoles(string userId)
        {
            IEnumerable<SysRole> allRoles = roleService.RoleRepository.GetList();
            IEnumerable<SysRole> userRoles = userService.GetRoles(userId);

            List<EasyUiTreeResult> treeResults = new List<EasyUiTreeResult>();
            foreach (SysRole userRole in userRoles)
            {
                EasyUiTreeResult treeResult = new EasyUiTreeResult()
                {
                    id = userRole.SysId,
                    text = userRole.RoleName,
                    recordStatus = userRole.RoleDesc,
                    @checked = true
                };
                allRoles = allRoles.Where(x => x.SysId != userRole.SysId);
                treeResults.Add(treeResult);
            }
            foreach (SysRole allRole in allRoles)
            {
                EasyUiTreeResult treeResult = new EasyUiTreeResult()
                {
                    id = allRole.SysId,
                    text = allRole.RoleName,
                    recordStatus = allRole.RoleDesc,
                    @checked = false
                };
                treeResults.Add(treeResult);
            }

            return treeResults.OrderBy(x => x.id).ToList();
        }
        /// <summary>
        /// 根据角色构建用户列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<EasyUiTreeResult> GetRoleUser(string roleId)
        {
            IEnumerable<SysUser> allUsers = RepositoryFactory.UserRepository.GetList();
            IEnumerable<SysUser> roleUsers = roleService.GetUsers(roleId);

            List<EasyUiTreeResult> treeResults = new List<EasyUiTreeResult>();

            foreach (SysUser roleUser in roleUsers)
            {
                EasyUiTreeResult treeResult = new EasyUiTreeResult()
                {
                    id = roleUser.SysId,
                    text = roleUser.UserName,
                    recordStatus = roleUser.RecordStatus,
                    @checked = true
                };
                allUsers = allUsers.Where(x => x.SysId != roleUser.SysId);
                treeResults.Add(treeResult);
            }
            foreach (SysUser allUser in allUsers)
            {
                EasyUiTreeResult treeResult = new EasyUiTreeResult()
                {
                    id = allUser.SysId,
                    text = allUser.UserName,
                    recordStatus = allUser.RecordStatus,
                    @checked = false
                };
                treeResults.Add(treeResult);
            }

            return treeResults;
        }
        public JsonResult GetIcos()
        {
            List<EasyUiTreeResult> results = new List<EasyUiTreeResult>();
            string filePath = Server.MapPath(@".\..\Content\easyui\themes\icon.css");
            StreamReader objReader = new StreamReader(filePath);
            while (!objReader.EndOfStream)
            {
                string str = objReader.ReadLine();
                if (!string.IsNullOrEmpty(str.Trim()))
                {
                    if (str.Substring(0, 1) == ".")
                    {
                        string ico = str.Substring(1, str.Length - 2);

                        //含有icon-标示的是easyui系统图标
                        if (ico.IndexOf("icon-")==-1)
                        {
                            EasyUiTreeResult result = new EasyUiTreeResult()
                            {
                                id = Guid.NewGuid().ToString(),
                                text = ico,
                                iconCls = ico
                            };

                            results.Add(result);
                        }
                    }
                }
            }

            return Json(results);
        }
        /// <summary>
        /// 传入有权限menu集合,构建符合tree显示数据结构的所有菜单集合
        /// </summary>
        /// <param name="userMenus"></param>
        /// <returns></returns>
        public List<EasyUiTreeResult> BuildTreeMenu(IEnumerable<SysMenu> userMenus)
        {
            //获取所有菜单
            IEnumerable<SysMenu> allMenus = menuService.GetAllMenu();
            //获取所有按钮数据
            //IEnumerable<SysButton> allButtons = RepositoryFactory.ButtonRepository.GetListByTable<SysButton>(Constant.TableSysButton,
            //                                                                        "SysId,MenuId,BtnName,BtnIcon,BtnOrder,BtnFunction,RecordStatus",
            //                                                                        null);

            IEnumerable<SysButton> allButtons = RepositoryFactory.ButtonRepository.GetList();

            //去除掉所有菜单中用户已有权限的菜单
            foreach (SysMenu userMenu in userMenus)
            {
                allMenus = allMenus.Where(x => x.SysId != userMenu.SysId);
            }
            //allMenus = allMenus.Where(x => !userMenus.Contains(x)) ;

            //将无权限菜单和有权限菜单合并
            allMenus = allMenus.Union(userMenus).OrderBy(x => x.MenuOrder);
            //将父菜单筛选出来,用作遍历
            var parentMenus = allMenus.Where(x => string.IsNullOrWhiteSpace(x.MenuParentId));

            //构建菜单的层次结构
            foreach (SysMenu menu in parentMenus)
            {
                var childs = RepositoryFactory.MenuRepository.GetChildre(allMenus, menu, false);
                if (childs != null && childs.Any())
                {
                    menu.Children = childs.OrderBy(c => c.MenuOrder);
                }
            }

            //构建菜单层次结构tree格式
            List<EasyUiTreeResult> treeResults = new List<EasyUiTreeResult>();
            foreach (SysMenu allMenu in parentMenus)
            {
                EasyUiTreeResult result = new EasyUiTreeResult()
                    {
                        id = allMenu.SysId,
                        text = allMenu.MenuName,
                        link = allMenu.MenuLink,
                        recordStatus = allMenu.RecordStatus,
                        @checked = allMenu.isCheck,
                        visible = allMenu.IsVisible != null && ((PrivilegeOperation)(allMenu.IsVisible.Value)) == PrivilegeOperation.Enable ? "启用" : "禁用"
                    };
                treeResults.Add(BuildTreeByLoop(allMenu, allButtons, result));
            }

            return treeResults;
        }
        /// <summary>
        /// 根据menu获取menu下所有button
        /// </summary>
        /// <param name="allButtons"></param>
        /// <param name="menu"></param>
        /// <returns></returns>
        private EasyUiTreeResult GetButtonsByMenu(IEnumerable<SysButton> allButtons, SysMenu menu)
        {
            var bts = allButtons.Where(x => x.MenuId == menu.SysId);
            bts = bts.Where(x => x.IsVisible == (long)PrivilegeOperation.Enable);

            List<EasyUiTreeResult> buttons = new List<EasyUiTreeResult>();
            if (bts.Any())
            {
                #region 如果此菜单下有可用按钮,判断添加按钮可用或者不可用

                if (menu.Buttons != null)
                {
                    foreach (SysButton button in menu.Buttons)
                    {
                        if (button.IsVisible == (long)PrivilegeOperation.Enable)
                        {
                            EasyUiTreeResult buttonc = new EasyUiTreeResult()
                            {
                                id = button.SysId,
                                text = button.BtnName,
                                iconCls = button.BtnIcon,
                                @checked = true,
                                link = button.BtnFunction,
                                order = button.BtnOrder,
                                recordStatus = button.RecordStatus,
                            };

                            buttons.Add(buttonc);
                        }

                        bts = bts.Where(x => x.SysId != button.SysId);
                    }

                    foreach (SysButton button in bts)
                    {

                        EasyUiTreeResult buttonc = new EasyUiTreeResult()
                        {
                            id = button.SysId,
                            text = button.BtnName,
                            iconCls = button.BtnIcon,
                            @checked = false,
                            link = button.BtnFunction,
                            order = button.BtnOrder,
                            recordStatus = button.RecordStatus,
                        };
                        buttons.Add(buttonc);

                    }
                }
                #endregion

                #region 如果此菜单无有权限按钮,添加全部按钮

                else
                {
                    foreach (SysButton button in bts)
                    {
                        EasyUiTreeResult buttonc = new EasyUiTreeResult()
                        {
                            id = button.SysId,
                            text = button.BtnName,
                            iconCls = button.BtnIcon,
                            @checked = false,
                            link = button.BtnFunction,
                            order = button.BtnOrder,
                            recordStatus = button.RecordStatus
                        };
                        buttons.Add(buttonc);
                    }
                }

                #endregion
            }

            EasyUiTreeResult treeResult = new EasyUiTreeResult()
            {
                id = menu.SysId,
                text = menu.MenuName,
                link = menu.MenuLink,
                iconCls = menu.MenuIcon,
                @checked = menu.isCheck,
                children = new EasyUiTreeResult[0],
                visible = menu.IsVisible != null && ((PrivilegeOperation)(menu.IsVisible.Value)) == PrivilegeOperation.Enable ? "启用" : "禁用",
                buttons = buttons.ToArray()
            };

            return treeResult;
        }