231 lines
9.7 KiB
TypeScript
231 lines
9.7 KiB
TypeScript
import type { Appointment, AppointmentForm, ArtistSchedule, PlanningCalendar } from '@/types/manager'
|
|
import { extractTimeText, normalizeDuration, resolveAppointmentDate, resolveAppointmentEndMinute, resolveAppointmentStartMinute } from '@/utils/planning'
|
|
import { addDays, formatDate, minutesOfDay, timeFromMinutes } from '@/utils/date'
|
|
|
|
const TIME_SLOT_MINUTES = 15
|
|
const DAY_MINUTES = 24 * 60
|
|
const DAY_END_COVER_MINUTE = 23 * 60 + 59
|
|
const DAY_MS = 24 * 60 * 60 * 1000
|
|
|
|
export interface AvailableTime {
|
|
value: string
|
|
rangeText: string
|
|
}
|
|
|
|
export interface AvailableTimeGroup {
|
|
key: string
|
|
title: string
|
|
times: AvailableTime[]
|
|
}
|
|
|
|
interface TimeSegment {
|
|
date: string
|
|
startMinute: number
|
|
endMinute: number
|
|
}
|
|
|
|
interface BusyRange {
|
|
startMinute: number
|
|
endMinute: number
|
|
}
|
|
|
|
export const buildAvailableTimeRangeLabel = (startMinute: number, durationMinutes: number): string => {
|
|
const endMinute = startMinute + durationMinutes
|
|
const endOffset = Math.floor(endMinute / DAY_MINUTES)
|
|
const endText = timeFromMinutes(endMinute)
|
|
if (endOffset === 0) {
|
|
return `${timeFromMinutes(startMinute)}-${endText}`
|
|
}
|
|
if (endOffset === 1) {
|
|
return `${timeFromMinutes(startMinute)}-次日${endText}`
|
|
}
|
|
return `${timeFromMinutes(startMinute)}-+${endOffset}天${endText}`
|
|
}
|
|
|
|
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 parseDateDayNumber = (dateText: string): number => {
|
|
const parts = String(dateText || '').split('-').map(Number)
|
|
const [year, month, day] = parts
|
|
if (
|
|
parts.length !== 3
|
|
|| year === undefined
|
|
|| month === undefined
|
|
|| day === undefined
|
|
|| parts.some(value => !Number.isFinite(value))
|
|
) {
|
|
throw new Error(`无效日期:${dateText}`)
|
|
}
|
|
return Math.floor(Date.UTC(year, month - 1, day) / DAY_MS)
|
|
}
|
|
|
|
const dateOffset = (baseDate: string, targetDate: string): number => {
|
|
return parseDateDayNumber(targetDate) - parseDateDayNumber(baseDate)
|
|
}
|
|
|
|
const resolveDateByOffset = (baseDate: string, offset: number): string => {
|
|
return formatDate(addDays(baseDate, offset))
|
|
}
|
|
|
|
const resolveCoverageEndMinute = (timeText: string | undefined): number => {
|
|
const minute = minutesOfDay(extractTimeText(timeText))
|
|
return minute >= DAY_END_COVER_MINUTE ? DAY_MINUTES : minute
|
|
}
|
|
|
|
const resolveAppointmentAbsoluteStartMinute = (item: Appointment, baseDate: string): number => {
|
|
return dateOffset(baseDate, resolveAppointmentDate(item)) * DAY_MINUTES + resolveAppointmentStartMinute(item)
|
|
}
|
|
|
|
const resolveAppointmentAbsoluteEndMinute = (item: Appointment, baseDate: string, startMinute: number): number => {
|
|
if (item.endTime) {
|
|
const endText = String(item.endTime)
|
|
if (/^\d{4}-\d{2}-\d{2}/.test(endText)) {
|
|
const absoluteEndMinute = dateOffset(baseDate, endText.slice(0, 10)) * DAY_MINUTES + minutesOfDay(extractTimeText(endText))
|
|
return absoluteEndMinute <= startMinute ? startMinute + Number(item.durationMinutes || 0) : absoluteEndMinute
|
|
}
|
|
}
|
|
if (item.durationMinutes && Number(item.durationMinutes) > 0) {
|
|
return startMinute + Number(item.durationMinutes)
|
|
}
|
|
const localEndMinute = resolveAppointmentEndMinute(item, startMinute % DAY_MINUTES)
|
|
const absoluteEndMinute = Math.floor(startMinute / DAY_MINUTES) * DAY_MINUTES + localEndMinute
|
|
return absoluteEndMinute <= startMinute ? absoluteEndMinute + DAY_MINUTES : absoluteEndMinute
|
|
}
|
|
|
|
const buildBusyRanges = (appointments: Appointment[], form: AppointmentForm): BusyRange[] => {
|
|
return appointments
|
|
.filter(item => String(item.artistId) === String(form.artistId))
|
|
.filter(item => !isCancelledAppointment(item))
|
|
.map(item => {
|
|
const startMinute = resolveAppointmentAbsoluteStartMinute(item, form.appointmentDate)
|
|
return { startMinute, endMinute: resolveAppointmentAbsoluteEndMinute(item, form.appointmentDate, startMinute) }
|
|
})
|
|
}
|
|
|
|
const hasBusyConflict = (startMinute: number, endMinute: number, ranges: BusyRange[]): boolean => {
|
|
return ranges.some(range => startMinute < range.endMinute && endMinute > range.startMinute)
|
|
}
|
|
|
|
const splitWindowSegments = (appointmentDate: string, startMinute: number, durationMinutes: number): TimeSegment[] => {
|
|
const segments: TimeSegment[] = []
|
|
const endMinute = startMinute + durationMinutes
|
|
let cursor = startMinute
|
|
while (cursor < endMinute) {
|
|
const offset = Math.floor(cursor / DAY_MINUTES)
|
|
const nextDayMinute = (offset + 1) * DAY_MINUTES
|
|
const segmentEndAbsoluteMinute = Math.min(endMinute, nextDayMinute)
|
|
segments.push({
|
|
date: resolveDateByOffset(appointmentDate, offset),
|
|
startMinute: cursor - offset * DAY_MINUTES,
|
|
endMinute: segmentEndAbsoluteMinute === nextDayMinute ? DAY_MINUTES : segmentEndAbsoluteMinute - offset * DAY_MINUTES
|
|
})
|
|
cursor = segmentEndAbsoluteMinute
|
|
}
|
|
return segments
|
|
}
|
|
|
|
export const resolveAppointmentCalendarEndDate = (form: AppointmentForm): string => {
|
|
const durationMinutes = normalizeDuration(form.durationMinutes)
|
|
if (!form.appointmentDate || !durationMinutes) {
|
|
return form.appointmentDate
|
|
}
|
|
const latestCandidateStartMinute = DAY_MINUTES - TIME_SLOT_MINUTES
|
|
const endOffset = Math.floor((latestCandidateStartMinute + durationMinutes - 1) / DAY_MINUTES)
|
|
return resolveDateByOffset(form.appointmentDate, Math.max(0, endOffset))
|
|
}
|
|
|
|
const findDaySummary = (calendar: PlanningCalendar, date: string) => {
|
|
return (calendar.daySummaries || []).find(item => item.date === date)
|
|
}
|
|
|
|
const isBusinessSegmentAvailable = (calendar: PlanningCalendar, segment: TimeSegment): boolean => {
|
|
const daySummary = findDaySummary(calendar, segment.date)
|
|
if (daySummary?.dayStatus === 'rest') {
|
|
return false
|
|
}
|
|
const businessStartTime = daySummary?.businessStartTime || calendar.businessStartTime
|
|
const businessEndTime = daySummary?.businessEndTime || calendar.businessEndTime
|
|
const businessStartMinute = businessStartTime ? minutesOfDay(extractTimeText(businessStartTime)) : segment.startMinute
|
|
const businessEndMinute = businessEndTime ? resolveCoverageEndMinute(businessEndTime) : segment.endMinute
|
|
return segment.startMinute >= businessStartMinute && segment.endMinute <= businessEndMinute
|
|
}
|
|
|
|
const isScheduleSegmentAvailable = (calendar: PlanningCalendar, form: AppointmentForm, segment: TimeSegment): boolean => {
|
|
return (calendar.schedules || [])
|
|
.filter((item: ArtistSchedule) => String(item.artistId) === String(form.artistId))
|
|
.filter(item => item.date === segment.date)
|
|
.filter(item => item.status === 1 && Number(item.capacity === undefined ? 1 : item.capacity) >= 1)
|
|
.some(schedule => {
|
|
const scheduleStartMinute = minutesOfDay(extractTimeText(schedule.startTime))
|
|
const scheduleEndMinute = resolveCoverageEndMinute(schedule.endTime)
|
|
return segment.startMinute >= scheduleStartMinute && segment.endMinute <= scheduleEndMinute
|
|
})
|
|
}
|
|
|
|
const isWindowAvailable = (calendar: PlanningCalendar, form: AppointmentForm, startMinute: number, durationMinutes: number, busyRanges: BusyRange[]): boolean => {
|
|
const endMinute = startMinute + durationMinutes
|
|
if (hasBusyConflict(startMinute, endMinute, busyRanges)) {
|
|
return false
|
|
}
|
|
const segments = splitWindowSegments(form.appointmentDate, startMinute, durationMinutes)
|
|
return segments.every(segment => isBusinessSegmentAvailable(calendar, segment) && isScheduleSegmentAvailable(calendar, form, segment))
|
|
}
|
|
|
|
export const buildAvailableTimeGroups = (calendar: PlanningCalendar, form: AppointmentForm): AvailableTimeGroup[] => {
|
|
const durationMinutes = normalizeDuration(form.durationMinutes)
|
|
if (!durationMinutes) {
|
|
return []
|
|
}
|
|
const daySummary = findDaySummary(calendar, form.appointmentDate)
|
|
if (daySummary?.dayStatus === 'rest') {
|
|
return []
|
|
}
|
|
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 => {
|
|
const businessStartTime = daySummary?.businessStartTime || calendar.businessStartTime
|
|
const businessEndTime = daySummary?.businessEndTime || calendar.businessEndTime
|
|
const scheduleStart = Math.max(
|
|
minutesOfDay(extractTimeText(schedule.startTime)),
|
|
businessStartTime ? minutesOfDay(extractTimeText(businessStartTime)) : 0
|
|
)
|
|
const scheduleEnd = Math.min(
|
|
resolveCoverageEndMinute(schedule.endTime),
|
|
businessEndTime ? resolveCoverageEndMinute(businessEndTime) : DAY_MINUTES
|
|
)
|
|
for (let minute = scheduleStart; minute < scheduleEnd; minute += TIME_SLOT_MINUTES) {
|
|
if (isWindowAvailable(calendar, form, minute, durationMinutes, busyRanges)) {
|
|
const period = getTimePeriod(minute)
|
|
const group = groupMap[period.key]
|
|
if (group) {
|
|
group.times.push({ value: timeFromMinutes(minute), rangeText: buildAvailableTimeRangeLabel(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)
|
|
}
|