统一甲屿云命名体系
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
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)
|
||||
}
|
||||
|
||||
export const buildAvailableTimeGroups = (calendar: PlanningCalendar, form: AppointmentForm): AvailableTimeGroup[] => {
|
||||
const durationMinutes = normalizeDuration(form.durationMinutes)
|
||||
if (!durationMinutes) {
|
||||
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 scheduleStart = minutesOfDay(extractTimeText(schedule.startTime))
|
||||
const scheduleEnd = minutesOfDay(extractTimeText(schedule.endTime))
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user