issue-ai/src/lib/monitor/resolution-side-effects.ts

35 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// src/lib/monitor/resolution-side-effects.ts
import { getDb } from '@/lib/db'
/**
* 结单副作用:回写 fault_records + 清理 reminder_state。
* 同步执行better-sqlite3对手动/外部/导入工单(无 fault_records是安全 no-op。
* 必须在 notifyTicketResolved 之前调用(写 DB 不依赖推送结果)。
*/
export function applyResolutionSideEffects(ticketId: number): void {
const db = getDb()
const ticket = db.prepare(`
SELECT ticket_no, close_time, assign_time, device_sn
FROM tickets WHERE id = ?
`).get(ticketId) as {
ticket_no: string; close_time: string | null; assign_time: string; device_sn: string
} | undefined
if (!ticket || !ticket.close_time) return
// 1. 回写 fault_records若存在 ongoing 记录)
const fault = db.prepare(
"SELECT id FROM fault_records WHERE order_number = ? AND status = 'ongoing'"
).get(ticket.ticket_no) as { id: number } | undefined
if (fault) {
const assignMs = new Date(ticket.assign_time.replace(' ', 'T') + '+08:00').getTime()
const closeMs = new Date(ticket.close_time.replace(' ', 'T') + '+08:00').getTime()
const durationSec = Math.floor((closeMs - assignMs) / 1000)
db.prepare(
"UPDATE fault_records SET status = 'resolved', recovery_time = ?, duration_seconds = ? WHERE id = ?"
).run(ticket.close_time, durationSec, fault.id)
}
// 2. 清理 reminder_state无对应记录时是安全 no-op
db.prepare("DELETE FROM reminder_state WHERE order_number = ?").run(ticket.ticket_no)
}