Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e9d7e2606 | ||
|
|
0fd0c4cd8b | ||
|
|
8b26d3b239 | ||
|
|
2ce8fdacf6 | ||
|
|
7d8d14fec1 | ||
|
|
6c0df6526e | ||
|
|
dbc3cd851f |
@@ -1,6 +1,6 @@
|
||||
# statcalc_api所有统计分析模块的api路由、接口
|
||||
|
||||
from fastapi import APIRouter , Depends , HTTPException
|
||||
from fastapi import APIRouter , Depends , HTTPException , Query
|
||||
from StatCalc.dao.statcalc_dao import *
|
||||
from StatCalc.schema.statcalc_request import *
|
||||
from databases import *
|
||||
@@ -9,13 +9,13 @@ statcalc_api = APIRouter()
|
||||
|
||||
@statcalc_api.get("/statcalc/age"
|
||||
,summary='人员信息查询'
|
||||
,description=f'查询所有超过30岁的学员的信息。'
|
||||
,description=f'查询所有大于等于输入年龄的学员的信息。'
|
||||
)
|
||||
def get_students(db=Depends(get_db)):
|
||||
result = get_age_30( db=db )
|
||||
if result:
|
||||
return result
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
def get_students( age:int = 0 ,db=Depends(get_db)):
|
||||
result = get_age_30( age=age , db=db )
|
||||
if result is None or len(result) == 0:
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
return result
|
||||
|
||||
@statcalc_api.get("/statcalc/students"
|
||||
,summary='人数统计'
|
||||
@@ -33,25 +33,29 @@ def get_students(headcount:get_student_dao
|
||||
,summary='优秀成绩学生信息查询'
|
||||
,description=f'查询所有科目考试成绩都在80分以上的学生。'
|
||||
)
|
||||
def get_excellent(db=Depends(get_db)):
|
||||
result = get_excellent_students( db=db )
|
||||
def get_excellent(sco:int = Query(80,ge=0, le=100,description="优秀成绩设定")
|
||||
, db=Depends(get_db)
|
||||
):
|
||||
result = get_excellent_students(sco=sco, db=db )
|
||||
if result:
|
||||
return result
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
|
||||
@statcalc_api.get("/statcalc/fail"
|
||||
,summary='不及格成绩查询'
|
||||
,description=f'查询有两次以上不及格的学生的姓名,班级和不及格成绩。'
|
||||
,description=f'查询有两次以上低于输入分数的学生的姓名,班级和不及格成绩。'
|
||||
)
|
||||
def get_students( db=Depends(get_db) ):
|
||||
result = get_score_80( db=db )
|
||||
def get_students(sco:int = Query(60,ge=0, le=100,description="不及格成绩设定")
|
||||
, db=Depends(get_db) ):
|
||||
result = get_score_60( sco=sco ,db=db )
|
||||
if result:
|
||||
return result
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
|
||||
@statcalc_api.get('/statcalc/average', summary='统计每次考试每个班级的平均分')
|
||||
def get_class_avg(db=Depends(get_db)):
|
||||
result = get_class_avg_dao(db)
|
||||
def get_class_avg( class_id:int = Query(1,ge=1, le=3,description="查询班级")
|
||||
, db=Depends(get_db)):
|
||||
result = get_class_avg_dao( class_id , db)
|
||||
if not result:
|
||||
return []
|
||||
return result
|
||||
@@ -62,22 +66,22 @@ from students.model.students_model import Students
|
||||
from class_management.model.class_management_model import ClassInfo
|
||||
|
||||
@statcalc_api.get('/statcalc/top',summary='就业薪资Top5')
|
||||
def salarytop5(db=Depends(get_db)):
|
||||
def salarytop5(num:int,db=Depends(get_db)):
|
||||
q = db.query(Employments, Company.employment_company, Students.name.label('sname'), ClassInfo.name.label('class_num')) \
|
||||
.join(Company, Employments.company_id == Company.id) \
|
||||
.join(Students, Employments.stuid == Students.id) \
|
||||
.join(ClassInfo, Employments.class_id == ClassInfo.id) \
|
||||
.filter(Employments.is_deleted == 0) \
|
||||
.order_by(Employments.employment_salary.desc()) \
|
||||
.limit(5).all()
|
||||
.limit(num).all()
|
||||
# q是4元组 (Employments对象, company_name, sname, class_num)
|
||||
return [{'学生姓名': row[2], '学生班级编号': row[3], '就业时间': str(row[0].offer_recived_time) if row[0].offer_recived_time else None, '公司': row[1]} for row in q]
|
||||
|
||||
@statcalc_api.get('/statcalc/job',summary='学生就业时长')
|
||||
def worktime(db=Depends(get_db)):
|
||||
def worktime(stuid:int,db=Depends(get_db)):
|
||||
q = db.query(Employments, Students.name.label('sname')) \
|
||||
.join(Students, Employments.stuid == Students.id) \
|
||||
.filter(Employments.is_deleted == 0) \
|
||||
.filter(Employments.stuid == stuid,Employments.is_deleted == 0) \
|
||||
.all()
|
||||
|
||||
return [
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
from pygments.lexers import sql
|
||||
from sentry_sdk.integrations import sqlalchemy
|
||||
|
||||
from StatCalc.schema.statcalc_request import *
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import *
|
||||
|
||||
|
||||
from students.model.students_model import Students
|
||||
|
||||
def get_age_30(db):
|
||||
q = db.query(Students).filter(Students.age>=30).all()
|
||||
def get_age_30( age , db):
|
||||
q = db.query(Students).filter(Students.age>=age).all()
|
||||
return q
|
||||
|
||||
|
||||
@@ -38,7 +35,7 @@ def get_class_count(g , db):
|
||||
from scores.model.score_model import Score
|
||||
|
||||
|
||||
def get_excellent_students(db):
|
||||
def get_excellent_students(sco,db):
|
||||
"""查询所有科目都在80分以上的优秀学生"""
|
||||
# 先查总科目数
|
||||
total_subjects = db.execute(text("SELECT COUNT(*) FROM subject")).scalar()
|
||||
@@ -46,7 +43,7 @@ def get_excellent_students(db):
|
||||
# 子查询:找出成绩>=80分的科目数等于总科目数的学生
|
||||
sq = db.query(Students.id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Score.score >= 80) \
|
||||
.filter(Score.score >= sco) \
|
||||
.group_by(Students.id) \
|
||||
.having(func.count(Score.id) == total_subjects) \
|
||||
.subquery()
|
||||
@@ -55,19 +52,19 @@ def get_excellent_students(db):
|
||||
q = db.query(Students.id, Students.name, ClassInfo.name.label('class_name'), Score.score) \
|
||||
.join(ClassInfo, ClassInfo.id == Students.class_id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score >= 80) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score >= sco) \
|
||||
.all()
|
||||
return [row._asdict() for row in q]
|
||||
|
||||
|
||||
from scores.model.score_model import Score
|
||||
|
||||
def get_score_80(db):
|
||||
def get_score_60(sco ,db):
|
||||
"""查询有两次以上不及格(<60分)的学生"""
|
||||
# 子查询:找出不及格超过2门的学生ID
|
||||
sq = db.query(Students.id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Score.score < 60) \
|
||||
.filter(Score.score < sco) \
|
||||
.group_by(Students.id) \
|
||||
.having(func.count(Score.id) > 2) \
|
||||
.subquery()
|
||||
@@ -76,18 +73,19 @@ def get_score_80(db):
|
||||
q = db.query(Students.name, ClassInfo.name.label('class_name'), Score.score) \
|
||||
.join(ClassInfo, ClassInfo.id == Students.class_id) \
|
||||
.join(Score, Score.sid == Students.id) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score < 60) \
|
||||
.filter(Students.id.in_(sq.select()), Score.score < sco) \
|
||||
.all()
|
||||
return [row._asdict() for row in q]
|
||||
|
||||
def get_class_avg_dao(db):
|
||||
def get_class_avg_dao(class_id , db):
|
||||
results = (db.query(Score.num,
|
||||
Score.cid,
|
||||
ClassInfo.name.label("class_name"),
|
||||
func.avg(Score.score).label("avg_score"))
|
||||
.join(ClassInfo, Score.cid == ClassInfo.id)
|
||||
.filter(Score.is_deleted == 0)
|
||||
.group_by(Score.num, Score.cid, ClassInfo.name)
|
||||
.group_by(Score.num, Score.cid, ClassInfo.name,ClassInfo.id)
|
||||
.having(ClassInfo.id == class_id)
|
||||
.order_by(func.avg(Score.score).desc())
|
||||
.all())
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
from fastapi import FastAPI,APIRouter
|
||||
from StatCalc.api.statcalc_api import statcalc_api
|
||||
from databases import *
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
StatCalc_API = APIRouter()
|
||||
StatCalc_API.include_router(statcalc_api)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
# students_schema:请求、响应的模型框架
|
||||
|
||||
from pydantic import BaseModel , Field , field_validator
|
||||
from datetime import datetime
|
||||
import enum
|
||||
|
||||
class get_student_dao(str , enum.Enum):
|
||||
|
||||
@@ -15,6 +15,7 @@ def add_tea(tea: NewTeaRequest=Depends(),db=Depends(get_db)):
|
||||
if not r:
|
||||
raise HTTPException(status_code=500,detail='添加失败!')
|
||||
return TeaResponse(totals=1,data=tea.model_dump())
|
||||
|
||||
@tea_api.get('/tea',response_model=TeaResponse,summary='查询老师',description='任意选择输入的信息数量,不输入则查询全部老师')
|
||||
def get_tea(t:TeaRequest=Depends(),db=Depends(get_db)): #开启会话窗口
|
||||
r = get_tea_dao(t.model_dump(),db=db)
|
||||
|
||||
@@ -14,7 +14,9 @@ def add_tea_dao(t,db):
|
||||
if existing:
|
||||
for key,value in t.items():
|
||||
if hasattr(existing,key) and value is not None and value != '':
|
||||
#hasattr(existing,key)统计有没有key,返回值是True或False
|
||||
setattr(existing,key,value)
|
||||
#将value的值赋值到existing的key中,配合循环,让所有输入的信息都覆盖原信息
|
||||
existing.is_delete = False
|
||||
db.commit()
|
||||
return '恢复修改成功'
|
||||
|
||||
@@ -31,7 +31,7 @@ def get_students( stu:StudentsQuery = Depends()
|
||||
f'时间格式:YYYY-MM-DD\n'
|
||||
f'班级ID和顾问ID必须已经存在才可以绑定'
|
||||
)
|
||||
def add_student( stu:StudentsResponse = Body(...)
|
||||
def add_student( stu:StudentsResponse =Depends()
|
||||
, db=Depends(get_db)
|
||||
):
|
||||
s_dict = stu.model_dump(exclude_unset=True)
|
||||
@@ -43,7 +43,7 @@ def add_student( stu:StudentsResponse = Body(...)
|
||||
,description=f'请输入需要更新信息的学生ID,学生手机号不可重复,填写即更新,为空即不更新。\n '
|
||||
f'时间格式:YYYY-MM-DD'
|
||||
,response_model=StudentsUpdate )
|
||||
def get_students( stu:StudentsUpdate = Body(...)
|
||||
def update_students( stu:StudentsUpdate =Depends()
|
||||
, db=Depends(get_db)
|
||||
):
|
||||
s_dict = stu.model_dump(exclude_unset=True)
|
||||
@@ -54,10 +54,10 @@ def get_students( stu:StudentsUpdate = Body(...)
|
||||
|
||||
@s_api.delete("/students"
|
||||
,summary='删除学生资料'
|
||||
,description=f'输入需要删除的学生信息,删除条件任选,但不可全为空,工资筛选能删除大于等于输入工资的所有人。\n '
|
||||
,description=f'输入需要删除的学生信息,删除条件任选,但不可全为空。\n '
|
||||
f'时间格式:YYYY-MM-DD'
|
||||
,response_model=list[StudentsDelete] ) # 逻辑删除/软删除
|
||||
def delete_students( stu:StudentsDelete = Body(...)
|
||||
def delete_students( stu:StudentsDelete =Depends()
|
||||
, db=Depends(get_db)
|
||||
):
|
||||
s_dict = stu.model_dump(exclude_unset=True)
|
||||
|
||||
@@ -8,6 +8,12 @@ class SexEnum(str , enum.Enum):
|
||||
m = '男'
|
||||
f = '女'
|
||||
|
||||
class EducationEnum(str , enum.Enum):
|
||||
z = '专科'
|
||||
ben = '本科'
|
||||
s = '硕士'
|
||||
bo = '博士'
|
||||
|
||||
# 查询
|
||||
class StudentsQuery(BaseModel):
|
||||
id:int | None = None
|
||||
@@ -20,18 +26,14 @@ class StudentsQuery(BaseModel):
|
||||
specialty:str | None = None
|
||||
enrollment_time:datetime | None = None
|
||||
graduate_time:datetime | None = None
|
||||
education:str | None = None
|
||||
education:EducationEnum | None = None
|
||||
# 外键字段
|
||||
class_id:int | None = None
|
||||
advisor_id:int | None = None
|
||||
# 额外字段
|
||||
phone:str | None = None
|
||||
|
||||
class EducationEnum(str , enum.Enum):
|
||||
z = '专科'
|
||||
ben = '本科'
|
||||
s = '硕士'
|
||||
bo = '博士'
|
||||
|
||||
|
||||
# 新建
|
||||
class StudentsResponse(BaseModel):
|
||||
@@ -47,7 +49,7 @@ class StudentsResponse(BaseModel):
|
||||
education:EducationEnum
|
||||
# 外键字段
|
||||
class_id:int = Field(..., gt=0, description='班级ID')
|
||||
advisor_id:int | None = None
|
||||
advisor_id:int = Field(..., gt=0, description='班级ID')
|
||||
# 额外字段
|
||||
phone:str = Field(...,min_length=11,max_length=11,description='学生手机号')
|
||||
|
||||
@@ -63,7 +65,7 @@ class StudentsUpdate(BaseModel):
|
||||
specialty:str | None = None
|
||||
enrollment_time:datetime | None = None
|
||||
graduate_time:datetime | None = None
|
||||
education:str | None = None
|
||||
education:EducationEnum | None = None
|
||||
# 外键字段
|
||||
class_id:int | None = None
|
||||
advisor_id:int | None = None
|
||||
@@ -82,7 +84,7 @@ class StudentsDelete(BaseModel):
|
||||
specialty:str | None = None
|
||||
enrollment_time:datetime | None = None
|
||||
graduate_time:datetime | None = None
|
||||
education:str | None = None
|
||||
education:EducationEnum | None = None
|
||||
# 外键字段
|
||||
class_id:int | None = None
|
||||
advisor_id:int | None = None
|
||||
|
||||
Reference in New Issue
Block a user