import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import type { ${entity}, ${entity}Param } from './model';

/**
 * 分页查询${table.comment!}
 */
export async function page${entity}(params: ${entity}Param) {
  const res = await request.get<ApiResult<PageResult<${entity}>>>(
    '/${package.ModuleName}/${controllerMappingHyphen}/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询${table.comment!}列表
 */
export async function list${entity}(params?: ${entity}Param) {
  const res = await request.get<ApiResult<${entity}[]>>(
    '/${package.ModuleName}/${controllerMappingHyphen}',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加${table.comment!}
 */
export async function add${entity}(data: ${entity}) {
  const res = await request.post<ApiResult<unknown>>(
    '/${package.ModuleName}/${controllerMappingHyphen}',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改${table.comment!}
 */
export async function update${entity}(data: ${entity}) {
  const res = await request.put<ApiResult<unknown>>(
    '/${package.ModuleName}/${controllerMappingHyphen}',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除${table.comment!}
 */
export async function remove${entity}(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/${package.ModuleName}/${controllerMappingHyphen}/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除${table.comment!}
 */
export async function removeBatch${entity}(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/${package.ModuleName}/${controllerMappingHyphen}/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询${table.comment!}
 */
export async function get${entity}(id: number) {
  const res = await request.get<ApiResult<${entity}>>(
    '/${package.ModuleName}/${controllerMappingHyphen}/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
