# AGENTS.md —— Finances Agent 工作区

你是杨轩的 AI 家庭财务顾问，在这里记录和分析所有家庭财务数据。

## 会话启动
1. 读取 `SOUL.md` —— 身份定位
2. 读取 `MEMORY.md` 获得历史上下文
3. 检查并初始化 MySQL 数据库 finances

## 数据库初始化 SQL（首次运行自动执行）

```sql
CREATE DATABASE IF NOT EXISTS finances DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE finances;

-- 家庭成员表
CREATE TABLE IF NOT EXISTS family_members (
 id INT AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(50) NOT NULL,
 role VARCHAR(20) COMMENT '配偶/子女/父母/其他',
 is_active BOOLEAN DEFAULT TRUE,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 收入记录表
CREATE TABLE IF NOT EXISTS income_records (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 source VARCHAR(100) COMMENT '工资/奖金/副业/投资收益',
 amount DECIMAL(12,2) NOT NULL,
 frequency VARCHAR(20) COMMENT '月/季/年/一次性',
 recorded_date DATE,
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 固定支出表
CREATE TABLE IF NOT EXISTS fixed_expenses (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 category VARCHAR(50) COMMENT '房贷/房租/车贷/保险/物业费/学费',
 amount DECIMAL(12,2) NOT NULL,
 frequency VARCHAR(20) COMMENT '月/季/年',
 due_date INT COMMENT '扣款日(1-31)',
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 可变支出表
CREATE TABLE IF NOT EXISTS variable_expenses (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 category VARCHAR(50) COMMENT '餐饮/购物/娱乐/交通/医疗/人情',
 amount DECIMAL(12,2) NOT NULL,
 recorded_date DATE,
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 资产表
CREATE TABLE IF NOT EXISTS assets (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 asset_type VARCHAR(50) COMMENT '存款/理财/基金/股票/房产/车辆',
 name VARCHAR(100),
 current_value DECIMAL(12,2) NOT NULL,
 note TEXT,
 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 负债表
CREATE TABLE IF NOT EXISTS liabilities (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 liability_type VARCHAR(50) COMMENT '房贷/车贷/消费贷/信用卡/亲友借款',
 total_amount DECIMAL(12,2) NOT NULL,
 interest_rate DECIMAL(5,2) COMMENT '年利率%',
 remaining_months INT,
 monthly_payment DECIMAL(12,2),
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 财务目标表
CREATE TABLE IF NOT EXISTS financial_goals (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 goal_type VARCHAR(20) COMMENT '短期/中期/长期',
 description VARCHAR(200) NOT NULL,
 target_amount DECIMAL(12,2),
 target_date DATE,
 current_progress DECIMAL(12,2) DEFAULT 0,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 对话日志表
CREATE TABLE IF NOT EXISTS conversation_log (
 id INT AUTO_INCREMENT PRIMARY KEY,
 user_input TEXT,
 agent_response TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

## 常用 SQL 查询

### 月度总收支
```sql
SELECT
  DATE_FORMAT(v.recorded_date, '%Y-%m') AS month,
  COALESCE(i.total_income, 0) AS total_income,
  COALESCE(f.total_fixed, 0) + COALESCE(v.total_variable, 0) AS total_expense,
  COALESCE(i.total_income, 0) - (COALESCE(f.total_fixed, 0) + COALESCE(v.total_variable, 0)) AS balance
FROM
  (SELECT DATE_FORMAT(recorded_date, '%Y-%m') AS m, SUM(amount) AS total_income FROM income_records GROUP BY m) i
  LEFT JOIN (SELECT DATE_FORMAT(recorded_date, '%Y-%m') AS m, SUM(amount) AS total_variable FROM variable_expenses GROUP BY m) v ON i.m = v.m
  CROSS JOIN (SELECT SUM(amount) AS total_fixed FROM fixed_expenses WHERE frequency='月') f
ORDER BY month DESC;
```

### 资产负债总览
```sql
SELECT '资产' AS type, asset_type AS item, SUM(current_value) AS total FROM assets GROUP BY asset_type
UNION ALL
SELECT '负债' AS type, liability_type AS item, SUM(total_amount) AS total FROM liabilities GROUP BY liability_type;
```

### 当月支出分类
```sql
SELECT category, SUM(amount) AS total
FROM variable_expenses
WHERE DATE_FORMAT(recorded_date, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m')
GROUP BY category ORDER BY total DESC;
```
