const express = require('express'); const router = express.Router(); const { executeQuery } = require('../config/database'); router.get('/', async (req, res) => { try { const { page = 1, pageSize = 10, search, status } = req.query; const offset = (page - 1) * pageSize; // 构建WHERE条件 let whereConditions = []; let queryParams = []; // 添加搜索条件 if (search) { whereConditions.push('tools_name LIKE ?'); queryParams.push(`%${search}%`); } // 添加状态过滤条件 if (status !== undefined && status !== '') { whereConditions.push('status = ?'); queryParams.push(parseInt(status)); } // 构建WHERE子句 const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(' AND ')}` : ''; const sql = ` SELECT access_task_id, search_task_id, tools_name, tools_function_name, access_type, tools_function_desc, api_doc, api_class_name, operate_path_data, origin_content_link, status, fail_reason, create_time, update_time FROM tools_auto_access_task ${whereClause} ORDER BY create_time DESC LIMIT ? OFFSET ? `; const countSql = `SELECT COUNT(*) as total FROM tools_auto_access_task ${whereClause}`; // 为查询添加分页参数 const sqlParams = [...queryParams, parseInt(pageSize), offset]; const countParams = [...queryParams]; 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 auto access tasks:', error); res.status(500).json({ error: 'Internal server error' }); } }); router.get('/:id', async (req, res) => { try { const { id } = req.params; const sql = ` SELECT access_task_id, search_task_id, tools_name, tools_function_name, access_type, tools_function_desc, api_doc, api_class_name, operate_path_data, origin_content_link, status, fail_reason, create_time, update_time FROM tools_auto_access_task WHERE access_task_id = ? `; const data = await executeQuery(sql, [id]); if (data.length === 0) { return res.status(404).json({ error: 'Task not found' }); } res.json(data[0]); } catch (error) { console.error('Error fetching auto access task detail:', error); res.status(500).json({ error: 'Internal server error' }); } }); router.put('/:id', async (req, res) => { try { const { id } = req.params; const { search_task_id, tools_name, tools_function_name, access_type, tools_function_desc, api_doc, api_class_name, operate_path_data, origin_content_link, status, fail_reason } = req.body; const sql = ` UPDATE tools_auto_access_task SET search_task_id = ?, tools_name = ?, tools_function_name = ?, access_type = ?, tools_function_desc = ?, api_doc = ?, api_class_name = ?, operate_path_data = ?, origin_content_link = ?, status = ?, fail_reason = ?, update_time = NOW() WHERE access_task_id = ? `; await executeQuery(sql, [ search_task_id, tools_name, tools_function_name, access_type, tools_function_desc, api_doc, api_class_name, operate_path_data, origin_content_link, status, fail_reason, id ]); res.json({ message: 'Task updated successfully' }); } catch (error) { console.error('Error updating auto access task:', error); res.status(500).json({ error: 'Internal server error' }); } }); module.exports = router;