scheduleHelper.js 3.09 KB
import {
  apiStatusToUi,
  buildYysjTimestampRange,
  dateTimeToSlotRange,
  formatDateYMD,
  formatTimeHM,
  pickRowField
} from '@/utils/appointmentHelper'

const STATUS_COLORS = ['blue', 'orange', 'green', 'purple', 'teal']

export function mapStoreScheduleItem(item, colorIndex = 0) {
  const yysj = pickRowField(item, 'yysj', 'Yysj')
  const yyjs = pickRowField(item, 'yyjs', 'Yyjs')
  const date = formatDateYMD(yysj)
  const startTime = formatTimeHM(yysj)
  const endTime = formatTimeHM(yyjs || yysj)
  const fStatus = pickRowField(item, 'F_Status', 'f_Status')
  const status = apiStatusToUi(fStatus)
  const therapistIds = Array.isArray(item.therapistIds) ? item.therapistIds.filter(Boolean) : []
  const therapistNames = Array.isArray(item.therapistNames) ? item.therapistNames.filter(Boolean) : []
  const itemLabels = Array.isArray(item.itemLabels) ? item.itemLabels : []

  return {
    id: pickRowField(item, 'id', 'Id'),
    memberId: pickRowField(item, 'gk', 'Gk'),
    memberName: pickRowField(item, 'gkxm', 'Gkxm') || '无',
    date,
    startTime,
    endTime,
    roomId: pickRowField(item, 'roomId', 'RoomId') || '',
    roomName: pickRowField(item, 'roomName', 'RoomName') || '',
    therapistIds,
    therapistNames,
    status,
    fStatus,
    remark: pickRowField(item, 'remark', 'Remark') || '',
    itemLabels,
    items: [],
    colorKey: STATUS_COLORS[colorIndex % STATUS_COLORS.length]
  }
}

export function buildWeekRangeTimestamps(weekStart) {
  if (!weekStart) return ''
  const start = new Date(weekStart)
  const end = new Date(start)
  end.setDate(end.getDate() + 6)
  end.setHours(23, 59, 59, 999)
  return buildYysjTimestampRange(start, end)
}

export function toSlotIndex(timeStr) {
  if (!timeStr) return null
  const [h, m] = String(timeStr).split(':').map(Number)
  if (Number.isNaN(h) || Number.isNaN(m)) return null
  return h * 2 + (m >= 30 ? 1 : 0)
}

export function slotIndexToTime(slotIndex) {
  const h = Math.floor(slotIndex / 2)
  const m = (slotIndex % 2) * 30
  return `${String(h).padStart(2, '0')}:${m === 0 ? '00' : '30'}`
}

export function attachSlotRange(booking) {
  const range = dateTimeToSlotRange(
    `${booking.date} ${booking.startTime}`,
    `${booking.date} ${booking.endTime}`
  )
  const slotStart = range ? range.startSlot : toSlotIndex(booking.startTime)
  const slotEnd = range ? range.endSlot : (toSlotIndex(booking.endTime) || (slotStart != null ? slotStart + 2 : 0))
  return {
    ...booking,
    slotStart: slotStart == null ? 0 : slotStart,
    slotEnd: slotEnd == null ? 0 : slotEnd
  }
}

/** 考勤日状态 → 排班格人事状态 */
export function mapAttendanceDayToScheduleStatus(dayItem) {
  if (!dayItem) return { type: 'normal', text: '正常' }
  const text = (dayItem.statusText || '').trim()
  const key = (dayItem.statusKey || '').trim()
  if (text.includes('请假') || text.includes('病假') || key === 'leave') {
    return { type: 'leave', text: text || '请假' }
  }
  if (text.includes('休息') || text.includes('休假') || key === 'rest') {
    return { type: 'vacation', text: text || '休息' }
  }
  return { type: 'normal', text: text || '正常' }
}