autoAccessTasks.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const express = require('express');
  2. const router = express.Router();
  3. const { executeQuery } = require('../config/database');
  4. router.get('/', async (req, res) => {
  5. try {
  6. const { page = 1, pageSize = 10, search, status } = req.query;
  7. const offset = (page - 1) * pageSize;
  8. // 构建WHERE条件
  9. let whereConditions = [];
  10. let queryParams = [];
  11. // 添加搜索条件
  12. if (search) {
  13. whereConditions.push('tools_name LIKE ?');
  14. queryParams.push(`%${search}%`);
  15. }
  16. // 添加状态过滤条件
  17. if (status !== undefined && status !== '') {
  18. whereConditions.push('status = ?');
  19. queryParams.push(parseInt(status));
  20. }
  21. // 构建WHERE子句
  22. const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(' AND ')}` : '';
  23. const sql = `
  24. SELECT access_task_id, search_task_id, tools_name, tools_function_name,
  25. access_type, tools_function_desc, api_doc, api_class_name,
  26. operate_path_data, origin_content_link, status, fail_reason,
  27. create_time, update_time
  28. FROM tools_auto_access_task
  29. ${whereClause}
  30. ORDER BY create_time DESC
  31. LIMIT ? OFFSET ?
  32. `;
  33. const countSql = `SELECT COUNT(*) as total FROM tools_auto_access_task ${whereClause}`;
  34. // 为查询添加分页参数
  35. const sqlParams = [...queryParams, parseInt(pageSize), offset];
  36. const countParams = [...queryParams];
  37. const [data, countResult] = await Promise.all([
  38. executeQuery(sql, sqlParams),
  39. executeQuery(countSql, countParams)
  40. ]);
  41. res.json({
  42. data,
  43. total: countResult[0].total,
  44. page: parseInt(page),
  45. pageSize: parseInt(pageSize)
  46. });
  47. } catch (error) {
  48. console.error('Error fetching auto access tasks:', error);
  49. res.status(500).json({ error: 'Internal server error' });
  50. }
  51. });
  52. router.get('/:id', async (req, res) => {
  53. try {
  54. const { id } = req.params;
  55. const sql = `
  56. SELECT access_task_id, search_task_id, tools_name, tools_function_name,
  57. access_type, tools_function_desc, api_doc, api_class_name,
  58. operate_path_data, origin_content_link, status, fail_reason,
  59. create_time, update_time
  60. FROM tools_auto_access_task
  61. WHERE access_task_id = ?
  62. `;
  63. const data = await executeQuery(sql, [id]);
  64. if (data.length === 0) {
  65. return res.status(404).json({ error: 'Task not found' });
  66. }
  67. res.json(data[0]);
  68. } catch (error) {
  69. console.error('Error fetching auto access task detail:', error);
  70. res.status(500).json({ error: 'Internal server error' });
  71. }
  72. });
  73. router.put('/:id', async (req, res) => {
  74. try {
  75. const { id } = req.params;
  76. const {
  77. search_task_id, tools_name, tools_function_name, access_type,
  78. tools_function_desc, api_doc, api_class_name, operate_path_data,
  79. origin_content_link, status, fail_reason
  80. } = req.body;
  81. const sql = `
  82. UPDATE tools_auto_access_task
  83. SET search_task_id = ?, tools_name = ?, tools_function_name = ?,
  84. access_type = ?, tools_function_desc = ?, api_doc = ?,
  85. api_class_name = ?, operate_path_data = ?, origin_content_link = ?,
  86. status = ?, fail_reason = ?, update_time = NOW()
  87. WHERE access_task_id = ?
  88. `;
  89. await executeQuery(sql, [
  90. search_task_id, tools_name, tools_function_name, access_type,
  91. tools_function_desc, api_doc, api_class_name, operate_path_data,
  92. origin_content_link, status, fail_reason, id
  93. ]);
  94. res.json({ message: 'Task updated successfully' });
  95. } catch (error) {
  96. console.error('Error updating auto access task:', error);
  97. res.status(500).json({ error: 'Internal server error' });
  98. }
  99. });
  100. module.exports = router;