Files
zyt/admin/local-preview/modules/roster.mjs
T

66 lines
3.7 KiB
JavaScript

// The seed is a local calendar date (not a UTC timestamp). Overlapping weeks keep
// identical IDs and shifts even when the shared preview snapshot rolls forward.
const names = ['林衡', '沈知禾', '许亦安', '顾清和', '赵元宁', '叶嘉言']
const dayTimes = [
['08:30', '11:30', 'day'], ['14:00', '17:00', 'day'], ['18:00', '21:00', 'night']
]
const isoDate = d => d.toISOString().slice(0, 10)
const ok = data => ({ code: 1, data, show: 0, msg: '' })
const list = (lists, count = lists.length) => ok({ lists, count, extend: {} })
export function makeRosterSnapshot(departments, now) {
if (!departments.length) return { doctors: [], slots: [] }
const doctors = names.map((name, i) => ({
id: 201 + i, name: name + '(演示)', dept_id: departments[i % departments.length].dept_id
}))
const monday = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()))
monday.setUTCDate(monday.getUTCDate() - (monday.getUTCDay() + 6) % 7)
const slots = []
// Five weeks before and two weeks after this Monday also cover the recent-30-day views.
for (let offset = -35; offset < 21; offset++) {
const d = new Date(monday)
d.setUTCDate(d.getUTCDate() + offset)
const date = isoDate(d), ordinal = Math.floor(d.getTime() / 86400000)
doctors.forEach((doctor, i) => {
const phase = (ordinal + i * 2) % 9
if (phase === 1 || phase === 6) return // No segment ≠ cancelled/closed shift.
const status = phase === 4 ? 2 : phase === 5 ? 4 : phase === 7 && i % 2 ? 3 : 1
const shifts = status === 1 && phase === 0 ? [0, 1] : [phase === 2 ? 2 : phase % 2]
for (const j of shifts) {
const [start_time, end_time, shift_type] = dayTimes[j]
const quota = status === 1 ? (phase % 3 === 0 ? 0 : 8 + i % 5) : 0
slots.push({
id: Number(date.replaceAll('-', '')) * 100 + i * 10 + j, doctor_id: doctor.id,
date, period: 'segment', start_time, end_time, shift_type, slot_minutes: 15,
status, quota, max_patients: 0, booked_count: 0, remark: ''
})
}
})
}
slots.sort((a, b) => a.date.localeCompare(b.date) || a.start_time.localeCompare(b.start_time) || a.id - b.id)
return { doctors, slots }
}
export function resolveRoster(pathname, params, snapshot) {
const q = params instanceof URLSearchParams ? Object.fromEntries(params) : params || {}
if (q.export != null) return null
if (pathname === '/adminapi/auth.admin/lists') {
// Never turn the general account-management API into an unscoped preview endpoint.
if (String(q.role_id) !== '1' || String(q.exclude_disabled) !== '1') return null
const rows = snapshot.doctors.filter(d => !q.name || d.name.includes(String(q.name).trim()))
const positive = (n, fallback) => Number.isSafeInteger(+n) && +n > 0 ? +n : fallback
const page = positive(q.page_no, 1), size = Math.min(1000, positive(q.page_size, 15))
return list(rows.slice((page - 1) * size, page * size).map(d => ({
...d, dept_id: [d.dept_id], dept_name: snapshot.departments.find(dep => dep.dept_id === d.dept_id)?.dept_name || ''
})), rows.length)
}
if (pathname !== '/adminapi/doctor.roster/lists') return null
const matches = snapshot.slots.filter(s =>
(!q.start_date || s.date >= q.start_date) && (!q.end_date || s.date <= q.end_date) &&
(!q.doctor_id || s.doctor_id === Number(q.doctor_id)) &&
(!q.status || s.status === Number(q.status)) && (!q.period || s.period === q.period)
)
// Production RosterLists returns all matching segments for the displayed week, without pagination.
return list(matches)
}