/*
 * @Author: tengmingxue 1473375109@qq.com
 * @Date: 2023-08-15 11:20:48
 * @LastEditors: tengmingxue 1473375109@qq.com
 * @LastEditTime: 2023-08-21 17:13:06
 * @FilePath: \xld-gis-admin\src\api\resource\map.ts
 * @Description: 地图资源接口
 */
import { defHttp } from '/@/utils/http/axios';
import { session } from "/@/utils/Memory.js";
import type { ErrorMessageMode } from '/#/axios';

enum Api {
    Login = '/callProvider'
}

const client = {
    grant_type: "password",
    scope: "all",
    client_id: "space",
    client_secret: 'e10adc3949ba59abbe56e057f20f883e',
}

const interfaceType = { apiUrl2: true };

/**
 * @description:获取地图资源列表
 */
export function list() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "getResourcePagedList");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '{"type":"MR","fields":{"DATA":["serviceid","servicename","servicetype","servicealiasname"],"METADATA":["publisher ","publishdate"]},"pageIndex":1,"pageSize":10 }');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? {} : JSON.parse(r.result)
                if(result?.items){
                    result.items.forEach((item,index) => {
                        item['xh'] = index + 1
                    })
                }
                resolve(result);
            })
    })
}


/**
 * @description:获取资源类型字典
 */
 export function getServiceTypes() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.SServicebaseProvider");
    formData.append("methodName", "getSServiceTypes");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '{"abbreviation":"MR","pid":"MapResource"}');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:获取资源标签
 */
 export function queryServiceTags() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.tags.TagsProvider");
    formData.append("methodName", "getAllByType");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '业务类型,服务专题,数据类型,年度');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:根据标签获取字典
 */
 export function queryDicsByName(name) {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.tags.TagsProvider");
    formData.append("methodName", "getAllByType");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", name);
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:获取坐标系列表
 */
 export function queryCoors() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.geomname.GeomNameCDProvider");
    formData.append("methodName", "getGeomNameList");
    formData.append("args[]", session.getItem('tokenV2'));
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}

/**
 * @description:新增地图资源
 */
 export function insertService(params) {
    if(!params) return
    const str = JSON.stringify(params)
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "insert");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]",str)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}


/**
 * @description:修改地图资源
 */
 export function updateService(params) {
    if(!params) return
    const str = JSON.stringify(params)
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "update");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]",str)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}


/**
 * @description:删除地图资源
 */
 export function deleteService(param) {
    if(!param) return
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "delete");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]",param)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.Login, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}

