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


/**
 * 分页查询广告图片
 */
export async function pageCmsAdRecord(params: CmsAdRecordParam) {
  const res = await request.get<ApiResult<PageResult<CmsAdRecord>>>(
    '/cms/cms-ad-record/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询广告图片列表
 */
export async function listCmsAdRecord(params?: CmsAdRecordParam) {
  const res = await request.get<ApiResult<CmsAdRecord[]>>(
    '/cms/cms-ad-record',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加广告图片
 */
export async function addCmsAdRecord(data: CmsAdRecord) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-ad-record',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改广告图片
 */
export async function updateCmsAdRecord(data: CmsAdRecord) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-ad-record',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除广告图片
 */
export async function removeCmsAdRecord(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-ad-record/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除广告图片
 */
export async function removeBatchCmsAdRecord(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-ad-record/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询广告图片
 */
export async function getCmsAdRecord(id: number) {
  const res = await request.get<ApiResult<CmsAdRecord>>(
    '/cms/cms-ad-record/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
