pendingTools.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 { search, toolsName, status, page = 1, pageSize = 10 } = req.query;
  7. const offset = (page - 1) * pageSize;
  8. let sql = `
  9. SELECT search_task_id, tools_name, tools_function_name, tools_function_desc,
  10. status, fail_reason, create_time, update_time
  11. FROM tools_info_search_task
  12. `;
  13. let params = [];
  14. let whereConditions = [];
  15. // 添加工具名称搜索条件
  16. if (toolsName) {
  17. whereConditions.push('tools_name LIKE ?');
  18. params.push(`%${toolsName}%`);
  19. }
  20. // 添加状态搜索条件
  21. if (status !== undefined && status !== '') {
  22. whereConditions.push('status = ?');
  23. params.push(parseInt(status));
  24. }
  25. // 添加全文搜索条件
  26. if (search) {
  27. whereConditions.push('(tools_name LIKE ? OR tools_function_name LIKE ? OR tools_function_desc LIKE ?)');
  28. params.push(`%${search}%`, `%${search}%`, `%${search}%`);
  29. }
  30. // 构建WHERE子句
  31. if (whereConditions.length > 0) {
  32. sql += ` WHERE ${whereConditions.join(' AND ')}`;
  33. }
  34. sql += ` ORDER BY create_time DESC LIMIT ? OFFSET ?`;
  35. params.push(parseInt(pageSize), offset);
  36. // 构建计数查询
  37. let countSql = `SELECT COUNT(*) as total FROM tools_info_search_task`;
  38. let countParams = [];
  39. if (whereConditions.length > 0) {
  40. countSql += ` WHERE ${whereConditions.join(' AND ')}`;
  41. // 重新构建计数查询的参数
  42. if (toolsName) {
  43. countParams.push(`%${toolsName}%`);
  44. }
  45. if (status !== undefined && status !== '') {
  46. countParams.push(parseInt(status));
  47. }
  48. if (search) {
  49. countParams.push(`%${search}%`, `%${search}%`, `%${search}%`);
  50. }
  51. }
  52. const [data, countResult] = await Promise.all([
  53. executeQuery(sql, params),
  54. executeQuery(countSql, countParams)
  55. ]);
  56. res.json({
  57. data,
  58. total: countResult[0].total,
  59. page: parseInt(page),
  60. pageSize: parseInt(pageSize)
  61. });
  62. } catch (error) {
  63. console.error('Error fetching pending tools:', error);
  64. res.status(500).json({ error: 'Internal server error' });
  65. }
  66. });
  67. // 新增待接入工具
  68. router.post('/', async (req, res) => {
  69. try {
  70. const {
  71. search_task_id,
  72. tools_name,
  73. tools_function_name,
  74. tools_function_desc,
  75. status = 0
  76. } = req.body;
  77. // 验证必填字段
  78. if (!search_task_id || !tools_name || !tools_function_name || !tools_function_desc) {
  79. return res.status(400).json({
  80. success: false,
  81. message: '缺少必填字段'
  82. });
  83. }
  84. // 检查search_task_id是否已存在
  85. const checkSql = 'SELECT search_task_id FROM tools_info_search_task WHERE search_task_id = ?';
  86. const existingTool = await executeQuery(checkSql, [search_task_id]);
  87. if (existingTool.length > 0) {
  88. return res.status(400).json({
  89. success: false,
  90. message: '工具ID已存在'
  91. });
  92. }
  93. // 插入新记录
  94. const insertSql = `
  95. INSERT INTO tools_info_search_task
  96. (search_task_id, tools_name, tools_function_name, tools_function_desc, status, create_time, update_time)
  97. VALUES (?, ?, ?, ?, ?, NOW(), NOW())
  98. `;
  99. const result = await executeQuery(insertSql, [
  100. search_task_id,
  101. tools_name,
  102. tools_function_name,
  103. tools_function_desc,
  104. status
  105. ]);
  106. res.json({
  107. success: true,
  108. message: '新增工具成功',
  109. data: {
  110. search_task_id,
  111. tools_name,
  112. tools_function_name,
  113. tools_function_desc,
  114. status
  115. }
  116. });
  117. } catch (error) {
  118. console.error('新增工具失败:', error);
  119. res.status(500).json({
  120. success: false,
  121. message: '服务器内部错误'
  122. });
  123. }
  124. });
  125. router.get('/:id', async (req, res) => {
  126. try {
  127. const { id } = req.params;
  128. const taskSql = `
  129. SELECT search_task_id, tools_name, tools_function_name, tools_function_desc,
  130. status, fail_reason, create_time, update_time
  131. FROM tools_info_search_task
  132. WHERE search_task_id = ?
  133. `;
  134. const detailSql = `
  135. SELECT search_task_id, search_channel, search_result, fail_reason,
  136. create_time, update_time
  137. FROM tools_info_search_task_detail
  138. WHERE search_task_id = ?
  139. `;
  140. const [taskData, detailData] = await Promise.all([
  141. executeQuery(taskSql, [id]),
  142. executeQuery(detailSql, [id])
  143. ]);
  144. if (taskData.length === 0) {
  145. return res.status(404).json({ error: 'Tool not found' });
  146. }
  147. res.json({
  148. task: taskData[0],
  149. detail: detailData[0] || null
  150. });
  151. } catch (error) {
  152. console.error('Error fetching pending tool detail:', error);
  153. res.status(500).json({ error: 'Internal server error' });
  154. }
  155. });
  156. router.put('/:id', async (req, res) => {
  157. try {
  158. const { id } = req.params;
  159. const { tools_name, tools_function_name, tools_function_desc, status, fail_reason } = req.body;
  160. const sql = `
  161. UPDATE tools_info_search_task
  162. SET tools_name = ?, tools_function_name = ?, tools_function_desc = ?,
  163. status = ?, fail_reason = ?, update_time = NOW()
  164. WHERE search_task_id = ?
  165. `;
  166. await executeQuery(sql, [tools_name, tools_function_name, tools_function_desc, status, fail_reason, id]);
  167. res.json({ message: 'Tool updated successfully' });
  168. } catch (error) {
  169. console.error('Error updating pending tool:', error);
  170. res.status(500).json({ error: 'Internal server error' });
  171. }
  172. });
  173. router.delete('/:id', async (req, res) => {
  174. try {
  175. const { id } = req.params;
  176. await executeQuery('DELETE FROM tools_info_search_task_detail WHERE search_task_id = ?', [id]);
  177. await executeQuery('DELETE FROM tools_info_search_task WHERE search_task_id = ?', [id]);
  178. res.json({ message: 'Tool deleted successfully' });
  179. } catch (error) {
  180. console.error('Error deleting pending tool:', error);
  181. res.status(500).json({ error: 'Internal server error' });
  182. }
  183. });
  184. module.exports = router;