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

/**
 * 分页查询代码仓库
 */
export async function pageOaAssetsCode(params: OaAssetsCodeParam) {
  const res = await request.get<ApiResult<PageResult<OaAssetsCode>>>(
    '/oa/oa-assets-code/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询代码仓库列表
 */
export async function listOaAssetsCode(params?: OaAssetsCodeParam) {
  const res = await request.get<ApiResult<OaAssetsCode[]>>(
    '/oa/oa-assets-code',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加代码仓库
 */
export async function addOaAssetsCode(data: OaAssetsCode) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-assets-code',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改代码仓库
 */
export async function updateOaAssetsCode(data: OaAssetsCode) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-assets-code',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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