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 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 params = new ArrayList<>(); List 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 params = new ArrayList<>(); List 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 set = new HashSet<>(); set.addAll(Arrays.asList(userIds)); Iterator iterator = set.iterator(); while (iterator.hasNext()) { String next = iterator.next(); updateUser(next, tag); } } }