import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { OaAssetsMysql, OaAssetsMysqlParam } from './model';

/**
 * 分页查询云数据库
 */
export async function pageOaAssetsMysql(params: OaAssetsMysqlParam) {
  const res = await request.get<ApiResult<PageResult<OaAssetsMysql>>>(
    '/oa/oa-assets-mysql/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询云数据库列表
 */
export async function listOaAssetsMysql(params?: OaAssetsMysqlParam) {
  const res = await request.get<ApiResult<OaAssetsMysql[]>>(
    '/oa/oa-assets-mysql',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加云数据库
 */
export async function addOaAssetsMysql(data: OaAssetsMysql) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-assets-mysql',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改云数据库
 */
export async function updateOaAssetsMysql(data: OaAssetsMysql) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-assets-mysql',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除云数据库
 */
export async function removeOaAssetsMysql(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-assets-mysql/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除云数据库
 */
export async function removeBatchOaAssetsMysql(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-assets-mysql/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询云数据库
 */
export async function getOaAssetsMysql(id: number) {
  const res = await request.get<ApiResult<OaAssetsMysql>>(
    '/oa/oa-assets-mysql/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
