inspections.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import type { VariablesContext } from "../middlewares.ts";
  2. export async function getInspectionTemplates(c: VariablesContext) {
  3. try {
  4. const apiClient = c.get('apiClient');
  5. const templates = await apiClient.database.table('inspection_templates')
  6. .where('is_active', true)
  7. .select();
  8. return c.json({ data: templates });
  9. } catch (error) {
  10. return c.json({ error: "获取巡检模板失败" }, 500);
  11. }
  12. }
  13. export async function createInspectionTemplate(c: VariablesContext) {
  14. try {
  15. const apiClient = c.get('apiClient');
  16. const templateData = await c.req.json();
  17. const [id] = await apiClient.database.table('inspection_templates').insert({
  18. ...templateData,
  19. created_at: apiClient.database.fn.now(),
  20. updated_at: apiClient.database.fn.now()
  21. });
  22. const [template] = await apiClient.database.table('inspection_templates')
  23. .where('id', id)
  24. .select();
  25. return c.json({ data: template });
  26. } catch (error) {
  27. return c.json({ error: "创建巡检模板失败" }, 500);
  28. }
  29. }
  30. export async function updateInspectionTemplate(c: VariablesContext) {
  31. try {
  32. const apiClient = c.get('apiClient');
  33. const id = Number(c.req.param('id'));
  34. const templateData = await c.req.json();
  35. await apiClient.database.table('inspection_templates')
  36. .where('id', id)
  37. .update({
  38. ...templateData,
  39. updated_at: apiClient.database.fn.now()
  40. });
  41. const [template] = await apiClient.database.table('inspection_templates')
  42. .where('id', id)
  43. .select();
  44. return c.json({ data: template });
  45. } catch (error) {
  46. return c.json({ error: "更新巡检模板失败" }, 500);
  47. }
  48. }
  49. export async function deleteInspectionTemplate(c: VariablesContext) {
  50. try {
  51. const apiClient = c.get('apiClient');
  52. const id = Number(c.req.param('id'));
  53. await apiClient.database.table('inspection_templates')
  54. .where('id', id)
  55. .update({
  56. is_active: false,
  57. updated_at: apiClient.database.fn.now()
  58. });
  59. return c.json({ data: { success: true } });
  60. } catch (error) {
  61. return c.json({ error: "删除巡检模板失败" }, 500);
  62. }
  63. }
  64. export async function createInspectionTask(c: VariablesContext) {
  65. try {
  66. const apiClient = c.get('apiClient');
  67. const taskData = await c.req.json();
  68. const [id] = await apiClient.database.table('inspection_tasks').insert({
  69. ...taskData,
  70. status: 'pending',
  71. created_at: apiClient.database.fn.now(),
  72. updated_at: apiClient.database.fn.now()
  73. });
  74. const [task] = await apiClient.database.table('inspection_tasks')
  75. .where('id', id)
  76. .select();
  77. return c.json({ data: task });
  78. } catch (error) {
  79. return c.json({ error: "创建巡检任务失败" }, 500);
  80. }
  81. }
  82. export async function getInspectionTasks(c: VariablesContext) {
  83. try {
  84. const apiClient = c.get('apiClient');
  85. const page = Number(c.req.query("page")) || 1;
  86. const pageSize = Number(c.req.query("pageSize")) || 10;
  87. const offset = (page - 1) * pageSize;
  88. const deviceTypes = c.req.query("deviceTypes");
  89. const query = apiClient.database.table('inspection_tasks');
  90. if (deviceTypes) {
  91. const types = deviceTypes.split(',');
  92. query.whereRaw('device_types @> ?', [JSON.stringify(types)]);
  93. }
  94. const total = await query.clone().count();
  95. const tasks = await query.limit(pageSize).offset(offset).select();
  96. return c.json({
  97. data: tasks,
  98. total: Number(total),
  99. page,
  100. pageSize
  101. });
  102. } catch (error) {
  103. return c.json({ error: "获取巡检任务失败" }, 500);
  104. }
  105. }
  106. export async function runInspectionTask(c: VariablesContext) {
  107. try {
  108. const apiClient = c.get('apiClient');
  109. const id = Number(c.req.param('id'));
  110. await apiClient.database.table('inspection_tasks')
  111. .where('id', id)
  112. .update({
  113. status: 'running',
  114. updated_at: apiClient.database.fn.now()
  115. });
  116. return c.json({ data: { success: true } });
  117. } catch (error) {
  118. return c.json({ error: "执行巡检任务失败" }, 500);
  119. }
  120. }
  121. export async function getInspectionResults(c: VariablesContext) {
  122. try {
  123. const apiClient = c.get('apiClient');
  124. const taskId = Number(c.req.param('taskId'));
  125. const results = await apiClient.database.table('inspection_results')
  126. .where('task_id', taskId)
  127. .select();
  128. return c.json({ data: results });
  129. } catch (error) {
  130. return c.json({ error: "获取巡检结果失败" }, 500);
  131. }
  132. }
  133. export async function exportInspectionReport(c: VariablesContext) {
  134. try {
  135. const apiClient = c.get('apiClient');
  136. const taskId = Number(c.req.param('taskId'));
  137. const results = await apiClient.database.table('inspection_results')
  138. .where('task_id', taskId)
  139. .select();
  140. return c.json({ data: { data: results, format: 'excel' } });
  141. } catch (error) {
  142. return c.json({ error: "导出巡检报告失败" }, 500);
  143. }
  144. }
  145. export async function getInspectionReportReceivers(c: VariablesContext) {
  146. try {
  147. const apiClient = c.get('apiClient');
  148. const receivers = await apiClient.database.table('inspection_report_receivers')
  149. .select();
  150. return c.json({ data: receivers });
  151. } catch (error) {
  152. return c.json({ error: "获取报告接收人失败" }, 500);
  153. }
  154. }
  155. export async function addInspectionReportReceiver(c: VariablesContext) {
  156. try {
  157. const apiClient = c.get('apiClient');
  158. const receiverData = await c.req.json();
  159. const [id] = await apiClient.database.table('inspection_report_receivers').insert({
  160. ...receiverData,
  161. created_at: apiClient.database.fn.now(),
  162. updated_at: apiClient.database.fn.now()
  163. });
  164. const [receiver] = await apiClient.database.table('inspection_report_receivers')
  165. .where('id', id)
  166. .select();
  167. return c.json({ data: receiver });
  168. } catch (error) {
  169. return c.json({ error: "添加报告接收人失败" }, 500);
  170. }
  171. }
  172. export async function removeInspectionReportReceiver(c: VariablesContext) {
  173. try {
  174. const apiClient = c.get('apiClient');
  175. const id = Number(c.req.param('id'));
  176. await apiClient.database.table('inspection_report_receivers')
  177. .where('id', id)
  178. .delete();
  179. return c.json({ data: { success: true } });
  180. } catch (error) {
  181. return c.json({ error: "删除报告接收人失败" }, 500);
  182. }
  183. }
  184. export async function createAutoInspectionTask(c: VariablesContext) {
  185. try {
  186. const apiClient = c.get('apiClient');
  187. const { taskNo, intervalDays, deviceTypes, cronExpression } = await c.req.json();
  188. if (!cronExpression && !intervalDays) {
  189. return c.json({ error: "定时任务必须提供cron表达式或间隔天数" }, 400);
  190. }
  191. const [id] = await apiClient.database.table('inspection_tasks').insert({
  192. task_no: taskNo,
  193. schedule_type: 'scheduled',
  194. cron_expression: cronExpression || `0 0 */${intervalDays} * *`,
  195. device_types: deviceTypes || [],
  196. run_immediately: false,
  197. status: 'pending',
  198. created_at: apiClient.database.fn.now(),
  199. updated_at: apiClient.database.fn.now()
  200. });
  201. const [task] = await apiClient.database.table('inspection_tasks')
  202. .where('id', id)
  203. .select();
  204. return c.json({ data: task });
  205. } catch (error) {
  206. return c.json({ error: "创建自动巡检任务失败" }, 500);
  207. }
  208. }
  209. export async function runManualInspectionTask(c: VariablesContext) {
  210. try {
  211. const apiClient = c.get('apiClient');
  212. const { deviceTypes } = await c.req.json();
  213. if (!deviceTypes || deviceTypes.length === 0) {
  214. return c.json({ error: "必须选择至少一个设备类型" }, 400);
  215. }
  216. const [id] = await apiClient.database.table('inspection_tasks').insert({
  217. name: `手动巡检-${new Date().toISOString()}`,
  218. schedule_type: 'manual',
  219. device_types: deviceTypes,
  220. run_immediately: true,
  221. status: 'running',
  222. created_at: apiClient.database.fn.now(),
  223. updated_at: apiClient.database.fn.now()
  224. });
  225. const [task] = await apiClient.database.table('inspection_tasks')
  226. .where('id', id)
  227. .select();
  228. return c.json({ data: task });
  229. } catch (error) {
  230. return c.json({ error: "执行手动巡检失败" }, 500);
  231. }
  232. }
  233. export async function createNewYearInspection(c: VariablesContext) {
  234. try {
  235. const apiClient = c.get('apiClient');
  236. const { year, deviceTypes, receivers } = await c.req.json();
  237. const [id] = await apiClient.database.table('inspection_tasks').insert({
  238. name: `${year}年新年巡检`,
  239. schedule_type: 'yearly',
  240. device_types: deviceTypes,
  241. report_receivers: receivers,
  242. status: 'pending',
  243. created_at: apiClient.database.fn.now(),
  244. updated_at: apiClient.database.fn.now()
  245. });
  246. const [task] = await apiClient.database.table('inspection_tasks')
  247. .where('id', id)
  248. .select();
  249. return c.json({ data: task });
  250. } catch (error) {
  251. return c.json({ error: "创建新年巡检任务失败" }, 500);
  252. }
  253. }
  254. export async function updateInspectionProgress(c: VariablesContext) {
  255. try {
  256. const apiClient = c.get('apiClient');
  257. const id = Number(c.req.param('id'));
  258. const { progress, checkedCount, issuesFound } = await c.req.json();
  259. await apiClient.database.table('inspection_tasks')
  260. .where('id', id)
  261. .update({
  262. progress,
  263. checked_count: checkedCount,
  264. issues_found: issuesFound,
  265. status: 'in_progress',
  266. updated_at: apiClient.database.fn.now()
  267. });
  268. const [task] = await apiClient.database.table('inspection_tasks')
  269. .where('id', id)
  270. .select();
  271. return c.json({ data: task });
  272. } catch (error) {
  273. return c.json({ error: "更新巡检进度失败" }, 500);
  274. }
  275. }
  276. export async function completeInspection(c: VariablesContext) {
  277. try {
  278. const apiClient = c.get('apiClient');
  279. const id = Number(c.req.param('id'));
  280. const { results } = await c.req.json();
  281. await apiClient.database.table('inspection_tasks')
  282. .where('id', id)
  283. .update({
  284. status: 'completed',
  285. end_time: apiClient.database.fn.now(),
  286. updated_at: apiClient.database.fn.now()
  287. });
  288. // 保存巡检结果
  289. await apiClient.database.table('inspection_results').insert(
  290. results.map((result: any) => ({
  291. task_id: id,
  292. item_id: result.itemId,
  293. status: result.status,
  294. notes: result.notes,
  295. created_at: apiClient.database.fn.now()
  296. }))
  297. );
  298. const [task] = await apiClient.database.table('inspection_tasks')
  299. .where('id', id)
  300. .select();
  301. return c.json({ data: task });
  302. } catch (error) {
  303. return c.json({ error: "完成巡检任务失败" }, 500);
  304. }
  305. }