数据同步页面增加按交易所同步按钮
This commit is contained in:
@@ -36,6 +36,15 @@ export interface InitStocksResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SyncStocksByExchangeResponse {
|
||||||
|
success: boolean
|
||||||
|
data: {
|
||||||
|
exchange: string
|
||||||
|
count: number
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function getToken(): string | null {
|
export function getToken(): string | null {
|
||||||
try {
|
try {
|
||||||
return localStorage.getItem('access_token')
|
return localStorage.getItem('access_token')
|
||||||
@@ -118,4 +127,7 @@ export const api = {
|
|||||||
|
|
||||||
initStocks: () =>
|
initStocks: () =>
|
||||||
request<InitStocksResponse>('/api/admin/data-sync/init-stocks', { method: 'POST' }),
|
request<InitStocksResponse>('/api/admin/data-sync/init-stocks', { method: 'POST' }),
|
||||||
|
|
||||||
|
syncStocksByExchange: (exchange: string) =>
|
||||||
|
request<SyncStocksByExchangeResponse>(`/api/admin/data-sync/stocks/${exchange}`, { method: 'POST' }),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,22 @@ import { useState } from 'react'
|
|||||||
import { Database, RefreshCw, CheckCircle, AlertCircle } from 'lucide-react'
|
import { Database, RefreshCw, CheckCircle, AlertCircle } from 'lucide-react'
|
||||||
import { api } from '@/lib/api'
|
import { api } from '@/lib/api'
|
||||||
|
|
||||||
|
const EXCHANGES = [
|
||||||
|
{ code: 'SSE', name: '上交所' },
|
||||||
|
{ code: 'SZSE', name: '深交所' },
|
||||||
|
{ code: 'BSE', name: '北交所' },
|
||||||
|
]
|
||||||
|
|
||||||
export function DataSync() {
|
export function DataSync() {
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState<string | null>(null)
|
||||||
const [result, setResult] = useState<{ count: number; message: string } | null>(null)
|
const [result, setResult] = useState<{ exchange?: string; count: number; message: string } | null>(null)
|
||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
|
|
||||||
const handleInitStocks = async () => {
|
const handleInitStocks = async () => {
|
||||||
if (!confirm('确定要从 Tushare 初始化股票列表?这会清空现有股票数据。')) {
|
if (!confirm('确定要从 Tushare 初始化股票列表?这会清空现有股票数据。')) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setLoading(true)
|
setLoading('init')
|
||||||
setError('')
|
setError('')
|
||||||
setResult(null)
|
setResult(null)
|
||||||
try {
|
try {
|
||||||
@@ -20,7 +26,24 @@ export function DataSync() {
|
|||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError(err?.message || '初始化失败')
|
setError(err?.message || '初始化失败')
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleSyncByExchange = async (exchange: string, name: string) => {
|
||||||
|
if (!confirm(`确定要同步${name}(${exchange})股票数据?`)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setLoading(exchange)
|
||||||
|
setError('')
|
||||||
|
setResult(null)
|
||||||
|
try {
|
||||||
|
const res = await api.syncStocksByExchange(exchange)
|
||||||
|
setResult(res.data)
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err?.message || '同步失败')
|
||||||
|
} finally {
|
||||||
|
setLoading(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +69,10 @@ export function DataSync() {
|
|||||||
{result && (
|
{result && (
|
||||||
<div className="flex items-start gap-2 rounded-btn border border-success/30 bg-success/10 px-3 py-2.5 text-xs text-success">
|
<div className="flex items-start gap-2 rounded-btn border border-success/30 bg-success/10 px-3 py-2.5 text-xs text-success">
|
||||||
<CheckCircle className="h-3.5 w-3.5 mt-px shrink-0" />
|
<CheckCircle className="h-3.5 w-3.5 mt-px shrink-0" />
|
||||||
<span>{result.message},共 {result.count} 条记录</span>
|
<span>
|
||||||
|
{result.message},共 {result.count} 条记录
|
||||||
|
{result.exchange ? `(${result.exchange})` : ''}
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@@ -58,25 +84,50 @@ export function DataSync() {
|
|||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
onClick={handleInitStocks}
|
onClick={handleInitStocks}
|
||||||
disabled={loading}
|
disabled={loading !== null}
|
||||||
className="inline-flex items-center gap-2 px-4 py-2 rounded-btn bg-accent text-white text-sm font-medium hover:bg-accent/90 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
className="inline-flex items-center gap-2 px-4 py-2 rounded-btn bg-accent text-white text-sm font-medium hover:bg-accent/90 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||||
>
|
>
|
||||||
{loading ? (
|
{loading === 'init' ? (
|
||||||
<RefreshCw className="h-4 w-4 animate-spin" />
|
<RefreshCw className="h-4 w-4 animate-spin" />
|
||||||
) : (
|
) : (
|
||||||
<RefreshCw className="h-4 w-4" />
|
<RefreshCw className="h-4 w-4" />
|
||||||
)}
|
)}
|
||||||
{loading ? '同步中...' : '开始同步'}
|
{loading === 'init' ? '同步中...' : '开始同步'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-card border border-border bg-surface p-5 space-y-4">
|
||||||
|
<div>
|
||||||
|
<h2 className="text-sm font-semibold text-foreground">按交易所同步</h2>
|
||||||
|
<p className="text-xs text-secondary mt-1">按交易所分别同步,不影响其他交易所数据</p>
|
||||||
|
</div>
|
||||||
|
<div className="grid grid-cols-3 gap-3">
|
||||||
|
{EXCHANGES.map(({ code, name }) => (
|
||||||
|
<button
|
||||||
|
key={code}
|
||||||
|
onClick={() => handleSyncByExchange(code, name)}
|
||||||
|
disabled={loading !== null}
|
||||||
|
className="inline-flex items-center justify-center gap-2 px-4 py-2 rounded-btn border border-border bg-surface text-foreground text-sm font-medium hover:bg-accent hover:text-white hover:border-accent disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||||
|
>
|
||||||
|
{loading === code ? (
|
||||||
|
<RefreshCw className="h-4 w-4 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<RefreshCw className="h-4 w-4" />
|
||||||
|
)}
|
||||||
|
{loading === code ? '同步中...' : `${name} (${code})`}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="rounded-card border border-border bg-surface p-5">
|
<div className="rounded-card border border-border bg-surface p-5">
|
||||||
<h2 className="text-sm font-semibold text-foreground mb-3">说明</h2>
|
<h2 className="text-sm font-semibold text-foreground mb-3">说明</h2>
|
||||||
<ul className="space-y-2 text-sm text-secondary">
|
<ul className="space-y-2 text-sm text-secondary">
|
||||||
<li>• 需要配置 TUSHARE_TOKEN 环境变量</li>
|
<li>• 需要配置 TUSHARE_TOKEN 环境变量</li>
|
||||||
<li>• 每次同步会清空 stocks 表并重新写入</li>
|
<li>• 初始化会清空 stocks 表并重新写入</li>
|
||||||
<li>• 仅获取基础字段:代码、名称、交易所、行业等</li>
|
<li>• 按交易所同步采用 upsert 策略,以 ts_code 为唯一键更新或插入</li>
|
||||||
|
<li>• 同步字段包括:代码、名称、交易所、行业、上市状态、上市日期、实控人等</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user