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 listNeInfo(query: Record<string, any>) {
  let totalSQL = 'select count(*) as total from ne_info where status=0 ';
  let rowsSQL = 'select * from ne_info where status=0 ';

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

  // 分页
  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/ne_info`,
    method: 'get',
    params: {
      totalSQL: 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['ne_info'];
      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 menuId 网元ID
 * @returns object
 */
export async function getNeInfo(id: string | number) {
  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/ne_info`,
    method: 'get',
    params: {
      SQL: `select * from ne_info where status=0 and 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['ne_info'][0]),
    });
  }
  return result;
}

/**
 * 新增网元
 * @param data 网元对象
 * @returns object
 */
export function addNeInfo(data: Record<string, any>) {
  data.port = `${data.port}`;
  return request({
    url: `/api/rest/systemManagement/v1/elementType/${data.neType}/objectType/neInfo`,
    method: 'post',
    data: data,
  });
}

/**
 * 修改网元
 * @param data 网元对象
 * @returns object
 */
export function updateNeInfo(data: Record<string, any>) {
  data.port = `${data.port}`;
  return request({
    url: `/api/rest/systemManagement/v1/elementType/${data.neType}/objectType/neInfo`,
    method: 'put',
    data: data,
  });
}

/**
 * 删除网元
 * @param noticeId 网元ID
 * @returns object
 */
export async function delNeInfo(data: Record<string, any>) {
  return request({
    url: `/api/rest/systemManagement/v1/elementType/${data.neType}/objectType/neInfo?ne_id=${data.neId}`,
    method: 'delete',
  });
}

/**
 * 获取网元网元列表
 * @returns object
 */
export async function getNelistAll() {
  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/elementType/omc_db/objectType/ne_info`,
    method: 'get',
    params: {
      SQL: `SELECT ne_type,ne_name,ne_id,ip FROM ne_info WHERE status = 0`,
    },
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && Array.isArray(result.data.data)) {
    let data = result.data.data[0];
    return Object.assign(result, {
      data: parseObjLineToHump(data['ne_info']),
    });
  }
  return result;
}

/**
 * 导出网元配置文件
 * @param
 * @returns bolb
 */
export function exportSet(data: Record<string, any>) {
  return request({
    url: `/api/rest/systemManagement/v1/elementType/${data.neType}/objectType/cm?ne_id=${data.neId}`,
    method: 'get',
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/octet-stream',
    },
  });
}

/**
 * 导入网元配置文件
 * @param data 网元对象
 * @returns object
 */
export function importFile(data: Record<string, any>) {
  let dataType: 'json' | 'form-data' = 'json';
  let url = `/systemManagement/v1/elementType/${data.neType}/objectType/cm?ne_id=${data.neId}`;
  let obj: any = { fileName: data.fileName };
  if (data.importType === 'local') {
    let formData = new FormData();
    formData.append('nfType', data.neType);
    formData.append('nfId', data.neId);
    formData.append('file', data.file);
    obj = formData;
    dataType = 'form-data';
  }

  // 处理FormData类型的data
  return request({
    url,
    method: 'post',
    data: obj,
    dataType,
  });
  if (data instanceof FormData) {
    // 处理FormData类型的data
    return request({
      url: `/systemManagement/v1/elementType/${data.get(
        'nfType'
      )}/objectType/cm?ne_id=${data.get('nfId')}`,
      method: 'post',
      data,
      dataType: 'form-data',
    });
  } else {
    // 处理普通对象类型的data
    return request({
      url: `/systemManagement/v1/elementType/${data.nfType}/objectType/cm?ne_id=${data.nfId}`,
      method: 'post',
      data: { fileName: data.fileName },
    });
  }
}

/**
 * 查询远程服务器上网元配置文件
 * @param data 网元对象
 * @returns object
 */
export async function listServerFile(data: Record<string, any>) {
  const result = await request({
    url: `databaseManagement/v1/omc_db/ne_backup?SQL= select * from ne_backup where ne_type ='${data.neType}'`,
    method: 'get',
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && Array.isArray(result.data.data)) {
    let data = result.data.data[0];
    return Object.assign(result, {
      data: parseObjLineToHump(data['ne_backup']),
    });
  }
  return result;
}

/**
 * 启动网元
 * @param
 * @returns bolb
 */
export function startNf(data: Record<string, any>) {
  return request({
    url: `/api/rest/systemManagement/v1/elementType/${data.neType}/objectType/service/start?ne_id=${data.neId}`,
    method: 'post',
  });
}

/**
 * 重启网元
 * @param
 * @returns bolb
 */
export function restartNf(data: Record<string, any>) {
  return request({
    url: `/api/rest/systemManagement/v1/elementType/${data.neType}/objectType/service/restart?ne_id=${data.neId}`,
    method: 'post',
  });
}

/**
 * 停止网元
 * @param
 * @returns bolb
 */
export function stopNf(data: Record<string, any>) {
  return request({
    url: `/api/rest/systemManagement/v1/elementType/${data.neType}/objectType/service/stop?ne_id=${data.neId}`,
    method: 'post',
  });
}
