# platform_db 平台库

## 数据库定位

platform_db 保存商户、玩家基础档案、后台用户、权限、API 密钥、域名、渠道配置等平台基础数据。建议使用 PostgreSQL，第一阶段单机部署，后续可迁移到 CockroachDB 或 PostgreSQL 分库。

## 核心表

### merchants 商户表

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| id | bigint | 商户 ID |
| merchant_code | varchar(64) | 商户编码，唯一 |
| name | varchar(128) | 商户名称 |
| api_key | varchar(128) | 商户公钥标识 |
| secret_hash | varchar(256) | 商户密钥哈希 |
| status | smallint | 1 正常，2 冻结 |
| callback_url | text | 默认回调地址 |
| created_at | timestamptz | 创建时间 |
| updated_at | timestamptz | 更新时间 |

### players 玩家基础表

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| id | bigint | 内部玩家 ID |
| merchant_id | bigint | 商户 ID |
| external_player_id | varchar(128) | 商户侧玩家 ID |
| nickname | varchar(128) | 昵称 |
| status | smallint | 状态 |
| created_at | timestamptz | 创建时间 |

## 关键约束

- `merchants.merchant_code` 必须唯一。
- `players(merchant_id, external_player_id)` 必须唯一。
- 密钥只能保存哈希或加密密文，不能明文落库。

## 示例 SQL

```sql
create unique index uk_merchants_code on merchants(merchant_code);
create unique index uk_players_merchant_external on players(merchant_id, external_player_id);
```

## merchant-gateway 第一阶段落地表

为了先跑通商户 API 和测试服部署，`merchant-gateway` 第一阶段会通过 `平台服务/merchant-gateway/migrations/0001_merchant_gateway_core.sql` 在 `jiekouapi` PostgreSQL 库中创建轻量表。后续正式拆分时，再迁移到更完整的 `platform_db`、`wallet_db` 和 `order_db`。

### merchant_players

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| merchant_id | text | 商户编码 |
| player_id | text | 商户侧玩家 ID |
| nickname | text | 昵称 |
| currency | varchar(16) | 币种 |
| status | varchar(32) | 玩家状态 |
| created_at | timestamptz | 创建时间 |
| updated_at | timestamptz | 更新时间 |

主键：`(merchant_id, player_id)`。

### merchant_sessions

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| token | text | 登录 token |
| merchant_id | text | 商户编码 |
| player_id | text | 玩家 ID |
| game_code | text | 游戏编码 |
| game_url | text | 返回给前端的游戏入口 |
| expires_at | timestamptz | 过期时间 |
| created_at | timestamptz | 创建时间 |

主键：`token`。
