- 🔧 账户名称优化:改为港股账户、美股账户、A股账户、加密货币账户 - ✨ 成交总额自动计算:根据数量 × 价格自动计算 - 🗑️ 删除持仓功能:支持通过卖出全部来删除持仓 - 🎨 Select 组件显示优化:修复了下拉框显示问题 - 📝 代码注释中文化
96 lines
3.3 KiB
TypeScript
96 lines
3.3 KiB
TypeScript
import { PrismaClient, MarketType } from '@prisma/client'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
async function main() {
|
|
// 创建默认用户
|
|
const user = await prisma.user.upsert({
|
|
where: { email: 'demo@portfolio.local' },
|
|
update: {},
|
|
create: {
|
|
email: 'demo@portfolio.local',
|
|
name: 'Demo User',
|
|
},
|
|
})
|
|
|
|
// 创建各市场的账户
|
|
const accounts = [
|
|
{ name: '港股账户', marketType: MarketType.HK, baseCurrency: 'HKD' },
|
|
{ name: '美股账户', marketType: MarketType.US, baseCurrency: 'USD' },
|
|
{ name: 'A股账户', marketType: MarketType.CN, baseCurrency: 'CNY' },
|
|
{ name: '加密货币账户', marketType: MarketType.CRYPTO, baseCurrency: 'USDT' },
|
|
]
|
|
|
|
for (const acc of accounts) {
|
|
await prisma.account.upsert({
|
|
where: { id: `${user.id}-${acc.marketType}` },
|
|
update: {},
|
|
create: {
|
|
id: `${user.id}-${acc.marketType}`,
|
|
userId: user.id,
|
|
name: acc.name,
|
|
marketType: acc.marketType,
|
|
baseCurrency: acc.baseCurrency,
|
|
balance: 0,
|
|
},
|
|
})
|
|
}
|
|
|
|
// 初始化汇率(以 USD 为基准)
|
|
const exchangeRates = [
|
|
{ fromCurrency: 'USD', toCurrency: 'USD', rate: 1, date: new Date('2026-01-01') },
|
|
{ fromCurrency: 'CNY', toCurrency: 'USD', rate: 0.137, date: new Date('2026-01-01') },
|
|
{ fromCurrency: 'HKD', toCurrency: 'USD', rate: 0.129, date: new Date('2026-01-01') },
|
|
{ fromCurrency: 'USDT', toCurrency: 'USD', rate: 1, date: new Date('2026-01-01') },
|
|
]
|
|
|
|
for (const rate of exchangeRates) {
|
|
await prisma.exchangeRate.upsert({
|
|
where: {
|
|
fromCurrency_toCurrency_effectiveDate: {
|
|
fromCurrency: rate.fromCurrency,
|
|
toCurrency: rate.toCurrency,
|
|
effectiveDate: rate.date,
|
|
},
|
|
},
|
|
update: {},
|
|
create: {
|
|
fromCurrency: rate.fromCurrency,
|
|
toCurrency: rate.toCurrency,
|
|
rate: rate.rate,
|
|
effectiveDate: rate.date,
|
|
},
|
|
})
|
|
}
|
|
|
|
// 初始化常见证券
|
|
const securities = [
|
|
{ symbol: '00700', name: '腾讯控股', market: MarketType.HK, currency: 'HKD', lotSize: 100, priceDecimals: 2, qtyDecimals: 0 },
|
|
{ symbol: '09988', name: '阿里巴巴', market: MarketType.HK, currency: 'HKD', lotSize: 100, priceDecimals: 2, qtyDecimals: 0 },
|
|
{ symbol: 'AAPL', name: 'Apple Inc.', market: MarketType.US, currency: 'USD', lotSize: 1, priceDecimals: 2, qtyDecimals: 0 },
|
|
{ symbol: 'MSFT', name: 'Microsoft Corp.', market: MarketType.US, currency: 'USD', lotSize: 1, priceDecimals: 2, qtyDecimals: 0 },
|
|
{ symbol: 'NVDA', name: 'NVIDIA Corp.', market: MarketType.US, currency: 'USD', lotSize: 1, priceDecimals: 2, qtyDecimals: 0 },
|
|
{ symbol: 'BTC', name: 'Bitcoin', market: MarketType.CRYPTO, currency: 'USDT', lotSize: 1, priceDecimals: 2, qtyDecimals: 8, isCrypto: true },
|
|
{ symbol: 'ETH', name: 'Ethereum', market: MarketType.CRYPTO, currency: 'USDT', lotSize: 1, priceDecimals: 2, qtyDecimals: 8, isCrypto: true },
|
|
]
|
|
|
|
for (const sec of securities) {
|
|
await prisma.security.upsert({
|
|
where: { symbol: sec.symbol },
|
|
update: {},
|
|
create: sec,
|
|
})
|
|
}
|
|
|
|
console.log('Seed completed:', { user, accountsCreated: accounts.length })
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|