许鹏程
2023-05-25 213cc37cbf0b2515a4de56cc1e01813211bad183
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package com.product.admin.service;
 
import java.util.*;
 
 
import com.product.common.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.admin.config.CmnConst;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.module.sys.entity.SystemUser;
import com.product.module.sys.service.UserService;
import com.product.util.BaseUtil;
 
/**
 * Copyright © 2020 lx-server
 *
 * @Title: LX-SERVER
 * @Project: base-server-admin
 * @Date: 2020年11月25日
 * @Author: Mr.Xu
 * @Description:更新登录用户信息
 */
@Component
public class UpdateLoginUserInfoService extends AbstractBaseService {
 
    @Autowired
    private BaseDao baseDao;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private RouterService routerService;
 
    @Autowired
    private SystemMenusService systemMenusService;
 
    /**
     * 增加需要更新方法及参数
     *
     * @param callBack 回调方法
     * @param params   回调方法所需参数
     */
    private void put(ThreadUpdateUserInfoService.CallBack callBack, Object... params) {
        ThreadUpdateUserInfoService instance = ThreadUpdateUserInfoService.getInstance();
        instance.add(callBack, params);
        instance.start();
    }
 
 
    public void updateRoleByClient(String client_uuid, boolean sync) {
        if (!StringUtils.isEmpty(client_uuid)) {
            DataTableEntity dataTableEntity = baseDao.listTable("select uuid FROM product_sys_role where org_level_uuid in (select uuid FROM product_sys_org_levels where client_uuid=?)", new Object[]{client_uuid});
            Object[] uuids = dataTableEntity.getUuids();
            updateUserInfoByUpdateRole(uuids, sync);
        }
    }
 
 
    /**
     * 更新用户信息根据用户id
     *
     * @param userId 用户id
     * @param type   更新类型
     * @param sync   是否异步
     */
    public void updateUserInfoByUserId(Object userId, int type, boolean sync) throws BaseException {
        if (sync) {
            put((Object... objects) -> {
                updateUserInfoByUserId(objects[0], Integer.valueOf(objects[1].toString()));
            }, userId, type);
        } else {
            updateUserInfoByUserId(userId, type);
        }
    }
 
    /**
     * 更新用户信息根据用户id
     *
     * @param userId
     * @throws BaseException
     */
    private void updateUserInfoByUserId(Object userId, int type) throws BaseException {
        if (userId == null || BaseUtil.strIsNull(userId.toString())) {
            return;
        }
        if (type < 0 && type > 2) {
            return;
        }
        updateUser(userId, getRefreshType(type));
    }
 
    /**
     * 更新用户信息根据用户id
     *
     * @param userIds
     * @param sync    是否异步
     * @throws BaseException
     */
    public void updateUserInfoByUserId(Object[] userIds, boolean sync) throws BaseException {
        if (userIds == null || userIds.length <= 0) {
            return;
        }
        if (sync) {
            put((Object... objects) -> {
                updateUserInfoByUserId(Arrays.asList(objects[0]).toArray(new Object[]{}));
            }, userIds);
        } else {
            updateUserInfoByUserId(userIds);
        }
    }
 
    /**
     * 更新用户信息根据用户id
     *
     * @param userIds
     * @throws BaseException
     */
    private void updateUserInfoByUserId(Object[] userIds) throws BaseException {
        if (userIds == null || userIds.length <= 0) {
            return;
        }
        for (int i = 0; i < userIds.length; i++) {
            updateUser(userIds[i], CmnConst.UPDATE_USER);
        }
    }
 
    /**
     * 更新用户信息根据岗位uuid
     *
     * @param jobPostUuid 岗位uuid
     * @param sync        是否异步
     * @throws BaseException
     */
    public void updateUserInfoByPostUuid(String jobPostUuid, boolean sync) throws BaseException {
        if (BaseUtil.strIsNull(jobPostUuid)) {
            return;
        }
        if (sync) {
            put((Object... objects) -> {
                updateUserInfoByPostUuid(objects[0].toString());
            }, jobPostUuid);
        } else {
            updateUserInfoByPostUuid(jobPostUuid);
        }
 
    }
 
    /**
     * 更新用户信息根据岗位uuid
     *
     * @param jobPostUuid
     * @throws BaseException
     */
    private void updateUserInfoByPostUuid(String jobPostUuid) throws BaseException {
        if (BaseUtil.strIsNull(jobPostUuid)) {
            return;
        }
        DataTableEntity listTable = baseDao.listTable("select group_concat(user_id) user_ids FROM product_sys_staffs where job_post_uuid = ?", new Object[]{jobPostUuid});
        updateUser(listTable, CmnConst.UPDATE_USER);
 
    }
 
    /**
     * 更新用户信息根据部门uuid
     *
     * @param deptUuid 部门uuid
     * @param sync     是否异步
     * @throws BaseException
     */
    public void updateUserInfoByDeptUuid(Object[] deptUuid, boolean sync) throws BaseException {
        if (deptUuid == null || deptUuid.length <= 0) {
            return;
        }
        if (sync) {
            put((Object... objects) -> {
                updateUserInfoByDeptUuid(Arrays.asList(objects[0]).toArray(new Object[]{}));
            }, deptUuid);
        } else {
            updateUserInfoByDeptUuid(deptUuid);
        }
    }
 
