1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package com.fjhx.utils;
- import cn.hutool.core.convert.Convert;
- import cn.hutool.extra.spring.SpringUtil;
- import org.springblade.core.tool.api.R;
- import org.springblade.core.tool.api.ResultCode;
- import org.springblade.system.user.feign.IUserSearchClient;
- import java.util.*;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- public class UserClientUtil {
- /**
- * 用户模块调用
- */
- private static IUserSearchClient userSearchClient = null;
- /**
- * 懒汉式单例
- */
- private synchronized static IUserSearchClient getUserSearchClient() {
- if (userSearchClient == null) {
- userSearchClient = SpringUtil.getBean(IUserSearchClient.class);
- }
- return userSearchClient;
- }
- /**
- * 通过userId获取
- *
- * @param userIdList 用户id列表
- * @return MAP<用户id, 用户名称>
- */
- public static Map<Long, String> getUserNameMap(List<Long> userIdList) {
- if (userIdList == null || userIdList.size() == 0) {
- return new HashMap<>();
- }
- R<Map<Long, String>> r = getUserSearchClient().mapByUser(userIdList);
- if (r.getCode() == ResultCode.SUCCESS.getCode()) {
- return r.getData();
- }
- return new HashMap<>();
- }
- /**
- * 通过userId获取
- *
- * @param list 数组
- * @param mapper Lambda方法
- * @return MAP<用户id, 用户名称>
- */
- public static <T> Map<Long, String> getUserNameMap(List<T> list, Function<T, Object> mapper) {
- List<Long> userIdList = list.stream().map(mapper)
- .map(Convert::toLong)
- .filter(Objects::nonNull).distinct()
- .collect(Collectors.toList());
- return getUserNameMap(userIdList);
- }
- /**
- * 赋值用户名称
- */
- public static void setUserName(List<Map<String, Object>> mapList, String getUserIdStr, String setUserNameStr) {
- Map<Long, String> userNameMap = UserClientUtil.getUserNameMap(mapList, item -> item.get(getUserIdStr));
- for (Map<String, Object> item : mapList) {
- item.put(setUserNameStr, userNameMap.get(Convert.toLong(item.get(getUserIdStr))));
- }
- }
- /**
- * 通过用户id获取用户、岗位、部门名称
- */
- public static Map<Long, Map<String, Object>> getNameByUserId(List<Long> userIdList) {
- R<Map<Long, Map<String, Object>>> r = getUserSearchClient().getNameByUserId(userIdList);
- if (r.getCode() == ResultCode.SUCCESS.getCode()) {
- return r.getData();
- }
- return new HashMap<>();
- }
- public static Map<String, Object> getNameByUserId(Long userId) {
- Map<Long, Map<String, Object>> userNameAndPostMap = getNameByUserId(Collections.singletonList(userId));
- return userNameAndPostMap.get(userId);
- }
- public static <T> Map<Long, Map<String, Object>> getNameByFunction(List<T> list, Function<T, Object> mapper) {
- List<Long> userIdList = list.stream()
- .map(mapper).map(Convert::toLong)
- .filter(Objects::nonNull).distinct()
- .collect(Collectors.toList());
- return getNameByUserId(userIdList);
- }
- }
|