118 lines
3.1 KiB
Plaintext
118 lines
3.1 KiB
Plaintext
generator client {
|
||
provider = "prisma-client-js"
|
||
}
|
||
|
||
datasource db {
|
||
provider = "postgresql"
|
||
url = env("DATABASE_URL")
|
||
}
|
||
|
||
// 用户表(预留扩展)
|
||
model User {
|
||
id String @id @default(cuid())
|
||
email String @unique
|
||
name String?
|
||
createdAt DateTime @default(now())
|
||
updatedAt DateTime @updatedAt
|
||
accounts Account[]
|
||
}
|
||
|
||
// 账户表
|
||
model Account {
|
||
id String @id @default(cuid())
|
||
userId String
|
||
user User @relation(fields: [userId], references: [id])
|
||
name String
|
||
marketType MarketType
|
||
baseCurrency String
|
||
balance Decimal @default(0) @db.Decimal(20, 4)
|
||
createdAt DateTime @default(now())
|
||
updatedAt DateTime @updatedAt
|
||
transactions Transaction[]
|
||
positions Position[]
|
||
|
||
@@index([userId])
|
||
}
|
||
|
||
enum MarketType {
|
||
US // 美股
|
||
CN // A股
|
||
HK // 港股
|
||
CRYPTO // 加密货币
|
||
}
|
||
|
||
// 证券参考表
|
||
model Security {
|
||
id String @id @default(cuid())
|
||
symbol String @unique
|
||
name String
|
||
market MarketType
|
||
currency String
|
||
lotSize Int @default(1) // 港股每手股数,A股=100
|
||
priceDecimals Int @default(2) // 价格精度
|
||
qtyDecimals Int @default(0) // 数量精度:股票=0,crypto=8
|
||
isCrypto Boolean @default(false)
|
||
createdAt DateTime @default(now())
|
||
updatedAt DateTime @updatedAt
|
||
}
|
||
|
||
// 交易流水表
|
||
model Transaction {
|
||
id String @id @default(cuid())
|
||
accountId String
|
||
account Account @relation(fields: [accountId], references: [id])
|
||
type TransactionType
|
||
symbol String?
|
||
quantity Decimal? @db.Decimal(20, 8)
|
||
price Decimal? @db.Decimal(20, 8)
|
||
amount Decimal @db.Decimal(20, 4)
|
||
fee Decimal @default(0) @db.Decimal(20, 4)
|
||
networkFee Decimal? @db.Decimal(20, 8)
|
||
currency String
|
||
exchangeRate Decimal? @db.Decimal(20, 8)
|
||
notes String?
|
||
executedAt DateTime
|
||
createdAt DateTime @default(now())
|
||
|
||
@@index([accountId, executedAt])
|
||
@@index([symbol])
|
||
}
|
||
|
||
enum TransactionType {
|
||
DEPOSIT // 入金
|
||
WITHDRAW // 出金
|
||
BUY // 买入
|
||
SELL // 卖出
|
||
DIVIDEND // 分红
|
||
INTEREST // 利息
|
||
FEE // 费用/手续费
|
||
}
|
||
|
||
// 持仓表(查询优化)
|
||
model Position {
|
||
id String @id @default(cuid())
|
||
accountId String
|
||
account Account @relation(fields: [accountId], references: [id])
|
||
symbol String
|
||
quantity Decimal @db.Decimal(20, 8)
|
||
averageCost Decimal @db.Decimal(20, 8)
|
||
currency String
|
||
updatedAt DateTime @updatedAt
|
||
|
||
@@unique([accountId, symbol])
|
||
@@index([accountId])
|
||
}
|
||
|
||
// 汇率参考表
|
||
model ExchangeRate {
|
||
id String @id @default(cuid())
|
||
fromCurrency String
|
||
toCurrency String
|
||
rate Decimal @db.Decimal(20, 8)
|
||
effectiveDate DateTime
|
||
createdAt DateTime @default(now())
|
||
|
||
@@unique([fromCurrency, toCurrency, effectiveDate])
|
||
@@index([fromCurrency, toCurrency])
|
||
}
|