import { api } from './request'; export interface Appointment { id: string; patient_name: string; doctor_name: string; department?: string; appointment_date: string; start_time: string; end_time: string; status: string; version: number; } export interface Doctor { id: string; name: string; department: string; title?: string; } export interface DoctorSchedule { id: string; doctor_id: string; date: string; start_time: string; end_time: string; max_appointments: number; current_appointments: number; available_count: number; } export async function listAppointments(patientId?: string, page = 1) { return api.get<{ data: Appointment[]; total: number }>('/health/appointments', { page, page_size: 20, ...(patientId && { patient_id: patientId }), }); } export async function getAppointment(id: string) { return api.get(`/health/appointments/${id}`); } export async function createAppointment(data: { patient_id: string; doctor_id: string; appointment_date: string; start_time: string; end_time: string; notes?: string; appointment_type?: string; }) { return api.post('/health/appointments', data); } export async function cancelAppointment(id: string, version: number) { return api.put(`/health/appointments/${id}/status`, { status: 'cancelled', version, }); } export async function getDoctorSchedules(doctorId: string, startDate: string, endDate: string) { const resp = await api.get<{ data: DoctorSchedule[]; total: number }>('/health/doctor-schedules', { doctor_id: doctorId, start_date: startDate, end_date: endDate, page_size: 50, }); // 计算可用号源数 for (const s of resp.data) { s.available_count = s.available_count ?? (s.max_appointments - s.current_appointments); } return resp; } export async function listDoctors(department?: string) { return api.get<{ data: Doctor[]; total: number }>('/health/doctors', { page_size: 100, ...(department && { department }), }); } export async function calendarView(startDate: string, endDate: string, doctorId?: string) { const raw = await api.get<{ date: string; schedules: DoctorSchedule[] }[]>( '/health/doctor-schedules/calendar', { start_date: startDate, end_date: endDate, ...(doctorId && { doctor_id: doctorId }), }, ); // 后台返回按日期分组的嵌套结构,展平为扁平数组并计算 available_count const flat: DoctorSchedule[] = []; for (const day of (raw as unknown as { date: string; schedules: DoctorSchedule[] }[])) { for (const s of day.schedules) { flat.push({ ...s, date: s.date || day.date, available_count: s.available_count ?? (s.max_appointments - s.current_appointments), }); } } return flat; }