|
|
|
|
|
"""
|
|
|
|
|
|
股票数据路由
|
|
|
|
|
|
"""
|
|
|
|
|
|
from typing import List
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
|
|
|
|
from app.db.session import get_db
|
|
|
|
|
|
from app.schemas.base import ResponseModel
|
|
|
|
|
|
from app.schemas.kline import KlineRequest, BatchKlineRequest
|
|
|
|
|
|
from app.services.stock_service import StockService
|
|
|
|
|
|
from app.core.security import get_current_user
|
|
|
|
|
|
from app.models.user import User
|
|
|
|
|
|
from app.utils.date_utils import parse_date
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/kline", response_model=ResponseModel)
|
|
|
|
|
|
async def get_stock_kline(
|
|
|
|
|
|
codes: str = Query(..., description="股票代码,多个用逗号分隔"),
|
|
|
|
|
|
start_date: str = Query(..., description="开始日期(YYYYMMDD)"),
|
|
|
|
|
|
end_date: str = Query(..., description="结束日期(YYYYMMDD)"),
|
|
|
|
|
|
period: str = Query("daily", description="周期: daily, min1, min5, min15, min30, min60"),
|
|
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
|
|
current_user: User = Depends(get_current_user)
|
|
|
|
|
|
):
|
|
|
|
|
|
"""获取股票K线数据"""
|
|
|
|
|
|
service = StockService(db)
|
|
|
|
|
|
code_list = [c.strip() for c in codes.split(",")]
|
|
|
|
|
|
start = parse_date(start_date)
|
|
|
|
|
|
end = parse_date(end_date)
|
|
|
|
|
|
|
|
|
|
|
|
data = service.get_kline(code_list, start, end, period)
|
|
|
|
|
|
return ResponseModel(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/kline/batch", response_model=ResponseModel)
|
|
|
|
|
|
async def batch_get_stock_kline(
|
|
|
|
|
|
request: BatchKlineRequest,
|
|
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
|
|
current_user: User = Depends(get_current_user)
|
|
|
|
|
|
):
|
|
|
|
|
|
"""批量获取股票K线数据"""
|
|
|
|
|
|
service = StockService(db)
|
|
|
|
|
|
start = parse_date(request.start_date)
|
|
|
|
|
|
end = parse_date(request.end_date)
|
|
|
|
|
|
|
|
|
|
|
|
data = service.get_kline(request.codes, start, end, request.period)
|
|
|
|
|
|
return ResponseModel(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/kline/{code}/chart", response_model=ResponseModel)
|
|
|
|
|
|
async def get_stock_kline_chart(
|
|
|
|
|
|
code: str,
|
|
|
|
|
|
start_date: str = Query(..., description="开始日期(YYYYMMDD)"),
|
|
|
|
|
|
end_date: str = Query(..., description="结束日期(YYYYMMDD)"),
|
|
|
|
|
|
period: str = Query("daily", description="周期: daily, min1, min5, min15, min30, min60"),
|
|
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
|
|
current_user: User = Depends(get_current_user)
|
|
|
|
|
|
):
|
|
|
|
|
|
"""获取股票K线图数据(ECharts格式)"""
|
|
|
|
|
|
service = StockService(db)
|
|
|
|
|
|
start = parse_date(start_date)
|
|
|
|
|
|
end = parse_date(end_date)
|
|
|
|
|
|
|
|
|
|
|
|
data = service.get_kline_chart_data(code, start, end, period)
|
|
|
|
|
|
return ResponseModel(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/snapshot", response_model=ResponseModel)
|
|
|
|
|
|
async def get_stock_snapshot(
|
|
|
|
|
|
codes: str = Query(..., description="股票代码,多个用逗号分隔"),
|
|
|
|
|
|
start_date: str = Query(..., description="开始日期(YYYYMMDD)"),
|
|
|
|
|
|
end_date: str = Query(..., description="结束日期(YYYYMMDD)"),
|
|
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
|
|
current_user: User = Depends(get_current_user)
|
|
|
|
|
|
):
|
|
|
|
|
|
"""获取股票历史快照数据"""
|
|
|
|
|
|
# 这里可以实现快照数据查询
|
|
|
|
|
|
return ResponseModel(data={"message": "功能开发中"})
|