from fastapi import APIRouter,HTTPException,Depends from core.database import get_db from models.employment import Employment from models.student import Student from models.score import Score from schemas.statistics import * from dao.statistics import * from schemas.common import SuccessResponse from sqlalchemy.orm import Session statistics_router=APIRouter(tags=['统计接口']) @statistics_router.post("/students/age-over-30",response_model=SuccessResponse) def api_age_over_thirty(input_age:GenderCount,db:Session=Depends(get_db)): try: age_over_thirty_list=age_over_thirty(Student,input_age.input_class,db) return SuccessResponse(status_code=200,msg=age_over_thirty_list) except NoExist as e: raise HTTPException(status_code=404,detail=str(e)) # 这边功能函数返回的是一个字典 @statistics_router.get("/classes/gender-count",response_model=SuccessResponse) def api_gender_count(input_class:int|None= None,db:Session=Depends(get_db)): try: result = gender_count(Student,db,input_class) return SuccessResponse(status_code=200,msg=result) except NoExist as e: raise HTTPException(status_code=404,detail=str(e)) @statistics_router.post("/scores/all-above-80",response_model=SuccessResponse) def all_above_eighty(page:int,score:StudentScore,db:Session=Depends(get_db)): try: result = score_over_eighty(score.score,page,Score,Student,db) return SuccessResponse(status_code=200,msg=result) except NoExist as e: raise HTTPException(status_code=404,detail=str(e)) @statistics_router.get("/scores/fail-more-than-two",response_model=SuccessResponse) def fail_two(num:int,offset_set:int,db:Session=Depends(get_db)): try: result = twice_error(num,offset_set,Score,Student,db) return SuccessResponse(status_code=200,msg=result) except NoExist as e: raise HTTPException(status_code=404,detail=str(e)) @statistics_router.get("/scores/class-average",response_model=SuccessResponse) def class_average(db:Session=Depends(get_db)): try: result = avg_class(Score,Student,db) return SuccessResponse(status_code=200,msg=result) except NoExist as e: raise HTTPException(status_code=404,detail=str(e)) @statistics_router.get("/employments/top5-salary",response_model=SuccessResponse) def top_salary(limit_set:int,db:Session=Depends(get_db)): try: result=max_salary(limit_set,Student,Employment,db) return SuccessResponse(status_code=200,msg=result) except NoExist as e: raise HTTPException(status_code=404,detail=str(e)) @statistics_router.get("/employments/student-duration",response_model=SuccessResponse) def employ_time(limit_set,db:Session=Depends(get_db)): try: result = student_market(limit_set,Employment,db) return SuccessResponse(status_code=200,msg=result) except NoExist as e: raise HTTPException(status_code=404,detail=str(e)) @statistics_router.get("/employments/class-average-duration",response_model=SuccessResponse) def avg_employ_time(db:Session=Depends(get_db)): try: 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))