更新统计模块查询入参。
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# statcalc_api所有统计分析模块的api路由、接口
|
||||
|
||||
from fastapi import APIRouter , Depends , HTTPException
|
||||
from fastapi import APIRouter , Depends , HTTPException , Query
|
||||
from StatCalc.dao.statcalc_dao import *
|
||||
from StatCalc.schema.statcalc_request import *
|
||||
from databases import *
|
||||
@@ -9,13 +9,13 @@ statcalc_api = APIRouter()
|
||||
|
||||
@statcalc_api.get("/statcalc/age"
|
||||
,summary='人员信息查询'
|
||||
,description=f'查询所有超过30岁的学员的信息。'
|
||||
,description=f'查询所有大于等于输入年龄的学员的信息。'
|
||||
)
|
||||
def get_students(db=Depends(get_db)):
|
||||
result = get_age_30( db=db )
|
||||
if result:
|
||||
return result
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
def get_students( age:int = 0 ,db=Depends(get_db)):
|
||||
result = get_age_30( age=age , db=db )
|
||||
if result is None or len(result) == 0:
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
return result
|
||||
|
||||
@statcalc_api.get("/statcalc/students"
|
||||
,summary='人数统计'
|
||||
@@ -33,25 +33,29 @@ def get_students(headcount:get_student_dao
|
||||
,summary='优秀成绩学生信息查询'
|
||||
,description=f'查询所有科目考试成绩都在80分以上的学生。'
|
||||
)
|
||||
def get_excellent(db=Depends(get_db)):
|
||||
result = get_excellent_students( db=db )
|
||||
def get_excellent(sco:int = Query(80,ge=0, le=100,description="优秀成绩设定")
|
||||
, db=Depends(get_db)
|
||||
):
|
||||
result = get_excellent_students(sco=sco, db=db )
|
||||
if result:
|
||||
return result
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
|
||||
@statcalc_api.get("/statcalc/fail"
|
||||
,summary='不及格成绩查询'
|
||||
,description=f'查询有两次以上不及格的学生的姓名,班级和不及格成绩。'
|
||||
,description=f'查询有两次以上低于输入分数的学生的姓名,班级和不及格成绩。'
|
||||
)
|
||||
def get_students( db=Depends(get_db) ):
|
||||
result = get_score_80( db=db )
|
||||
def get_students(sco:int = Query(60,ge=0, le=100,description="不及格成绩设定")
|
||||
, db=Depends(get_db) ):
|
||||
result = get_score_60( sco=sco ,db=db )
|
||||
if result:
|
||||
return result
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
|
||||
@statcalc_api.get('/statcalc/average', summary='统计每次考试每个班级的平均分')
|
||||
def get_class_avg(db=Depends(get_db)):
|
||||
result = get_class_avg_dao(db)
|
||||
def get_class_avg( class_id:int = Query(1,ge=1, le=3,description="查询班级")
|
||||
, db=Depends(get_db)):
|
||||
result = get_class_avg_dao( class_id , db)
|
||||
if not result:
|
||||
return []
|
||||
return result
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
from pygments.lexers import sql
|
||||
from sentry_sdk.integrations import sqlalchemy
|
||||
|
||||
from StatCalc.schema.statcalc_request import *
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import *
|
||||
|
||||
|
||||
from students.model.students_model import Students
|
||||
|
||||
def get_age_30(db):
|
||||
q = db.query(Students).filter(Students.age>=30).all()
|
||||
def get_age_30( age , db):
|
||||
q = db.query(Students).filter(Students.age>=age).all()
|
||||
return q
|
||||
|
||||
|
||||
@@ -38,7 +35,7 @@ def get_class_count(g , db):
|
||||
from scores.model.score_model import Score
|
||||
|
||||
|
||||
def get_excellent_students(db):
|
||||
def get_excellent_students(sco,db):
|
||||
"""查询所有科目都在80分以上的优秀学生"""
|
||||
# 先查总科目数
|
||||
total_subjects = db.execute(text("SELECT COUNT(*) FROM subject")).scalar()
|
||||
@@ -46,7 +43,7 @@ def get_excellent_students(db):
|
||||
# 子查询:找出成绩>=80分的科目数等于总科目数的学生
|
||||
sq = db.query(Students.id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Score.score >= 80) \
|
||||
.filter(Score.score >= sco) \
|
||||
.group_by(Students.id) \
|
||||
.having(func.count(Score.id) == total_subjects) \
|
||||
.subquery()
|
||||
@@ -55,19 +52,19 @@ def get_excellent_students(db):
|
||||
q = db.query(Students.id, Students.name, ClassInfo.name.label('class_name'), Score.score) \
|
||||
.join(ClassInfo, ClassInfo.id == Students.class_id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score >= 80) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score >= sco) \
|
||||
.all()
|
||||
return [row._asdict() for row in q]
|
||||
|
||||
|
||||
from scores.model.score_model import Score
|
||||
|
||||
def get_score_80(db):
|
||||
def get_score_60(sco ,db):
|
||||
"""查询有两次以上不及格(<60分)的学生"""
|
||||
# 子查询:找出不及格超过2门的学生ID
|
||||
sq = db.query(Students.id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Score.score < 60) \
|
||||
.filter(Score.score < sco) \
|
||||
.group_by(Students.id) \
|
||||
.having(func.count(Score.id) > 2) \
|
||||
.subquery()
|
||||
@@ -76,18 +73,19 @@ def get_score_80(db):
|
||||
q = db.query(Students.name, ClassInfo.name.label('class_name'), Score.score) \
|
||||
.join(ClassInfo, ClassInfo.id == Students.class_id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score < 60) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score < sco) \
|
||||
.all()
|
||||
return [row._asdict() for row in q]
|
||||
|
||||
def get_class_avg_dao(db):
|
||||
def get_class_avg_dao(class_id , db):
|
||||
results = (db.query(Score.num,
|
||||
Score.cid,
|
||||
ClassInfo.name.label("class_name"),
|
||||
func.avg(Score.score).label("avg_score"))
|
||||
.join(ClassInfo, Score.cid == ClassInfo.id)
|
||||
.filter(Score.is_deleted == 0)
|
||||
.group_by(Score.num, Score.cid, ClassInfo.name)
|
||||
.group_by(Score.num, Score.cid, ClassInfo.name,ClassInfo.id)
|
||||
.having(ClassInfo.id == class_id)
|
||||
.order_by(func.avg(Score.score).desc())
|
||||
.all())
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
from fastapi import FastAPI,APIRouter
|
||||
from StatCalc.api.statcalc_api import statcalc_api
|
||||
from databases import *
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
StatCalc_API = APIRouter()
|
||||
StatCalc_API.include_router(statcalc_api)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
# students_schema:请求、响应的模型框架
|
||||
|
||||
from pydantic import BaseModel , Field , field_validator
|
||||
from datetime import datetime
|
||||
import enum
|
||||
|
||||
class get_student_dao(str , enum.Enum):
|
||||
|
||||
Reference in New Issue
Block a user