2026-06-29 20:58:39 +08:00
|
|
|
import type { Appointment, AppointmentForm, ArtistSchedule, PlanningCalendar } from '@/types/manager'
|
|
|
|
|
import { extractTimeText, normalizeDuration, resolveAppointmentDate, resolveAppointmentEndMinute, resolveAppointmentStartMinute } from '@/utils/planning'
|
|
|
|
|
import { minutesOfDay, timeFromMinutes } from '@/utils/date'
|
|
|
|
|
|
|
|
|
|
const TIME_SLOT_MINUTES = 15
|
|
|
|
|
|
|
|
|
|
export interface AvailableTime {
|
|
|
|
|
value: string
|
|
|
|
|
rangeText: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AvailableTimeGroup {
|
|
|
|
|
key: string
|
|
|
|
|
title: string
|
|
|
|
|
times: AvailableTime[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buildTimeRangeLabel = (startMinute: number, durationMinutes: number): string => {
|
|
|
|
|
return `${timeFromMinutes(startMinute)}-${timeFromMinutes(startMinute + durationMinutes)}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getTimePeriod = (minute: number): { key: string; title: string } => {
|
|
|
|
|
if (minute < 12 * 60) {
|
|
|
|
|
return { key: 'morning', title: '上午' }
|
|
|
|
|
}
|
|
|
|
|
if (minute < 18 * 60) {
|
|
|
|
|
return { key: 'afternoon', title: '下午' }
|
|
|
|
|
}
|
|
|
|
|
return { key: 'evening', title: '晚上' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isCancelledAppointment = (item: Appointment): boolean => item.status === 3 || item.statusText === '已取消'
|
|
|
|
|
|
|
|
|
|
const buildBusyRanges = (appointments: Appointment[], form: AppointmentForm): Array<{ startMinute: number; endMinute: number }> => {
|
|
|
|
|
return appointments
|
|
|
|
|
.filter(item => String(item.artistId) === String(form.artistId))
|
|
|
|
|
.filter(item => resolveAppointmentDate(item) === form.appointmentDate)
|
|
|
|
|
.filter(item => !isCancelledAppointment(item))
|
|
|
|
|
.map(item => {
|
|
|
|
|
const startMinute = resolveAppointmentStartMinute(item)
|
|
|
|
|
return { startMinute, endMinute: resolveAppointmentEndMinute(item, startMinute) }
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hasBusyConflict = (startMinute: number, endMinute: number, ranges: Array<{ startMinute: number; endMinute: number }>): boolean => {
|
|
|
|
|
return ranges.some(range => startMinute < range.endMinute && endMinute > range.startMinute)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 02:08:51 +08:00
|
|
|
const clampToBusinessHours = (startMinute: number, endMinute: number, businessStartTime?: string, businessEndTime?: string): { startMinute: number; endMinute: number } => {
|
|
|
|
|
const businessStartMinute = businessStartTime ? minutesOfDay(businessStartTime) : startMinute
|
|
|
|
|
const businessEndMinute = businessEndTime ? minutesOfDay(businessEndTime) : endMinute
|
|
|
|
|
return {
|
|
|
|
|
startMinute: Math.max(startMinute, businessStartMinute),
|
|
|
|
|
endMinute: Math.min(endMinute, businessEndMinute)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-29 20:58:39 +08:00
|
|
|
export const buildAvailableTimeGroups = (calendar: PlanningCalendar, form: AppointmentForm): AvailableTimeGroup[] => {
|
|
|
|
|
const durationMinutes = normalizeDuration(form.durationMinutes)
|
|
|
|
|
if (!durationMinutes) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
2026-07-01 02:08:51 +08:00
|
|
|
const daySummary = (calendar.daySummaries || []).find(item => item.date === form.appointmentDate)
|
|
|
|
|
if (daySummary?.dayStatus === 'rest') {
|
|
|
|
|
return []
|
|
|
|
|
}
|
2026-06-29 20:58:39 +08:00
|
|
|
const schedules = (calendar.schedules || [])
|
|
|
|
|
.filter((item: ArtistSchedule) => String(item.artistId) === String(form.artistId))
|
|
|
|
|
.filter(item => item.date === form.appointmentDate)
|
|
|
|
|
.filter(item => item.status === 1 && Number(item.capacity === undefined ? 1 : item.capacity) >= 1)
|
|
|
|
|
const busyRanges = buildBusyRanges(calendar.appointments || [], form)
|
|
|
|
|
const groupMap: Record<string, AvailableTimeGroup> = {
|
|
|
|
|
morning: { key: 'morning', title: '上午', times: [] },
|
|
|
|
|
afternoon: { key: 'afternoon', title: '下午', times: [] },
|
|
|
|
|
evening: { key: 'evening', title: '晚上', times: [] }
|
|
|
|
|
}
|
|
|
|
|
schedules.forEach(schedule => {
|
2026-07-01 02:08:51 +08:00
|
|
|
const range = clampToBusinessHours(
|
|
|
|
|
minutesOfDay(extractTimeText(schedule.startTime)),
|
|
|
|
|
minutesOfDay(extractTimeText(schedule.endTime)),
|
|
|
|
|
daySummary?.businessStartTime,
|
|
|
|
|
daySummary?.businessEndTime
|
|
|
|
|
)
|
|
|
|
|
const scheduleStart = range.startMinute
|
|
|
|
|
const scheduleEnd = range.endMinute
|
2026-06-29 20:58:39 +08:00
|
|
|
for (let minute = scheduleStart; minute + durationMinutes <= scheduleEnd; minute += TIME_SLOT_MINUTES) {
|
|
|
|
|
const endMinute = minute + durationMinutes
|
|
|
|
|
if (!hasBusyConflict(minute, endMinute, busyRanges)) {
|
|
|
|
|
const period = getTimePeriod(minute)
|
|
|
|
|
const group = groupMap[period.key]
|
|
|
|
|
if (group) {
|
|
|
|
|
group.times.push({ value: timeFromMinutes(minute), rangeText: buildTimeRangeLabel(minute, durationMinutes) })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return Object.values(groupMap)
|
|
|
|
|
.map(group => ({
|
|
|
|
|
...group,
|
|
|
|
|
times: group.times.filter((item, index, array) => array.findIndex(candidate => candidate.value === item.value) === index)
|
|
|
|
|
}))
|
|
|
|
|
.filter(group => group.times.length > 0)
|
|
|
|
|
}
|