import { request } from '@umijs/max';
import type { ApiResponse, PageResult } from '@/types/api';

{{- if not .OnlyTemplate}}

// {{.StructName}} {{.Description}}类型
export interface {{.StructName}} {
	{{- if .GvaModel }}
	ID?: number;
	CreatedAt?: string;
	UpdatedAt?: string;
	{{- end }}
	{{- range .Fields}}
	{{.FieldJson}}{{if not .Require}}?{{end}}: {{ GenerateTSType .FieldType }}; // {{.FieldDesc}}
	{{- end }}
	{{- if .AutoCreateResource }}
	createdBy?: number;
	updatedBy?: number;
	deletedBy?: number;
	{{- end }}
	{{- if .IsTree }}
	children?: {{.StructName}}[];
	parentID?: number;
	{{- end }}
}

// {{.StructName}}Search {{.Description}}搜索条件
export interface {{.StructName}}Search {
	page: number;
	pageSize: number;
	{{- if .GvaModel }}
	createdAtRange?: string[];
	{{- end }}
	{{- range .Fields}}
		{{- if .FieldSearchType}}
	{{.FieldJson}}?: {{ GenerateTSType .FieldType }};
		{{- end}}
	{{- end}}
	{{- if .NeedSort}}
	sort?: string;
	order?: string;
	{{- end}}
}

/**
 * 创建{{.Description}}
 * @param data {{.Description}}数据
 */
export const create{{.StructName}} = (data: {{.StructName}}) => {
	return request<ApiResponse<null>>('/{{.Abbreviation}}/create{{.StructName}}', {
		method: 'POST',
		data,
	});
};

/**
 * 删除{{.Description}}
 * @param params 包含{{.PrimaryField.FieldJson}}的参数
 */
export const delete{{.StructName}} = (params: { {{.PrimaryField.FieldJson}}: {{ GenerateTSType .PrimaryField.FieldType }} }) => {
	return request<ApiResponse<null>>('/{{.Abbreviation}}/delete{{.StructName}}', {
		method: 'DELETE',
		params,
	});
};

/**
 * 批量删除{{.Description}}
 * @param params 包含{{.PrimaryField.FieldJson}}s数组的参数
 */
export const delete{{.StructName}}ByIds = (params: { {{.PrimaryField.FieldJson}}s: {{ GenerateTSType .PrimaryField.FieldType }}[] }) => {
	return request<ApiResponse<null>>('/{{.Abbreviation}}/delete{{.StructName}}ByIds', {
		method: 'DELETE',
		params,
	});
};

/**
 * 更新{{.Description}}
 * @param data {{.Description}}数据
 */
export const update{{.StructName}} = (data: {{.StructName}}) => {
	return request<ApiResponse<null>>('/{{.Abbreviation}}/update{{.StructName}}', {
		method: 'PUT',
		data,
	});
};

/**
 * 根据ID获取{{.Description}}
 * @param params 包含{{.PrimaryField.FieldJson}}的参数
 */
export const find{{.StructName}} = (params: { {{.PrimaryField.FieldJson}}: {{ GenerateTSType .PrimaryField.FieldType }} }) => {
	return request<ApiResponse<{{.StructName}}>>('/{{.Abbreviation}}/find{{.StructName}}', {
		method: 'GET',
		params,
	});
};

/**
 * 获取{{.Description}}列表
 * @param params 搜索条件
 */
export const get{{.StructName}}List = (params: {{.StructName}}Search) => {
	return request<ApiResponse<PageResult<{{.StructName}}>>>('/{{.Abbreviation}}/get{{.StructName}}List', {
		method: 'GET',
		params,
	});
};

{{- if .HasDataSource}}
/**
 * 获取{{.Description}}数据源
 */
export const get{{.StructName}}DataSource = () => {
	return request<ApiResponse<Record<string, Array<{ label: string; value: any }>>>>('/{{.Abbreviation}}/get{{.StructName}}DataSource', {
		method: 'GET',
	});
};
{{- end}}

{{- end}}

/**
 * 获取{{.Description}}公开数据（无需鉴权）
 */
export const get{{.StructName}}Public = () => {
	return request<ApiResponse<any>>('/{{.Abbreviation}}/get{{.StructName}}Public', {
		method: 'GET',
	});
};
