const { getDBConnection } = require('./file-utils'); // 获取所有待办事项 function getTodos() { try { const db = getDBConnection(true); // 创建todos表(如果不存在) db.prepare(` CREATE TABLE IF NOT EXISTS todos ( id INTEGER PRIMARY KEY AUTOINCREMENT, project TEXT NOT NULL DEFAULT '其它', subproject TEXT NOT NULL DEFAULT '其它', title TEXT NOT NULL, text TEXT, adduser TEXT NOT NULL, exeuser TEXT, completed BOOLEAN DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, sche_time DATETIME DEFAULT CURRENT_TIMESTAMP, complete_time DATETIME DEFAULT CURRENT_TIMESTAMP ) `).run(); const todos = db.prepare('SELECT * FROM todos ORDER BY created_at DESC').all(); return todos; } catch (error) { console.error('获取待办事项失败:', error); throw error; } } // 添加待办事项 function addTodo(todoData) { try { if (!todoData.title) { throw new Error('待办事项标题不能为空'); } const db = getDBConnection(); // 获取当前本地时间 const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); const hours = String(now.getHours()).padStart(2, '0'); const minutes = String(now.getMinutes()).padStart(2, '0'); const seconds = String(now.getSeconds()).padStart(2, '0'); const localDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 处理计划时间 let scheTime = todoData.sche_time; if (scheTime) { // 如果sche_time是datetime-local格式(YYYY-MM-DDTHH:mm),转换为数据库格式 if (scheTime.includes('T')) { const [datePart, timePart] = scheTime.split('T'); scheTime = `${datePart} ${timePart}:00`; } } else { scheTime = localDateTime; } const result = db.prepare(` INSERT INTO todos ( project, subproject, title, text, adduser, exeuser, completed, created_at, sche_time ) VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?) `).run( todoData.project || '其它', todoData.subproject || '其它', todoData.title, todoData.text || '', todoData.adduser || '系统', todoData.exeuser || null, localDateTime, scheTime ); if (result.changes > 0) { return { success: true, id: result.lastInsertRowid, message: '待办事项添加成功' }; } else { throw new Error('待办事项添加失败'); } } catch (error) { console.error('添加待办事项失败:', error); throw error; } } // 更新待办事项状态 function updateTodoStatus(id, completed, exeuser, title, text, sche_time) { try { const db = getDBConnection(); // 获取当前本地时间 const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); const hours = String(now.getHours()).padStart(2, '0'); const minutes = String(now.getMinutes()).padStart(2, '0'); const seconds = String(now.getSeconds()).padStart(2, '0'); const localDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 处理计划时间 let scheTime = sche_time; if (scheTime) { // 如果sche_time是datetime-local格式(YYYY-MM-DDTHH:mm),转换为数据库格式 if (scheTime.includes('T')) { const [datePart, timePart] = scheTime.split('T'); scheTime = `${datePart} ${timePart}`; } } const result = db.prepare(` UPDATE todos SET completed = ?, exeuser = ?, title = ?, text = ?, sche_time = ?, complete_time = CASE WHEN ? = 1 THEN ? ELSE complete_time END WHERE id = ? `).run( completed ? 1 : 0, exeuser || null, title, text || '', scheTime || null, completed ? 1 : 0, completed ? localDateTime : null, id ); if (result.changes > 0) { return { success: true, message: '待办事项更新成功' }; } else { throw new Error('待办事项不存在'); } } catch (error) { console.error('更新待办事项失败:', error); throw error; } } // 删除待办事项 function deleteTodo(id) { try { const db = getDBConnection(); const result = db.prepare('DELETE FROM todos WHERE id = ?').run(id); return { success: true, message: result.changes > 0 ? '待办事项删除成功' : '待办事项不存在或已被删除' }; } catch (error) { console.error('删除待办事项失败:', error); throw error; } } module.exports = { getTodos, addTodo, updateTodoStatus, deleteTodo };