12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- # -*- coding: utf-8 -*-
- from typing import Union
- from fastapi import Query, Depends
- from sqlalchemy import text
- from sqlalchemy.ext.asyncio import AsyncSession
- from crud.mark import crud_mark
- from models.user import Teacher
- from utils.depends import get_async_db, get_current_user
- async def analysis_line_chart(
- cid: int = Query(..., description="班级ID"),
- year: Union[int, str] = Query(None,
- description="年份,单个或范围,范围逗号分隔,形式:开始年,结束年"),
- db: AsyncSession = Depends(get_async_db),
- current_user: Teacher = Depends(get_current_user)):
- if year and year.isdigit():
- filters = {"class_id": cid, "year": year}
- else:
- filters = [
- text(f"class_id = {cid}"
- if year is None else f"class_id = {cid} AND year IN ({year})")
- ]
- # 查询
- _, db_tasks = await crud_mark.find_all(db,
- filters=filters,
- return_fields=[
- "name", "year", "score",
- "high_score", "avg_score",
- "low_score"
- ])
- # 组装返回结构
- x = []
- scores = []
- high_scores = []
- avg_scores = []
- low_scores = []
- for item in db_tasks:
- x.append(f"{item.name}({item.year})")
- scores.append(item.score)
- high_scores.append(item.high_score)
- avg_scores.append(item.avg_score)
- low_scores.append(item.low_score)
- data = {
- "score": {
- "x": x,
- "y": scores
- },
- "high_score": {
- "x": x,
- "y": high_scores
- },
- "avg_score": {
- "x": x,
- "y": avg_scores
- },
- "low_scores": {
- "x": x,
- "y": low_scores
- }
- }
- return {"data": data}
|