Files
DoubaoTeam/dao/statistics.py
T
2026-09-21 20:51:07 +08:00

84 lines
4.0 KiB
Python

from schemas.statistics import NoExist
from sqlalchemy import func
from time import time
#`GET /statistics/students/age-over-30`
#查询所有超过 30 岁的学员信息
def age_over_thirty(Student,db):
student_list=db.query(Student).filter(Student.age>30,Student.is_delted==0).all()
if not student_list :
raise NoExist('无目标年龄大于30岁')
return student_list
def gender_count(Student,db):
result=db.query(Student.class_id,Student.gender,func.count(Student.id)).filter(Student.is_deleted==0).group_by(Student.class_id,Student.gender).all()
if not result :
raise NoExist('班里招点人吧,要倒闭了')
dict1 = {}
sum_stu = 0
for i,j,k in result:
if i not in dict1 :
dict1[i]={'男':0,'女':0}
dict1[i][j]+=k
sum_stu+=k
dict1['总人数'] = sum_stu
return dict1
#查询每次考试成绩都在 80 分以上的学生编号、姓名和成绩
def score_over_eighty(Score,Student,db):
student_list=db.query(Student.id, Student.student_name)\
.join(Score,Score.student_id==Student.id).filter(Student.is_deleted==0,Score.is_deleted==0).group_by(Student.student_id,Student.student_name)\
.having(func.min(Score.score)>80).all()
if not student_list :
raise NoExist('全是学渣')
list1 = []
for i in student_list:
student_list2 = db.query(Student.student_no, Student.student_name,Score.score).join(Score,Score.student_id==Student.id).filter(i.id==Student.id).all()
if student_list2 :
list1.extend(student_list2)
return list1
#查询有两次以上不及格的学生姓名、班级和不及格成绩
def twice_error(Score,Student,db):
student_list=db.query(Score.student_id).filter(Score.score<60,Score.is_deleted==0).group_by(Score.student_id).having(func.count(Score.id)>2).all()
if not student_list :
raise NoExist('全是学霸')
list1=[]
for i in student_list:
result=db.query(Student.student_name,Student.class_id,Score.score).join(Score, Student.id==Score.student_id)\
.filter(i.student_id==Score.student_id,Score.score<60,Student.is_deleted==0,Score.is_deleted==0).all()
list1.extend(result)
return list1
#统计每次考试每个班级的平均分,并按照平均分从高到低排序
def avg_class(Score,Student,db):
student_list = db.query(Student.class_id,Score.exam_seq,func.avg(Score.score)).join(Score, Student.id == Score.student_id).filter(Score.is_deleted==0,Student.is_deleted==0).group_by(Student.class_id,Score.exam_seq).order_by(func.avg(Score.score).desc()).all()
if not student_list :
raise NoExist('招个人吧')
return student_list
#统计就业薪资最高的前五名学生姓名、班级、就业时间、就业公司
def max_salary(Student,Employment,db):
student_list = db.query(Student.student_name,Student.class_id,Employment.offer_date,Employment.company_name).join(Employment, Student.id == Employment.student_id).filter(Student.is_deleted==0,Employment.is_deleted==0).order_by(Employment.salary.desc()).limit(5).all()
if not student_list :
raise NoExist('没就业的啊兄弟?')
return student_list
# - 统计每个学生的就业时长
# - 就业时长 = `offer_date - employment_open_date`
def student_market(Employment,db):
student_list = db.query(Employment.student_id,func.datediff(Employment.offer_date,Employment.employment_open_date)).filter(Employment.is_deleted==0).all()
return student_list
# - 统计每个班级的平均就业时长
# - 只统计有就业开放时间的学生
def avg_emptime(Employment,Student,db):
student_list = (db.query(Student.class_id,\
func.avg(func.datediff(Employment.offer_date,Employment.employment_open_date)))\
.join(Employment, Student.id==Employment.student_id).filter(Student.is_deleted==0,Employment.is_deleted==0,Employment.employment_open_date.is_not(None)))\
.group_by(Student.class_id).all()
if not student_list :
raise NoExist('一个就业都没有吗')
return student_list