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 where alarm_status='0'`;
  let rowsSQL = `select * from alarm where alarm_status='0'`;
  // 查询
  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 pageNum = (query.pageNum - 1) * query.pageSize;
  const limtSql = ` order by clear_time desc limit ${pageNum},${query.pageSize} `;

  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/alarm`,
    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'];
      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 data 鉴权对象
 * @returns object
 */
export function updateConfirm(data: Record<string, any>) {
  var time = new Date();
  const userName = useUserStore().userName;
  let finalData = {
    alarm: {
      ack_time: parseDateToStr(time),
      ack_user: userName,
      ack_state: '1',
    },
  };

  return request({
    url: `/api/rest/databaseManagement/v1/update/omc_db/alarm?WHERE=id='${data.id}'`,
    method: 'put',
    data: finalData,
  });
}

/**
 * 取消确认告警
 * @param data 鉴权对象
 * @returns object
 */
export function cancelConfirm(data: (string | number)[]) {
  var time = new Date();
  let finalData = {
    alarm: {
      ack_time: parseDateToStr(time),
      ack_user: '',
      ack_state: '0',
    },
  };

  return request({
    url: `/api/rest/databaseManagement/v1/update/omc_db/alarm?WHERE=id in(${data.join(
      ','
    )})`,
    method: 'put',
    data: finalData,
  });
}

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