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';
import useUserStore from '@/store/modules/user';

/**
 * 查询列表
 * @param query 查询参数
 * @returns object
 */
export async function listAct(query: Record<string, any>) {
  let totalSQL = `select count(*)  as total from alarm_event where 1=1  `;
  let rowsSQL = `select * from alarm_event where 1=1  `;
  // 查询
  let querySQL = '';
  if (query.alarmCode) {
    querySQL += ` and alarm_code = '${query.alarmCode}' `;
  }

  if (query.alarmType) {
    querySQL += ` and alarm_type = '${query.alarmType}' `;
  }

  if (query.pvFlag) {
    querySQL += ` and pv_flag = '${query.pvFlag}' `;
  }

  if (query.neId) {
    querySQL += ` and ne_id like '%${query.neId}%' `;
  }

  if (query.neName) {
    querySQL += ` and ne_name like '%${query.neName}%' `;
  }

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

  if (query.beginTime && query.endTime) {
    querySQL += ` and event_time BETWEEN '${query.beginTime}' and ' ${query.endTime}'`;
  }

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

  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/alarm_event`,
    method: 'get',
    params: {
      SQL: 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['alarm_event'];
      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 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;
}
