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(Collection<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);
    }

    /**
     * 通过userId获取userName
     *
     * @param userId 用户id
     * @return 用户名
     */
    public static String getUserName(Long userId) {
        return getUserNameMap(Collections.singletonList(userId)).get(userId);
    }

    /**
     * 赋值用户名称
     */
    public static void setUserName(List<Map<String, Object>> mapList, String getUserIdStr, String setUserNameStr) {
        if (mapList.size() == 0) return;
        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获取用户(userName)、岗位(postName)、部门(deptName)名称
     */
    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);
    }

}
