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 listPerfTask(query: Record<string, any>) {
  let totalSQL = 'select count(*) as total from measure_task where 1=1 ';
  let rowsSQL = 'select * from measure_task where 1=1 ';

  // 查询
  let querySQL = '';
  if (query.neType) {
    querySQL += ` and ne_type like '%${query.neType}%' `;
  }

  // 分页
  const pageNum = (query.pageNum - 1) * query.pageSize;
  const limtSql = ` limit ${pageNum},${query.pageSize} `;

  // 发起请求
  const result = await request({
    url: `/databaseManagement/v1/select/omc_db/measure_task`,
    method: 'get',
    params: {
      totalSQL: totalSQL + querySQL,
      rowsSQL: rowsSQL + querySQL + limtSql,
    },
  });

  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS) {
    const data: DataList = {
      total: 0,
      rows: [],
      code: result.code,
      msg: result.msg,
    };
    result.data.data.forEach((item: any) => {
      const itemData = item['measure_task'];
      if (Array.isArray(itemData)) {
        if (itemData.length === 1 && itemData[0]['total'] >= 0) {
          data.total = itemData[0]['total'];
        } else {
          data.rows = itemData.map(v => parseObjLineToHump(v));
        }
      }
    });
    return data;
  }
  return result;
}

/**
 * 查询任务详细
 * @param id 网元ID
 * @returns object
 */
export async function getTraceTask(id: string | number) {
  // 发起请求
  const result = await request({
    url: `/databaseManagement/v1/select/omc_db/trace_task`,
    method: 'get',
    params: {
      SQL: `select * from trace_task where id = ${id}`,
    },
  });
  // 解析数据
  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_task'][0]),
    });
  }
  return result;
}

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

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

/**
 * 删除任务
 * @param noticeId 网元ID
 * @returns object
 */
export async function delTraceTask(id: string) {
  return request({
    url: `/traceManagement/v1/subscriptions?id=${id}`,
    method: 'delete',
  });
}

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