Files
group_xinghuo_jinrong/scripts/core/01-ddl.sql
T
zhanghongyu_0626 3fb0ceb334 Enhance suitability assessment and documentation
- Implemented `check_suitability` method in `CoreReadOnlyRepository` for suitability determination based on customer and product risk levels.
- Added `build_suitability_log_row` function in `suitability.py` for mapping suitability check results to `risk_suitability_log`.
- Updated `AGENTS.md`, `ENVIRONMENT.md`, and `FLOW.md` to reflect changes in suitability assessment processes and documentation.
- Revised `FRAMEWORK.md` and `MEMORY.md` to clarify project structure and data flow related to suitability checks.
- Expanded `TODO.md` with tasks related to logging and auditing suitability assessments.
2026-09-07 11:15:30 +08:00

195 lines
11 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- jinrong_core 表结构 · 对齐客户资料(用户信息示例 / 适当性指南 / AML 规则)
USE jinrong_core;
-- 风险等级字典(客户 C1~C5 · 产品 R1~R5)
CREATE TABLE core_risk_grade (
code VARCHAR(4) NOT NULL PRIMARY KEY COMMENT 'C1~C5 或 R1~R5',
grade_type ENUM('customer','product') NOT NULL,
display_name VARCHAR(32) NOT NULL,
sort_order TINYINT UNSIGNED NOT NULL
) ENGINE=InnoDB COMMENT='风险等级字典';
CREATE TABLE core_industry (
industry_code VARCHAR(16) NOT NULL PRIMARY KEY,
industry_name VARCHAR(64) NOT NULL
) ENGINE=InnoDB COMMENT='行业分类';
-- C×R 适当性匹配矩阵(《个人投资者适当性管理指南》第十二条)
CREATE TABLE core_suitability_rule (
id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
customer_risk_code CHAR(2) NOT NULL COMMENT 'C1~C5',
product_risk_code CHAR(2) NOT NULL COMMENT 'R1~R5',
match_result ENUM('allowed','allowed_with_disclosure','forbidden') NOT NULL,
rule_ref VARCHAR(32) NOT NULL DEFAULT 'JR-AST-012',
UNIQUE KEY uk_cx_r (customer_risk_code, product_risk_code),
CONSTRAINT fk_suit_cust_risk FOREIGN KEY (customer_risk_code) REFERENCES core_risk_grade(code),
CONSTRAINT fk_suit_prod_risk FOREIGN KEY (product_risk_code) REFERENCES core_risk_grade(code)
) ENGINE=InnoDB COMMENT='适当性匹配规则(L0 权威)';
-- 内部员工(含 RBAC 角色种子,供 JWT/鉴权联调对照)
CREATE TABLE core_staff (
staff_id VARCHAR(64) NOT NULL PRIMARY KEY,
display_name VARCHAR(64) NOT NULL,
staff_type ENUM('advisor','analyst','risk_officer','compliance','ops') NOT NULL,
roles JSON NOT NULL COMMENT 'JWT roles 数组,如 ["advisor"]',
tenant_id VARCHAR(32) NOT NULL DEFAULT 'TENANT-001',
is_active TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB COMMENT='内部员工主档(模拟 IdP 账号源)';
-- 客户主档 L0(KYC + 财务概况 · 对齐用户信息数据示例)
CREATE TABLE core_customer (
customer_id VARCHAR(64) NOT NULL PRIMARY KEY,
display_name VARCHAR(64) NOT NULL COMMENT '脱敏展示名',
gender ENUM('M','F','U') NOT NULL DEFAULT 'U',
birth_date DATE NULL,
age TINYINT UNSIGNED NULL,
id_no_mask VARCHAR(32) NULL COMMENT '310101199903XXXXXX',
occupation VARCHAR(64) NULL,
employer VARCHAR(128) NULL,
education ENUM('high_school','associate','bachelor','master','doctor','other') NULL,
marital_status ENUM('single','married','widowed','divorced','other') NULL,
city VARCHAR(64) NULL,
address_mask VARCHAR(256) NULL,
phone_mask VARCHAR(16) NULL,
email_mask VARCHAR(64) NULL,
annual_income DECIMAL(18,2) NULL COMMENT '家庭年收入(元)',
financial_asset DECIMAL(18,2) NULL COMMENT '金融资产规模(不含房产)',
monthly_investable DECIMAL(18,2) NULL COMMENT '月可投资金额',
is_hnw TINYINT(1) NOT NULL DEFAULT 0 COMMENT '高净值客户',
service_tier ENUM('normal','vip','diamond') NOT NULL DEFAULT 'normal',
is_pep TINYINT(1) NOT NULL DEFAULT 0 COMMENT '政治公众人物',
aml_risk_level ENUM('low','medium','high') NOT NULL DEFAULT 'low',
invest_experience_years TINYINT UNSIGNED NULL,
first_invest_date DATE NULL,
tenant_id VARCHAR(32) NOT NULL DEFAULT 'TENANT-001',
open_date DATE NOT NULL,
is_active TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
KEY idx_service_tier (service_tier),
KEY idx_hnw (is_hnw),
KEY idx_aml (aml_risk_level)
) ENGINE=InnoDB COMMENT='客户主档 L0 · KYC';
-- 正式风险测评 L0(对齐适当性指南 16 题问卷 + FM-03 风评过期)
CREATE TABLE core_customer_risk (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
customer_id VARCHAR(64) NOT NULL,
risk_code CHAR(2) NOT NULL COMMENT 'C1~C5',
questionnaire_score SMALLINT UNSIGNED NULL COMMENT '问卷总分 20-100',
max_loss_tolerance_pct DECIMAL(5,2) NULL COMMENT '最大可承受亏损比例',
investment_goal VARCHAR(128) NULL,
investment_horizon ENUM('short','medium','long','flexible') NULL,
investor_category ENUM('ordinary','professional','professional_pending') NOT NULL DEFAULT 'ordinary',
professional_approved_at DATE NULL,
is_authoritative TINYINT(1) NOT NULL DEFAULT 1,
evaluated_at DATE NOT NULL,
expires_at DATE NOT NULL COMMENT '风评有效期(通常 evaluated_at+12 月)',
source VARCHAR(32) NOT NULL DEFAULT 'risk_questionnaire',
UNIQUE KEY uk_customer_current (customer_id),
KEY idx_risk (risk_code),
KEY idx_expires (expires_at),
KEY idx_investor_cat (investor_category),
CONSTRAINT fk_cust_risk_customer FOREIGN KEY (customer_id) REFERENCES core_customer(customer_id),
CONSTRAINT fk_cust_risk_code FOREIGN KEY (risk_code) REFERENCES core_risk_grade(code)
) ENGINE=InnoDB COMMENT='客户正式风险测评 L0';
CREATE TABLE core_customer_advisor (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
customer_id VARCHAR(64) NOT NULL,
advisor_id VARCHAR(64) NOT NULL COMMENT '对应 core_staff.staff_id',
rel_status ENUM('active','transferred','closed') NOT NULL DEFAULT 'active',
effective_from DATE NOT NULL,
effective_to DATE NULL,
UNIQUE KEY uk_cust_advisor_from (customer_id, advisor_id, effective_from),
KEY idx_advisor (advisor_id, rel_status),
CONSTRAINT fk_ca_customer FOREIGN KEY (customer_id) REFERENCES core_customer(customer_id),
CONSTRAINT fk_ca_advisor FOREIGN KEY (advisor_id) REFERENCES core_staff(staff_id)
) ENGINE=InnoDB COMMENT='客户-代理人归属';
-- 产品主档(对齐 C-11:风险等级 + 期限 + 起购金额)
CREATE TABLE core_product (
product_id VARCHAR(64) NOT NULL PRIMARY KEY,
product_name VARCHAR(128) NOT NULL,
product_type ENUM(
'money','bond','mixed','stock','index',
'wealth_mgmt','private_fund','insurance','structured'
) NOT NULL,
min_risk_code CHAR(2) NOT NULL COMMENT 'R1~R5 最低适配',
min_subscribe_amount DECIMAL(18,2) NOT NULL DEFAULT 1.00 COMMENT '起购金额(元)',
term_days INT UNSIGNED NULL COMMENT '产品期限(天),NULL=灵活开放',
requires_disclosure TINYINT(1) NOT NULL DEFAULT 0 COMMENT '购买前需签署风险揭示书',
industry_code VARCHAR(16) NULL,
fee_rate DECIMAL(6,4) NULL,
is_open TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
KEY idx_min_risk (min_risk_code),
KEY idx_product_type (product_type),
CONSTRAINT fk_product_risk FOREIGN KEY (min_risk_code) REFERENCES core_risk_grade(code),
CONSTRAINT fk_product_industry FOREIGN KEY (industry_code) REFERENCES core_industry(industry_code)
) ENGINE=InnoDB COMMENT='产品主档';
CREATE TABLE core_holding (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
customer_id VARCHAR(64) NOT NULL,
product_id VARCHAR(64) NOT NULL,
qty DECIMAL(18,4) NOT NULL,
cost_amount DECIMAL(18,2) NOT NULL,
market_value DECIMAL(18,2) NOT NULL,
pnl_pct DECIMAL(8,4) NOT NULL COMMENT '盈亏比例',
as_of DATE NOT NULL,
UNIQUE KEY uk_cust_product (customer_id, product_id),
KEY idx_customer (customer_id),
CONSTRAINT fk_hold_customer FOREIGN KEY (customer_id) REFERENCES core_customer(customer_id),
CONSTRAINT fk_hold_product FOREIGN KEY (product_id) REFERENCES core_product(product_id)
) ENGINE=InnoDB COMMENT='持仓快照';
-- 交易流水(扩展 AML 字段 · 对齐反洗钱规则 RW-001~020)
CREATE TABLE core_trade (
trade_id VARCHAR(64) NOT NULL PRIMARY KEY,
customer_id VARCHAR(64) NOT NULL,
product_id VARCHAR(64) NOT NULL,
trade_type ENUM('subscribe','redeem','convert') NOT NULL,
amount DECIMAL(18,2) NOT NULL,
qty DECIMAL(18,4) NULL,
channel ENUM('online','mobile','counter','other') NOT NULL DEFAULT 'online',
counterparty_account_mask VARCHAR(32) NULL,
counterparty_name VARCHAR(64) NULL,
is_cash TINYINT(1) NOT NULL DEFAULT 0,
payer_name VARCHAR(64) NULL COMMENT '代付人(RW-014)',
is_third_party_pay TINYINT(1) NOT NULL DEFAULT 0,
trade_status ENUM('confirmed','pending','cancelled') NOT NULL DEFAULT 'confirmed',
traded_at DATETIME(3) NOT NULL,
KEY idx_customer_time (customer_id, traded_at),
KEY idx_amount (amount, traded_at),
KEY idx_channel_time (channel, traded_at),
CONSTRAINT fk_trade_customer FOREIGN KEY (customer_id) REFERENCES core_customer(customer_id),
CONSTRAINT fk_trade_product FOREIGN KEY (product_id) REFERENCES core_product(product_id)
) ENGINE=InnoDB COMMENT='交易流水';
CREATE TABLE core_cash_flow (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
customer_id VARCHAR(64) NOT NULL,
flow_type ENUM('in','out') NOT NULL,
flow_subtype ENUM('deposit','withdraw','transfer_in','transfer_out','subscribe','redeem','other') NOT NULL DEFAULT 'other',
amount DECIMAL(18,2) NOT NULL,
channel ENUM('online','mobile','counter','other') NOT NULL DEFAULT 'online',
counterparty_account_mask VARCHAR(32) NULL,
counterparty_name VARCHAR(64) NULL,
remark VARCHAR(128) NULL,
occurred_at DATETIME(3) NOT NULL,
KEY idx_customer (customer_id, occurred_at),
KEY idx_flow_subtype (flow_subtype, occurred_at),
CONSTRAINT fk_cf_customer FOREIGN KEY (customer_id) REFERENCES core_customer(customer_id)
) ENGINE=InnoDB COMMENT='资金进出';
CREATE TABLE core_product_nav (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
product_id VARCHAR(64) NOT NULL,
nav DECIMAL(10,4) NOT NULL,
daily_chg_pct DECIMAL(8,4) NOT NULL,
nav_date DATE NOT NULL,
UNIQUE KEY uk_product_date (product_id, nav_date),
CONSTRAINT fk_nav_product FOREIGN KEY (product_id) REFERENCES core_product(product_id)
) ENGINE=InnoDB COMMENT='产品净值';