kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.highdatas.mdm.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.highdatas.mdm.entity.TRole;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.entity.TUserGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.util.*;
 
@ConfigurationProperties(prefix = "user")
@Component
@Slf4j
public class UserRoleClient {
 
    @Value("${user.url}")
    String url;
 
    String prefix ;
    @Autowired
    RedisClient redisClient;
 
    public UserRoleClient() {
        this.prefix = "/api/datacvg/";
    }
 
    public TUser getTUserById(String userId) {
        Object redisValObj = redisClient.getRedisValObj(RedisClient.getRealRedisKey(userId));
        if (redisValObj != null) {
            JSONObject object = (JSONObject) redisValObj;
            TUser user = JSONObject.parseObject(object.toJSONString(), TUser.class);
            return user;
        }
        String url = this.url + prefix + "user/selectUser";
        Map<String, String> params = new LinkedHashMap<>();
        params.put("userId",userId);
        String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params, MediaType.APPLICATION_JSON);
        log.info(s);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
            return null;
        }else {
            JSONObject data = result.getJSONObject(Constant.Data);
            data = data.getJSONObject(Constant.userDO);
            TUser user = JSON.toJavaObject(data, TUser.class);
            if (user == null){
                return null;
            }
            redisClient.putRedisValObj(RedisClient.getRealRedisKey(userId), user);
            return user;
        }
    }
 
    public TRole getRoleByRoleId(String roleId) {
        Object redisValObj = redisClient.getRedisValObj(RedisClient.getRealRedisKey(roleId));
        if (redisValObj != null) {
            JSONObject object = (JSONObject) redisValObj;
            TRole user = JSONObject.parseObject(object.toJSONString(), TRole.class);
            return user;
        }
        try {
            String url =  this.url + prefix + "role/selectRole";
            Map<String, String> params = new LinkedHashMap<>();
            params.put("roleId", roleId);
            String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params, MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return null;
            }else {
                JSONObject data = result.getJSONObject(Constant.Data);
                TRole role = JSON.toJavaObject(data, TRole.class);
                if (role == null){
                    return null;
                }
                redisClient.putRedisValObj(RedisClient.getRealRedisKey(roleId), role);
                return role;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
 
    }
 
    public  List<TUserGroup> getUserGroupByUserId(String userId) {
 
        String key = RedisClient.getRealRedisKey(userId + "-getUserGroupByUserId");
        List<Object> redisList = redisClient.getRedisList(key);
        if (redisList != null && !redisList.isEmpty()) {
            List<TUserGroup> result = new ArrayList<>();
            for (Object o : redisList) {
                JSONObject object = (JSONObject) o;
                TUserGroup menu = JSONObject.parseObject(object.toJSONString(), TUserGroup.class);
                result.add(menu);
            }
            return result;
        }
 
        String url =  this.url + prefix + "group/userGroupInfo";
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("userId",userId);
        String urlParamsByMap = HttpUtils.getUrlParamsByMap(params);
        String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, null,urlParamsByMap, MediaType.APPLICATION_JSON);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
            return null;
        }else {
            JSONArray groupArray = result.getJSONArray(Constant.Data);
            List<TUserGroup> tUserGroups = JSONArray.parseArray(groupArray.toJSONString(), TUserGroup.class);
            if (tUserGroups == null || tUserGroups.isEmpty()) {
                return null;
            }
            redisClient.putRedisList(key, (tUserGroups));
            return tUserGroups;
        }
    }
 
    public  List<TRole> getTRoleListByUserId(String userId) {
 
        String key = RedisClient.getRealRedisKey(userId + "-getTRoleListByUserId");
        List<Object> redisList = redisClient.getRedisList(key);
        if (redisList != null && !redisList.isEmpty()) {
            List<TRole> result = new ArrayList<>();
            for (Object o : redisList) {
                JSONObject object = (JSONObject) o;
                TRole menu = JSONObject.parseObject(object.toJSONString(), TRole.class);
                result.add(menu);
            }
            return result;
        }
        String url =  this.url + prefix + "userRole/selectUserRoleList";
        Map<String, String> params = new LinkedHashMap<>();
        params.put("userId",userId);
        String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, params, MediaType.APPLICATION_JSON);
        JSONObject result = (JSONObject) JSON.parse(s);
        String sucess = result.getString(Constant.Success);
        if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
            return null;
        }else {
            JSONArray groupArray = result.getJSONArray(Constant.Data);
            List<TRole> tRoles = JSONArray.parseArray(groupArray.toJSONString(), TRole.class);
            if (tRoles == null || tRoles.isEmpty()) {
                return null;
            }
            redisClient.putRedisList(key, tRoles);
            return tRoles;
        }
    }
 
    public List<TRole> getRoleListByGroupId(String groupId) {
        String key = RedisClient.getRealRedisKey(groupId + "-getTRoleListByUserId");
        List<Object> redisList = redisClient.getRedisList(key);
        if (redisList != null && !redisList.isEmpty()) {
            List<TRole> result = new ArrayList<>();
            for (Object o : redisList) {
                JSONObject object = (JSONObject) o;
                TRole menu = JSONObject.parseObject(object.toJSONString(), TRole.class);
                result.add(menu);
            }
            return result;
        }
        try {
            String url =  this.url + prefix + "group/groupRoleInfo";
            Map<String, Object> params = new LinkedHashMap<>();
            params.put("groupId", groupId);
            String urlParamsByMap = HttpUtils.getUrlParamsByMap(params);
            String s = HttpUtils.HttpRestClient(url, HttpMethod.POST, null,urlParamsByMap, MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return null;
            }else {
                JSONArray data = result.getJSONArray(Constant.Data);
                List<TRole> role = JSONArray.parseArray(data.toJSONString(), TRole.class);
                if (role == null || role.isEmpty()){
                    return null;
                }
                redisClient.putRedisList(key, role);
                return role;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
 
    }
 
    public boolean addRoleByUser(String userId, List<String> roleIds, String userName) {
        try {
            String url =  this.url + prefix + "userRole/addRoleUser";
            Map<String, Object> params = new LinkedHashMap<>();
            params.put("userId", userId);
            params.put("listRoleId", roleIds);
            params.put("createUser", userId);
            String s = HttpUtils.HttpRestClientByObjectParams(url, HttpMethod.POST, params, null, null,MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return false;
            }else {
                boolean added = result.getBoolean(Constant.Data);
                return added;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
 
    public boolean deleteRoleByUser(String userId, List<String> roleIds) {
        try {
            String url =  this.url + prefix + "userRole/deleteRoleUsers";
            Map<String, Object> params = new LinkedHashMap<>();
            params.put("userId", userId);
            params.put("listRoleId", roleIds);
            String s = HttpUtils.HttpRestClientByObjectParams(url, HttpMethod.POST, params, null, null,MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return false;
            }else {
                boolean added = result.getBoolean(Constant.Data);
                return added;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
 
    }
 
    public boolean addRoleByGroupId(String userId, List<String> roleIds, String groupId) {
        try {
            String url =  this.url + prefix + "group/addGroupRole";
            HashMap<String, String> headerMap = new HashMap<>();
            headerMap.put("userId", userId);
 
            Map<String, Object> params = new LinkedHashMap<>();
            params.put("groupId", groupId);
            params.put("roleIdList", roleIds);
            String s = HttpUtils.HttpRestClientByObjectParams(url, HttpMethod.POST, params, null,headerMap,MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return false;
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
 
    public List<TUser> listUserRoleGroup() {
        try {
            String url =  this.url + prefix + "user/listUserRoleGroup";
            String s = HttpUtils.HttpRestClientByObjectParams(url, HttpMethod.POST, null, null,null,MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return null;
            }
            JSONArray jsonArray = result.getJSONArray(Constant.Data);
            List<TUser> tUsers = JSONObject.parseArray(jsonArray.toJSONString(), TUser.class);
            return tUsers;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
 
 
    public boolean deleteRoleByGroupId(List<String> roleIds, String groupId) {
        try {
            String url =  this.url + prefix + "group/delGroupRole";
            Map<String, Object> params = new LinkedHashMap<>();
            params.put("groupId", groupId);
            params.put("roleIdList", roleIds);
            String s = HttpUtils.HttpRestClientByObjectParams(url, HttpMethod.POST, params, null,null,MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return false;
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
 
    public List<String> getUserListByRole(String roleId) {
        String key = RedisClient.getRealRedisKey(roleId + "-getUserListByRole");
        List<Object> redisList = redisClient.getRedisList(key);
        if (redisList != null && !redisList.isEmpty()) {
            List<String> result = new ArrayList<>();
            for (Object o : redisList) {
                result.add(o.toString());
            }
            return result;
        }
        try {
            String url =  this.url + prefix + "userRole/selectUserMap";
            Map<String, Object> params = new LinkedHashMap<>();
            params.put("roleId", roleId);
            String s = HttpUtils.HttpRestClientByObjectParams(url, HttpMethod.POST, params, null,null,MediaType.APPLICATION_JSON);
            JSONObject result = (JSONObject) JSON.parse(s);
            String sucess = result.getString(Constant.Success);
            if (StringUtils.isEmpty(sucess) || !Boolean.valueOf(sucess)) {
                return null;
            }
            List<String> array = result.getObject(Constant.Data, List.class);
 
            redisClient.putRedisList(key,array);
            return array;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}