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

146 lines
5.9 KiB
JavaScript

// Local, synthetic scheduling ledger. No transport, database or business writes.
// IDs are deterministic for a calendar slot; appointment status is evaluated at snapshot time.
const statusNames = {
registered: '已挂号',
completed: '已完成',
canceled: '已取消',
expired: '已过号'
}
const pad = (n) => String(n).padStart(2, '0')
const iso = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
const minuteOf = (s) => Number(s.slice(0, 2)) * 60 + Number(s.slice(3, 5))
const asTime = (m) => `${pad(Math.floor(m / 60))}:${pad(m % 60)}`
export function makeAppointments({ doctors, slots, patients, assistants, now }) {
const rows = [],
occupied = new Set(),
today = iso(now),
minute = now.getHours() * 60 + now.getMinutes()
const byDoctor = new Map(doctors.map((d) => [d.id, d]))
for (const slot of slots) {
if (slot.status !== 1) continue
const doc = byDoctor.get(slot.doctor_id),
team = assistants.filter((a) => a.dept_id === doc.dept_id)
const capacity = Math.min(
slot.quota || Infinity,
Math.floor((minuteOf(slot.end_time) - minuteOf(slot.start_time)) / slot.slot_minutes)
)
const n = Math.min(capacity, 2 + ((slot.id + doc.id) % 7))
for (let i = 0; i < n; i++) {
const id = slot.id * 16 + i,
start = minuteOf(slot.start_time) + i * slot.slot_minutes
const future = slot.date > today || (slot.date === today && start >= minute)
const status =
id % 11 === 0
? 'canceled'
: future
? 'registered'
: id % 9 === 0
? 'expired'
: 'completed'
let pi = (Math.floor(slot.id / 100) + doc.id * 7 + i * 5) % patients.length
const occupiedKey = (p) => `${slot.date}/${start}/${p.id}`
for (let j = 0; j < patients.length && occupied.has(occupiedKey(patients[pi])); j++)
pi = (pi + 1) % patients.length
const patient = patients[pi]
if (status !== 'canceled') {
if (occupied.has(occupiedKey(patient))) continue
occupied.add(occupiedKey(patient))
slot.booked_count++
}
rows.push({
id,
booking_no: `YY-${slot.date.replaceAll('-', '')}-${doc.id}-${pad(Math.floor(start / slot.slot_minutes))}`,
slot_id: slot.id,
doctor_id: doc.id,
dept_id: doc.dept_id,
assistant_id: team[(i + doc.id) % team.length].id,
patient_id: patient.id,
date: slot.date,
time: asTime(start),
status,
visit_type: i % 3 ? '复诊' : '初诊'
})
}
}
return rows
}
export function resolveAppointments(pathname, q, snapshot, memberIdForPatient) {
if (pathname !== '/adminapi/local.preview/appointments' || q.export != null) return null
const { appointments, departments, doctors, assistants, patients, slots, orders, now } =
snapshot
const map = (a, id = 'id') => new Map(a.map((v) => [v[id], v]))
const [ds, dr, as, ps, ss] = [
map(departments, 'dept_id'),
map(doctors),
map(assistants),
map(patients),
map(slots)
]
const counts = new Map()
for (const o of orders)
if (o.patient_id) counts.set(o.patient_id, 1 + (counts.get(o.patient_id) || 0))
const kw = String(q.keyword || '')
.trim()
.toLowerCase(),
today = iso(now)
let matches = appointments.filter(
(a) =>
(!q.dept_id || a.dept_id === Number(q.dept_id)) &&
(!q.patient_id || a.patient_id === Number(q.patient_id)) &&
(!q.start_date || a.date >= q.start_date) &&
(!q.end_date || a.date <= q.end_date) &&
(!kw ||
[
a.booking_no,
ps.get(a.patient_id).patient_name,
dr.get(a.doctor_id).name,
as.get(a.assistant_id).name
].some((s) => s.toLowerCase().includes(kw)))
)
const all = matches.length,
summary = Object.fromEntries(
Object.keys(statusNames).map((s) => [s, matches.filter((r) => r.status === s).length])
)
if (q.status) matches = matches.filter((a) => a.status === q.status)
// Today's bookings first, then upcoming earliest first, then history most recent first.
const group = (a) => (a.date === today ? 0 : a.date > today ? 1 : 2)
matches.sort(
(a, b) =>
group(a) - group(b) ||
(group(a) === 2 ? b.date.localeCompare(a.date) : a.date.localeCompare(b.date)) ||
a.time.localeCompare(b.time) ||
a.id - b.id
)
const size = Math.min(100, Math.max(1, parseInt(q.page_size, 10) || 15)),
page = Math.max(1, parseInt(q.page_no, 10) || 1)
const lists = matches
.slice((page - 1) * size, page * size)
.map((a) => ({
...a,
member_id: memberIdForPatient(a.patient_id),
patient_name: ps.get(a.patient_id).patient_name,
doctor_name: dr.get(a.doctor_id).name,
dept_name: ds.get(a.dept_id).dept_name,
assistant_name: as.get(a.assistant_id).name,
status_name: statusNames[a.status],
start_time: ss.get(a.slot_id).start_time,
end_time: ss.get(a.slot_id).end_time,
patient_order_count: counts.get(a.patient_id) || 0
}))
return {
code: 1,
data: {
lists,
count: matches.length,
all,
summary,
departments,
today,
patient_name: ps.get(Number(q.patient_id))?.patient_name || ''
},
show: 0,
msg: ''
}
}