import type { DataGrid, Pagination } from '@/types/global';
import type { TableData } from '@arco-design/web-vue/es/table/interface';
import axios from 'axios';
import qs from 'query-string';

/**
 * ${table.comment}创建请求
 */
export interface ${vue.featureEntity}CreateRequest {
  #foreach($field in ${table.fields})
  #if("$!field.propertyName" != "id")
  #if("$field.propertyType" == "String" || "$field.propertyType" == "Date")
  ${field.propertyName}?: string;
  #elseif("$field.propertyType" == "Integer" || "$field.propertyType" == "Long")
  ${field.propertyName}?: number;
  #elseif("$field.propertyType" == "Boolean")
  ${field.propertyName}?: boolean;
  #else
  ${field.propertyName}?: any;
  #end
  #end
  #end
}

/**
 * ${table.comment}更新请求
 */
export interface ${vue.featureEntity}UpdateRequest extends ${vue.featureEntity}CreateRequest {
  id?: number;
}

/**
 * ${table.comment}查询请求
 */
export interface ${vue.featureEntity}QueryRequest extends Pagination {
  searchValue?: string;
  #foreach($field in ${table.fields})
  #if("$field.propertyType" == "String" || "$field.propertyType" == "Date")
  ${field.propertyName}?: string;
  #elseif("$field.propertyType" == "Integer" || "$field.propertyType" == "Long")
  ${field.propertyName}?: number;
  #elseif("$field.propertyType" == "Boolean")
  ${field.propertyName}?: boolean;
  #else
  ${field.propertyName}?: any;
  #end
  #end
}

/**
 * ${table.comment}查询响应
 */
export interface ${vue.featureEntity}QueryResponse extends TableData {
  #foreach($field in ${table.fields})
  #if("$field.propertyType" == "String")
  ${field.propertyName}: string;
  #elseif("$field.propertyType" == "Integer" || "$field.propertyType" == "Long" || "$field.propertyType" == "Date")
  ${field.propertyName}: number;
  #elseif("$field.propertyType" == "Boolean")
  ${field.propertyName}: boolean;
  #else
  ${field.propertyName}: any;
  #end
  #end
  createTime: number;
  updateTime: number;
  creator: string;
  updater: string;
}

/**
 * $apiComment.create
 */
export function create${vue.featureEntity}(request: ${vue.featureEntity}CreateRequest) {
  return axios.post('/${package.ModuleName}/${typeHyphen}/create', request);
}

/**
 * $apiComment.updateById
 */
export function update${vue.featureEntity}(request: ${vue.featureEntity}UpdateRequest) {
  return axios.put('/${package.ModuleName}/${typeHyphen}/update', request);
}

/**
 * $apiComment.getById
 */
export function get${vue.featureEntity}(id: number) {
  return axios.get<${vue.featureEntity}QueryResponse>('/${package.ModuleName}/${typeHyphen}/get', { params: { id } });
}

/**
 * $apiComment.getByIdList
 */
export function batchGet${vue.featureEntity}List(idList: Array<number>) {
  return axios.get<${vue.featureEntity}QueryResponse[]>('/${package.ModuleName}/${typeHyphen}/batch-get', {
    params: { idList },
    paramsSerializer: params => {
      return qs.stringify(params, { arrayFormat: 'comma' });
    }
  });
}

/**
 * $apiComment.queryList
 */
export function get${vue.featureEntity}List(request: ${vue.featureEntity}QueryRequest) {
  return axios.post<Array<${vue.featureEntity}QueryResponse>>('/${package.ModuleName}/${typeHyphen}/list', request);
}

/**
 * $apiComment.queryPage
 */
export function get${vue.featureEntity}Page(request: ${vue.featureEntity}QueryRequest) {
  return axios.post<DataGrid<${vue.featureEntity}QueryResponse>>('/${package.ModuleName}/${typeHyphen}/query', request);
}

/**
 * $apiComment.deleteById
 */
export function delete${vue.featureEntity}(id: number) {
  return axios.delete('/${package.ModuleName}/${typeHyphen}/delete', { params: { id } });
}

/**
 * $apiComment.batchDelete
 */
export function batchDelete${vue.featureEntity}(idList: Array<number>) {
  return axios.delete('/${package.ModuleName}/${typeHyphen}/batch-delete', {
    params: { idList },
    paramsSerializer: params => {
      return qs.stringify(params, { arrayFormat: 'comma' });
    }
  });
}

