Files

124 lines
4.7 KiB
Python

from sqlalchemy import func, desc
from sqlalchemy.exc import SQLAlchemyError
from model.Employment import Employment
from fastapi import HTTPException
from model.Students import Student
from model.Score import Scores
# ① 超30岁学员
def fun1(db):
try:
rows = db.query(Student).filter(Student.age>30).all()
return [{'id': i.stu_id, 'name': i.name} for i in rows]
except SQLAlchemyError as e:
db.rollback()
raise HTTPException(status_code=400,detail=f'查询超三十岁学员信息失败:{str(e)}')
# ② 各班人数 + 男女比
def fun2(db):
try:
rows = db.query(
Student.stu_class,
Student.gender,
func.count(1).label('num')
).group_by(Student.stu_class, Student.gender).all()
return [{'班级': r.stu_class, '性别': r.gender, '人数':r.num } for r in rows]
except Exception as e:
db.rollback()
raise HTTPException(status_code=400,detail=f'查询各班人数与男女比例失败:{str(e)}')
# ③ 每次考试都≥80分的学生
def fun3(db):
try:
print('1111111111-----')
rows = db.query(
Scores.stu_id,
Student.name,
func.min(Scores.score).label('min_score')
).join(Student, Student.stu_id == Scores.stu_id) \
.group_by(Scores.stu_id, Student.name) \
.having(func.min(Scores.score) >= 80).all()
t = ['stu_id', 'name', 'min_score']
l = [dict(zip(t, row)) for row in rows]
return l
except Exception as e:
db.rollback()
raise HTTPException(status_code=400,detail=f'查询成绩大于80的学生失败:{str(e)}')
# ④ 两次以上不及格的学生
def fun4(db):
try:
rows = db.query(
Student.name,
Student.stu_class,
).join(Scores, Scores.stu_id == Student.stu_id) \
.filter(Scores.score < 60) \
.group_by(Scores.stu_id,Student.name,Student.stu_class) \
.having(func.count(Scores.id) >= 2).all()
t = ['name', 'stu_class']
l = [dict(zip(t, row)) for row in rows]
return l
except Exception as e:
db.rollback()
raise HTTPException(status_code=400,detail=f'查询两次不及格的学生失败:{str(e)}')
# ⑤ 每次考试各班平均分(降序)
def fun5(db):
try:
print('5555555555')
rows = db.query(
Scores.exam_seq,
Student.stu_class,
func.avg(Scores.score).label('avg_score')
).join(Student, Student.stu_id == Scores.stu_id) \
.group_by(Scores.exam_seq, Student.stu_class) \
.order_by(desc('avg_score')).all()
t = ['exam_seq', 'stu_class','avg_score']
l = [dict(zip(t, row)) for row in rows]
return l
except Exception as e:
db.rollback()
raise HTTPException(status_code=400, detail=f'查询各班平均分失败:{str(e)}')
# ⑥ 薪资前五名
def fun6(db):
try:
rows = db.query(Student.name, Student.stu_class,Employment.work_date,Employment.company,Employment.salary).join(Student, Student.stu_id == Employment.s_id).order_by(Employment.salary).limit(5).all()
t = ['s_name','stu_class','work_date','company','salary']
l = [dict(zip(t, row)) for row in rows]
print(l)
return {'code': 200, 'data': l}
except Exception as e:
db.rollback()
raise HTTPException(status_code=400,detail=f'查询薪资前五名失败失败:{str(e)}')
# ⑦ 每个学生就业时长
def fun7(db):
try:
rows = db.query(Employment.s_id,func.datediff(Employment.offer_date,Employment.open_date).label('offer_s')).group_by(Employment.s_id,Employment.offer_date,Employment.open_date).all()
t = ['s_id', 'offer_s']
l = [dict(zip(t, row)) for row in rows]
return {'code': 200, 'data': l}
except Exception as e:
db.rollback()
raise HTTPException(status_code=400,detail=f'查询每个学生的就业时长失败:{str(e)}')
# ⑧ 各班平均就业时长
def fun8(db):
try:
rows = db.query(
Student.stu_class,
func.avg(func.datediff(Employment.offer_date, Employment.open_date)).label('avg_duration')
).join(Student, Student.stu_id == Employment.s_id).filter(
Employment.open_date.isnot(None)
).group_by(Student.stu_class).all()
t = ['stu_class', 'avg_duration']
l = [dict(zip(t, row)) for row in rows]
return l
except Exception as e:
db.rollback()
raise HTTPException(status_code=400,detail=f'查询各班平均就业时长失败:{str(e)}')