package com.bonus.material.purchase.config;

import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.system.api.RemoteDictDataService;
import com.bonus.system.api.RemoteUserService;
import com.bonus.system.api.domain.SysDictData;
import com.bonus.system.api.domain.SysUser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author ma_sh
 * @create 2024/10/23 19:06
 */
@Component
@Slf4j
public class RemoteConfig {

    @Resource
    private RemoteDictDataService remoteDictDataService;

    @Resource
    private RemoteUserService remoteUserService;

    /**
     * 获取字典值
     * @param dictType
     * @return
     */
    public Map<String, String> getDictValue(String dictType) {
        if (StringUtils.isBlank(dictType)) {
            return Collections.emptyMap();
        }
        try {
            Map<String, String> dictMap = new LinkedHashMap<>();
            AjaxResult ajaxResult = remoteDictDataService.dictType(dictType, SecurityConstants.INNER);
            if (ajaxResult.isSuccess()) {
                // 假设 ajaxResult.get("data") 返回的是 List<LinkedHashMap>
                List<LinkedHashMap> rawData = (List<LinkedHashMap>) ajaxResult.get("data");
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                // 将 rawData 转换为 SysDictData 列表
                List<SysDictData> dataList = rawData.stream()
                        .map(rawDatum -> objectMapper.convertValue(rawDatum, SysDictData.class))
                        .collect(Collectors.toList());
                // 使用 Map 存储字典数据以提高查找速度
                dictMap = dataList.stream()
                        .collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
            }
            return dictMap;
        } catch (Exception e) {
            log.error("远程调用查询字典键值失败：", e.getMessage());
        }
        return Collections.emptyMap();
    }

    /**
     * 获取用户名
     * @param userIds
     * @return
     */
    public Map<Long, String> getUserName(Long[] userIds) {
        if (userIds == null || userIds.length == 0) {
            return Collections.emptyMap();
        }
        try {
            AjaxResult ajaxResult = remoteUserService.getUsers(userIds, SecurityConstants.INNER);
            // 假设 ajaxResult.get("data") 返回的是 List<LinkedHashMap>
            List<LinkedHashMap> rawData = (List<LinkedHashMap>) ajaxResult.get("data");
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            List<SysUser> dataList = rawData.stream()
                    .map(rawDatum -> objectMapper.convertValue(rawDatum, SysUser.class))
                    .collect(Collectors.toList());
            // 创建一个以 createBy 为键，nickName 为值的映射
            Map<Long, String> nickNameMap = dataList.stream()
                    .collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName));
            return nickNameMap;
        } catch (Exception e) {
            log.error("远程调用查询用户名失败：", e.getMessage());
        }
        return Collections.emptyMap();
    }
}

