141 lines
5.8 KiB
Python
141 lines
5.8 KiB
Python
from fastapi import HTTPException
|
|
from sqlalchemy import func
|
|
from model.all_model import Student_Model,WlScore,Employment_info
|
|
|
|
# 实现根据年龄查询学生信息的功能
|
|
def select_age(min_age,max_age,db):
|
|
try:
|
|
if min_age is None and max_age is None:
|
|
raise HTTPException(status_code=400, detail="请至少传入一个参数")
|
|
db = db.query(Student_Model)
|
|
if min_age is not None:
|
|
db = db.filter(Student_Model.age >= min_age,Student_Model.delete_status == 0)
|
|
if max_age is not None:
|
|
db = db.filter(Student_Model.age <= max_age,Student_Model.delete_status == 0)
|
|
return db.all()
|
|
except HTTPException:
|
|
raise
|
|
except Exception:
|
|
raise HTTPException(status_code=500,detail="查询异常")
|
|
|
|
# 实现统计每个班级的学员总数以及男女生的总数的功能
|
|
def select_all(db):
|
|
try:
|
|
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()
|
|
all_cnt = sum(i.cnt for i in l1)
|
|
|
|
l2 = []
|
|
for i in l1:
|
|
l2.append({
|
|
"all_cnt": all_cnt,
|
|
"class_id": i.class_id,
|
|
"gender": i.gender,
|
|
"cnt": i.cnt
|
|
})
|
|
return l2
|
|
except Exception:
|
|
raise HTTPException(status_code=500,detail="查询异常")
|
|
|
|
# 实现根据成绩查询学员信息的功能
|
|
def select_score(min_score,max_score,db):
|
|
try:
|
|
if min_score is None and max_score is None:
|
|
raise HTTPException(status_code=400, detail="请至少传入一个参数")
|
|
db = db.query(WlScore.score
|
|
,Student_Model.stu_name
|
|
,Student_Model.age
|
|
,Student_Model.gender
|
|
,Student_Model.native_place
|
|
,Student_Model.school
|
|
,Student_Model.major
|
|
,Student_Model.degree
|
|
,Student_Model.admission_date
|
|
,Student_Model.graduation_date
|
|
,Student_Model.progress
|
|
)\
|
|
.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 max_score is not None:
|
|
db = db.filter(WlScore.score <= max_score)
|
|
return db.all()
|
|
except HTTPException:
|
|
raise
|
|
except Exception:
|
|
raise HTTPException(status_code=500,detail="查询异常")
|
|
|
|
# 实现统计每次考试每个班级的平均分并排序的功能
|
|
def select_avg_score(db):
|
|
try:
|
|
l1 = db.query(Student_Model.class_id,func.avg(WlScore.score).label('avg_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(Employment_info.salary
|
|
,Student_Model.stu_name
|
|
,Student_Model.age
|
|
,Student_Model.gender
|
|
,Student_Model.native_place
|
|
,Student_Model.school
|
|
,Student_Model.major
|
|
,Student_Model.degree
|
|
,Student_Model.admission_date
|
|
,Student_Model.graduation_date
|
|
,Student_Model.progress)\
|
|
.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_avg_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="查询异常")
|
|
|
|
#统计每个学⽣的就业时⻓
|
|
def select_days(db):
|
|
try:
|
|
l2 =db.query(
|
|
Employment_info.stu_id
|
|
,Employment_info.employment_opening_date
|
|
,Employment_info.offer_issuance_date
|
|
,func.datediff(
|
|
Employment_info.offer_issuance_date
|
|
,Employment_info.employment_opening_date
|
|
)
|
|
.label("employment_duration_day")
|
|
).filter(
|
|
Employment_info.delete_status == 0
|
|
).all()
|
|
return l2
|
|
except Exception:
|
|
raise HTTPException(status_code=500, detail="查询异常") |