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 tag 配置ID
 * @returns object
 */
export async function getConfigInfo(tag: string) {
  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/omc_db/config`,
    method: 'get',
    params: {
      SQL: `SELECT * FROM config WHERE config_tag = '${tag}'`,
    },
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && Array.isArray(result.data.data)) {
    let data = result.data.data[0];
    return Object.assign(result, {
      data: parseObjLineToHump(data['config'][0]),
    });
  }
  return result;
}

/**
 * 修改配置
 * @param data 配置对象
 * @returns object
 */
export async function updateConfig(tag: string, data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/databaseManagement/v1/omc_db/config?WHERE=config_tag='${tag}'`,
    method: 'put',
    data: { data },
  });
  // 解析数据
  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;
}
