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 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 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 getUserGroupByUserId(String userId) { String key = RedisClient.getRealRedisKey(userId + "-getUserGroupByUserId"); List redisList = redisClient.getRedisList(key); if (redisList != null && !redisList.isEmpty()) { List 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 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 tUserGroups = JSONArray.parseArray(groupArray.toJSONString(), TUserGroup.class); if (tUserGroups == null || tUserGroups.isEmpty()) { return null; } redisClient.putRedisList(key, (tUserGroups)); return tUserGroups; } } public List getTRoleListByUserId(String userId) { String key = RedisClient.getRealRedisKey(userId + "-getTRoleListByUserId"); List redisList = redisClient.getRedisList(key); if (redisList != null && !redisList.isEmpty()) { List 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 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 tRoles = JSONArray.parseArray(groupArray.toJSONString(), TRole.class); if (tRoles == null || tRoles.isEmpty()) { return null; } redisClient.putRedisList(key, tRoles); return tRoles; } } public List getRoleListByGroupId(String groupId) { String key = RedisClient.getRealRedisKey(groupId + "-getTRoleListByUserId"); List redisList = redisClient.getRedisList(key); if (redisList != null && !redisList.isEmpty()) { List 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 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 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 roleIds, String userName) { try { String url = this.url + prefix + "userRole/addRoleUser"; Map 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 roleIds) { try { String url = this.url + prefix + "userRole/deleteRoleUsers"; Map 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 roleIds, String groupId) { try { String url = this.url + prefix + "group/addGroupRole"; HashMap headerMap = new HashMap<>(); headerMap.put("userId", userId); Map 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 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 tUsers = JSONObject.parseArray(jsonArray.toJSONString(), TUser.class); return tUsers; }catch (Exception e){ e.printStackTrace(); return null; } } public boolean deleteRoleByGroupId(List roleIds, String groupId) { try { String url = this.url + prefix + "group/delGroupRole"; Map 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 getUserListByRole(String roleId) { String key = RedisClient.getRealRedisKey(roleId + "-getUserListByRole"); List redisList = redisClient.getRedisList(key); if (redisList != null && !redisList.isEmpty()) { List result = new ArrayList<>(); for (Object o : redisList) { result.add(o.toString()); } return result; } try { String url = this.url + prefix + "userRole/selectUserMap"; Map 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 array = result.getObject(Constant.Data, List.class); redisClient.putRedisList(key,array); return array; }catch (Exception e){ e.printStackTrace(); return null; } } }