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

/**
 * 查询操作维护接口设置
 * @param tag 配置ID
 * @returns object
 */
export async function getOperationSet() {
  // 发起请求
  const result = await request({
    url: `/api/rest/databaseManagement/v1/omc_db/config`,
    method: 'get',
    params: {
      SQL: `SELECT * FROM config WHERE config_tag = 'operationSet'`,
    },
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && Array.isArray(result.data.data)) {
    let data = result.data.data[0];
    const v = parseObjLineToHump(data['config'][0]);
    let vJSON = {};
    try {
      vJSON = JSON.parse(v.valueJson);
    } catch (error) {
      console.error(error);
    }
    return Object.assign(result, {
      data: vJSON,
    });
  }
  return result;
}

/**
 * 修改操作维护接口设置
 * @param data 配置对象
 * @returns object
 */
export async function updateOperationSet(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/databaseManagement/v1/omc_db/config?WHERE=config_tag='operationSet'`,
    method: 'put',
    data: { data: { value_json: JSON.stringify(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[language] };
    }
  }
  return result;
}
