| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- 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);
- }
- }
|