# 金融服务数据库设计文档

## 1. 概述

本文档详细描述了金融服务(finance-service)的数据库设计，包括贷款管理、保险管理、金融产品等核心业务数据表结构。

## 2. 数据库环境

- 数据库类型：MySQL 8.0
- 字符集：utf8mb4
- 排序规则：utf8mb4_unicode_ci
- 数据库名：xlxumu_finance

## 3. 表结构设计

### 3.1 贷款产品表 (loan_products)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 贷款产品ID |
| name | VARCHAR(100) | NOT NULL | 产品名称 |
| description | TEXT |  | 产品描述 |
| interest_rate | DECIMAL(5,4) | NOT NULL | 年利率 |
| term_months | INT | NOT NULL | 贷款期限(月) |
| min_amount | DECIMAL(12,2) | NOT NULL | 最小贷款金额 |
| max_amount | DECIMAL(12,2) | NOT NULL | 最大贷款金额 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'ACTIVE' | 产品状态(ACTIVE:启用, INACTIVE:停用) |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

### 3.2 贷款申请表 (loan_applications)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 贷款申请ID |
| user_id | BIGINT | NOT NULL | 申请人用户ID |
| product_id | BIGINT | NOT NULL | 贷款产品ID |
| amount | DECIMAL(12,2) | NOT NULL | 申请金额 |
| term_months | INT | NOT NULL | 申请期限(月) |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'SUBMITTED' | 申请状态(SUBMITTED:已提交, APPROVED:已批准, REJECTED:已拒绝, DISBURSED:已放款, SETTLED:已结清, OVERDUE:已逾期) |
| purpose | VARCHAR(200) |  | 贷款用途 |
| contact_info | JSON |  | 联系信息 |
| credit_score | INT |  | 信用评分 |
| approval_time | DATETIME |  | 审批时间 |
| disbursement_time | DATETIME |  | 放款时间 |
| settlement_time | DATETIME |  | 结清时间 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- INDEX idx_user_id (user_id)
- INDEX idx_product_id (product_id)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

### 3.3 贷款还款计划表 (loan_repayment_schedules)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 还款计划ID |
| loan_id | BIGINT | NOT NULL | 贷款申请ID |
| installment_number | INT | NOT NULL | 期数 |
| due_date | DATE | NOT NULL | 应还日期 |
| principal_amount | DECIMAL(12,2) | NOT NULL | 应还本金 |
| interest_amount | DECIMAL(12,2) | NOT NULL | 应还利息 |
| total_amount | DECIMAL(12,2) | NOT NULL | 应还总额 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'PENDING' | 还款状态(PENDING:待还款, PAID:已还款, OVERDUE:已逾期) |
| paid_date | DATE |  | 实际还款日期 |
| paid_amount | DECIMAL(12,2) |  | 实际还款金额 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- INDEX idx_loan_id (loan_id)
- INDEX idx_due_date (due_date)
- INDEX idx_status (status)
- UNIQUE INDEX uk_loan_installment (loan_id, installment_number)

### 3.4 保险产品表 (insurance_products)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 保险产品ID |
| name | VARCHAR(100) | NOT NULL | 产品名称 |
| description | TEXT |  | 产品描述 |
| premium_rate | DECIMAL(5,4) | NOT NULL | 保险费率 |
| coverage_amount | DECIMAL(12,2) | NOT NULL | 保险金额 |
| term_months | INT | NOT NULL | 保险期限(月) |
| type | VARCHAR(50) | NOT NULL | 保险类型(CATTLE:牲畜保险, PROPERTY:财产保险, LIABILITY:责任保险) |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'ACTIVE' | 产品状态(ACTIVE:启用, INACTIVE:停用) |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- INDEX idx_type (type)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

### 3.5 保险投保表 (insurance_policies)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 保单ID |
| user_id | BIGINT | NOT NULL | 投保人用户ID |
| product_id | BIGINT | NOT NULL | 保险产品ID |
| policy_no | VARCHAR(50) | UNIQUE, NOT NULL | 保单号 |
| insured_amount | DECIMAL(12,2) | NOT NULL | 投保金额 |
| premium_amount | DECIMAL(12,2) | NOT NULL | 保费金额 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'ACTIVE' | 保单状态(ACTIVE:有效, EXPIRED:已过期, CLAIMED:已理赔, CANCELLED:已取消) |
| start_date | DATE | NOT NULL | 保险起始日期 |
| end_date | DATE | NOT NULL | 保险结束日期 |
| beneficiary_info | JSON |  | 受益人信息 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- UNIQUE INDEX uk_policy_no (policy_no)
- INDEX idx_user_id (user_id)
- INDEX idx_product_id (product_id)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

