import type { VariablesContext } from "../middlewares.ts"; export async function getInspectionTemplates(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const templates = await apiClient.database.table('inspection_templates') .where('is_active', true) .select(); return c.json({ data: templates }); } catch (error) { return c.json({ error: "获取巡检模板失败" }, 500); } } export async function createInspectionTemplate(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const templateData = await c.req.json(); const [id] = await apiClient.database.table('inspection_templates').insert({ ...templateData, created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }); const [template] = await apiClient.database.table('inspection_templates') .where('id', id) .select(); return c.json({ data: template }); } catch (error) { return c.json({ error: "创建巡检模板失败" }, 500); } } export async function updateInspectionTemplate(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param('id')); const templateData = await c.req.json(); await apiClient.database.table('inspection_templates') .where('id', id) .update({ ...templateData, updated_at: apiClient.database.fn.now() }); const [template] = await apiClient.database.table('inspection_templates') .where('id', id) .select(); return c.json({ data: template }); } catch (error) { return c.json({ error: "更新巡检模板失败" }, 500); } } export async function deleteInspectionTemplate(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param('id')); await apiClient.database.table('inspection_templates') .where('id', id) .update({ is_active: false, updated_at: apiClient.database.fn.now() }); return c.json({ data: { success: true } }); } catch (error) { return c.json({ error: "删除巡检模板失败" }, 500); } } export async function createInspectionTask(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const taskData = await c.req.json(); const [id] = await apiClient.database.table('inspection_tasks').insert({ ...taskData, status: 'pending', created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }); const [task] = await apiClient.database.table('inspection_tasks') .where('id', id) .select(); return c.json({ data: task }); } catch (error) { return c.json({ error: "创建巡检任务失败" }, 500); } } export async function getInspectionTasks(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const page = Number(c.req.query("page")) || 1; const pageSize = Number(c.req.query("pageSize")) || 10; const offset = (page - 1) * pageSize; const deviceTypes = c.req.query("deviceTypes"); const query = apiClient.database.table('inspection_tasks'); if (deviceTypes) { const types = deviceTypes.split(','); query.whereRaw('device_types @> ?', [JSON.stringify(types)]); } const total = await query.clone().count(); const tasks = await query.limit(pageSize).offset(offset).select(); return c.json({ data: tasks, total: Number(total), page, pageSize }); } catch (error) { return c.json({ error: "获取巡检任务失败" }, 500); } } export async function runInspectionTask(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param('id')); await apiClient.database.table('inspection_tasks') .where('id', id) .update({ status: 'running', updated_at: apiClient.database.fn.now() }); return c.json({ data: { success: true } }); } catch (error) { return c.json({ error: "执行巡检任务失败" }, 500); } } export async function getInspectionResults(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const taskId = Number(c.req.param('taskId')); const results = await apiClient.database.table('inspection_results') .where('task_id', taskId) .select(); return c.json({ data: results }); } catch (error) { return c.json({ error: "获取巡检结果失败" }, 500); } } export async function exportInspectionReport(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const taskId = Number(c.req.param('taskId')); const results = await apiClient.database.table('inspection_results') .where('task_id', taskId) .select(); return c.json({ data: { data: results, format: 'excel' } }); } catch (error) { return c.json({ error: "导出巡检报告失败" }, 500); } } export async function getInspectionReportReceivers(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const receivers = await apiClient.database.table('inspection_report_receivers') .select(); return c.json({ data: receivers }); } catch (error) { return c.json({ error: "获取报告接收人失败" }, 500); } } export async function addInspectionReportReceiver(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const receiverData = await c.req.json(); const [id] = await apiClient.database.table('inspection_report_receivers').insert({ ...receiverData, created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }); const [receiver] = await apiClient.database.table('inspection_report_receivers') .where('id', id) .select(); return c.json({ data: receiver }); } catch (error) { return c.json({ error: "添加报告接收人失败" }, 500); } } export async function removeInspectionReportReceiver(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param('id')); await apiClient.database.table('inspection_report_receivers') .where('id', id) .delete(); return c.json({ data: { success: true } }); } catch (error) { return c.json({ error: "删除报告接收人失败" }, 500); } } export async function createAutoInspectionTask(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const { taskNo, intervalDays, deviceTypes, cronExpression } = await c.req.json(); if (!cronExpression && !intervalDays) { return c.json({ error: "定时任务必须提供cron表达式或间隔天数" }, 400); } const [id] = await apiClient.database.table('inspection_tasks').insert({ task_no: taskNo, schedule_type: 'scheduled', cron_expression: cronExpression || `0 0 */${intervalDays} * *`, device_types: deviceTypes || [], run_immediately: false, status: 'pending', created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }); const [task] = await apiClient.database.table('inspection_tasks') .where('id', id) .select(); return c.json({ data: task }); } catch (error) { return c.json({ error: "创建自动巡检任务失败" }, 500); } } export async function runManualInspectionTask(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const { deviceTypes } = await c.req.json(); if (!deviceTypes || deviceTypes.length === 0) { return c.json({ error: "必须选择至少一个设备类型" }, 400); } const [id] = await apiClient.database.table('inspection_tasks').insert({ name: `手动巡检-${new Date().toISOString()}`, schedule_type: 'manual', device_types: deviceTypes, run_immediately: true, status: 'running', created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }); const [task] = await apiClient.database.table('inspection_tasks') .where('id', id) .select(); return c.json({ data: task }); } catch (error) { return c.json({ error: "执行手动巡检失败" }, 500); } } export async function createNewYearInspection(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const { year, deviceTypes, receivers } = await c.req.json(); const [id] = await apiClient.database.table('inspection_tasks').insert({ name: `${year}年新年巡检`, schedule_type: 'yearly', device_types: deviceTypes, report_receivers: receivers, status: 'pending', created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }); const [task] = await apiClient.database.table('inspection_tasks') .where('id', id) .select(); return c.json({ data: task }); } catch (error) { return c.json({ error: "创建新年巡检任务失败" }, 500); } } export async function updateInspectionProgress(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param('id')); const { progress, checkedCount, issuesFound } = await c.req.json(); await apiClient.database.table('inspection_tasks') .where('id', id) .update({ progress, checked_count: checkedCount, issues_found: issuesFound, status: 'in_progress', updated_at: apiClient.database.fn.now() }); const [task] = await apiClient.database.table('inspection_tasks') .where('id', id) .select(); return c.json({ data: task }); } catch (error) { return c.json({ error: "更新巡检进度失败" }, 500); } } export async function completeInspection(c: VariablesContext) { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param('id')); const { results } = await c.req.json(); await apiClient.database.table('inspection_tasks') .where('id', id) .update({ status: 'completed', end_time: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }); // 保存巡检结果 await apiClient.database.table('inspection_results').insert( results.map((result: any) => ({ task_id: id, item_id: result.itemId, status: result.status, notes: result.notes, created_at: apiClient.database.fn.now() })) ); const [task] = await apiClient.database.table('inspection_tasks') .where('id', id) .select(); return c.json({ data: task }); } catch (error) { return c.json({ error: "完成巡检任务失败" }, 500); } }