This commit is contained in:
maruizhi
2026-09-21 17:47:50 +08:00
parent f97b46961a
commit 87e6083295
5 changed files with 87 additions and 13 deletions
+1 -1
View File
@@ -27,4 +27,4 @@ def get_emp_info2(class_id: int,db=Depends(get_db)):
@app.post('employment/students/{stu_id}')
def create_emp_info(stu_id: int,db=Depends(get_db)):
pass
+22 -5
View File
@@ -1,21 +1,38 @@
from fastapi import APIRouter,Depends
from dao.statistics_dao import select_all,select_age,select_score
from dao.statistics_dao import select_all,select_age,select_score,select_avg_score,select_salary,select_days
from schema.student_schema import StudentResponse
from schema.statistics_schema import AllStuResponse,AvgDayResponse
from util.database import get_db
StatisticsAPI = APIRouter()
@StatisticsAPI.get("/age",response_model=list[StudentResponse],tags=['统计分析模块'],description='根据年龄查询学生信息')
@StatisticsAPI.get("/age",response_model=list[StudentResponse],tags=['统计分析模块'],summary='根据年龄查询学生信息')
def get_students(min_age:int|None=None,max_age:int|None=None,db=Depends(get_db)):
l = select_age(min_age,max_age,db)
return l
@StatisticsAPI.get("/all",response_model=StudentResponse,tags=['统计分析模块'],description='统计每个班级的学员总数和男女生总数')
@StatisticsAPI.get("/all",response_model=list[AllStuResponse],tags=['统计分析模块'],summary='统计每个班级的学员总数和男女生总数')
def get_students(db=Depends(get_db)):
l = select_all(db)
return [i for i in l]
return l
@StatisticsAPI.get("/score",response_model=StudentResponse,tags=['统计分析模块'],description='根据成绩查询学生信息')
@StatisticsAPI.get("/score",response_model=StudentResponse,tags=['统计分析模块'],summary='根据成绩查询学生信息')
def get_students(min_score:int|None=None,max_score:int|None=None,db=Depends(get_db)):
l = select_score(min_score,max_score,db)
return [i for i in l]
@StatisticsAPI.get("/avg_score",response_model=StudentResponse,tags=['统计分析模块'],summary='统计每次考试每个班级的平均分并排序')
def get_scores(db=Depends(get_db)):
l = select_avg_score(db)
return [i for i in l]
@StatisticsAPI.get("/salary",response_model=list[StudentResponse],tags=['统计分析模块'],summary='统计薪资最高的前五名的学员信息')
def get_scores(db=Depends(get_db)):
l = select_salary(db)
return l
@StatisticsAPI.get("/avg_day",response_model=list[AvgDayResponse],tags=['统计分析模块'],summary='统计每个班级的平均就业时长')
def get_days(db=Depends(get_db)):
l = select_days(db)
return l
+51 -5
View File
@@ -1,7 +1,6 @@
from fastapi import HTTPException,Depends
from fastapi import HTTPException
from sqlalchemy import func
from model.all_model import Student_Model,WlScore
from util.database import get_db
from model.all_model import Student_Model,WlScore,Employment_info
# 实现根据年龄查询学生信息的功能
def select_age(min_age,max_age,db):
@@ -23,7 +22,8 @@ def select_age(min_age,max_age,db):
# 实现统计每个班级的学员总数以及男女生的总数的功能
def select_all(db):
try:
l1 = db.query(Student_Model.class_id,Student_Model.gender,func.count(1)).group_by(Student_Model.class_id,Student_Model.gender).all()
l1 = db.query(Student_Model.class_id,Student_Model.gender,func.count(1).label('cnt'))\
.group_by(Student_Model.class_id,Student_Model.gender).all()
return l1
except Exception:
raise HTTPException(status_code=500,detail="查询异常")
@@ -34,7 +34,8 @@ def select_score(min_score,max_score,db):
if min_score is None and max_score is None:
raise HTTPException(status_code=400, detail="请至少传入一个参数")
db = db.query(Student_Model,WlScore.score,WlScore.score)\
.join(WlScore,WlScore.stu_id == Student_Model.stu_id,WlScore.delete_status==0,WlScore.delete_status == 0)
.join(WlScore,WlScore.stu_id == Student_Model.stu_id)\
.filter(WlScore.delete_status==0,Student_Model.delete_status == 0)
if min_score is not None:
db = db.filter(WlScore.score >= min_score)
if min_score is not None:
@@ -45,3 +46,48 @@ def select_score(min_score,max_score,db):
except Exception:
raise HTTPException(status_code=500,detail="查询异常")
# 实现统计每次考试每个班级的平均分并排序的功能
def select_avg_score(db):
try:
l1 = db.query(func.avg(WlScore.score))\
.join(Student_Model,WlScore.stu_id == Student_Model.stu_id)\
.filter(WlScore.delete_status==0,Student_Model.delete_status == 0)\
.group_by(Student_Model.class_id)\
.order_by(func.avg(WlScore.score)).all()
return l1
except Exception:
raise HTTPException(status_code=500,detail="查询异常")
# 实现统计薪资最高的前五名的学员信息
def select_salary(db):
try:
l1 = db.query(Student_Model,Employment_info.salary)\
.join(Employment_info,Employment_info.stu_id==Student_Model.stu_id)\
.filter(Employment_info.delete_status==0,Student_Model.delete_status == 0)\
.order_by(Employment_info.salary.desc())\
.limit(5).all()
return l1
except Exception:
raise HTTPException(status_code=500,detail="查询异常")
# 实现统计每个班级的平均就业时长
def select_days(db):
try:
l1 = db.query(Student_Model.class_id,
func.coalesce(
func.avg(
func.datediff(
Employment_info.offer_issuance_date, Employment_info.employment_opening_date
)
),0).label("avg_days")
).join(Employment_info,Employment_info.stu_id == Student_Model.stu_id)\
.filter(Student_Model.delete_status == 0,
Employment_info.delete_status == 0,
Employment_info.employment_opening_date.isnot(None),
Employment_info.offer_issuance_date.isnot(None),)\
.group_by( Student_Model.class_id)\
.order_by(func.avg(func.datediff(Employment_info.offer_issuance_date,Student_Model.admission_date)).desc()).all()
return l1
except Exception:
raise HTTPException(status_code=500, detail="查询异常")
+1 -1
View File
@@ -3,7 +3,7 @@ from api.student_api import StudentAPI
from api.statistics_api import StatisticsAPI
from api.teacher_api import teacher_api
from api.grade_api import gradeAPI
app = FastAPI()
app = FastAPI(title='沃林学⽣管理系统')
app.include_router(StudentAPI)
app.include_router(StatisticsAPI)
+11
View File
@@ -0,0 +1,11 @@
from pydantic import BaseModel
class AllStuResponse(BaseModel):
class_id: int
gender: str | None
cnt: int
class AvgDayResponse(BaseModel):
class_id: int
avg_days: str | None