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

/**
 * 分页查询服务器成员管理
 */
export async function pageOaAssetsUser(params: OaAssetsUserParam) {
  const res = await request.get<ApiResult<PageResult<OaAssetsUser>>>(
    '/oa/oa-assets-user/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询服务器成员管理列表
 */
export async function listOaAssetsUser(params?: OaAssetsUserParam) {
  const res = await request.get<ApiResult<OaAssetsUser[]>>(
    '/oa/oa-assets-user',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加服务器成员管理
 */
export async function addOaAssetsUser(data: OaAssetsUser) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-assets-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改服务器成员管理
 */
export async function updateOaAssetsUser(data: OaAssetsUser) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-assets-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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