const express = require("express"); const router = express.Router(); const { executeQuery } = require("../config/database"); router.get("/", async (req, res) => { try { const { search, toolsName, status, page = 1, pageSize = 10 } = req.query; const offset = (page - 1) * pageSize; let sql = ` SELECT search_task_id, tools_name, tools_function_name, tools_function_desc, status, fail_reason, create_time, update_time FROM tools_info_search_task `; let params = []; let whereConditions = []; // 添加工具名称搜索条件 if (toolsName) { whereConditions.push("tools_name LIKE ?"); params.push(`%${toolsName}%`); } // 添加状态搜索条件 if (status !== undefined && status !== "") { whereConditions.push("status = ?"); params.push(parseInt(status)); } // 添加全文搜索条件 if (search) { whereConditions.push("(tools_name LIKE ? OR tools_function_name LIKE ? OR tools_function_desc LIKE ?)"); params.push(`%${search}%`, `%${search}%`, `%${search}%`); } // 构建WHERE子句 if (whereConditions.length > 0) { sql += ` WHERE ${whereConditions.join(" AND ")}`; } sql += ` ORDER BY create_time DESC LIMIT ? OFFSET ?`; params.push(parseInt(pageSize), offset); // 构建计数查询 let countSql = `SELECT COUNT(*) as total FROM tools_info_search_task`; let countParams = []; if (whereConditions.length > 0) { countSql += ` WHERE ${whereConditions.join(" AND ")}`; // 重新构建计数查询的参数 if (toolsName) { countParams.push(`%${toolsName}%`); } if (status !== undefined && status !== "") { countParams.push(parseInt(status)); } if (search) { countParams.push(`%${search}%`, `%${search}%`, `%${search}%`); } } const [data, countResult] = await Promise.all([executeQuery(sql, params), executeQuery(countSql, countParams)]); res.json({ data, total: countResult[0].total, page: parseInt(page), pageSize: parseInt(pageSize), }); } catch (error) { console.error("Error fetching pending tools:", error); res.status(500).json({ error: "Internal server error" }); } }); // 新增待接入工具 router.post("/", async (req, res) => { try { const { search_task_id, tools_name, tools_function_name, tools_function_desc } = req.body; // 验证必填字段 if (!search_task_id || !tools_name || !tools_function_name || !tools_function_desc) { return res.status(400).json({ success: false, message: "缺少必填字段", }); } // 检查search_task_id是否已存在 const checkSql = "SELECT search_task_id FROM tools_info_search_task WHERE search_task_id = ?"; const existingTool = await executeQuery(checkSql, [search_task_id]); if (existingTool.length > 0) { return res.status(400).json({ success: false, message: "工具ID已存在", }); } // 插入新记录 const insertSql = ` INSERT INTO tools_info_search_task (search_task_id, tools_name, tools_function_name, tools_function_desc, status, create_time, update_time) VALUES (?, ?, ?, ?, ?, NOW(), NOW()) `; const result = await executeQuery(insertSql, [ search_task_id, tools_name, tools_function_name, tools_function_desc, ]); res.json({ success: true, message: "新增工具成功", data: { search_task_id, tools_name, tools_function_name, tools_function_desc, }, }); } catch (error) { console.error("新增工具失败:", error); res.status(500).json({ success: false, message: "服务器内部错误", }); } }); router.get("/:id", async (req, res) => { try { const { id } = req.params; const taskSql = ` SELECT search_task_id, tools_name, tools_function_name, tools_function_desc, status, fail_reason, create_time, update_time FROM tools_info_search_task WHERE search_task_id = ? `; const detailSql = ` SELECT id, search_task_id, search_channel, query, status, search_result, fail_reason, create_time, update_time FROM tools_info_search_task_detail WHERE search_task_id = ? `; const [taskData, detailData] = await Promise.all([executeQuery(taskSql, [id]), executeQuery(detailSql, [id])]); if (taskData.length === 0) { return res.status(404).json({ error: "Tool not found" }); } res.json({ task: taskData[0], detail: detailData.length > 0 ? detailData : null, }); } catch (error) { console.error("Error fetching pending tool detail:", error); res.status(500).json({ error: "Internal server error" }); } }); router.put("/:id", async (req, res) => { try { const { id } = req.params; const { tools_name, tools_function_name, tools_function_desc, status, fail_reason } = req.body; const sql = ` UPDATE tools_info_search_task SET tools_name = ?, tools_function_name = ?, tools_function_desc = ?, status = ?, fail_reason = ?, update_time = NOW() WHERE search_task_id = ? `; await executeQuery(sql, [ tools_name ?? null, tools_function_name ?? null, tools_function_desc ?? null, status ?? null, fail_reason ?? null, id, ]); res.json({ message: "Tool updated successfully" }); } catch (error) { console.error("Error updating pending tool:", error); res.status(500).json({ error: "Internal server error" }); } }); // 创建详情信息 router.post("/detail", async (req, res) => { try { const { search_task_id, search_channel, query, status, search_result, fail_reason } = req.body; const sql = ` INSERT INTO tools_info_search_task_detail (search_task_id, search_channel, query, status, search_result, fail_reason, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW()) `; const result = await executeQuery(sql, [ search_task_id, search_channel ?? null, query ?? null, status ?? 1, search_result ?? null, fail_reason ?? null, ]); res.json({ message: "Detail created successfully", id: result.insertId, }); } catch (error) { console.error("Error creating detail:", error); res.status(500).json({ error: "Internal server error" }); } }); // 更新详情信息 router.put("/detail/:detailId", async (req, res) => { try { const { detailId } = req.params; const { search_channel, query, status, search_result, fail_reason } = req.body; const sql = ` UPDATE tools_info_search_task_detail SET search_channel = ?, query = ?, status = ?, search_result = ?, fail_reason = ?, update_time = NOW() WHERE id = ? `; await executeQuery(sql, [ search_channel ?? null, query ?? null, status ?? null, search_result ?? null, fail_reason ?? null, detailId, ]); res.json({ message: "Detail updated successfully" }); } catch (error) { console.error("Error updating detail:", error); res.status(500).json({ error: "Internal server error" }); } }); router.delete("/:id", async (req, res) => { try { const { id } = req.params; await executeQuery("DELETE FROM tools_info_search_task_detail WHERE search_task_id = ?", [id]); await executeQuery("DELETE FROM tools_info_search_task WHERE search_task_id = ?", [id]); res.json({ message: "Tool deleted successfully" }); } catch (error) { console.error("Error deleting pending tool:", error); res.status(500).json({ error: "Internal server error" }); } }); module.exports = router;