Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# api/stats
|
||||
# 本文件定义统计分析相关的所有 API 路由(Controller 层)
|
||||
from itertools import count
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from database import get_db
|
||||
from dao.users_dao import UserDAO
|
||||
from scheme.statistics_scheme import StatsResponse
|
||||
from scheme.users import UserCreate, UserUpdate, UserResponse
|
||||
|
||||
router = APIRouter()
|
||||
# **多维度班级统计**:统计每个班级的总人数,以及按性别(男、女)细分的人数分布。
|
||||
@router.get("/class/stats/gender", response_model=List[StatsResponse])
|
||||
def get_class_stats(db: Session = Depends(get_db)):
|
||||
sbg = db.query(StuInfo.cls_id, count(StuInfo.id)).group_by(StuInfo.cls_id, StuInfo.gender).all()
|
||||
+1
-1
@@ -8,7 +8,7 @@ from sqlalchemy.orm import sessionmaker
|
||||
# 1. 配置 MySQL 数据库连接 URL
|
||||
# 格式:mysql+pymysql://用户名:密码@主机:端口/数据库名?编码
|
||||
# 请将下面的 'root', '123456', 'localhost', '3306', 'test_db' 替换为你自己的实际信息
|
||||
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:123456@localhost:3306/test_db"
|
||||
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:123456@localhost:3306/max_code_sms"
|
||||
|
||||
# 2. 创建数据库引擎
|
||||
# - pool_pre_ping=True 表示每次从连接池取出连接前先 ping 一下,防止使用已断开的连接
|
||||
|
||||
@@ -5,7 +5,7 @@ from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from database import engine, Base
|
||||
from api1 import users # 导入 users 子路由
|
||||
from api1 import users, statistics_api # 导入 users 子路由
|
||||
|
||||
# 1. 创建数据库表(如果表不存在)
|
||||
# Base.metadata.create_all 会扫描所有继承 Base 的模型,生成对应的 CREATE TABLE 语句
|
||||
@@ -32,6 +32,8 @@ app.add_middleware(
|
||||
# prefix 为路由前缀,所有用户接口都以 /api/users 开头
|
||||
app.include_router(users.router, prefix="/api/users", tags=["用户管理"])
|
||||
|
||||
app.include_router(statistics_api.router, prefix="/api/stats", tags=["统计分析"])
|
||||
|
||||
# 5. 根路径
|
||||
@app.get("/")
|
||||
async def root():
|
||||
@@ -41,7 +43,7 @@ async def root():
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(
|
||||
"main6:app", # 指定应用位置(模块名:应用变量名)
|
||||
"main:app", # 指定应用位置(模块名:应用变量名)
|
||||
host="localhost", # 监听所有网络接口
|
||||
port=8001, # 端口
|
||||
reload=True # 开发模式,代码变动自动重启
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#教师建表语句
|
||||
from sqlalchemy import Column, String, Integer
|
||||
|
||||
from database import Base
|
||||
|
||||
|
||||
class Teacher(Base):
|
||||
__tablename__ = "ted_info" # 表名
|
||||
|
||||
# 字段定义
|
||||
id = Column(Integer, primary_key=True, index=True) # 主键,索引
|
||||
name = Column(String(20),nullable=False) # 用户名,非空
|
||||
phone = Column(String(20),unique=True,nullable=False) # 电话,唯一,非空
|
||||
type = Column(String(20), nullable=False) # 教师职位,非空
|
||||
is_deleted = Column(Integer, default=0) # 逻辑删除(软删除),0是未删除,1是删除,默认为0
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# scheme/statistics_scheme.py
|
||||
from pydantic import BaseModel, Field, EmailStr
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
# ---------- 请求模型 ----------
|
||||
# class UserCreate(BaseModel):
|
||||
# username: str = Field(..., min_length=3, max_length=50)
|
||||
# email: EmailStr
|
||||
# full_name: Optional[str] = Field(None, max_length=100)
|
||||
#
|
||||
# class UserUpdate(BaseModel):
|
||||
# username: Optional[str] = Field(None, min_length=3, max_length=50)
|
||||
# email: Optional[EmailStr] = None
|
||||
# full_name: Optional[str] = Field(None, max_length=100)
|
||||
|
||||
# ---------- 响应模型 ----------
|
||||
# 多维度班级统计
|
||||
class StatsResponse(BaseModel):
|
||||
total_count: int
|
||||
man_count: int
|
||||
female_count: int
|
||||
Reference in New Issue
Block a user