patients.go
| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "log/slog" |
| 7 | "strings" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | type Patient struct { |
| 12 | ID int64 |
| 13 | Name, Dob, Phone, Notes string |
| 14 | Archived bool |
| 15 | Created, Updated time.Time |
| 16 | Befunde, Sessions int |
| 17 | LastDate string // letzter Befund oder letzte Behandlung (YYYY-MM-DD) |
| 18 | } |
| 19 | |
| 20 | type Befund struct { |
| 21 | ID, PatientID int64 |
| 22 | Date, Title string |
| 23 | Data string // JSON, siehe Paket befund |
| 24 | Created, Updated time.Time |
| 25 | } |
| 26 | |
| 27 | type Session struct { |
| 28 | ID, PatientID int64 |
| 29 | Date, Goal, Note string |
| 30 | } |
| 31 | |
| 32 | // ---------- Patienten ---------- |
| 33 | |
| 34 | func (s *Store) Patients(ctx context.Context, q string, archived bool) ([]Patient, error) { |
| 35 | rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, p.dob, p.phone, p.notes, p.archived, p.created_at, p.updated_at, |
| 36 | (SELECT count(*) FROM befunde b WHERE b.patient_id = p.id), |
| 37 | (SELECT count(*) FROM sessions x WHERE x.patient_id = p.id), |
| 38 | COALESCE(max((SELECT max(date) FROM befunde b WHERE b.patient_id = p.id), |
| 39 | (SELECT max(date) FROM sessions x WHERE x.patient_id = p.id)), '') |
| 40 | FROM patients p |
| 41 | WHERE p.archived = ? AND (? = '' OR p.name LIKE '%' || ? || '%' OR p.dob LIKE '%' || ? || '%') |
| 42 | ORDER BY p.updated_at DESC LIMIT 500`, archived, q, q, q) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | defer rows.Close() |
| 47 | var out []Patient |
| 48 | for rows.Next() { |
| 49 | p, err := scanPatient(rows, true) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | out = append(out, p) |
| 54 | } |
| 55 | return out, rows.Err() |
| 56 | } |
| 57 | |
| 58 | func scanPatient(sc scanner, withStats bool) (Patient, error) { |
| 59 | var p Patient |
| 60 | var c, u int64 |
| 61 | var last sql.NullString |
| 62 | dest := []any{&p.ID, &p.Name, &p.Dob, &p.Phone, &p.Notes, &p.Archived, &c, &u} |
| 63 | if withStats { |
| 64 | dest = append(dest, &p.Befunde, &p.Sessions, &last) |
| 65 | } |
| 66 | err := sc.Scan(dest...) |
| 67 | p.Created, p.Updated, p.LastDate = time.Unix(c, 0), time.Unix(u, 0), last.String |
| 68 | return p, err |
| 69 | } |
| 70 | |
| 71 | func (s *Store) Patient(ctx context.Context, id int64) (Patient, error) { |
| 72 | p, err := scanPatient(s.db.QueryRowContext(ctx, `SELECT id, name, dob, phone, notes, archived, created_at, updated_at |
| 73 | FROM patients WHERE id = ?`, id), false) |
| 74 | return p, notFound(err) |
| 75 | } |
| 76 | |
| 77 | func (s *Store) CreatePatient(ctx context.Context, p Patient) (int64, error) { |
| 78 | res, err := s.db.ExecContext(ctx, `INSERT INTO patients (name, dob, phone, notes) VALUES (?, ?, ?, ?)`, p.Name, p.Dob, p.Phone, p.Notes) |
| 79 | if err != nil { |
| 80 | return 0, err |
| 81 | } |
| 82 | return res.LastInsertId() |
| 83 | } |
| 84 | |
| 85 | func (s *Store) UpdatePatient(ctx context.Context, p Patient) error { |
| 86 | res, err := s.db.ExecContext(ctx, `UPDATE patients SET name=?, dob=?, phone=?, notes=?, updated_at=unixepoch() WHERE id=?`, |
| 87 | p.Name, p.Dob, p.Phone, p.Notes, p.ID) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if n, _ := res.RowsAffected(); n == 0 { |
| 92 | return ErrNotFound |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (s *Store) SetArchived(ctx context.Context, id int64, archived bool) error { |
| 98 | _, err := s.db.ExecContext(ctx, `UPDATE patients SET archived=?, updated_at=unixepoch() WHERE id=?`, archived, id) |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | func (s *Store) DeletePatient(ctx context.Context, id int64) error { |
| 103 | _, err := s.db.ExecContext(ctx, `DELETE FROM patients WHERE id = ?`, id) |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | // touchPatient sortiert den Patienten in der Liste nach oben; ein Fehler ist nicht kritisch. |
| 108 | func (s *Store) touchPatient(ctx context.Context, id int64) { |
| 109 | if _, err := s.db.ExecContext(ctx, `UPDATE patients SET updated_at = unixepoch() WHERE id = ?`, id); err != nil { |
| 110 | slog.Warn("patient updated_at", "id", id, "err", err) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // FindPatient sucht exakt nach Name (+ Geburtsdatum, falls angegeben) – für den Import. |
| 115 | func (s *Store) FindPatient(ctx context.Context, name, dob string) (int64, bool, error) { |
| 116 | var id int64 |
| 117 | err := s.db.QueryRowContext(ctx, `SELECT id FROM patients WHERE lower(trim(name)) = lower(trim(?)) |
| 118 | AND (? = '' OR dob = '' OR dob = ?) ORDER BY id LIMIT 1`, name, dob, dob).Scan(&id) |
| 119 | if err == sql.ErrNoRows { |
| 120 | return 0, false, nil |
| 121 | } |
| 122 | return id, err == nil, err |
| 123 | } |
| 124 | |
| 125 | // ---------- Befunde ---------- |
| 126 | |
| 127 | const befundCols = `id, patient_id, date, title, data, created_at, updated_at` |
| 128 | |
| 129 | func scanBefund(sc scanner) (Befund, error) { |
| 130 | var b Befund |
| 131 | var c, u int64 |
| 132 | err := sc.Scan(&b.ID, &b.PatientID, &b.Date, &b.Title, &b.Data, &c, &u) |
| 133 | b.Created, b.Updated = time.Unix(c, 0), time.Unix(u, 0) |
| 134 | return b, err |
| 135 | } |
| 136 | |
| 137 | func (s *Store) Befunde(ctx context.Context, pid int64) ([]Befund, error) { |
| 138 | rows, err := s.db.QueryContext(ctx, `SELECT `+befundCols+` FROM befunde WHERE patient_id = ? ORDER BY date DESC, id DESC`, pid) |
| 139 | if err != nil { |
| 140 | return nil, err |
| 141 | } |
| 142 | defer rows.Close() |
| 143 | var out []Befund |
| 144 | for rows.Next() { |
| 145 | b, err := scanBefund(rows) |
| 146 | if err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | out = append(out, b) |
| 150 | } |
| 151 | return out, rows.Err() |
| 152 | } |
| 153 | |
| 154 | func (s *Store) Befund(ctx context.Context, id int64) (Befund, error) { |
| 155 | b, err := scanBefund(s.db.QueryRowContext(ctx, `SELECT `+befundCols+` FROM befunde WHERE id = ?`, id)) |
| 156 | return b, notFound(err) |
| 157 | } |
| 158 | |
| 159 | func (s *Store) CreateBefund(ctx context.Context, b Befund) (int64, error) { |
| 160 | if b.Date == "" { |
| 161 | b.Date = time.Now().Format("2006-01-02") |
| 162 | } |
| 163 | if strings.TrimSpace(b.Title) == "" { |
| 164 | b.Title = "Befund" |
| 165 | } |
| 166 | if b.Data == "" { |
| 167 | b.Data = "{}" |
| 168 | } |
| 169 | res, err := s.db.ExecContext(ctx, `INSERT INTO befunde (patient_id, date, title, data) VALUES (?, ?, ?, ?)`, |
| 170 | b.PatientID, b.Date, b.Title, b.Data) |
| 171 | if err != nil { |
| 172 | return 0, err |
| 173 | } |
| 174 | s.touchPatient(ctx, b.PatientID) |
| 175 | return res.LastInsertId() |
| 176 | } |
| 177 | |
| 178 | func (s *Store) UpdateBefund(ctx context.Context, b Befund) error { |
| 179 | res, err := s.db.ExecContext(ctx, `UPDATE befunde SET date=?, title=?, data=?, updated_at=unixepoch() WHERE id=?`, |
| 180 | b.Date, b.Title, b.Data, b.ID) |
| 181 | if err != nil { |
| 182 | return err |
| 183 | } |
| 184 | if n, _ := res.RowsAffected(); n == 0 { |
| 185 | return ErrNotFound |
| 186 | } |
| 187 | s.touchPatient(ctx, b.PatientID) |
| 188 | return nil |
| 189 | } |
| 190 | |
| 191 | func (s *Store) DeleteBefund(ctx context.Context, id int64) (int64, error) { |
| 192 | var pid int64 |
| 193 | err := s.db.QueryRowContext(ctx, `DELETE FROM befunde WHERE id = ? RETURNING patient_id`, id).Scan(&pid) |
| 194 | return pid, notFound(err) |
| 195 | } |
| 196 | |
| 197 | // ---------- Behandlungsverlauf ---------- |
| 198 | |
| 199 | func (s *Store) Sessions(ctx context.Context, pid int64) ([]Session, error) { |
| 200 | rows, err := s.db.QueryContext(ctx, `SELECT id, patient_id, date, goal, note FROM sessions |
| 201 | WHERE patient_id = ? ORDER BY date DESC, id DESC`, pid) |
| 202 | if err != nil { |
| 203 | return nil, err |
| 204 | } |
| 205 | defer rows.Close() |
| 206 | var out []Session |
| 207 | for rows.Next() { |
| 208 | var x Session |
| 209 | if err := rows.Scan(&x.ID, &x.PatientID, &x.Date, &x.Goal, &x.Note); err != nil { |
| 210 | return nil, err |
| 211 | } |
| 212 | out = append(out, x) |
| 213 | } |
| 214 | return out, rows.Err() |
| 215 | } |
| 216 | |
| 217 | func (s *Store) AddSession(ctx context.Context, x Session) error { |
| 218 | if x.Date == "" { |
| 219 | x.Date = time.Now().Format("2006-01-02") |
| 220 | } |
| 221 | _, err := s.db.ExecContext(ctx, `INSERT INTO sessions (patient_id, date, goal, note) VALUES (?, ?, ?, ?)`, x.PatientID, x.Date, x.Goal, x.Note) |
| 222 | if err == nil { |
| 223 | s.touchPatient(ctx, x.PatientID) |
| 224 | } |
| 225 | return err |
| 226 | } |
| 227 | |
| 228 | func (s *Store) DeleteSession(ctx context.Context, id int64) (int64, error) { |
| 229 | var pid int64 |
| 230 | err := s.db.QueryRowContext(ctx, `DELETE FROM sessions WHERE id = ? RETURNING patient_id`, id).Scan(&pid) |
| 231 | return pid, notFound(err) |
| 232 | } |
| 233 | |
| 234 | // ---------- Wissen ↔ Patient ---------- |
| 235 | |
| 236 | func (s *Store) PatientEntries(ctx context.Context, pid int64) ([]Entry, error) { |
| 237 | return s.entries(ctx, `SELECT `+entryCols+` FROM patient_entries pe JOIN entries e ON e.id = pe.entry_id |
| 238 | WHERE pe.patient_id = ? ORDER BY e.kind, e.title COLLATE NOCASE`, pid) |
| 239 | } |
| 240 | |
| 241 | func (s *Store) LinkPatientEntry(ctx context.Context, pid, eid int64) error { |
| 242 | _, err := s.db.ExecContext(ctx, `INSERT OR IGNORE INTO patient_entries (patient_id, entry_id) VALUES (?, ?)`, pid, eid) |
| 243 | return err |
| 244 | } |
| 245 | |
| 246 | func (s *Store) UnlinkPatientEntry(ctx context.Context, pid, eid int64) error { |
| 247 | _, err := s.db.ExecContext(ctx, `DELETE FROM patient_entries WHERE patient_id = ? AND entry_id = ?`, pid, eid) |
| 248 | return err |
| 249 | } |
| 250 | |
| 251 | // EntriesOfKind z. B. alle Tests als Vorschlagsliste im Befund. |
| 252 | func (s *Store) EntriesOfKind(ctx context.Context, kinds ...string) ([]Entry, error) { |
| 253 | if len(kinds) == 0 { |
| 254 | return s.entries(ctx, `SELECT `+entryCols+` FROM entries e ORDER BY e.kind, e.title COLLATE NOCASE`) |
| 255 | } |
| 256 | ph := strings.TrimSuffix(strings.Repeat("?,", len(kinds)), ",") |
| 257 | args := make([]any, len(kinds)) |
| 258 | for i, k := range kinds { |
| 259 | args[i] = k |
| 260 | } |
| 261 | return s.entries(ctx, `SELECT `+entryCols+` FROM entries e WHERE e.kind IN (`+ph+`) ORDER BY e.kind, e.title COLLATE NOCASE`, args...) |
| 262 | } |
| 263 | |
| 264 | // ---------- Handouts je Patient ---------- |
| 265 | |
| 266 | func (s *Store) HandoutsForPatient(ctx context.Context, pid int64) ([]Handout, error) { |
| 267 | rows, err := s.db.QueryContext(ctx, `SELECT h.id, h.title, h.recipient, h.notes, h.created_at, h.updated_at, |
| 268 | (SELECT count(*) FROM handout_items i WHERE i.handout_id = h.id) |
| 269 | FROM handouts h WHERE h.patient_id = ? ORDER BY h.updated_at DESC`, pid) |
| 270 | if err != nil { |
| 271 | return nil, err |
| 272 | } |
| 273 | defer rows.Close() |
| 274 | var out []Handout |
| 275 | for rows.Next() { |
| 276 | var h Handout |
| 277 | var c, u int64 |
| 278 | if err := rows.Scan(&h.ID, &h.Title, &h.Recipient, &h.Notes, &c, &u, &h.Count); err != nil { |
| 279 | return nil, err |
| 280 | } |
| 281 | h.Created, h.Updated = time.Unix(c, 0), time.Unix(u, 0) |
| 282 | out = append(out, h) |
| 283 | } |
| 284 | return out, rows.Err() |
| 285 | } |
| 286 | |
| 287 | func (s *Store) CreateHandoutFor(ctx context.Context, title string, p Patient) (int64, error) { |
| 288 | res, err := s.db.ExecContext(ctx, `INSERT INTO handouts (title, recipient, patient_id) VALUES (?, ?, ?)`, title, p.Name, p.ID) |
| 289 | if err != nil { |
| 290 | return 0, err |
| 291 | } |
| 292 | return res.LastInsertId() |
| 293 | } |
| 294 | |
| 295 | // HandoutPatient liefert den zugeordneten Patienten eines Handouts (0 = keiner). |
| 296 | func (s *Store) HandoutPatient(ctx context.Context, hid int64) (int64, string, error) { |
| 297 | var pid sql.NullInt64 |
| 298 | var name sql.NullString |
| 299 | err := s.db.QueryRowContext(ctx, `SELECT h.patient_id, p.name FROM handouts h LEFT JOIN patients p ON p.id = h.patient_id |
| 300 | WHERE h.id = ?`, hid).Scan(&pid, &name) |
| 301 | return pid.Int64, name.String, notFound(err) |
| 302 | } |
| 303 |