hutongqing
2024-12-10 8d341db9d2d5699d527c88c935f0c4ce255a57a4
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
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<ExpandoObject> roles = _dbContext.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 = _dbContext.Db.Queryable("Sys_RoleDataPermission", "x").Select(RoleDataSelectModes).ToList();
            foreach (var item in roleDatas)
            {
                roleDataDynamics.Add(item);
            }
 
            List<ExpandoObject> users = _dbContext.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
                    {
                        AuthorityScope = role.AuthorityScope,
                        RoleId = role.RoleId,
                        RoleName = role.RoleName,
                        ParentId = role.ParentId,
                        UserId = user.UserId,
                        UserName = user.UserName,
                        WarehouseIds = ids
                    });
                }
            }
 
            UserRoles = userRoles;
            _cacheService.AddOrUpdate("UserRoles", userRoles.Serialize());
 
            return Task.CompletedTask;
        }
 
        public Task StopAsync(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
 
        List<SelectModel> RoleSelectModes = new List<SelectModel>()
        {
            new SelectModel()
            {
                 FieldName = "RoleId",
            },
            new SelectModel()
            {
                 FieldName = "ParentId",
            },
            new SelectModel()
            {
                 FieldName = "RoleName",
            },
            new SelectModel()
            {
                 FieldName = "AuthorityScope",
            }
        };
 
        List<SelectModel> UserSelectModes = new List<SelectModel>()
        {
            new SelectModel()
            {
                 FieldName = "UserId",
            },
            new SelectModel()
            {
                 FieldName = "UserName",
            },
            new SelectModel()
            {
                 FieldName = "RoleId",
            },
            new SelectModel()
            {
                 FieldName = "RoleName",
            }
        };
 
        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; }
    }
}