chengsc
3 天以前 f124565982920c54c7c92d27d0a0c6bf61689e83
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.ModelDto.SysDto;
using Model.ModelVm.SysVm;
using SqlSugar;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IDAL.ISysInterface;
 
namespace WMS.DAL.SysInfrastructure
{
    /// <summary>
    /// 角色权限仓储实践
    /// </summary>
    public class RoleRightRepository : BaseRepository<SysRoleRight>, IRoleRightRepository
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        public RoleRightRepository() : base(Db)
        {
        }
 
 
        public List<RoleRightDto> GetRoleMenuList(int userId)
        {
            try
            {
                var user = Db.Queryable<SysUserInfor>().First(m => m.Id == userId && m.IsDel == "0");
                if (user == null)
                {
                    throw new Exception("未查询到登录人信息");
                }
                if (string.IsNullOrWhiteSpace(user.RoleNo))
                {
                    throw new Exception("未查询到登录人的所属角色信息");
                }
 
                string str = $@"select role.Id RoleId, menu.Id MenuId,roleright.RoleNo,roleright.MenuNo,role.RoleName,
                        menu.MenuName,menu.ParentNo,menu.Ord,menu.Url,menu.level,menu.IsEnable,role.Demo RoleDemo,menu.Demo MenuDemo 
                        from SysRoleRight roleright 
                        join SysFunctionMenu menu on roleright.MenuNo = menu.MenuNo 
                        join SysRoles role on roleright.RoleNo = role.RoleNo
                        where role.IsDel = '0' and menu.IsDel = '0' 
                        and roleright.IsDel = '0' and roleright.RoleNo = '{user.RoleNo}' ";
 
                var roleRightDtos = Db.Ado.SqlQuery<RoleRightDto>(str);
                return roleRightDtos.OrderBy(m => m.Ord).ToList();
 
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 获取角色权限信息列表
        /// 多表:角色权限、角色、菜单
        /// </summary>
        /// <returns></returns>
        public List<RoleRightDto> GetRoleRightList()
        {
            string str = "select role.Id RoleId,menu.Id MenuId,roleright.RoleNo,roleright.MenuNo,role.RoleName,menu.MenuName,menu.ParentNo,menu.Ord,menu.Url,menu.level,menu.IsEnable,role.Demo RoleDemo,menu.Demo MenuDemo from SysRoleRight roleright join SysFunctionMenu menu on roleright.MenuNo = menu.MenuNo join SysRoles role on roleright.RoleNo = role.RoleNo where role.IsDel = @isdel";
 
            List<RoleRightDto> roleRightDtos = Db.Ado.SqlQuery<RoleRightDto>(str, new
            {
                isdel = "0", //角色是否删除
            });
            return roleRightDtos;
        }
 
        /// <summary>
        /// 根据角色id获取当前角色所拥有权限
        /// </summary>
        /// <param name="id">角色id</param>
        /// <returns></returns>
        public List<RoleRightDto> GetRoleRightListById(int id)
        {
            string str = "select * from SysRoleRight roleright left join SysRoles role on roleright.RoleNo = role.RoleNo left join SysFunctionMenu menu1 on menu1.MenuNo = roleright.MenuNo where role.Id = @id and roleright.IsDel = @isdel";
            List<RoleRightDto> rolerightlist = Db.Ado.SqlQuery<RoleRightDto>(str, new
            {
                isdel = "0", //角色权限是否删除
                id //id
            });
            return rolerightlist;
        }
 
        /// <summary>
        /// 获取角色权限信息列表(单表)
        /// </summary>
        /// <returns></returns>
        public List<SysRoleRight> GetRoleRightOneListById(int id)
        {
            string str = "select RoleNo,MenuNo from SysRoleRight where Isdel = @isdel and RoleNo = (select RoleNo from SysRoles where IsDel = @roleisdel and Id = @id)";
            List<SysRoleRight> roleRights = Db.Ado.SqlQuery<SysRoleRight>(str, new
            {
                roleisdel = "0", //角色是否删除
                isdel = "0", //角色权限是否删除
                id //id
            });
            return roleRights;
        }
 
        /// <summary>
        /// 根据角色号获取角色权限信息列表(单表)
        /// </summary>
        /// <param name="RoleNo">角色号</param>
        /// <returns></returns>
        public List<SysRoleRight> GetRoleRightOneListByNo(string RoleNo)
        {
            string str = "select * from SysRoleRight where RoleNo = @roleno";
            List<SysRoleRight> roleRights = Db.Ado.SqlQuery<SysRoleRight>(str, new
            {
                roleno = RoleNo //角色号
            });
            return roleRights;
        }
 
        /// <summary>
        /// 新增角色权限信息
        /// </summary>
        /// <param name="roleright">角色权限实体模型</param>
        /// <returns></returns>
        public async Task<int> InsertRoleRight(SysRoleRight roleright)
        {
            string str = "insert into SysRoleRight values(@roleno, @menuno, @isdel, @createtime, @createuser, null, null)";
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                roleno = roleright.RoleNo, //角色号
                menuno = roleright.MenuNo, //菜单号
                isdel = "0", //是否删除
                createtime = Db.GetDate(), //创建时间
                createuser = roleright.CreateUser //创建人
            });
            return i;
        }
 
        /// <summary>
        /// 删除角色权限信息
        /// </summary>
        /// <param name="RoleNo">角色号</param>
        /// <returns></returns>
        public async Task<int> DeleteRoleRight(string RoleNo)
        {
            string str = "delete from SysRoleRight where RoleNo = @roleno";
            //删除
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                roleno = RoleNo //角色号
            });
            return i;
        }
 
        /// <summary>
        /// 删除子级角色权限信息
        /// </summary>
        /// <param name="RoleNo">角色号</param>
        /// <returns></returns>
        public async Task<int> DeleteChildRoleRight(string RoleNo, string MenuNo)
        {
            try
            {
                //将菜单号进行分割 存储数组中
                var arr = MenuNo.Split(',');
 
                List<SysRoles> roleList = Db.Ado.SqlQuery<SysRoles>($"select * from SysRoles where CreateUser in (select Id from SysUserInfor where RoleNo='{RoleNo}') ");
                foreach (var item in roleList)
                {
                    List<SysRoleRight> roleRightList = Db.Ado.SqlQuery<SysRoleRight>($"select * from SysRoleRight where RoleNo='{item.RoleNo}'");
                    foreach (var right in roleRightList)
                    {
                        int first1 = Array.IndexOf(arr, right.MenuNo);
                        if (first1 <= -1)
                        {
                            await Db.Ado.ExecuteCommandAsync($"delete from SysRoleRight where RoleNo = '{item.RoleNo}' and MenuNo='{right.MenuNo}' ");
                        }
                    }
                }
                return 1;
            }
            catch(Exception ex)
            {
                throw new Exception("编辑角色权限信息异常", ex);
            }
        }
 
        /// <summary>
        /// 分配权限显示
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public List<RoleRightDto> GetRoleRightRBAC(string str)
        {
            List<RoleRightDto> rolerightdto = Db.Ado.SqlQuery<RoleRightDto>(str);
            return rolerightdto;
        }
    }
}