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

/**
 * 分页查询服务
 */
export async function pageOaAssetsServer(params: OaAssetsServerParam) {
  const res = await request.get<ApiResult<PageResult<OaAssetsServer>>>(
    '/oa/oa-assets-server/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询服务列表
 */
export async function listOaAssetsServer(params?: OaAssetsServerParam) {
  const res = await request.get<ApiResult<OaAssetsServer[]>>(
    '/oa/oa-assets-server',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加服务
 */
export async function addOaAssetsServer(data: OaAssetsServer) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-assets-server',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改服务
 */
export async function updateOaAssetsServer(data: OaAssetsServer) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-assets-server',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

/**
 * 根据id查询服务
 */
export async function getOaAssetsServer(id: number) {
  const res = await request.get<ApiResult<OaAssetsServer>>(
    '/oa/oa-assets-server/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
