3 Commits
Author SHA1 Message Date
wolin_ck666_999 00984d5c10 上传文件至「dao」 2026-09-22 14:12:31 +08:00
wolin_ck666_999 22583badc2 上传文件至「api」 2026-09-22 14:11:25 +08:00
wolin_ck666_999 dcb39957f5 Merge pull request '上传文件至「api」' (#4) from kfx into main
Reviewed-on: #4
2026-09-22 13:22:30 +08:00
2 changed files with 179 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
from fastapi import APIRouter, Depends,FastAPI
from model.teaModel import Session
from dao.dao1 import fun1,fun2,fun3,fun4,fun5,fun6, fun7 ,fun8
StatsAPI = APIRouter(prefix='/stats', tags=['统计分析'])
app = FastAPI()
app.include_router(StatsAPI)
def get_db():
db=Session()
try:
yield db
finally:
db.close()
@StatsAPI.get('/age-over-30', summary='超过30岁学员')
def stat_age(db= Depends(get_db)):
rows = fun1(db)
return {'code': 200, 'data':rows }
@StatsAPI.get('/class-gender', summary='各班人数及男女比')
def stat_class_gender(db= Depends(get_db)):
rows = fun2(db)
return {'code': 200, 'data': rows}
@StatsAPI.get('/all-pass-80', summary='每次考试都≥80分')
def stat_pass80(db= Depends(get_db)):
rows = fun3(db)
return {'code': 200, 'data': rows}
@StatsAPI.get('/two-fail', summary='两次以上不及格')
def stat_fail(db= Depends(get_db)):
rows = fun4(db)
return {'code': 200, 'data': rows}
@StatsAPI.get('/avg-score', summary='各班每次考试平均分')
def stat_avg(db= Depends(get_db)):
rows = fun5(db)
return {'code': 200, 'data': rows}
@StatsAPI.get('/top5-salary', summary='薪资前五名')
def stat_top5(db= Depends(get_db)):
rows = fun6(db)
return rows
@StatsAPI.get('/duration', summary='每个学生就业时长')
def stat_duration(db= Depends(get_db)):
rows = fun7(db)
return {'code': 200, 'data': rows}
@StatsAPI.get('/avg-duration', summary='各班平均就业时长')
def stat_avg_duration(db= Depends(get_db)):
rows = fun8(db)
return {'code': 200, 'data': rows}
if __name__ == '__main__':
import uvicorn
uvicorn.run('cxjk:app',host='127.0.0.1',port=9999)
+123
View File
@@ -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)}')