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

/**
 * 查询任务列表
 * @param query 查询参数
 * @returns object
 */
export async function listPerfThreshold(query: Record<string, any>) {
  let totalSQL = 'select count(*) as total from measure_threshold where 1=1 ';
  let rowsSQL = 'select * from measure_threshold where 1=1 ';

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

  // 排序
  let sortSql = ' order by create_time ';
  if (query.sortOrder === 'asc') {
    sortSql += ' asc ';
  } else {
    sortSql += ' desc ';
  }
  // 分页
  const pageNum = (query.pageNum - 1) * query.pageSize;
  const limtSql = ` limit ${pageNum},${query.pageSize} `;

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

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

/**
 * 查询性能门限详细
 * @param id 网元ID
 * @returns object
 */
export async function getPerfThre(id: string | number) {
  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/measure_threshold`,
    method: 'GET',
    params: {
      SQL: `select * from measure_threshold 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['measure_threshold'][0]),
    });
  }
  return result;
}

/**
 * 新增性能门限
 * @param data 网元对象
 * @returns object
 */
export function addPerfThre(data: Record<string, any>) {
  let obj: any = {
    ne_type: data.neType,
    kpi_set: data.kpiSet,
    status: 'Inactive',
    orig_severity: data.origSeverity,
    threshold: data.threshold,
  };

  return request({
    url: `/api/rest/databaseManagement/v1/omc_db/measure_threshold`,
    method: 'POST',
    data: { measure_threshold: [obj] },
  });
}

/**
 * 修改性能测量任务
 * @param data 网元对象
 * @returns object
 */
export function updatePerfThre(data: Record<string, any>) {
  let obj: any = {
    ne_type: data.neType,
    kpi_set: data.kpiSet,
    status: 'Inactive',
    orig_severity: data.origSeverity,
    threshold: ''+data.threshold,
  };
  return request({
    url: `/api/rest/databaseManagement/v1/omc_db/measure_threshold?WHERE=id=${data.id}`,
    method: 'PUT',
    data: { measure_threshold: obj },
  });
}

/**
 * 删除性能门限
 * @param noticeId 网元ID
 * @returns object
 */
export async function delPerfThre(data: Record<string, any>) {
  return request({
    url: `/api/rest/databaseManagement/v1/omc_db/measure_threshold?WHERE=id=${data.id}`,
    method: 'DELETE',
  });
}

/**
 * 获取网元跟踪接口列表
 * @returns object
 */
export async function getNePerformanceList() {
  // 发起请求
  const result = await request({
    url: `/api/rest/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;
}

/**
 * 激活任务
 * @param
 * @returns bolb
 */
export function threRun(data: Record<string, any>) {
  return request({
    url: `/api/rest/databaseManagement/v1/omc_db/measure_threshold?WHERE=id=${data.id}`,
    method: 'PUT',
    data: { data: { status: 'Active' } },
  });
}

/**
 * 挂起任务
 * @param
 * @returns bolb
 */
export function threStop(data: Record<string, any>) {
  return request({
    url: `/api/rest/databaseManagement/v1/omc_db/measure_threshold?WHERE=id=${data.id}`,
    method: 'PUT',
    data: { data: { status: 'Inactive' } },
  });
}
