150 lines
4.3 KiB
Python
150 lines
4.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
import os
|
|
from ...core.database import get_db
|
|
from ...schemas.lottery import (
|
|
SSQLottery, SSQLotteryCreate,
|
|
DLTLottery, DLTLotteryCreate,
|
|
LotteryQuery
|
|
)
|
|
from ...services.lottery import LotteryService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/ssq/", response_model=SSQLottery)
|
|
def create_ssq_lottery(
|
|
lottery: SSQLotteryCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
return LotteryService.create_ssq_lottery(db, lottery)
|
|
|
|
|
|
@router.post("/dlt/", response_model=DLTLottery)
|
|
def create_dlt_lottery(
|
|
lottery: DLTLotteryCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
return LotteryService.create_dlt_lottery(db, lottery)
|
|
|
|
|
|
@router.get("/ssq/", response_model=List[SSQLottery])
|
|
def get_ssq_lotteries(
|
|
query: LotteryQuery = Depends(),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
lotteries, total = LotteryService.get_ssq_lotteries(db, query)
|
|
return lotteries
|
|
|
|
|
|
@router.get("/dlt/", response_model=List[DLTLottery])
|
|
def get_dlt_lotteries(
|
|
query: LotteryQuery = Depends(),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
lotteries, total = LotteryService.get_dlt_lotteries(db, query)
|
|
return lotteries
|
|
|
|
|
|
@router.get("/ssq/statistics")
|
|
def get_ssq_statistics(db: Session = Depends(get_db)):
|
|
return LotteryService.get_ssq_statistics(db)
|
|
|
|
|
|
@router.get("/dlt/statistics")
|
|
def get_dlt_statistics(db: Session = Depends(get_db)):
|
|
return LotteryService.get_dlt_statistics(db)
|
|
|
|
|
|
@router.post("/ssq/import")
|
|
async def import_ssq_data(
|
|
file: UploadFile = File(...),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
if not file.filename.endswith('.json'):
|
|
raise HTTPException(
|
|
status_code=400, detail="Only JSON files are allowed")
|
|
|
|
# 保存上传的文件
|
|
file_path = f"temp_{file.filename}"
|
|
try:
|
|
with open(file_path, "wb") as buffer:
|
|
content = await file.read()
|
|
buffer.write(content)
|
|
|
|
# 导入数据
|
|
count = LotteryService.import_ssq_data(db, file_path)
|
|
return {"message": f"Successfully imported {count} records"}
|
|
finally:
|
|
# 清理临时文件
|
|
if os.path.exists(file_path):
|
|
os.remove(file_path)
|
|
|
|
|
|
@router.post("/dlt/import")
|
|
async def import_dlt_data(
|
|
file: UploadFile = File(...),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
if not file.filename.endswith('.json'):
|
|
raise HTTPException(
|
|
status_code=400, detail="Only JSON files are allowed")
|
|
|
|
# 保存上传的文件
|
|
file_path = f"temp_{file.filename}"
|
|
try:
|
|
with open(file_path, "wb") as buffer:
|
|
content = await file.read()
|
|
buffer.write(content)
|
|
|
|
# 导入数据
|
|
count = LotteryService.import_dlt_data(db, file_path)
|
|
return {"message": f"Successfully imported {count} records"}
|
|
finally:
|
|
# 清理临时文件
|
|
if os.path.exists(file_path):
|
|
os.remove(file_path)
|
|
|
|
|
|
@router.post("/update/ssq")
|
|
async def update_ssq_data(db: Session = Depends(get_db)):
|
|
"""更新双色球数据"""
|
|
try:
|
|
result = LotteryService.update_ssq_data(db)
|
|
return {
|
|
"success": True,
|
|
"message": f"双色球数据更新成功,新增 {result.get('new_count', 0)} 条记录",
|
|
"data": result
|
|
}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"更新双色球数据失败: {str(e)}")
|
|
|
|
|
|
@router.post("/update/dlt")
|
|
async def update_dlt_data(db: Session = Depends(get_db)):
|
|
"""更新大乐透数据"""
|
|
try:
|
|
result = LotteryService.update_dlt_data(db)
|
|
return {
|
|
"success": True,
|
|
"message": f"大乐透数据更新成功,新增 {result.get('new_count', 0)} 条记录",
|
|
"data": result
|
|
}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"更新大乐透数据失败: {str(e)}")
|
|
|
|
|
|
@router.post("/update/all")
|
|
async def update_all_lottery_data(db: Session = Depends(get_db)):
|
|
"""更新所有彩票数据"""
|
|
try:
|
|
result = LotteryService.update_all_lottery_data(db)
|
|
return {
|
|
"success": True,
|
|
"message": "所有彩票数据更新完成",
|
|
"data": result
|
|
}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"更新彩票数据失败: {str(e)}")
|