123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- 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,
- status = 0
- } = 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,
- status
- ]);
- res.json({
- success: true,
- message: '新增工具成功',
- data: {
- search_task_id,
- tools_name,
- tools_function_name,
- tools_function_desc,
- status
- }
- });
- } 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 search_task_id, search_channel, 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[0] || 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, tools_function_name, tools_function_desc, status, fail_reason, 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.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;
|