package com.fjhx.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.fjhx.entity.FileInfo;
import com.fjhx.feign.IFileClient;
import org.springblade.core.tool.api.R;

import java.util.*;
import java.util.stream.Collectors;

public class FileClientUtil {

    // app名称
    private static final String APPLICATION_NAME = SpringUtil.getApplicationName();
    // fileClient
    private static final IFileClient fileClient = SpringUtil.getBean(IFileClient.class);
    // 默认文件业务类型
    private static final Integer DEFAULT_TYPE = 1;

    /**
     * 文件绑定业务id
     *
     * @param businessId   业务id
     * @param businessType 业务类型
     * @param paramList    文件信息
     */
    public static void bindingFile(Long businessId, Integer businessType, List<FileInfo> paramList) {
        if (ObjectUtil.isEmpty(paramList)) return;
        R result = fileClient.bindingFile(APPLICATION_NAME, businessId, businessType, paramList);
        Assert.eqTrue(result.isSuccess(), "保存文件信息失败");
    }

    public static void bindingFile(Long businessId, List<FileInfo> paramList) {
        bindingFile(businessId, DEFAULT_TYPE, paramList);
    }

    public static void bindingFile(Long businessId, Integer businessType, FileInfo param) {
        if (ObjectUtil.isEmpty(param)) return;
        R result = fileClient.bindingFile(APPLICATION_NAME, businessId, businessType, Collections.singletonList(param));
        Assert.eqTrue(result.isSuccess(), "保存文件信息失败");
    }

    public static void bindingFile(Long businessId, FileInfo param) {
        bindingFile(businessId, DEFAULT_TYPE, param);
    }

    /**
     * 修改文件绑定信息
     *
     * @param businessId   业务id
     * @param businessType 业务类型
     * @param paramList    文件信息
     */
    public static void againBindingFile(Long businessId, Integer businessType, List<FileInfo> paramList) {
        if (ObjectUtil.isEmpty(paramList)) return;
        R result = fileClient.againBindingFile(APPLICATION_NAME, businessId, businessType, paramList);
        Assert.eqTrue(result.isSuccess(), "保存文件信息失败");
    }

    public static void againBindingFile(Long businessId, List<FileInfo> paramList) {
        againBindingFile(businessId, DEFAULT_TYPE, paramList);
    }

    public static void againBindingFile(Long businessId, Integer businessType, FileInfo param) {
        if (ObjectUtil.isEmpty(param)) return;
        R result = fileClient.againBindingFile(APPLICATION_NAME, businessId, businessType, Collections.singletonList(param));
        Assert.eqTrue(result.isSuccess(), "保存文件信息失败");
    }

    public static void againBindingFile(Long businessId, FileInfo param) {
        againBindingFile(businessId, DEFAULT_TYPE, param);
    }


    /**
     * 删除文件绑定信息
     *
     * @param businessId 业务id
     */
    public static void relieveBindingFile(Long businessId) {
        R result = fileClient.relieveBindingFile(businessId);
        Assert.eqTrue(result.isSuccess(), "文件删除失败");
    }

    /**
     * 获取文件信息
     */
    public static FileInfo getFileInfo(Long businessId) {
        R<List<FileInfo>> result = fileClient.getFileInfo(Collections.singletonList(businessId));
        List<FileInfo> data = result.getData();
        return data.size() == 0 ? null : data.get(0);
    }

    public static List<FileInfo> getFileInfoList(Long businessId) {
        R<List<FileInfo>> result = fileClient.getFileInfo(Collections.singletonList(businessId));
        return result.getData();
    }

    public static Map<Long, FileInfo> getFileInfoMap(List<Long> businessId) {
        if (ObjectUtil.isEmpty(businessId)) {
            return new HashMap<>();
        }
        R<List<FileInfo>> result = fileClient.getFileInfo(businessId);
        return result.getData().stream().collect(Collectors.toMap(
                FileInfo::getBusinessId,
                item -> item,
                (v1, v2) -> v1
        ));
    }

    /**
     * 获取附件信息
     */
    public static Map<Long, List<FileInfo>> getFileInfoListMap(List<Long> businessId) {
        R<List<FileInfo>> result = fileClient.getFileInfo(businessId);
        List<FileInfo> fileInfoList = Assert.result(result);
        return fileInfoList.stream().collect(Collectors.groupingBy(FileInfo::getBusinessId));
    }

    /**
     * 赋值附件
     */
    public static void setFileInfoListMap(List<Map<String, Object>> list, String businessIdName, String fileInfoListName) {
        List<Long> businessIdList = list.stream().map(item -> Convert.toLong(item.get(businessIdName)))
                .filter(Objects::nonNull).collect(Collectors.toList());

        if (businessIdList.size() == 0) {
            return;
        }

        Map<Long, List<FileInfo>> fileInfoListMap = getFileInfoListMap(businessIdList);

        for (Map<String, Object> stringMap : list) {
            List<FileInfo> fileInfoList = fileInfoListMap.get(Convert.toLong(stringMap.get(businessIdName)));
            if (ObjectUtil.isNotEmpty(fileInfoList)) {
                stringMap.put(fileInfoListName, fileInfoList);
            } else {
                stringMap.put(fileInfoListName, new ArrayList<>());
            }
        }
    }

    /**
     * 赋值附件
     */
    public static void setFileInfoListMap(List<Map<String, Object>> list) {
        setFileInfoListMap(list, "id", "fileInfoList");
    }


}