    /**
     * 更新用户信息根据部门uuid
     *
     * @param deptUuid
     * @throws BaseException
     */
    private void updateUserInfoByDeptUuid(Object[] deptUuid) throws BaseException {
        if (deptUuid == null || deptUuid.length <= 0) {
            return;
        }
        String sql = "select group_concat(user_id) user_ids FROM product_sys_staffs where " + BaseUtil.buildQuestionMarkFilter("dept_uuid", deptUuid.length, true);
        DataTableEntity listTable = baseDao.listTable(sql, deptUuid);
        updateUser(listTable, CmnConst.UPDATE_USER);
    }
 
    /**
     * 更新用户信息根据公司uuid
     *
     * @param compnayUuid
     * @param type        0=更新菜单 1=更新用户信息 2=更新菜单、用户信息
     * @param sync        是否异步
     * @throws BaseException
     */
    public void updateUserInfoByOrgLevelUuid(Object[] compnayUuid, int type, boolean sync) throws BaseException {
        if (compnayUuid == null) {
            return;
        }
        if (type < 0 && type > 2) {
            return;
        }
        if (sync) {
            put((Object... objects) -> {
                List<Object> objects1 = Arrays.asList(objects[0]);
                updateUserInfoByOrgLevelUuid(objects1.toArray(new Object[]{}), (int) objects[1]);
            }, compnayUuid, type);
        } else {
            updateUserInfoByOrgLevelUuid(compnayUuid, type);
        }
    }
 
    /**
     * 更新用户信息根据公司uuid
     *
     * @param compnayUuid
     * @param type        0=更新菜单 1=更新用户信息 2=更新菜单、用户信息
     * @throws BaseException
     */
    private void updateUserInfoByOrgLevelUuid(Object[] compnayUuid, int type) throws BaseException {
        if (compnayUuid == null) {
            return;
        }
        if (type < 0 && type > 2) {
            return;
        }
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        List<Object> asList = Arrays.asList(compnayUuid);
        params.addAll(asList);
        params.addAll(asList);
        String filterIn = BaseUtil.buildQuestionMarkFilter("org_level_uuid", compnayUuid.length, true);
        String filterLike = "";
        for (int i = 1; i < compnayUuid.length; i++) {
            filterLike += " or concat(',',org_level_uuid,',') like concat('%',concat(',',?,','),'%')";
        }
        sql.append(" select group_concat(user_id) user_ids from ( ");
        sql.append(" select user_id FROM product_sys_staffs where ");
        sql.append(filterIn);
        sql.append(" union all ");
        sql.append(" select user_id FROM product_sys_org_manager where concat(',',org_level_uuid,',') like concat('%',concat(',',?,','),'%')");
        sql.append(filterLike);
        sql.append(" ) a");
        DataTableEntity listTable = baseDao.listTable(sql.toString(), params.toArray());
        updateUser(listTable, getRefreshType(type));
    }
 
    /**
     * 所属角色下所有用户
     *
     * @param role_uuids
     * @param sync       是否异步
     * @throws BaseException
     */
    public void updateUserInfoByUpdateRole(Object[] role_uuids, boolean sync) throws BaseException {
        if (role_uuids == null || role_uuids.length <= 0) {
            return;
        }
        if (sync) {
            put((Object... objects) -> {
                updateUserInfoByUpdateRole(Arrays.asList(objects).toArray(new Object[]{}));
            }, role_uuids);
        } else {
            updateUserInfoByUpdateRole(role_uuids);
        }
    }
 
    /**
     * 所属角色下所有用户
     *
     * @param role_uuids
     * @throws BaseException
     */
    private void updateUserInfoByUpdateRole(Object[] role_uuids) throws BaseException {
        if (role_uuids == null || role_uuids.length <= 0) {
            return;
        }
        String[] roles = Arrays.asList(role_uuids).toArray(new String[]{});
        routerService.updateRouter(roles);
        systemMenusService.refreshMenuByRole(roles);
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        List<Object> asList = Arrays.asList(role_uuids);
        params.addAll(asList);
        params.addAll(asList);
        String filter = "";
        for (int i = 1; i < role_uuids.length; i++) {
            filter += " or concat(',',role_uuids,',') like concat('%',concat(',',?,','),'%')";
        }
        sql.append(" select group_concat(user_id) user_ids from ( ");
        sql.append(
                " select user_id FROM product_sys_staffs where concat(',',role_uuids,',') like concat('%',concat(',',?,','),'%') ");
        sql.append(filter);
        sql.append(" union all ");
        sql.append(
                " select user_id FROM product_sys_org_manager where concat(',',role_uuids,',') like concat('%',concat(',',?,','),'%') ");
        sql.append(filter);
        sql.append(" ) a ");
        DataTableEntity listTable = baseDao.listTable(sql.toString(), params.toArray());
        updateUser(listTable, CmnConst.UPDATE_MENU);
    }
 
