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 listAct(query: Record<string, any>) {
  return await request({
    url: `/neData/alarm/log/event`,
    method: 'GET',
    params: query,
  });
}

/**
 * 事件告警导出
 * @param query 查询参数
 * @returns bolb
 */
export async function exportAll(query: Record<string, any>) {
  let rowsSQL = `select * from alarm_event where 1=1`;
  // 查询
  let querySQL = '';
  querySQL += query.alarm_code
    ? ` and alarm_code = '${query.alarm_code}' `
    : '';
  querySQL += query.alarm_type
    ? ` and alarm_type = '${query.alarm_type}' `
    : '';
  querySQL += query.pv_flag ? ` and pv_flag = '${query.pv_flag}' ` : '';
  querySQL += query.orig_severity
    ? ` and orig_severity in('${query.orig_severity}' )`
    : '';
  querySQL += query.ne_id ? ` and ne_id like '%${query.ne_id}%' ` : '';
  querySQL += query.ne_name ? ` and ne_name like '%${query.ne_name}%' ` : '';
  querySQL += query.ne_type ? ` and ne_type like '%${query.ne_type}%' ` : '';
  querySQL +=
    query.beginTime && query.endTime
      ? ` and event_time BETWEEN '${query.beginTime}' and ' ${query.endTime}'`
      : '';

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

  if (result.code === RESULT_CODE_SUCCESS) {
    let v = result.data.data[0];
    const vArr = parseObjLineToHump(v['alarm_event']);
    result.data = vArr == null ? [] : vArr;
  }
  return result;
}
