diff --git a/main.py b/main.py index be06463..24639e6 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ from apscheduler.schedulers.background import BackgroundScheduler from fastapi import FastAPI from starlette.middleware.cors import CORSMiddleware -from config.database import get_db +from config.database import SessionLocal from routers import routers from service import agent_service @@ -16,7 +16,8 @@ def article_job_task(): """定时执行的任务""" try: print(f"今日资讯定时任务执行: {datetime.now()}") - agent_service.query_article_agent(get_db()) + db = SessionLocal() + agent_service.query_article_agent(db) except Exception as e: print(f"今日资讯定时任务异常: {e}") @@ -24,7 +25,8 @@ def recipe_job_task(): """定时执行的任务""" try: print(f"今日菜谱定时任务执行: {datetime.now()}") - agent_service.query_recipe_agent(get_db()) + db = SessionLocal() + agent_service.query_recipe_agent(db) except Exception as e: print(f"今日菜谱定时任务异常: {e}") diff --git a/models/health.py b/models/health.py index da6e9bc..6bdc740 100644 --- a/models/health.py +++ b/models/health.py @@ -1,4 +1,4 @@ -from sqlalchemy import (Column, Date, JSON) +from sqlalchemy import (Column, Date, JSON, String) from models.base import AuditBase @@ -14,3 +14,8 @@ class Recipe(AuditBase): class Report(AuditBase): content = Column(JSON, nullable=False, default=dict, comment="内容") + +class User(AuditBase): + unique_id = Column(String(45), nullable=False, comment="唯一标识") + level = Column(String(45), nullable=False, comment="会员等级") + expiry_date = Column(Date, nullable=False, comment="截止日期") diff --git a/routers/__init__.py b/routers/__init__.py index cde2883..c46f46a 100644 --- a/routers/__init__.py +++ b/routers/__init__.py @@ -1,7 +1,9 @@ from .agent import router as agent_router from .report import router as report_router +from .user import router as user_router routers = [ agent_router, - report_router + report_router, + user_router, ] diff --git a/routers/user.py b/routers/user.py new file mode 100644 index 0000000..a9d89a2 --- /dev/null +++ b/routers/user.py @@ -0,0 +1,12 @@ +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session + +from config.database import get_db +from service import user_service + +router = APIRouter(prefix="/user", tags=["User"]) + + +@router.get("/{id}") +def query_user_level(id: str, db: Session = Depends(get_db)): + return user_service.query_user_level(id, db) diff --git a/service/agent_service.py b/service/agent_service.py index 4b6723d..c0f83a2 100644 --- a/service/agent_service.py +++ b/service/agent_service.py @@ -53,6 +53,16 @@ def get_latest_article(db: Session): ) article = result.scalar_one_or_none() + if article is None: + return [ + { + "title": "", + "content": "", + "summary": "", + "category": "" + } + ] + return article.content @@ -99,6 +109,15 @@ def get_latest_recipe(db: Session): ) recipe = result.scalar_one_or_none() + if recipe is None: + return [ + { + "name": "", + "tags": [], + "content": "" + } + ] + return recipe.content diff --git a/service/user_service.py b/service/user_service.py new file mode 100644 index 0000000..8e5a89a --- /dev/null +++ b/service/user_service.py @@ -0,0 +1,16 @@ +from sqlalchemy import select +from sqlalchemy.orm import Session + +from models.health import User + + +def query_user_level(unique_id: str, db: Session): + result = db.execute( + select(User).where(User.unique_id == unique_id) + ) + user = result.scalar_one_or_none() + + if user is None: + return {} + + return user