From 00984d5c109738cf35eb7e46e47fdc7dae728449 Mon Sep 17 00:00:00 2001 From: wolin_ck666_999 Date: Tue, 22 Sep 2026 14:12:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=E3=80=8Cdao=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/sj_dao.py | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 dao/sj_dao.py diff --git a/dao/sj_dao.py b/dao/sj_dao.py new file mode 100644 index 0000000..5fde35b --- /dev/null +++ b/dao/sj_dao.py @@ -0,0 +1,123 @@ +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)}') + + + -- 2.54.0