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

/**
 * 查询软件列表
 * @param query 查询参数
 * @returns object
 */
export async function listNeSoftware(query: Record<string, any>) {
  let totalSQL = 'select count(id) as total from ne_software ';
  let rowsSQL = ' select * from ne_software ';

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

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

  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/select/omc_db/ne_software`,
    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_software'];
      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 noticeId 网元ID
 * @returns object
 */
export async function delNeSoftware(data: Record<string, any>) {
  return request({
    url: `/api/rest/systemManagement/v1/${data.neType}/software/${data.version}`,
    method: 'delete',
  });
}

/**
 * 获取软件信息文件
 * @param menuId 网元ID
 * @returns object
 */
export async function downloadNeSoftware(data: Record<string, any>) {
  return await request({
    url: `/api/rest/systemManagement/v1/${data.neType}/software/${data.version}`,
    method: 'get',
    responseType: 'blob',
  });
}

/**
 * 上传文件
 * @param data 表单数据对象
 * @returns object
 */
export function uploadNeSoftware(data: FormData) {
  return request({
    url: `/api/rest/systemManagement/v1/${data.get('nf')}/software/${data.get(
      'version'
    )}`,
    method: 'post',
    data,
    dataType: 'form-data',
  });
}

/**
 * 下发文件
 * @param data 数据对象
 * @returns object
 */
export async function sendNeSoftware(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/systemManagement/v1/${data.neType}/software/${data.version}/${data.neId}`,
    method: 'post',
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data.data) {
    let rows = result.data.data.affectedRows;
    if (rows) {
      delete result.data;
      return result;
    } else {
      return { code: RESULT_CODE_ERROR, msg: RESULT_MSG_ERROR };
    }
  }
  return result;
}

/**
 * 激活文件
 * @param data 数据对象
 * @returns object
 */
export async function runNeSoftware(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/systemManagement/v1/${data.neType}/software/${data.version}/${data.neId}`,
    method: 'put',
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data.data) {
    let rows = result.data.data.affectedRows;
    if (rows) {
      delete result.data;
      return result;
    } else {
      return { code: RESULT_CODE_ERROR, msg: RESULT_MSG_ERROR };
    }
  }
  return result;
}

/**
 * 回退文件
 * @param data 数据对象
 * @returns object
 */
export async function backNeSoftware(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/systemManagement/v1/${data.neType}/software/${data.version}/${data.neId}`,
    method: 'patch',
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data.data) {
    let rows = result.data.data.affectedRows;
    if (rows) {
      delete result.data;
      return result;
    } else {
      return { code: RESULT_CODE_ERROR, msg: RESULT_MSG_ERROR };
    }
  }
  return result;
}

/**
 * 查询版本列表
 * @param query 查询参数
 * @returns object
 */
export async function listNeVersion(query: Record<string, any>) {
  let totalSQL = 'select count(id) as total from ne_version ';
  let rowsSQL = 'select * from ne_version ';

  // 查询
  let querySQL = 'where 1=1';
  if (query.neType) {
    querySQL += ` and ne_type like '%${query.neType}%' `;
  }
  if (query.status) {
    querySQL += ` and status = '${query.status}' `;
  }
  if (query.beginTime && query.endTime) {
    querySQL += ` and update_time BETWEEN '${query.beginTime}' AND '${query.endTime}' `;
  }

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

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