bklLiudl
2024-05-25 484e5129e4c9a671c5660a556a24bd306f1fdd9b
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
142
143
144
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.ModelDto.SysDto;
using WMS.Entity;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL;
using WMS.IDAL.ISysInterface;
using Model.ModelVm.SysVm;
 
namespace WMS.BLL.SysServer
{
    /// <summary>
    /// RBAC服务实践
    /// </summary>
    public class RBACServer : IRBACServer
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        public IRBACRepository _rbac { get; set; }
        private readonly IMenuRepository _menu;
        private readonly IRoleRightRepository _roleright;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rbac"></param>
        /// <param name="menu"></param>
        /// <param name="roleright"></param>
        public RBACServer(IRBACRepository rbac, IMenuRepository menu, IRoleRightRepository roleright)
        {
            _rbac = rbac;
            _menu = menu;
            _roleright = roleright;
        }
 
        /// <summary>
        /// 获取rbac信息列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<RBAC>> GetRBACLists(int roleid)
        {
            List<RBAC> rbaclist = await _rbac.GetRBACLists(roleid);
            return rbaclist;
        }
 
        /// <summary>
        /// 根据角色id获取对应菜单权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<RoleRightDto> GetRoleRightList(int id)
        {
            List<RoleRightDto> roleRightDtos = _roleright.GetRoleRightListById(id);
            return roleRightDtos;
        }
 
        /// <summary>
        /// 根据角色Id获取对应的菜单选中
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetMenuByroleIdNew(int id)
        {
            try
            {
                List<RoleRightDto> rolerightlist = _roleright.GetRoleRightListById(id);
                //var list = DataContext.WmsSysRight.Where(m => m.UserGroupId == id).ToList();
                var str = new List<string>();
                foreach (var item in rolerightlist)
                {
                    str.Add(item.MenuNo);
                }
                string sql1 = $"select * from SysFunctionMenu where IsDel = '0' and ParentNo = ''";
                List<FunctionMenuDto> parent = _menu.GetMenuList(sql1).OrderBy(m => m.MenuNo).Select(m => new FunctionMenuDto()
                {
                    //Id = m.Id,
                    MenuNo = m.MenuNo,
                    ParentId = m.ParentNo,
                    title = m.MenuName,
                    //expand = str.Contains(m.Id),
                    @checked = str.Contains(m.MenuNo)
                }).ToList();
 
                string sql2 = $"select * from SysFunctionMenu where IsDel = '0' and ParentNo != ''";
                List<FunctionMenuDto> child = _menu.GetMenuList(sql2).OrderBy(m => m.MenuNo).Select(m => new FunctionMenuDto()
                {
                    //Id = m.Id,
                    MenuNo = m.MenuNo,
                    ParentId = m.ParentNo,
                    title = m.MenuName,
                    Expand = str.Contains(m.MenuNo),
                    @checked = str.Contains(m.MenuNo)
                }).ToList();
 
                List<FunctionMenuDto> data = new List<FunctionMenuDto>();
 
                foreach (var item in parent)
                {
                    data.Add(item);
                    FunNew(child, item);
                }
                return data;
 
            }
            catch (Exception ex)
            {
                throw new Exception("获取角色对应的菜单信息" + ex.Message);
            }
        }
        public void FunNew(List<FunctionMenuDto> all, FunctionMenuDto curItem)
        {
            List<FunctionMenuDto> childItems = all.Where(m => m.ParentId == curItem.MenuNo).ToList(); //得到子节点
            curItem.children = childItems; //将子节点加入
 
            //遍历子节点,进行递归,寻找子节点的子节点
            foreach (var subItem in childItems)
            {
                FunNew(all, subItem);
            }
 
        }
 
 
 
        /// <summary>
        /// 分配权限显示
        /// </summary>
        /// <param name="MenuNo"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<FunctionMenuDto> GetRoleRightRBAC(string MenuNo, string UserId)
        {
            List<FunctionMenuDto> menudto = _menu.GetMenuDtoList(MenuNo,UserId);
 
            return menudto;
        }
    }
}