pendingTools.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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([executeQuery(sql, params), executeQuery(countSql, countParams)]);
  53. res.json({
  54. data,
  55. total: countResult[0].total,
  56. page: parseInt(page),
  57. pageSize: parseInt(pageSize),
  58. });
  59. } catch (error) {
  60. console.error("Error fetching pending tools:", error);
  61. res.status(500).json({ error: "Internal server error" });
  62. }
  63. });
  64. // 新增待接入工具
  65. router.post("/", async (req, res) => {
  66. try {
  67. const { search_task_id, tools_name, tools_function_name, tools_function_desc } = req.body;
  68. // 验证必填字段
  69. if (!search_task_id || !tools_name || !tools_function_name || !tools_function_desc) {
  70. return res.status(400).json({
  71. success: false,
  72. message: "缺少必填字段",
  73. });
  74. }
  75. // 检查search_task_id是否已存在
  76. const checkSql = "SELECT search_task_id FROM tools_info_search_task WHERE search_task_id = ?";
  77. const existingTool = await executeQuery(checkSql, [search_task_id]);
  78. if (existingTool.length > 0) {
  79. return res.status(400).json({
  80. success: false,
  81. message: "工具ID已存在",
  82. });
  83. }
  84. // 插入新记录
  85. const insertSql = `
  86. INSERT INTO tools_info_search_task
  87. (search_task_id, tools_name, tools_function_name, tools_function_desc, status, create_time, update_time)
  88. VALUES (?, ?, ?, ?, ?, NOW(), NOW())
  89. `;
  90. const result = await executeQuery(insertSql, [
  91. search_task_id,
  92. tools_name,
  93. tools_function_name,
  94. tools_function_desc,
  95. ]);
  96. res.json({
  97. success: true,
  98. message: "新增工具成功",
  99. data: {
  100. search_task_id,
  101. tools_name,
  102. tools_function_name,
  103. tools_function_desc,
  104. },
  105. });
  106. } catch (error) {
  107. console.error("新增工具失败:", error);
  108. res.status(500).json({
  109. success: false,
  110. message: "服务器内部错误",
  111. });
  112. }
  113. });
  114. router.get("/:id", async (req, res) => {
  115. try {
  116. const { id } = req.params;
  117. const taskSql = `
  118. SELECT search_task_id, tools_name, tools_function_name, tools_function_desc,
  119. status, fail_reason, create_time, update_time
  120. FROM tools_info_search_task
  121. WHERE search_task_id = ?
  122. `;
  123. const detailSql = `
  124. SELECT id, search_task_id, search_channel, query, status,
  125. search_result, fail_reason, create_time, update_time
  126. FROM tools_info_search_task_detail
  127. WHERE search_task_id = ?
  128. `;
  129. const [taskData, detailData] = await Promise.all([executeQuery(taskSql, [id]), executeQuery(detailSql, [id])]);
  130. if (taskData.length === 0) {
  131. return res.status(404).json({ error: "Tool not found" });
  132. }
  133. res.json({
  134. task: taskData[0],
  135. detail: detailData.length > 0 ? detailData : null,
  136. });
  137. } catch (error) {
  138. console.error("Error fetching pending tool detail:", error);
  139. res.status(500).json({ error: "Internal server error" });
  140. }
  141. });
  142. router.put("/:id", async (req, res) => {
  143. try {
  144. const { id } = req.params;
  145. const { tools_name, tools_function_name, tools_function_desc, status, fail_reason } = req.body;
  146. const sql = `
  147. UPDATE tools_info_search_task
  148. SET tools_name = ?, tools_function_name = ?, tools_function_desc = ?,
  149. status = ?, fail_reason = ?, update_time = NOW()
  150. WHERE search_task_id = ?
  151. `;
  152. await executeQuery(sql, [
  153. tools_name ?? null,
  154. tools_function_name ?? null,
  155. tools_function_desc ?? null,
  156. status ?? null,
  157. fail_reason ?? null,
  158. id,
  159. ]);
  160. res.json({ message: "Tool updated successfully" });
  161. } catch (error) {
  162. console.error("Error updating pending tool:", error);
  163. res.status(500).json({ error: "Internal server error" });
  164. }
  165. });
  166. // 创建详情信息
  167. router.post("/detail", async (req, res) => {
  168. try {
  169. const { search_task_id, search_channel, query, status, search_result, fail_reason } = req.body;
  170. const sql = `
  171. INSERT INTO tools_info_search_task_detail
  172. (search_task_id, search_channel, query, status, search_result, fail_reason, create_time, update_time)
  173. VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())
  174. `;
  175. const result = await executeQuery(sql, [
  176. search_task_id,
  177. search_channel ?? null,
  178. query ?? null,
  179. status ?? 1,
  180. search_result ?? null,
  181. fail_reason ?? null,
  182. ]);
  183. res.json({
  184. message: "Detail created successfully",
  185. id: result.insertId,
  186. });
  187. } catch (error) {
  188. console.error("Error creating detail:", error);
  189. res.status(500).json({ error: "Internal server error" });
  190. }
  191. });
  192. // 更新详情信息
  193. router.put("/detail/:detailId", async (req, res) => {
  194. try {
  195. const { detailId } = req.params;
  196. const { search_channel, query, status, search_result, fail_reason } = req.body;
  197. const sql = `
  198. UPDATE tools_info_search_task_detail
  199. SET search_channel = ?, query = ?, status = ?,
  200. search_result = ?, fail_reason = ?, update_time = NOW()
  201. WHERE id = ?
  202. `;
  203. await executeQuery(sql, [
  204. search_channel ?? null,
  205. query ?? null,
  206. status ?? null,
  207. search_result ?? null,
  208. fail_reason ?? null,
  209. detailId,
  210. ]);
  211. res.json({ message: "Detail updated successfully" });
  212. } catch (error) {
  213. console.error("Error updating detail:", error);
  214. res.status(500).json({ error: "Internal server error" });
  215. }
  216. });
  217. router.delete("/:id", async (req, res) => {
  218. try {
  219. const { id } = req.params;
  220. await executeQuery("DELETE FROM tools_info_search_task_detail WHERE search_task_id = ?", [id]);
  221. await executeQuery("DELETE FROM tools_info_search_task WHERE search_task_id = ?", [id]);
  222. res.json({ message: "Tool deleted successfully" });
  223. } catch (error) {
  224. console.error("Error deleting pending tool:", error);
  225. res.status(500).json({ error: "Internal server error" });
  226. }
  227. });
  228. module.exports = router;