from fastapi import APIRouter, Query
from Model.EducationDataModel import EducationDataModel


# 创建APIRouter实例
router = APIRouter(prefix="/EducationData", tags=["教育统计数据"])


@router.get("/byYear")
async def get_education_data_by_year(
    year: int = Query(default=2023, ge=2015, le=2028,
                      description="年份: 2015-2028范围内的年份")
):
    """获取指定年份所有学段的教育数据"""
    try:
        # 调用EducationDataModel的方法获取数据
        data = EducationDataModel.get_education_data_by_year(year)
        
        # 返回包含状态和数据的响应
        return {
            "code": 200,
            "message": "success",
            "data": data
        }
    except Exception as e:
        # 异常处理
        return {
            "code": 500,
            "message": f"获取数据失败: {str(e)}",
            "data": []
        }



