
import { defHttp } from '/@/utils/http/axios';
import { session } from "/@/utils/Memory.js";
import type { ErrorMessageMode } from '/#/axios';
const interfaceType = { apiUrl2: true };


enum Api {
    callProvider = "/callProvider",
    List = '/base-center/system/selectSystemPowerInterface',
    GetGroup = '/interface-center/interface/getGroup',
    GetGroupId = '/interface-center/interface/getInterfacesAndPowerByUserId',
    GetInterfaceDetail = '/interface-center/interface/getInterfaceDetail',
    GetInterfaces = '/interface-center/interface/getInterfaces',
    //数据监控接口
    //（1）大数据平台告警信息
    BigDataMonitor = '/interface-center/monitor/bigData/events',
    //(2) 大数据平台hdfs容量信息
    HdfsCapacity = '/interface-center/monitor/bigData/hdfsCapacity',
    //(3) 获取接口api总数,当月调用次数,总调用次数
    GetApiData = '/interface-center/monitor/getApiData',
    //(4) 获取接口近三十天每日的调用次数
    GetApiUseCountDays = '/interface-center/monitor/getApiUseCountDays',
    //(5) 获取接口近半年每月的调用次数
    GetApiUseCountMouths = '/interface-center/monitor/getApiUseCountMouths',
    //(6) 各分析统计
    BusinessStatistics = '/xldanalysis-center/xldanalyst/BusinessStatistics',

    //(7) 分类统计
    GetInterfacesTotalNum = '/interface-center/interface/getInterfacesTotalNum',
    //(8) 获取地图服务,文件数据,数据库数据,工具数据个数
    GetSpaceData = '/interface-center/monitor/getSpaceData',
    //(9) 统计文件个数和总文件大小
    GetDataStoreGroupByStatistics = '/interface-center/monitor/getDataStoreGroupByStatistics'

}

/**
 * @description:获取应用列表
 */
export function interfaceList(params) {
    var e = {
        keyStr: params.keyword || "",
        page: params.page || 1,
        rows: params.rows || 1000,
        systemKey: "app",
        isgetPowered: false
    }
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.List, params: e })
            .then((r) => {
                if (r.resp_code == 0 && r.datas) {
                    resolve(r.datas);
                } else {
                    resolve([]);
                }
            })
    })
}


/**
 * @description:获取接口列表的分组
 */
export function getGroup() {
    return new Promise<void>((resolve, reject) => {
        defHttp.get({ url: Api.GetGroup })
            .then((r) => {
                if (r.resp_code == 0 && r.datas && r.datas.length) {
                    resolve(r.datas);
                } else {
                    resolve([]);
                }
            })
    })
}


/**
 * @description:通过组id获取列表
 */
export function getGroupId(e) {
    //?groupId=58c725b0-9620-45ea-a517-3d57d3ccf0e9&name=&pageNum=1&pageSize=6
    var params = {
        groupId: e.groupId,
        pageSize: e.rows,
        pageNum: e.page,
    }
    return new Promise<void>((resolve, reject) => {
        defHttp.get({ url: Api.GetGroupId, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas) {
                    console.log("获取接口资源列表:", r.datas);
                    resolve(r.datas);
                } else {
                    resolve([]);
                }
            })
    })
}

/**
 * @description:通过组id获取列表
 */
export function getInterfaceDetail(id: string) {
    var params = {
        interfaceId: id
    }
    return new Promise<void>((resolve) => {
        defHttp.get({ url: Api.GetInterfaceDetail, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas) {
                    resolve(r.datas);
                } else {
                    resolve({});
                }
            })
    })
}

/**
 * @description:获取所有接口
 */
export function getAllInterface(e) {
    var params = {
        groupId: e.groupId || null,
        name: e.name || null,
        pageSize: 100000,
        pageNum: 1,
    }
    return new Promise<void>((resolve) => {
        defHttp.get({ url: Api.GetInterfaces, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas && r.datas.length) {
                    resolve(r.datas);
                } else {
                    resolve();
                }
            })
    })
}


/**
 * @description:各分析统计
 *  @param:
 *     startTime  开始时间     2022-01-29
 *     endTime    结束时间     2022-09-29
*/
export function queryBusinessStatistics(params) {
    return new Promise<void>((resolve) => {
        defHttp.post({ url: Api.BusinessStatistics, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas && r.datas.length) {
                    resolve(r.datas);
                } else {
                    resolve();
                }
            })
    })
}


/**
 * @description:获取接口近三十天每日的调用次数
*/
export function queryApiUseCountDays(params) {
    return new Promise<void>((resolve) => {
        defHttp.get({ url: Api.GetApiUseCountDays, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas && r.datas) {
                    resolve(r.datas);
                } else {
                    resolve();
                }
            })
    })
}

/**
 * @description:获取接口近半年每月的调用次数
*/
export function queryApiUseCountMouths(params) {
    return new Promise<void>((resolve) => {
        defHttp.get({ url: Api.GetApiUseCountMouths, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas && r.datas) {
                    resolve(r.datas);
                } else {
                    resolve();
                }
            })
    })
}



/**
 * @description:数据资源分类统计
 */
export function StatisticDataResource() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "getStatistics");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", 'false');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.callProvider, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? {} : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:查询接口总数
 *  @param:
*/
export function queryInterfacesTotalNum(params) {
    return new Promise<void>((resolve) => {
        defHttp.get({ url: Api.GetInterfacesTotalNum, params })
            .then((r) => {
                if (r.resp_code === 0 && r.datas) {
                    resolve(r.datas);
                } else {
                    resolve(0);
                }
            })
    })
}

/**
 * @description:获取地图服务,文件数据,数据库数据,工具数据个数
 *  @param:
*/
export function querySpaceData(params) {
    return new Promise<void>((resolve) => {
        defHttp.get({ url: Api.GetSpaceData, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas && r.datas.length) {
                    resolve(r.datas);
                } else {
                    resolve();
                }
            })
    })
}

/**
 * @description:统计文件个数和总文件大小
 *  @param:
*/
export function DataStoreGroupByStatistics(params) {
    return new Promise<void>((resolve) => {
        defHttp.get({ url: Api.GetDataStoreGroupByStatistics, params })
            .then((r) => {
                if (r.resp_code == 0 && r.datas && r.datas.length) {
                    resolve(r.datas);
                } else {
                    resolve();
                }
            })
    })
}


