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


/**
 * 分页查询书籍记录表
 */
export async function pageCmsDocsBook(params: CmsDocsBookParam) {
  const res = await request.get<ApiResult<PageResult<CmsDocsBook>>>(
    '/cms/cms-docs-book/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询书籍记录表列表
 */
export async function listCmsDocsBook(params?: CmsDocsBookParam) {
  const res = await request.get<ApiResult<CmsDocsBook[]>>(
    '/cms/cms-docs-book',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加书籍记录表
 */
export async function addCmsDocsBook(data: CmsDocsBook) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-docs-book',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改书籍记录表
 */
export async function updateCmsDocsBook(data: CmsDocsBook) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-docs-book',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除书籍记录表
 */
export async function removeCmsDocsBook(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-docs-book/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除书籍记录表
 */
export async function removeBatchCmsDocsBook(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-docs-book/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询书籍记录表
 */
export async function getCmsDocsBook(id: number) {
  const res = await request.get<ApiResult<CmsDocsBook>>(
    '/cms/cms-docs-book/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
