From 65997d9c3147dfe54ea7ec619486c91aea3e318c Mon Sep 17 00:00:00 2001 From: lookerzz <1031516901@qq.com> Date: Mon, 21 Sep 2026 14:34:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E6=A8=A1=E5=9D=97=E5=86=99?= =?UTF-8?q?=E5=A5=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/statistics.py | 28 ++++++++++++++++++++++++++++ dao/statistics.py | 30 ++++++++++++++++++++++++++++++ main.py | 2 ++ schemas/statistics.py | 4 ++++ 4 files changed, 64 insertions(+) diff --git a/api/statistics.py b/api/statistics.py index e69de29..082f7f4 100644 --- a/api/statistics.py +++ b/api/statistics.py @@ -0,0 +1,28 @@ +from fastapi import APIRouter,HTTPException,Depends +from DoubaoTeam.core.database import get_db +from DoubaoTeam.models.student import Student +from DoubaoTeam.models.score import Score +from DoubaoTeam.dao.statistics import * +from DoubaoTeam.schemas.common import SuccessResponse +from sqlalchemy.orm import Session + + +statistics_router=APIRouter() + + +@statistics_router.get("/scores/age-over-30",response_model=SuccessResponse) +def api_age_over_thirty(db:Session=Depends(get_db)): + try: + age_over_thirty_list=age_over_thirty(Student,db) + return SuccessResponse(status_code=200,msg=age_over_thirty_list) + except NoExist as e: + raise HTTPException(status_code=404,detail=str(e)) +# 这边功能函数返回的是一个字典 +@statistics_router.get("/classes/gender-count",response_model=SuccessResponse) +def api_gender_count(db:Session=Depends(get_db)): + try: + result = gender_count(Student,db) + return SuccessResponse(status_code=200,msg=result) + except NoExist as e: + raise HTTPException(status_code=404,detail=str(e)) + diff --git a/dao/statistics.py b/dao/statistics.py index e69de29..6b7c9e8 100644 --- a/dao/statistics.py +++ b/dao/statistics.py @@ -0,0 +1,30 @@ +from DoubaoTeam.schemas.statistics import NoExist +from sqlalchemy import func + +#`GET /statistics/students/age-over-30` +#查询所有超过 30 岁的学员信息 + +def age_over_thirty(Student,db): + student_list=db.query(Student).filter(Student.age>=30).all() + if not student_list : + raise NoExist('无目标年龄大于30岁') + return student_list + +def gender_count(Student,db): + result=db.query(Student.class_id,Student.gender,func.count(Student.id)).group_by(Student.class_id,Student.gender).all() + if not result : + raise NoExist('班里招点人吧,要倒闭了') + dict1 = {} + for i,j,k in result: + if i not in dict1 : + dict1[i]={'男':0,'女':0} + dict[i][j]+=k + return dict1 +#查询每次考试成绩都在 80 分以上的学生编号、姓名和成绩 +def score_over_eighty(Score,Student,db): + student_list=db.query(Student.id, Student.student_name)\ + .join(Student,Score.student_id==Student.id).group_by(Student.student_no,Student.student_name)\ + .having(func.min(Score.score)>80).all() + for i in student_list: + student_list2 = db.query(Student.student_no, Student.student_name,Score.score).join(Student,Score.student_id==Student.id).filter(i.id==Student.id).all() + diff --git a/main.py b/main.py index fe32962..718cc8c 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ from fastapi import FastAPI from api.example import example_router from middleware import log_middleware +from api.statistics import statistics_router import uvicorn app = FastAPI() app.middleware("http")(log_middleware) @@ -15,6 +16,7 @@ app.middleware("http")(log_middleware) #-------------张昕浩--------------- app.include_router(example_router) +app.include_router(statistics_router, prefix="/statistics") #-------------曾凯--------------- diff --git a/schemas/statistics.py b/schemas/statistics.py index e69de29..cdd636f 100644 --- a/schemas/statistics.py +++ b/schemas/statistics.py @@ -0,0 +1,4 @@ +class NoExist(Exception): + def __init__(self,value): + self.value = value + pass \ No newline at end of file