### 3.6 理赔申请表 (claims)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 理赔申请ID |
| policy_id | BIGINT | NOT NULL | 关联保单ID |
| claim_no | VARCHAR(50) | UNIQUE, NOT NULL | 理赔编号 |
| claim_amount | DECIMAL(12,2) | NOT NULL | 理赔金额 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'SUBMITTED' | 理赔状态(SUBMITTED:已提交, APPROVED:已批准, REJECTED:已拒绝, SETTLED:已赔付) |
| incident_date | DATE | NOT NULL | 事故日期 |
| incident_description | TEXT | NOT NULL | 事故描述 |
| supporting_documents | JSON |  | 支持文件 |
| approval_time | DATETIME |  | 审批时间 |
| settlement_time | DATETIME |  | 赔付时间 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- UNIQUE INDEX uk_claim_no (claim_no)
- INDEX idx_policy_id (policy_id)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

## 4. 表关系说明

1. 贷款产品与贷款申请是一对多关系
2. 贷款申请与还款计划是一对多关系
3. 保险产品与保险投保是一对多关系
4. 保险投保与理赔申请是一对多关系
5. 用户与贷款申请、保险投保等通过用户ID关联

## 5. 数据初始化

### 5.1 贷款产品状态枚举值
- ACTIVE: 启用
- INACTIVE: 停用

### 5.2 贷款申请状态流转
- SUBMITTED -> APPROVED -> DISBURSED -> SETTLED
- SUBMITTED -> REJECTED
- DISBURSED -> OVERDUE (特殊情况下)

### 5.3 还款状态枚举值
- PENDING: 待还款
- PAID: 已还款
- OVERDUE: 已逾期

### 5.4 保险产品状态枚举值
- ACTIVE: 启用
- INACTIVE: 停用

### 5.5 保单状态枚举值
- ACTIVE: 有效
- EXPIRED: 已过期
- CLAIMED: 已理赔
- CANCELLED: 已取消

### 5.6 理赔状态枚举值
- SUBMITTED: 已提交
- APPROVED: 已批准
- REJECTED: 已拒绝
- SETTLED: 已赔付

## 6. SQL脚本

```sql
-- 创建数据库
CREATE DATABASE IF NOT EXISTS xlxumu_finance 
  CHARACTER SET utf8mb4 
  COLLATE utf8mb4_unicode_ci;

USE xlxumu_finance;

-- 贷款产品表
CREATE TABLE loan_products (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  description TEXT,
  interest_rate DECIMAL(5,4) NOT NULL,
  term_months INT NOT NULL,
  min_amount DECIMAL(12,2) NOT NULL,
  max_amount DECIMAL(12,2) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 贷款申请表
CREATE TABLE loan_applications (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  product_id BIGINT NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  term_months INT NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'SUBMITTED',
  purpose VARCHAR(200),
  contact_info JSON,
  credit_score INT,
  approval_time DATETIME,
  disbursement_time DATETIME,
  settlement_time DATETIME,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_user_id (user_id),
  INDEX idx_product_id (product_id),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 贷款还款计划表
CREATE TABLE loan_repayment_schedules (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  loan_id BIGINT NOT NULL,
  installment_number INT NOT NULL,
  due_date DATE NOT NULL,
  principal_amount DECIMAL(12,2) NOT NULL,
  interest_amount DECIMAL(12,2) NOT NULL,
  total_amount DECIMAL(12,2) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'PENDING',
  paid_date DATE,
  paid_amount DECIMAL(12,2),
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_loan_id (loan_id),
  INDEX idx_due_date (due_date),
  INDEX idx_status (status),
  UNIQUE INDEX uk_loan_installment (loan_id, installment_number)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 保险产品表
CREATE TABLE insurance_products (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  description TEXT,
  premium_rate DECIMAL(5,4) NOT NULL,
  coverage_amount DECIMAL(12,2) NOT NULL,
  term_months INT NOT NULL,
  type VARCHAR(50) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_type (type),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 保险投保表
CREATE TABLE insurance_policies (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  product_id BIGINT NOT NULL,
  policy_no VARCHAR(50) UNIQUE NOT NULL,
  insured_amount DECIMAL(12,2) NOT NULL,
  premium_amount DECIMAL(12,2) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
  start_date DATE NOT NULL,
  end_date DATE NOT NULL,
  beneficiary_info JSON,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  UNIQUE INDEX uk_policy_no (policy_no),
  INDEX idx_user_id (user_id),
  INDEX idx_product_id (product_id),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 理赔申请表
CREATE TABLE claims (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  policy_id BIGINT NOT NULL,
  claim_no VARCHAR(50) UNIQUE NOT NULL,
  claim_amount DECIMAL(12,2) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'SUBMITTED',
  incident_date DATE NOT NULL,
  incident_description TEXT NOT NULL,
  supporting_documents JSON,
  approval_time DATETIME,
  settlement_time DATETIME,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  UNIQUE INDEX uk_claim_no (claim_no),
  INDEX idx_policy_id (policy_id),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```