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


/**
 * 分页查询网站信息记录表
 */
export async function pageCmsWebsite(params: CmsWebsiteParam) {
  const res = await request.get<ApiResult<PageResult<CmsWebsite>>>(
    '/cms/cms-website/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询网站
 */
export async function getCmsWebsiteAll(id: number) {
  const res = await request.get<ApiResult<CmsWebsite>>(
    '/cms/cms-website/getAll/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询网站信息记录表列表
 */
export async function listCmsWebsite(params?: CmsWebsiteParam) {
  const res = await request.get<ApiResult<CmsWebsite[]>>(
    '/cms/cms-website',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加网站信息记录表
 */
export async function addCmsWebsite(data: CmsWebsite) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-website',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改网站信息记录表
 */
export async function updateCmsWebsite(data: CmsWebsite) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-website',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除网站信息记录表
 */
export async function removeCmsWebsite(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-website/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 根据id查询网站信息记录表
 */
export async function getCmsWebsite(id: number) {
  const res = await request.get<ApiResult<CmsWebsite>>(
    '/cms/cms-website/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 清除缓存
 */
export async function removeSiteInfoCache(key?: string) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-website/clearSiteInfo/' + key
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 分页查询网站信息记录表
 * @param params
 */
export async function pageCmsWebsiteAll(params: CmsWebsiteParam) {
  const res = await request.get<ApiResult<PageResult<CmsWebsite>>>(
    '/cms/cms-website/pageAll',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
