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

/**
 * 获取活动告警数
 * @returns object
 */
export async function getActiveAlarmTotal() {
  // 发起请求
  const result = await request({
    url: `/neData/alarm/list`,
    method: 'GET',
    params: {
      alarmStatus: '1',
      sortField: 'event_time',
      sortOrder: 'desc',
      pageNum: 1,
      pageSize: 1,
    },
  });

  return {
    code: result.code,
    msg: result.msg,
    data: result.data.total ?? 0,
  };
}

/**
 * 查询列表
 * @param query 查询参数
 * @returns object
 */
export async function listAct(query: Record<string, any>) {
  return await request({
    url: `/neData/alarm/list`,
    method: 'GET',
    params: query,
  });
}

/**
 * 确认告警信息
 * @param ids 记录ID
 * @param state 状态 true确认 false取消确认
 * @returns object
 */
export function ackAlarm(ids: number[], state: boolean) {
  return request({
    url: `/neData/alarm/ack'`,
    method: 'PUT',
    data: {
      ids: ids,
      ackState: state,
    },
  });
}

/**
 * 设置告警显示过滤
 * @param data 鉴权对象
 * @returns object
 */
export function showPass(data: Record<string, any>) {
  let querySQL = '';
  querySQL += data.alarm_code ? ` and alarm_code != ${data.alarm_code} ` : '';
  querySQL += data.alarm_type
    ? ` and alarm_type !=\\'${data.alarm_type}\\'`
    : '';
  querySQL += data.pv_flag ? ` and pv_flag !=\\'${data.pv_flag} \\'` : '';
  querySQL += data.orig_severity
    ? ` and orig_severity !=\\'${data.orig_severity}\\'`
    : '';
  querySQL += data.ne_id ? ` and ne_id !=\\'${data.ne_id}\\' ` : '';
  querySQL += data.ne_type ? ` and ne_type !=\\'${data.ne_type}\\' ` : '';

  let toBackJson = {
    data: {
      value: querySQL,
      value_json: JSON.stringify(data),
    },
  };

  return request({
    url: `/api/rest/databaseManagement/v1/omc_db/config?WHERE=config_tag='displayFilter'`,
    method: 'PUT',
    data: toBackJson,
  });
}
/**
 * 获取告警显示过滤条件
 * @param data 鉴权对象
 * @returns object
 */
export function getPass() {
  return request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/config`,
    method: 'GET',
    params: {
      SQL: "SELECT value_json,value FROM config WHERE config_tag ='displayFilter'",
    },
  });
}

/**
 * 清除告警信息
 * @param ids 记录ID
 * @returns object
 */
export function clearAlarm(ids: number[]) {
  return request({
    url: `/neData/alarm/clear`,
    method: 'PUT',
    data: { ids },
  });
}

/**
 * 告警信息导出
 * @param params 查询列表条件
 * @returns object
 */
export function exportAlarm(params: Record<string, any>) {
  return request({
    url: '/neData/alarm/export',
    method: 'GET',
    params: params,
    responseType: 'blob',
    timeout: 60_000,
  });
}

/**
 * 手工同步
 * @param data 鉴权对象
 * @returns object
 */
export function listSync() {
  return request({
    url: `/api/rest/faultManagement/v1/elementType/all/objectType/alarms`,
    method: 'GET',
    timeout: 180_000,
  });
}

/**
 * 活动告警导出
 * @param query 查询参数
 * @returns bolb
 */
export async function exportAll(query: Record<string, any>) {
  let rowsSQL = `select * from alarm where alarm_status='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`,
    method: 'GET',
    params: {
      rowsSQL: rowsSQL + querySQL,
    },
  });

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

/**
 * 展示页全部告警
 * @param query 查询参数
 * @returns bolb
 */
export async function origGet() {
  let totalSQL = `select count(*) as value,orig_severity as name from alarm WHERE alarm_status='1'  and orig_severity!='Event' group by orig_severity`;

  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/alarm`,
    method: 'GET',
    params: {
      SQL: totalSQL,
    },
    timeout: 30_000,
  });
  ////

  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS) {
    const itemData = result.data.data;
    if (Array.isArray(itemData)) {
      const v = itemData[0]['alarm'];
      if (Array.isArray(v)) {
        result.data = v;
      }
      if (v === null) {
        result.data = [];
      }
    }
  }
  return result;
}

/**
 * 查询前三的网元
 * @param filterFlag 查询参数
 * @returns object
 */
export async function top3Sel(filterFlag?: string) {
  let filter = ` WHERE alarm_status='1'and orig_severity='${filterFlag}'`;
  if (!filterFlag) filter = "WHERE alarm_status='1'";

  let top3SQL = `select count(*) as value,ne_type as name   from alarm ${filter} and orig_severity!='Event' group by ne_type ORDER BY value  desc limit 0,3 `;

  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/alarm`,
    method: 'GET',
    params: {
      SQL: top3SQL,
    },
    timeout: 30_000,
  });

  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS) {
    const itemData = result.data.data;
    if (Array.isArray(itemData)) {
      const v = itemData[0]['alarm'];
      if (Array.isArray(v)) {
        result.data = v;
      }
      if (v === null) {
        result.data = [];
      }
    }
  }
  return result;
}
