合并最新main并解决学生接口冲突

This commit is contained in:
wolin_liyujie
2026-09-21 21:50:08 +08:00
3 changed files with 81 additions and 22 deletions
+2 -2
View File
@@ -11,7 +11,7 @@ from sqlalchemy.orm import Session
statistics_router=APIRouter()
@statistics_router.get("/scores/age-over-30",response_model=SuccessResponse)
@statistics_router.get("/students/age-over-30",response_model=SuccessResponse)
def api_age_over_thirty(db:Session=Depends(get_db)):
try:
age_over_thirty_list=age_over_thirty(Student,db)
@@ -71,7 +71,7 @@ def employ_time(db:Session=Depends(get_db)):
@statistics_router.get("/employments/class-average-duration",response_model=SuccessResponse)
def avg_employ_time(db:Session=Depends(get_db)):
try:
result = avg_class(Employment,Student,db)
result = avg_emptime(Employment,Student,db)
return SuccessResponse(status_code=200,msg=result)
except NoExist as e:
raise HTTPException(status_code=404,detail=str(e))
+78 -19
View File
@@ -5,76 +5,135 @@ from time import time
#查询所有超过 30 岁的学员信息
def age_over_thirty(Student,db):
student_list=db.query(Student).filter(Student.age>=30).all()
student_list=db.query(Student).filter(Student.age>30,Student.is_deleted==0).all()
if not student_list :
raise NoExist('无目标年龄大于30岁')
return student_list
# 把每个学生对象需要返回的字段,明确放进普通字典。
result = []
for student in student_list:
result.append({
"id": student.id,
"student_no": student.student_no,
"student_name": student.student_name,
"class_id": student.class_id,
"consultant_id": student.consultant_id,
"native_place": student.native_place,
"graduation_school": student.graduation_school,
"major": student.major,
"enrollment_date": student.enrollment_date,
"graduation_date": student.graduation_date,
"education": student.education,
"age": student.age,
"gender": student.gender,
"create_at": student.create_at,
"update_at": student.update_at,
"is_deleted": student.is_deleted,
})
return result
def gender_count(Student,db):
result=db.query(Student.class_id,Student.gender,func.count(Student.id)).group_by(Student.class_id,Student.gender).all()
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 = {}
for i,j,k in result:
if i not in dict1 :
dict1[i]={'男':0,'女':0}
dict[i][j]+=k
dict1[i]={'男':0,'女':0,'总人数':0}
dict1[i][j]+=k
dict1[i]['总人数']+=k
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).group_by(Student.student_no,Student.student_name)\
.join(Score,Score.student_id==Student.id).filter(Student.is_deleted==0,Score.is_deleted==0).group_by(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()
student_list2 = db.query(Student.student_no, Student.student_name,Score.score).join(Score,Score.student_id==Student.id).filter(i.id==Student.id,Score.is_deleted==0).all()
if student_list2 :
list1.extend(student_list2)
for student_no, student_name, score in student_list2:
list1.append({
"student_no": student_no,
"student_name": student_name,
"score": float(score) if score is not None else None,
})
return list1
#查询有两次以上不及格的学生姓名、班级和不及格成绩
def twice_error(Score,Student,db):
student_list=db.query(Score.student_id).filter(Score.score<60).group_by(Score.student_id).having(func.count(Score.id)>1).all()
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).all()
list1.extend(result)
.filter(i.student_id==Score.student_id,Score.score<60,Student.is_deleted==0,Score.is_deleted==0).all()
for student_name, class_id, score in result:
list1.append({
"student_name": student_name,
"class_id": class_id,
"score": float(score) if score is not None else None,
})
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).group_by(Student.class_id,Score.exam_seq).order_by(func.avg(Score.score).desc()).all()
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
result = []
for class_id, exam_seq, average_score in student_list:
result.append({
"class_id": class_id,
"exam_seq": exam_seq,
"average_score": float(average_score) if average_score is not None else None,
})
return result
#统计就业薪资最高的前五名学生姓名、班级、就业时间、就业公司
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).order_by(Employment.salary_max.desc()).limit(5).all()
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
result = []
for student_name, class_id, offer_date, company_name in student_list:
result.append({
"student_name": student_name,
"class_id": class_id,
"offer_date": offer_date,
"company_name": company_name,
})
return result
# - 统计每个学生的就业时长
# - 就业时长 = `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)).all()
return student_list
student_list = db.query(Employment.student_id,func.datediff(Employment.offer_date,Employment.employment_open_date)).filter(Employment.is_deleted==0).all()
result = []
for student_id, duration_days in student_list:
result.append({
"student_id": student_id,
"duration_days": duration_days,
})
return result
# - 统计每个班级的平均就业时长
# - 只统计有就业开放时间的学生
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(Employment.employment_open_date.is_not(None)))\
.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
result = []
for class_id, average_days in student_list:
result.append({
"class_id": class_id,
"average_days": float(average_days) if average_days is not None else None,
})
return result
+1 -1
View File
@@ -35,7 +35,7 @@ class Classes( Base ): # 在python里的名字
, nullable = False # 声明是"非空"
)
# ————创建班级"更新时间"的字段名————
updated_at = Column( DATETIME # 声明是字段数据类型是"日期时间"
update_at = Column( DATETIME # 声明是字段数据类型是"日期时间"
, default=datetime.now #声明是第一次更新的时间就是第一次创建的时间一致
, onupdate=datetime.now # 声明"最后修改的时间"
, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")