100 lines
4.4 KiB
Python
100 lines
4.4 KiB
Python
# ============================================================
|
||
# api/api_analysis.py —— 统计分析模块路由层(Controller)
|
||
# 职责:定义各类统计查询接口,调用 dao 层的统计分析函数
|
||
# 统一前缀:/analysis(在 main.py 中挂载)
|
||
# ============================================================
|
||
|
||
from fastapi import APIRouter, Query, Depends
|
||
from sqlalchemy.orm import Session
|
||
|
||
# 导入 dao 层的统计分析函数
|
||
from dao.age_analysis import (
|
||
analysis_student_age, # 年龄区间统计
|
||
analysis_class_gender, # 班级性别分布
|
||
analysis_score_over, # 达标学生(每次考试都在分数线以上)
|
||
analysis_score_not_qualified, # 不及格次数达阈值学生
|
||
analysis_avg_score_order, # 每次考试班级平均分
|
||
analysis_employment_salary_order, # 就业薪资 Top N
|
||
analysis_student_employment_time, # 学生就业时长
|
||
avg_employment_time, # 班级平均就业时长
|
||
)
|
||
from database import get_db # 数据库会话依赖
|
||
# 导入统计接口的响应模型
|
||
from scheme.schema_analysis import (
|
||
StudentOut,
|
||
StudentGenderOut,
|
||
StudentRecordOut1,
|
||
StudentRecordOut2,
|
||
StudentRecordOut3,
|
||
StudentRecordOut4,
|
||
StudentRecordOut5,
|
||
StudentRecordOut6,
|
||
)
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
# 动态年龄范围查询(GET /analysis/analysis_age):
|
||
# 支持输入年龄阈值(大于等于/小于等于)动态查询学员信息
|
||
@router.get("/analysis_age", response_model=list[StudentOut])
|
||
async def analysis_age(
|
||
db: Session = Depends(get_db),
|
||
min_age: int | None = Query(None, ge=0, description="请输入查询年龄大于等于"),
|
||
max_age: int | None = Query(None, ge=0, description="请输入查询年龄小于等于"),
|
||
):
|
||
return analysis_student_age(db, min_age, max_age)
|
||
|
||
|
||
# 统计每个班级的总人数,以及按性别(男、女)细分的人数分布(GET /analysis/analysis_gender)
|
||
@router.get("/analysis_gender", response_model=list[StudentGenderOut])
|
||
async def analysis_gender(db: Session = Depends(get_db)):
|
||
return analysis_class_gender(db)
|
||
|
||
|
||
# 查询每次考试成绩都在输入分数线(如80分)以上的学生的编号、姓名和成绩(GET /analysis/analysis_scores)
|
||
@router.get("/analysis_scores", response_model=list[StudentRecordOut1])
|
||
async def analysis_scores(
|
||
db: Session = Depends(get_db),
|
||
score: float = Query(0, ge=0, le=100, description="请输入查询学生每次考试分数在多少以上"),
|
||
):
|
||
return analysis_score_over(db, score)
|
||
|
||
|
||
# 查询有输入指定次数(如两次)以上不及格的学生的姓名、班级和不及格成绩明细(GET /analysis/analysis_not_qualified)
|
||
@router.get("/analysis_not_qualified", response_model=list[StudentRecordOut2])
|
||
async def analysis_not_qualified(
|
||
db: Session = Depends(get_db),
|
||
n: int = Query(1, ge=1, description="请输入不及格次数下限"),
|
||
):
|
||
return analysis_score_not_qualified(db, n)
|
||
|
||
|
||
# 统计每次考试每个班级的平均分,支持按分数从高到低或从低到高动态排序(GET /analysis/analysis_avg_score)
|
||
@router.get("/analysis_avg_score", response_model=list[StudentRecordOut3])
|
||
async def analysis_avg_score(
|
||
db: Session = Depends(get_db),
|
||
sort: str = Query("asc", description="请输入排序类型 asc/desc"),
|
||
):
|
||
return analysis_avg_score_order(db, sort)
|
||
|
||
|
||
# 统计就业薪资排名 Top N(动态输入 N)的学生的姓名、班级、就业时间和就业公司(GET /analysis/analysis_salary_top)
|
||
@router.get("/analysis_salary_top", response_model=list[StudentRecordOut4])
|
||
async def analysis_salary_top(
|
||
db: Session = Depends(get_db),
|
||
n: int = Query(1, ge=1, description="请输入取前几名薪资"),
|
||
):
|
||
return analysis_employment_salary_order(db, n)
|
||
|
||
|
||
# 统计每个学生的就业时长(offer下发时间 - 就业开放时间,单位秒)(GET /analysis/analysis_employment_time)
|
||
@router.get("/analysis_employment_time", response_model=list[StudentRecordOut5])
|
||
async def analysis_employment_time(db: Session = Depends(get_db)):
|
||
return analysis_student_employment_time(db)
|
||
|
||
|
||
# 统计每个班级的平均就业时长(仅统计进入就业阶段,即有就业开放时间的学生)(GET /analysis/analysis_avg_employment_time)
|
||
@router.get("/analysis_avg_employment_time", response_model=list[StudentRecordOut6])
|
||
async def analysis_avg_employment_time(db: Session = Depends(get_db)):
|
||
return avg_employment_time(db)
|