hwh
2024-05-28 94885ec296274e3e6a848a65c41febf3434bdf9b
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Model.ModelDto.SysDto;
using Model.ModelVm.SysVm;
using WMS.DAL.SystemService;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
 
namespace WMS.BLL.SysServer
{
    /// <summary>
    /// 角色权限服务实践
    /// </summary>
    public class RoleRightServer : IRoleRightServer
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        public IRoleRightRepository _rolerights { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roles"></param>
        public RoleRightServer(IRoleRightRepository rolerights)
        {
            _rolerights = rolerights;
        }
 
        /// <summary>
        /// 获取用户的菜单权限 ----csc
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public List<RoleRightDto> GetRoleMenuList(int userId)
        {
            try
            {
                var user = _rolerights.GetRoleMenuList(userId);
                return user;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 获取角色权限信息列表
        /// 多表:角色权限、角色、菜单
        /// </summary>
        /// <returns></returns>
        public List<RoleRightDto> GetRoleRightList()
        {
            List<RoleRightDto> roleRightDtos = _rolerights.GetRoleRightList();
            return roleRightDtos;
        }
 
        /// <summary>
        /// 根据id获取角色权限信息列表(单表)
        /// </summary>
        /// <param name="rolerightids">角色权限id</param>
        /// <returns></returns>
        public List<SysRoleRight> GetRoleRightOneListById(int id)
        {
            List<SysRoleRight> roleRights = _rolerights.GetRoleRightOneListById(id);
            return roleRights;
        }
 
        /// <summary>
        /// 根据角色号获取角色权限信息列表(单表)
        /// </summary>
        /// <param name="roleno">角色好</param>
        /// <returns></returns>
        public List<SysRoleRight> GetRoleRightOneListByNo(string roleno)
        {
            List<SysRoleRight> roleRights = _rolerights.GetRoleRightOneListByNo(roleno);
            return roleRights;
        }
 
        /// <summary>
        /// 新增角色权限信息
        /// </summary>
        /// <param name="MenuNo">菜单号</param>
        /// <param name="RoleNo">角色号</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> InsertRoleRight(string MenuNo, string RoleNo, string userId)
        {
            //捕获异常
            try
            {
                //将菜单号进行分割 存储数组中
                var arr = MenuNo.Split(',');
                int i = 0;
                //查询该角色是否拥有权限
                List<SysRoleRight> rolelist = GetRoleRightOneListByNo(RoleNo);
                //有
                if (rolelist.Count > 0)
                {
                    //删除该角色所有菜单信息
                    i = await DeleteRoleRight(RoleNo);
                }
                //无
                else if (rolelist.Count == 0)
                {
                    i = 1;
                }
                //开始新增权限
                if (i > 0)
                {
                    SysRoleRight roleright = new SysRoleRight();
                    //循环将菜单数组分别加入到数据模型中
                    for (int j = 0; j < arr.Count(); j++)
                    {
                        roleright.RoleNo = RoleNo;//角色号
                        roleright.MenuNo = arr[j];//菜单号
                        roleright.CreateUser = Convert.ToInt32(userId);//创建人
 
                        //成功 将菜单号和角色号新增为新的数据
                        i = await _rolerights.InsertRoleRight(roleright);
                    }
                    //删除子级角色权限信息。(角色权限减少,角色下级权限同时减少)
                    await _rolerights.DeleteChildRoleRight(RoleNo, MenuNo);
                    //返回
                    return i;
 
                }
                else
                {
                    return i;
                }
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("新增角色权限信息异常", ex);
            }
        }
 
        /// <summary>
        /// 删除角色权限信息
        /// </summary>
        /// <param name="RoleNo">角色号</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> DeleteRoleRight(string RoleNo)
        {
            //捕获异常
            try
            {
                //删除
                int i = await _rolerights.DeleteRoleRight(RoleNo);
                return i;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("删除角色权限信息异常", ex);
            }
        }
    }
}