prepare('UPDATE ticket_acks SET acked_at = ? WHERE staff_id = ? AND ticket_id = ?'); $upd->execute([$now, $staffId, $ticketId]); if ($upd->rowCount() === 0) { $exists = $pdo->prepare('SELECT 1 FROM ticket_acks WHERE staff_id = ? AND ticket_id = ?'); $exists->execute([$staffId, $ticketId]); if (!$exists->fetchColumn()) { $pdo->prepare('INSERT INTO ticket_acks (staff_id, ticket_id, acked_at) VALUES (?,?,?)')->execute([$staffId, $ticketId, $now]); } } } catch (Throwable $e) { error_log('ticket_ack: ' . $e->getMessage()); } } /** * Tickets, die für den Mitarbeiter Aufmerksamkeit brauchen. * @return array{unassigned:list,mine:list} */ function staff_attention_tickets(PDO $pdo, array $staff, int $limit = 300): array { [$deptSql, $deptParams] = dept_filter_sql(staff_department_ids($staff), 't.department_id'); $snooze = function_exists('ticket_not_snoozed_sql') ? ticket_not_snoozed_sql('t') : '1=1'; $stmt = $pdo->prepare( "SELECT t.id, t.assigned_staff_id FROM tickets t LEFT JOIN ticket_acks a ON a.ticket_id = t.id AND a.staff_id = ? WHERE t.status = 'open' AND t.is_spam = 0 AND t.merged_into IS NULL AND t.source <> 'staff' AND (t.assigned_staff_id IS NULL OR t.assigned_staff_id = ?) AND {$snooze}{$deptSql} AND (a.acked_at IS NULL OR a.acked_at < COALESCE((SELECT MAX(th.created_at) FROM ticket_threads th WHERE th.ticket_id = t.id AND th.author_type = 'customer'), t.created_at)) ORDER BY t.id DESC LIMIT " . max(1, $limit) ); $stmt->execute([(int) $staff['id'], (int) $staff['id'], ...$deptParams]); $out = ['unassigned' => [], 'mine' => []]; foreach ($stmt->fetchAll() as $row) { $out[$row['assigned_staff_id'] === null ? 'unassigned' : 'mine'][] = (int) $row['id']; } return $out; }