UserClientUtil.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.fjhx.utils;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.extra.spring.SpringUtil;
  4. import org.springblade.core.tool.api.R;
  5. import org.springblade.core.tool.api.ResultCode;
  6. import org.springblade.system.user.feign.IUserSearchClient;
  7. import java.util.*;
  8. import java.util.function.Function;
  9. import java.util.stream.Collectors;
  10. public class UserClientUtil {
  11. /**
  12. * 用户模块调用
  13. */
  14. private static IUserSearchClient userSearchClient = null;
  15. /**
  16. * 懒汉式单例
  17. */
  18. private synchronized static IUserSearchClient getUserSearchClient() {
  19. if (userSearchClient == null) {
  20. userSearchClient = SpringUtil.getBean(IUserSearchClient.class);
  21. }
  22. return userSearchClient;
  23. }
  24. /**
  25. * 通过userId获取
  26. *
  27. * @param userIdList 用户id列表
  28. * @return MAP<用户id, 用户名称>
  29. */
  30. public static Map<Long, String> getUserNameMap(List<Long> userIdList) {
  31. if (userIdList == null || userIdList.size() == 0) {
  32. return new HashMap<>();
  33. }
  34. R<Map<Long, String>> r = getUserSearchClient().mapByUser(userIdList);
  35. if (r.getCode() == ResultCode.SUCCESS.getCode()) {
  36. return r.getData();
  37. }
  38. return new HashMap<>();
  39. }
  40. /**
  41. * 通过userId获取
  42. *
  43. * @param list 数组
  44. * @param mapper Lambda方法
  45. * @return MAP<用户id, 用户名称>
  46. */
  47. public static <T> Map<Long, String> getUserNameMap(List<T> list, Function<T, Object> mapper) {
  48. List<Long> userIdList = list.stream().map(mapper)
  49. .map(Convert::toLong)
  50. .filter(Objects::nonNull).distinct()
  51. .collect(Collectors.toList());
  52. return getUserNameMap(userIdList);
  53. }
  54. /**
  55. * 赋值用户名称
  56. */
  57. public static void setUserName(List<Map<String, Object>> mapList, String getUserIdStr, String setUserNameStr) {
  58. Map<Long, String> userNameMap = UserClientUtil.getUserNameMap(mapList, item -> item.get(getUserIdStr));
  59. for (Map<String, Object> item : mapList) {
  60. item.put(setUserNameStr, userNameMap.get(Convert.toLong(item.get(getUserIdStr))));
  61. }
  62. }
  63. /**
  64. * 通过用户id获取用户、岗位、部门名称
  65. */
  66. public static Map<Long, Map<String, Object>> getNameByUserId(List<Long> userIdList) {
  67. R<Map<Long, Map<String, Object>>> r = getUserSearchClient().getNameByUserId(userIdList);
  68. if (r.getCode() == ResultCode.SUCCESS.getCode()) {
  69. return r.getData();
  70. }
  71. return new HashMap<>();
  72. }
  73. public static Map<String, Object> getNameByUserId(Long userId) {
  74. Map<Long, Map<String, Object>> userNameAndPostMap = getNameByUserId(Collections.singletonList(userId));
  75. return userNameAndPostMap.get(userId);
  76. }
  77. public static <T> Map<Long, Map<String, Object>> getNameByFunction(List<T> list, Function<T, Object> mapper) {
  78. List<Long> userIdList = list.stream()
  79. .map(mapper).map(Convert::toLong)
  80. .filter(Objects::nonNull).distinct()
  81. .collect(Collectors.toList());
  82. return getNameByUserId(userIdList);
  83. }
  84. }