    /**
     * 更新客户下的所有用户
     *
     * @param clientUuid 客户uuid
     * @param type       更新类型
     * @param sync       是否异步
     * @throws BaseException
     */
    public void updateUserInfoByUpdateClient(String clientUuid, int type, boolean sync) throws BaseException {
        if (BaseUtil.strIsNull(clientUuid)) {
            return;
        }
        if (type < 0 && type > 2) {
            return;
        }
        if (sync) {
            put((Object... objects) -> {
                updateUserInfoByUpdateClient((String) objects[0], (int) objects[1]);
            }, clientUuid, type);
        } else {
            updateUserInfoByUpdateClient(clientUuid, type);
        }
    }
 
    /**
     * 更新客户下的所有用户
     *
     * @param clientUuid
     * @throws BaseException
     */
    private void updateUserInfoByUpdateClient(String clientUuid, int type) throws BaseException {
        if (BaseUtil.strIsNull(clientUuid)) {
            return;
        }
        if (type < 0 && type > 2) {
            return;
        }
        String sql = "select GROUP_CONCAT(user_id) user_ids from product_sys_staffs where org_level_uuid in (select uuid FROM product_sys_org_levels where client_uuid=?)";
        DataTableEntity listTable = baseDao.listTable(sql, new Object[]{clientUuid});
        updateUser(listTable, getRefreshType(type));
    }
 
    public String getRefreshType(int j) {
        if (j == 0) {
            return CmnConst.UPDATE_MENU;
        }
        if (j == 1) {
            return CmnConst.UPDATE_USER;
        }
        if (j == 2) {
            return CmnConst.UPDATE_MENU_USER;
        }
        return null;
    }
 
    /**
     * 创建管理员绑定员工
     *
     * @param userId
     * @throws BaseException
     */
    public void updateUserInfoByManagerStaffBindClient(String userId, boolean sync) throws BaseException {
        if (sync) {
            put((Object... objects) -> {
                updateUser(objects[0], CmnConst.UPDATE_MENU_USER);
            }, userId);
        } else {
            updateUser(userId, CmnConst.UPDATE_MENU_USER);
        }
    }
 
    /**
     * 创建管理员绑定员工
     *
     * @param userId
     * @throws BaseException
     */
    private void updateUserInfoByManagerStaffBindClient(String userId) throws BaseException {
        updateUser(userId, CmnConst.UPDATE_MENU_USER);
    }
 
    /**
     * 更新user信息 根据userid
     *
     * @param UserId 用户id
     * @param tag    更新类型
     * @throws BaseException
     */
    private void updateUser(Object UserId, String tag) throws BaseException {
        if (UserId == null) {
            return;
        }
        SystemUser systemUser = DataPoolCacheImpl.getInstance().getSessionUser(String.valueOf(UserId));
        if (systemUser != null && systemUser.getToken_info() != null) {
            try {
                String updateType = systemUser.getUpdateType();
                if (!StringUtils.isEmpty(updateType)) {
                    if (updateType.equals(tag)) {
                        return;
                    } else if (updateType.equals(getRefreshType(2))) {
                        return;
                    } else {
                        tag = getRefreshType(2);
                    }
                }
                SystemUser user = userService.getSystemUserByName(systemUser.getUser_account());
                String org_level_uuid = systemUser.getOrg_level_uuid();
                if (!StringUtils.isEmpty(org_level_uuid) && !org_level_uuid.equals(user.getOrg_level_uuid())) {
                    user.changeCompany(org_level_uuid);
                }
                user.setToken_info(systemUser.getToken_info());
                user.setClientType(systemUser.getClientType());
                user.setVersion(systemUser.getVersion());
                user.setVersionUuid(systemUser.getVersionUuid());
                user.setLanguageCode(user.getDefault_language());
                user.setRefreshToken(systemUser.getRefreshToken());
                user.setUpdateType(tag);
                //token与用户关联,放入缓存中
                DataPoolCacheImpl.getInstance().updateSessionUser(user);
            } catch (BaseException e) {
                e.printStackTrace();
                throw e;
            }
        }
    }
 
    /**
     * 更新user信息
     *
     * @param dt  用户信息集合  key = user_ids
     * @param tag 更新类型
     * @throws BaseException
     */
    private void updateUser(DataTableEntity dt, String tag) throws BaseException {
        String userId = null;
        if (BaseUtil.dataTableIsEmpty(dt)) {
            return;
        }
        if (BaseUtil.strIsNull(tag)) {
            return;
        }
        FieldSetEntity fs = dt.getData().get(0);
        if (fs != null) {
            userId = fs.getString("user_ids");
        }
        if (StringUtils.isEmpty(userId)) {
            return;
        }
        String[] userIds = userId.split(",");
        Set<String> set = new HashSet<>();
        set.addAll(Arrays.asList(userIds));
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            updateUser(next, tag);
        }
    }
 
}