import type { AxiosResponse } from 'axios'
import type { PageInfo, Result } from '@cacp/ui'
import request from '@/utils/request'

import type {${ClassName},${ClassName}Query}  from '@/types/${moduleName}/${businessName}'

const contextPath = '/${moduleName}/${businessName}'

// 查询${functionName}列表
export async function getList(query: ${ClassName}Query): Promise<Result<PageInfo<${ClassName}>>> {
  const res: AxiosResponse<Result<PageInfo<${ClassName}>>> = await request.post(`${contextPath}/get-list`, query)
  return res.data
}

// 获取${functionName}详情
export async function getDetail(${pkColumn.javaField}: ${pkColumn.javaType.toLowerCase()}): Promise<Result<${ClassName}>> {
  const res: AxiosResponse<Result<${ClassName}>> = await request.get(`${contextPath}/get-detail?${pkColumn.javaField}=${${pkColumn.javaField}}`)
  return res.data
}

// 新增${functionName}
export async function insert(row: ${ClassName}): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/insert-${className}`, row)
  return res.data
}

// 更新${functionName}
export async function update(row: ${ClassName}): Promise<Result<number>> {
  const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/update-${className}`, row)
  return res.data
}

// 删除${functionName}
export async function remove(${className}s: ${pkColumn.javaType.toLowerCase()} | ${pkColumn.javaType.toLowerCase()}[]): Promise<Result<number>> {
  // 统一转换为数组格式
  const ids = Array.isArray(${className}s) ? ${className}s : [${className}s];
  const res: AxiosResponse<Result<number>> = await request.post(
          `${contextPath}/delete-${className}?ids`,ids
  )
  return res.data
}
