hutongqing
2025-01-11 97b9f7d9650b426fa540e7aaba7739a2e7080ab0
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
using HslCommunication.WebSocket;
using Microsoft.Extensions.Hosting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Seed;
using ICacheService = WIDESEA_Core.Caches.ICacheService;
 
namespace WIDESEA_Core.HostedService
{
    public class PermissionDataHostService : IHostedService
    {
        private readonly DBContext _dbContext;
        private readonly ICacheService _cacheService;
        private readonly WebSocketServer _server;
 
        public static List<UserRole> UserRoles;
 
        public PermissionDataHostService(DBContext dbContext, ICacheService cacheService, WebSocketServer server)
        {
            _dbContext = dbContext;
            _cacheService = cacheService;
            _server = server;
        }
 
        public Task StartAsync(CancellationToken cancellationToken)
        {
            List<UserRole> userRoles = GetUserRoles(_dbContext.Db);
            UserRoles = userRoles;
            _cacheService.AddOrUpdate("UserRoles", userRoles.Serialize());
 
            return Task.CompletedTask;
        }
 
        public Task StopAsync(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
 
        public static List<UserRole> GetUserRoles(ISqlSugarClient db)
        {
            List<ExpandoObject> roles = db.Queryable("Sys_Role", "x").Select(RoleSelectModes).ToList();
            List<dynamic> roleDynamics = new List<dynamic>();
            foreach (var item in roles)
            {
                roleDynamics.Add(item);
            }
 
            List<dynamic> roleDataDynamics = new List<dynamic>();
            List<ExpandoObject> roleDatas = db.Queryable("Sys_RoleDataPermission", "x").Select(RoleDataSelectModes).ToList();
            foreach (var item in roleDatas)
            {
                roleDataDynamics.Add(item);
            }
 
            List<ExpandoObject> users = db.Queryable("Sys_User", "x").Select(UserSelectModes).ToList();
 
            List<UserRole> userRoles = new List<UserRole>();
            foreach (var item in users)
            {
                dynamic user = item;
                dynamic? role = roleDynamics.FirstOrDefault(x => x.RoleId == user.RoleId);
                List<object> warehouseIds = roleDataDynamics.Where(x => x.RoleId == user.RoleId).Select(x => x.WarehouseId).ToList();
                List<int> ids = new List<int>();
                for (int i = 0; i < warehouseIds.Count; i++)
                {
                    ids.Add(Convert.ToInt32(warehouseIds[i]));
                }
                if (role != null)
                {
                    userRoles.Add(new UserRole
                    {
                        RoleId = role.RoleId,
                        RoleName = role.RoleName,
                        ParentId = role.ParentId,
                        UserId = user.UserId,
                        UserName = user.UserName,
                        WarehouseIds = ids
                    });
                }
            }
            return userRoles;
        }
 
        public static List<UserRole> GetUserRoles(ISqlSugarClient db, int userId)
        {
            List<ExpandoObject> roles = db.Queryable("Sys_Role", "x").Select(RoleSelectModes).ToList();
            List<dynamic> roleDynamics = new List<dynamic>();
            foreach (var item in roles)
            {
                roleDynamics.Add(item);
            }
 
            List<dynamic> roleDataDynamics = new List<dynamic>();
            List<ExpandoObject> roleDatas = db.Queryable("Sys_RoleDataPermission", "x").Select(RoleDataSelectModes).ToList();
            foreach (var item in roleDatas)
            {
                roleDataDynamics.Add(item);
            }
 
            List<ExpandoObject> users = db.Queryable("Sys_User", "x").Where($"UserId={userId}").Select(UserSelectModes).ToList();
 
            List<UserRole> userRoles = new List<UserRole>();
            foreach (var item in users)
            {
                dynamic user = item;
                dynamic? role = roleDynamics.FirstOrDefault(x => x.RoleId == user.RoleId);
                List<object> warehouseIds = roleDataDynamics.Where(x => x.RoleId == user.RoleId).Select(x => x.WarehouseId).ToList();
                List<int> ids = new List<int>();
                for (int i = 0; i < warehouseIds.Count; i++)
                {
                    ids.Add(Convert.ToInt32(warehouseIds[i]));
                }
                if (role != null)
                {
                    userRoles.Add(new UserRole
                    {
                        RoleId = role.RoleId,
                        RoleName = role.RoleName,
                        ParentId = role.ParentId,
                        UserId = user.UserId,
                        UserName = user.UserName,
                        WarehouseIds = ids
                    });
                }
            }
            return userRoles;
        }
 
        static List<SelectModel> RoleSelectModes = new List<SelectModel>()
        {
            new SelectModel()
            {
                 FieldName = "RoleId",
            },
            new SelectModel()
            {
                 FieldName = "ParentId",
            },
            new SelectModel()
            {
                 FieldName = "RoleName",
            },
        };
 
        static List<SelectModel> UserSelectModes = new List<SelectModel>()
        {
            new SelectModel()
            {
                 FieldName = "UserId",
            },
            new SelectModel()
            {
                 FieldName = "UserName",
            },
            new SelectModel()
            {
                 FieldName = "RoleId",
            },
            new SelectModel()
            {
                 FieldName = "RoleName",
            }
        };
 
        static List<SelectModel> RoleDataSelectModes = new List<SelectModel>()
        {
            new SelectModel()
            {
                 FieldName = "RoleId",
            },
            new SelectModel()
            {
                 FieldName = "WarehouseId",
            }
        };
    }
 
    public class UserRole
    {
        public int UserId { get; set; }
 
        public int RoleId { get; set; }
 
        public string RoleName { get; set; }
 
        public string UserName { get; set; }
 
        public int ParentId { get; set; }
 
        public int AuthorityScope { get; set; }
 
        public List<int> WarehouseIds { get; set; }
    }
}