const express = require("express"); const router = express.Router(); const { executeQuery } = require("../config/database"); router.get("/", async (req, res) => { try { const { page = 1, pageSize = 10, toolsName, mcpToolsName, status } = req.query; const offset = (page - 1) * pageSize; // 构建WHERE条件 let whereConditions = []; let sqlParams = []; let countParams = []; if (toolsName) { whereConditions.push("tools_name LIKE ?"); sqlParams.push(`%${toolsName}%`); countParams.push(`%${toolsName}%`); } if (mcpToolsName) { whereConditions.push("mcp_tools_name LIKE ?"); sqlParams.push(`%${mcpToolsName}%`); countParams.push(`%${mcpToolsName}%`); } if (status) { whereConditions.push("status = ?"); sqlParams.push(status); countParams.push(status); } const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(' AND ')}` : ''; const sql = ` SELECT tools_id, tools_name, tools_function_name, mcp_tools_name, tools_full_name, tools_desc, tools_version, access_task_id, status, call_type, api_provider, api_url_path, create_time, update_time FROM tools_library ${whereClause} ORDER BY create_time DESC LIMIT ? OFFSET ? `; const countSql = `SELECT COUNT(*) as total FROM tools_library ${whereClause}`; // 添加分页参数 sqlParams.push(parseInt(pageSize), offset); const [data, countResult] = await Promise.all([ executeQuery(sql, sqlParams), executeQuery(countSql, countParams), ]); res.json({ data, total: countResult[0].total, page: parseInt(page), pageSize: parseInt(pageSize), }); } catch (error) { console.error("Error fetching tools library:", error); res.status(500).json({ error: "Internal server error" }); } }); router.get("/:id", async (req, res) => { try { const { id } = req.params; const sql = ` SELECT tools_id, tools_name, tools_function_name, mcp_tools_name, tools_full_name, tools_desc, tools_version, access_task_id, status, call_type, api_provider, api_url_path, operate_path_data, params_definition, response_desc, create_time, update_time FROM tools_library WHERE tools_id = ? `; const data = await executeQuery(sql, [id]); if (data.length === 0) { return res.status(404).json({ error: "Tool not found" }); } res.json(data[0]); } catch (error) { console.error("Error fetching 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, mcp_tools_name, tools_full_name, tools_desc, tools_version, access_task_id, status, call_type, api_provider, api_url_path, operate_path_data, params_definition, response_desc, } = req.body; const sql = ` UPDATE tools_library SET tools_name = ?, tools_function_name = ?, mcp_tools_name = ?, tools_full_name = ?, tools_desc = ?, tools_version = ?, access_task_id = ?, status = ?, call_type = ?, api_provider = ?, api_url_path = ?, operate_path_data = ?, params_definition = ?, response_desc = ?, update_time = NOW() WHERE tools_id = ? `; // 将 undefined 值转换为 null,避免数据库绑定参数错误 const params = [ tools_name ?? null, tools_function_name ?? null, mcp_tools_name ?? null, tools_full_name ?? null, tools_desc ?? null, tools_version ?? null, access_task_id ?? null, status ?? null, call_type ?? null, api_provider ?? null, api_url_path ?? null, operate_path_data ?? null, params_definition ?? null, response_desc ?? null, id, ]; await executeQuery(sql, params); res.json({ message: "Tool updated successfully" }); } catch (error) { console.error("Error updating tool:", error); res.status(500).json({ error: "Internal server error" }); } }); router.post("/:id/publish", async (req, res) => { try { const { id } = req.params; const sql = ` UPDATE tools_library SET status = 'normal', update_time = NOW() WHERE tools_id = ? `; await executeQuery(sql, [id]); res.json({ message: "Tool published successfully" }); } catch (error) { console.error("Error publishing tool:", error); res.status(500).json({ error: "Internal server error" }); } }); module.exports = router;