import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { request } from '@/plugins/http-fetch';
import { parseObjLineToHump } from '@/utils/parse-utils';

/**
 * 查询跟踪任务列表
 * @param query 查询参数
 * @returns object
 */
export async function listTraceTask(query: Record<string, any>) {
  return request({
    url: '/trace/task/list',
    method: 'get',
    params: query,
  });
}

/**
 * 查询跟踪任务信息
 * @param id 网元ID
 * @returns object
 */
export async function getTraceTask(id: string | number) {
  return request({
    url: `/trace/task/${id}`,
    method: 'get',
  });
}

/**
 * 新增任务
 * @param data 网元对象
 * @returns object
 */
export function addTraceTask(data: Record<string, any>) {
  return request({
    url: `/trace/task`,
    method: 'post',
    data: data,
  });
}

/**
 * 修改任务
 * @param data 网元对象
 * @returns object
 */
export function updateTraceTask(data: Record<string, any>) {
  return request({
    url: `/trace/task`,
    method: 'put',
    data: data,
  });
}

/**
 * 跟踪任务删除
 * @param ids ID多个逗号分隔
 * @returns object
 */
export async function delTraceTask(ids: string) {
  return request({
    url: `/trace/task/${ids}`,
    method: 'delete',
  });
}

/**
 * 跟踪任务文件
 * @param query 对象
 * @returns object
 */
export function filePullTask(traceId: string) {
  return request({
    url: '/trace/task/filePull',
    method: 'get',
    params: { traceId },
    responseType: 'blob',
    timeout: 60_000,
  });
}

/**
 * 获取网元跟踪接口列表
 * @returns object
 */
export async function getNeTraceInterfaceAll() {
  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/elementType/omc_db/objectType/ne_info`,
    method: 'get',
    params: {
      SQL: `SELECT ne_type,interface FROM trace_info GROUP BY ne_type,interface`,
    },
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && Array.isArray(result.data.data)) {
    let data = result.data.data[0];
    return Object.assign(result, {
      data: parseObjLineToHump(data['trace_info']),
    });
  }
  return result;
}
