wzp
2021-09-01 2891fe0769189be39c9634b2cbc1841dbd52d022
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.IRepository;
 
namespace FineAdmin.Service
{
    public class ModuleService : BaseService<ModuleModel>, IModuleService
    {
        public IModuleRepository ModuleRepository { get; set; }
        public IButtonService ButtonService { get; set; }
        public IRoleAuthorizeService RoleAuthorizeService { get; set; }
 
        public dynamic GetListByFilter(ModuleModel filter, PageInfo pageInfo)
        {
            throw new NotImplementedException();
        }
 
        /// <summary>
        /// 获得菜单列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public dynamic GetModuleList(int roleId)
        {
            IEnumerable<ModuleModel> allMenus = GetModuleListByRoleId(roleId);
            List<Tree> treeList = new List<Tree>();
            var rootMenus = allMenus.Where(x => x.ParentId == 0).OrderBy(x => x.SortCode);
            foreach (var item in rootMenus)
            {
                var _tree = new Tree { id = item.Id, title = item.FullName, href = item.UrlAddress, fontFamily = item.FontFamily, icon = item.Icon };
                GetModuleListByModuleId(treeList, allMenus, _tree, item.Id);
                treeList.Add(_tree);
            }
            var result = treeList;
            return result;
        }
 
        /// <summary>
        /// 根据一级菜单加载子菜单列表
        /// </summary>
        /// <param name="treeList"></param>
        /// <param name="allMenus"></param>
        /// <param name="tree"></param>
        /// <param name="moduleId"></param>
        private void GetModuleListByModuleId(List<Tree> treeList, IEnumerable<ModuleModel> allMenus, Tree tree, int moduleId)
        {
            var childMenus = allMenus.Where(x => x.ParentId == moduleId).OrderBy(x => x.SortCode);
            if (childMenus != null && childMenus.Count() > 0)
            {
                List<Tree> _children = new List<Tree>();
                foreach (var item in childMenus)
                {
                    var _tree = new Tree { id = item.Id, title = item.FullName, href = item.UrlAddress, fontFamily = item.FontFamily, icon = item.Icon };
                    _children.Add(_tree);
                    tree.children = _children;
                    GetModuleListByModuleId(treeList, allMenus, _tree, item.Id);
                }
            }
        }
 
        /// <summary>
        /// 根据角色ID获取菜单列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        private IEnumerable<ModuleModel> GetModuleListByRoleId(int roleId)
        {
            string sql = @"SELECT m.* FROM dbsys_Module m";
            var list = ModuleRepository.GetModuleListByRoleId(sql, roleId);
            return list;
        }
 
        /// <summary>
        /// Module treeSelect数据列表
        /// </summary>
        public IEnumerable<TreeSelect> GetModuleTreeSelect()
        {
            IEnumerable<ModuleModel> moduleList = BaseRepository.GetAll("Id,FullName,ParentId", "ORDER BY SortCode ASC");
            var rootModuleList = moduleList.Where(x => x.ParentId == 0).OrderBy(x => x.SortCode);
            List<TreeSelect> treeSelectList = new List<TreeSelect>();
            foreach (var item in rootModuleList)
            {
                TreeSelect tree = new TreeSelect
                {
                    id = item.Id,
                    name = item.FullName,
                    open = false
                };
                GetModuleChildren(treeSelectList, moduleList, tree, item.Id);
                treeSelectList.Add(tree);
            }
            return treeSelectList;
        }
 
        /// <summary>
        /// 递归遍历treeSelectList
        /// </summary>
        private void GetModuleChildren(List<TreeSelect> treeSelectList, IEnumerable<ModuleModel> moduleList, TreeSelect tree, int id)
        {
            var childModuleList = moduleList.Where(x => x.ParentId == id).OrderBy(x => x.SortCode);
            if (childModuleList != null && childModuleList.Count() > 0)
            {
                List<TreeSelect> _children = new List<TreeSelect>();
                foreach (var item in childModuleList)
                {
                    TreeSelect _tree = new TreeSelect
                    {
                        id = item.Id,
                        name = item.FullName,
                        open = false
                    };
                    _children.Add(_tree);
                    tree.children = _children;
                    GetModuleChildren(treeSelectList, moduleList, _tree, item.Id);
                }
            }
        }
        /// <summary>
        /// 获取所有菜单列表及可用按钮权限列表
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public IEnumerable<ModuleModel> GetModuleButtonList(int roleId)
        {
            string returnFields = "Id,ParentId,FullName,Icon,SortCode";
            string orderby = "ORDER BY SortCode ASC";
            IEnumerable<ModuleModel> list = GetAll(returnFields, orderby);
            foreach (var item in list)
            {
                item.ModuleButtonHtml = ButtonService.GetButtonListHtmlByRoleIdModuleId(roleId, item.Id);
                item.IsChecked = RoleAuthorizeService.GetListByRoleIdModuleId(roleId, item.Id).Count() > 0 ? true : false;
            }
            return list;
        }
    }
}