Rework LLM classification around user categories
Move category CRUD into the config route so renames sync historical deals, feed per-user categories into the LLM prompt, cache clients per user, and drop the separate classify route.
This commit is contained in:
+57
-20
@@ -6,20 +6,32 @@ const router = express.Router();
|
||||
// GET /api/stats/overview — 总览统计(基于 deals)
|
||||
router.get("/overview", (req, res) => {
|
||||
const db = getDB();
|
||||
const { year, month } = req.query;
|
||||
|
||||
let where = "WHERE d.user_id = ?";
|
||||
const params = [req.userId];
|
||||
const { year, month, start_date, end_date } = req.query;
|
||||
|
||||
// 日期条件:支持任意区间(财年),也保留旧的 year/month
|
||||
const dateConds = [];
|
||||
const dateParams = [];
|
||||
if (start_date) {
|
||||
dateConds.push("d.deal_date >= ?");
|
||||
dateParams.push(start_date);
|
||||
}
|
||||
if (end_date) {
|
||||
dateConds.push("d.deal_date <= ?");
|
||||
dateParams.push(end_date);
|
||||
}
|
||||
if (year) {
|
||||
where += " AND strftime('%Y', d.deal_date) = ?";
|
||||
params.push(String(year));
|
||||
dateConds.push("strftime('%Y', d.deal_date) = ?");
|
||||
dateParams.push(String(year));
|
||||
if (month) {
|
||||
where += " AND strftime('%m', d.deal_date) = ?";
|
||||
params.push(String(month).padStart(2, "0"));
|
||||
dateConds.push("strftime('%m', d.deal_date) = ?");
|
||||
dateParams.push(String(month).padStart(2, "0"));
|
||||
}
|
||||
}
|
||||
|
||||
let where = "WHERE d.user_id = ?";
|
||||
const params = [req.userId, ...dateParams];
|
||||
if (dateConds.length) where += " AND " + dateConds.join(" AND ");
|
||||
|
||||
const stats = db
|
||||
.prepare(
|
||||
`
|
||||
@@ -76,23 +88,33 @@ router.get("/overview", (req, res) => {
|
||||
)
|
||||
.all(...params);
|
||||
|
||||
const dateCondSql = dateConds.length ? " AND " + dateConds.join(" AND ") : "";
|
||||
const maxIncomeStmt = db.prepare(
|
||||
`SELECT id, amount, counterparty, deal_date, ai_description
|
||||
FROM deals WHERE category=? AND direction='income' AND user_id=?
|
||||
ORDER BY amount DESC LIMIT 1`,
|
||||
FROM deals d WHERE d.category=? AND d.direction='income' AND d.user_id=?${dateCondSql}
|
||||
ORDER BY d.amount DESC LIMIT 1`,
|
||||
);
|
||||
const maxExpenseStmt = db.prepare(
|
||||
`SELECT id, amount, counterparty, deal_date, ai_description
|
||||
FROM deals WHERE category=? AND direction='expense' AND user_id=?
|
||||
ORDER BY ABS(amount) DESC LIMIT 1`,
|
||||
FROM deals d WHERE d.category=? AND d.direction='expense' AND d.user_id=?${dateCondSql}
|
||||
ORDER BY ABS(d.amount) DESC LIMIT 1`,
|
||||
);
|
||||
|
||||
for (const cat of categoryBreakdown) {
|
||||
cat.max_income = maxIncomeStmt.get(cat.category, req.userId) || null;
|
||||
cat.max_expense = maxExpenseStmt.get(cat.category, req.userId) || null;
|
||||
cat.max_income =
|
||||
maxIncomeStmt.get(cat.category, req.userId, ...dateParams) || null;
|
||||
cat.max_expense =
|
||||
maxExpenseStmt.get(cat.category, req.userId, ...dateParams) || null;
|
||||
}
|
||||
|
||||
res.json({ overview: stats, categoryStats, categoryBreakdown });
|
||||
// 全量日期范围(不受筛选影响),供前端枚举财年
|
||||
const dateRange = db
|
||||
.prepare(
|
||||
"SELECT MIN(deal_date) as min_date, MAX(deal_date) as max_date FROM deals WHERE user_id = ?",
|
||||
)
|
||||
.get(req.userId);
|
||||
|
||||
res.json({ overview: stats, categoryStats, categoryBreakdown, dateRange });
|
||||
});
|
||||
|
||||
// 解析 categories 查询参数
|
||||
@@ -107,14 +129,29 @@ function parseCategories(query) {
|
||||
// GET /api/stats/monthly — 月度趋势(支持 ?categories=餐饮,购物 筛选)
|
||||
router.get("/monthly", (req, res) => {
|
||||
const db = getDB();
|
||||
const months = Math.abs(parseInt(req.query.months) || 12);
|
||||
const { start_date, end_date } = req.query;
|
||||
const cats = parseCategories(req.query.categories);
|
||||
|
||||
let catFilter = "";
|
||||
const params = ["-" + months, req.userId];
|
||||
const conds = ["d.user_id = ?"];
|
||||
const params = [req.userId];
|
||||
|
||||
if (start_date) {
|
||||
conds.push("d.deal_date >= ?");
|
||||
params.push(start_date);
|
||||
}
|
||||
if (end_date) {
|
||||
conds.push("d.deal_date <= ?");
|
||||
params.push(end_date);
|
||||
}
|
||||
// 只有显式传了 months 才限制为「最近 N 个月」;
|
||||
// 不传任何时间参数 = 不限制(全部时间)
|
||||
if (!start_date && !end_date && req.query.months != null) {
|
||||
const months = Math.abs(parseInt(req.query.months) || 12);
|
||||
conds.push("d.deal_date >= DATE('now', ? || ' months')");
|
||||
params.push("-" + months);
|
||||
}
|
||||
if (cats && cats.length > 0) {
|
||||
catFilter = " AND d.category IN (" + cats.map(() => "?").join(",") + ")";
|
||||
conds.push("d.category IN (" + cats.map(() => "?").join(",") + ")");
|
||||
params.push(...cats);
|
||||
}
|
||||
|
||||
@@ -127,7 +164,7 @@ router.get("/monthly", (req, res) => {
|
||||
SUM(CASE WHEN d.direction = 'income' THEN d.amount ELSE 0 END) as income,
|
||||
SUM(CASE WHEN d.direction = 'expense' THEN ABS(d.amount) ELSE 0 END) as expense
|
||||
FROM deals d
|
||||
WHERE d.deal_date >= DATE('now', ? || ' months') AND d.user_id = ?${catFilter}
|
||||
WHERE ${conds.join(" AND ")}
|
||||
GROUP BY strftime('%Y-%m', d.deal_date)
|
||||
ORDER BY month ASC
|
||||
`,
|
||||
|
||||
Reference in New Issue
Block a user