tq[MESSAGE]:
[TYPE]: [JIRA]:
This commit is contained in:
@@ -8,10 +8,16 @@
|
||||
├── user (用户表)
|
||||
|
||||
基础数据相关
|
||||
├── brokers (券商表)
|
||||
├── broker (券商表)
|
||||
├── stock_info (股票基本信息表)
|
||||
└── stock_daily_price (股票每日收盘价表)
|
||||
|
||||
财务报表相关(见 财务报表模块-产品与数据库设计.md)
|
||||
├── financial_report (报表主表)
|
||||
├── financial_report_line (行项目表)
|
||||
├── financial_report_stats (预计算统计表)
|
||||
└── financial_item_category (自定义分类表,可选)
|
||||
|
||||
账户相关
|
||||
├── positions (持仓表)
|
||||
├── position_changes (持仓变更记录表)
|
||||
@@ -513,6 +519,157 @@ WHERE p.auto_price_update = true
|
||||
|
||||
---
|
||||
|
||||
## 财务报表相关表设计
|
||||
|
||||
> 详细产品与设计说明见:[财务报表模块-产品与数据库设计.md](./财务报表模块-产品与数据库设计.md)
|
||||
|
||||
### financial_report 报表主表
|
||||
|
||||
| 字段名 | 数据类型 | 约束 | 说明 |
|
||||
|--------|---------|------|------|
|
||||
| id | BIGSERIAL | PRIMARY KEY | 主键 |
|
||||
| stock_code | VARCHAR(20) | NOT NULL | 股票代码,关联 stock_info |
|
||||
| market | VARCHAR(20) | NOT NULL | 市场,关联 stock_info |
|
||||
| report_date | DATE | NOT NULL | 报告期截止日 |
|
||||
| period_type | VARCHAR(20) | NOT NULL | annual / quarterly |
|
||||
| statement_type | VARCHAR(30) | NOT NULL | balance_sheet / income_statement / cash_flow |
|
||||
| source_file_name | VARCHAR(255) | | 来源文件名 |
|
||||
| created_at | TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
|
||||
| updated_at | TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 更新时间 |
|
||||
|
||||
```sql
|
||||
CREATE TABLE financial_report (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
stock_code VARCHAR(20) NOT NULL,
|
||||
market VARCHAR(20) NOT NULL,
|
||||
report_date DATE NOT NULL,
|
||||
period_type VARCHAR(20) NOT NULL,
|
||||
statement_type VARCHAR(30) NOT NULL,
|
||||
source_file_name VARCHAR(255),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(stock_code, market, report_date, statement_type),
|
||||
CONSTRAINT fk_financial_report_stock FOREIGN KEY (stock_code, market)
|
||||
REFERENCES stock_info(stock_code, market) ON DELETE CASCADE,
|
||||
CONSTRAINT check_period_type CHECK (period_type IN ('annual', 'quarterly')),
|
||||
CONSTRAINT check_statement_type CHECK (statement_type IN ('balance_sheet', 'income_statement', 'cash_flow'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_financial_report_stock_market ON financial_report(stock_code, market);
|
||||
CREATE INDEX idx_financial_report_report_date ON financial_report(report_date);
|
||||
CREATE INDEX idx_financial_report_statement_type ON financial_report(statement_type);
|
||||
|
||||
CREATE TRIGGER update_financial_report_updated_at
|
||||
BEFORE UPDATE ON financial_report
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
COMMENT ON TABLE financial_report IS '财务报表主表:公司+报告期+表型';
|
||||
COMMENT ON COLUMN financial_report.statement_type IS 'balance_sheet(资产负债表)/income_statement(利润表)/cash_flow(现金流量表)';
|
||||
```
|
||||
|
||||
### financial_report_line 行项目表
|
||||
|
||||
| 字段名 | 数据类型 | 约束 | 说明 |
|
||||
|--------|---------|------|------|
|
||||
| id | BIGSERIAL | PRIMARY KEY | 主键 |
|
||||
| report_id | BIGINT | NOT NULL, FK | 所属报表 |
|
||||
| item_name | VARCHAR(200) | NOT NULL | 科目名称(原文) |
|
||||
| item_code | VARCHAR(50) | | 科目编码(可选) |
|
||||
| value | DECIMAL(24, 6) | | 数值 |
|
||||
| unit | VARCHAR(20) | | 单位 |
|
||||
| row_order | INT | DEFAULT 0 | 行顺序 |
|
||||
| created_at | TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
|
||||
|
||||
```sql
|
||||
CREATE TABLE financial_report_line (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
report_id BIGINT NOT NULL REFERENCES financial_report(id) ON DELETE CASCADE,
|
||||
item_name VARCHAR(200) NOT NULL,
|
||||
item_code VARCHAR(50),
|
||||
value DECIMAL(24, 6),
|
||||
unit VARCHAR(20),
|
||||
row_order INT DEFAULT 0,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_financial_report_line_report_id ON financial_report_line(report_id);
|
||||
CREATE INDEX idx_financial_report_line_item_name ON financial_report_line(report_id, item_name);
|
||||
|
||||
COMMENT ON TABLE financial_report_line IS '财务报表行项目,存原始科目名与数值,支持多市场科目差异';
|
||||
```
|
||||
|
||||
### financial_report_stats 预计算统计表
|
||||
|
||||
| 字段名 | 数据类型 | 约束 | 说明 |
|
||||
|--------|---------|------|------|
|
||||
| id | BIGSERIAL | PRIMARY KEY | 主键 |
|
||||
| report_id | BIGINT | NOT NULL, FK | 所属报表 |
|
||||
| stat_key | VARCHAR(80) | NOT NULL | 如 total_assets, net_profit, roe |
|
||||
| stat_value | DECIMAL(24, 6) | | 统计值 |
|
||||
| dimension | VARCHAR(50) | | 可选维度 |
|
||||
| created_at | TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
|
||||
| updated_at | TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 更新时间 |
|
||||
|
||||
```sql
|
||||
CREATE TABLE financial_report_stats (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
report_id BIGINT NOT NULL REFERENCES financial_report(id) ON DELETE CASCADE,
|
||||
stat_key VARCHAR(80) NOT NULL,
|
||||
stat_value DECIMAL(24, 6),
|
||||
dimension VARCHAR(50),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(report_id, stat_key, dimension)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_financial_report_stats_report_id ON financial_report_stats(report_id);
|
||||
CREATE INDEX idx_financial_report_stats_stat_key ON financial_report_stats(stat_key);
|
||||
|
||||
CREATE TRIGGER update_financial_report_stats_updated_at
|
||||
BEFORE UPDATE ON financial_report_stats
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
COMMENT ON TABLE financial_report_stats IS '报表预计算统计项,上传时计算并写入';
|
||||
```
|
||||
|
||||
### financial_item_category 自定义分类表(可选)
|
||||
|
||||
| 字段名 | 数据类型 | 约束 | 说明 |
|
||||
|--------|---------|------|------|
|
||||
| id | BIGSERIAL | PRIMARY KEY | 主键 |
|
||||
| user_id | BIGINT | FK user, 可选 | 多用户时按用户隔离 |
|
||||
| market | VARCHAR(20) | NOT NULL | 市场 |
|
||||
| statement_type | VARCHAR(30) | NOT NULL | 表型 |
|
||||
| item_name_pattern | VARCHAR(200) | NOT NULL | 科目名或匹配模式 |
|
||||
| category_slug | VARCHAR(60) | NOT NULL | 分类标签,如 cash_like, operating |
|
||||
| sort_order | INT | DEFAULT 0 | 展示顺序 |
|
||||
| created_at | TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
|
||||
| updated_at | TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 更新时间 |
|
||||
|
||||
```sql
|
||||
CREATE TABLE financial_item_category (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT REFERENCES user(user_id) ON DELETE CASCADE,
|
||||
market VARCHAR(20) NOT NULL,
|
||||
statement_type VARCHAR(30) NOT NULL,
|
||||
item_name_pattern VARCHAR(200) NOT NULL,
|
||||
category_slug VARCHAR(60) NOT NULL,
|
||||
sort_order INT DEFAULT 0,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_financial_item_category_user_market ON financial_item_category(user_id, market, statement_type);
|
||||
|
||||
CREATE TRIGGER update_financial_item_category_updated_at
|
||||
BEFORE UPDATE ON financial_item_category
|
||||
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
COMMENT ON TABLE financial_item_category IS '科目名到自定义分类的映射,用于按类现金/投资资产等聚合展示';
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 账户相关表设计
|
||||
|
||||
### positions 持仓表
|
||||
|
||||
Reference in New Issue
Block a user