207 lines
5.4 KiB
JavaScript
207 lines
5.4 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { pool } = require('../db');
|
|
const { broadcastEvent } = require('./sse');
|
|
|
|
/**
|
|
* GET /api/memos
|
|
* 获取指定家庭的备忘待办清单
|
|
*/
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const familyId = req.query.familyId || 'family-001';
|
|
const { status, search, period } = req.query;
|
|
|
|
let query = 'SELECT * FROM family_memo WHERE family_id = ?';
|
|
const params = [familyId];
|
|
|
|
if (status === 'pending') {
|
|
query += ' AND is_completed = 0';
|
|
} else if (status === 'completed') {
|
|
query += ' AND is_completed = 1';
|
|
}
|
|
|
|
if (period && period !== 'all') {
|
|
query += ' AND due_period = ?';
|
|
params.push(period);
|
|
}
|
|
|
|
if (search && search.trim()) {
|
|
query += ' AND (content LIKE ? OR created_by LIKE ?)';
|
|
params.push(`%${search.trim()}%`, `%${search.trim()}%`);
|
|
}
|
|
|
|
query += ' ORDER BY is_completed ASC, created_at DESC';
|
|
|
|
const [rows] = await pool.query(query, params);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows
|
|
});
|
|
} catch (err) {
|
|
console.error('Failed to get memos:', err);
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* POST /api/memos/clear-completed
|
|
* 清理指定家庭已完成的历史备忘
|
|
*/
|
|
router.post('/clear-completed', async (req, res) => {
|
|
try {
|
|
const { familyId = 'family-001' } = req.body;
|
|
const [result] = await pool.query(
|
|
'DELETE FROM family_memo WHERE family_id = ? AND is_completed = 1',
|
|
[familyId]
|
|
);
|
|
|
|
broadcastEvent({
|
|
type: 'memos_cleared',
|
|
familyId,
|
|
clearedCount: result.affectedRows
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: `已成功清理 ${result.affectedRows} 项已完成备忘`,
|
|
affectedRows: result.affectedRows
|
|
});
|
|
} catch (err) {
|
|
console.error('Failed to clear completed memos:', err);
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* POST /api/memos
|
|
* 新增备忘待办事项
|
|
*/
|
|
router.post('/', async (req, res) => {
|
|
try {
|
|
const { familyId = 'family-001', content, duePeriod = 'today', operatorName = '家人' } = req.body;
|
|
if (!content || !content.trim()) {
|
|
return res.status(400).json({ success: false, error: '备忘待办内容不能为空' });
|
|
}
|
|
|
|
const memoId = `memo-${Date.now()}-${Math.random().toString(36).substring(2, 6)}`;
|
|
await pool.query(
|
|
`INSERT INTO family_memo (memo_id, family_id, content, due_period, is_completed, created_by)
|
|
VALUES (?, ?, ?, ?, 0, ?)`,
|
|
[memoId, familyId, content.trim(), duePeriod, operatorName]
|
|
);
|
|
|
|
const [created] = await pool.query('SELECT * FROM family_memo WHERE memo_id = ?', [memoId]);
|
|
|
|
broadcastEvent({
|
|
type: 'memo_created',
|
|
familyId,
|
|
memo: created[0]
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: created[0]
|
|
});
|
|
} catch (err) {
|
|
console.error('Failed to create memo:', err);
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* PUT /api/memos/:memoId
|
|
* 更新备忘事项(勾选完成/修改内容)
|
|
*/
|
|
router.put('/:memoId', async (req, res) => {
|
|
try {
|
|
const { memoId } = req.params;
|
|
const { isCompleted, content, duePeriod } = req.body;
|
|
|
|
const [rows] = await pool.query('SELECT * FROM family_memo WHERE memo_id = ?', [memoId]);
|
|
if (rows.length === 0) {
|
|
return res.status(404).json({ success: false, error: '备忘事项不存在' });
|
|
}
|
|
|
|
const updates = [];
|
|
const params = [];
|
|
|
|
if (isCompleted !== undefined) {
|
|
updates.push('is_completed = ?');
|
|
params.push(isCompleted ? 1 : 0);
|
|
if (isCompleted) {
|
|
updates.push('completed_at = CURRENT_TIMESTAMP');
|
|
} else {
|
|
updates.push('completed_at = NULL');
|
|
}
|
|
}
|
|
|
|
if (content !== undefined && content.trim()) {
|
|
updates.push('content = ?');
|
|
params.push(content.trim());
|
|
}
|
|
|
|
if (duePeriod !== undefined) {
|
|
updates.push('due_period = ?');
|
|
params.push(duePeriod);
|
|
}
|
|
|
|
if (updates.length > 0) {
|
|
params.push(memoId);
|
|
await pool.query(
|
|
`UPDATE family_memo SET ${updates.join(', ')} WHERE memo_id = ?`,
|
|
params
|
|
);
|
|
}
|
|
|
|
const [updated] = await pool.query('SELECT * FROM family_memo WHERE memo_id = ?', [memoId]);
|
|
|
|
broadcastEvent({
|
|
type: 'memo_updated',
|
|
familyId: rows[0].family_id,
|
|
memo: updated[0]
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: updated[0]
|
|
});
|
|
} catch (err) {
|
|
console.error('Failed to update memo:', err);
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* DELETE /api/memos/:memoId
|
|
* 删除备忘事项
|
|
*/
|
|
router.delete('/:memoId', async (req, res) => {
|
|
try {
|
|
const { memoId } = req.params;
|
|
const [rows] = await pool.query('SELECT * FROM family_memo WHERE memo_id = ?', [memoId]);
|
|
if (rows.length === 0) {
|
|
return res.status(404).json({ success: false, error: '备忘事项不存在' });
|
|
}
|
|
|
|
await pool.query('DELETE FROM family_memo WHERE memo_id = ?', [memoId]);
|
|
|
|
broadcastEvent({
|
|
type: 'memo_deleted',
|
|
familyId: rows[0].family_id,
|
|
memoId
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '备忘事项已删除'
|
|
});
|
|
} catch (err) {
|
|
console.error('Failed to delete memo:', err);
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|