Merge remote-tracking branch 'origin/conding' into conding

This commit is contained in:
2026-09-21 11:46:14 +08:00
6 changed files with 77 additions and 25 deletions
View File
+37 -4
View File
@@ -1,11 +1,44 @@
from fastapi import APIRouter
from dao.student_dao import select_age
from fastapi import APIRouter,Depends,HTTPException
from schema.student_schema import StudentResponse
from dao.student_dao import delete_student_dao,add_student_dao,update_student_dao,get_student_dao,select_age
from schema.student_schema import StudentRequest,StudentResponse
from model.student_model import Student_Model
from util.database import get_db
studentapi = APIRouter()
@studentapi.get("/age",response_model=StudentResponse,tags=['统计分析模块'],description='根据年龄查询学生信息')
StudentAPI = APIRouter()
@StudentAPI.get("/age",response_model=StudentResponse,tags=['统计分析模块'],description='根据年龄查询学生信息')
def get_students(min_age:int|None=None,max_age:int|None=None):
l = select_age(min_age,max_age)
return [i for i in l]
@StudentAPI.get('',description='查询学生信息')
def get_students(stu_id:int|None=None
,stu_name:str|None=None
,class_id:int|None=None
,db=Depends(get_db)
,page:int=1
,page_size:int=10):
r=get_student_dao(stu_id,stu_name,class_id,db,page,page_size)
if r:
return r
raise HTTPException(status_code=404,detail='学生不存在')
@StudentAPI.put('/{stu_id}')
def update_orders(s:StudentRequest,stu_id:int,db=Depends(get_db)):
d = s.model_dump(exclude_unset=True)
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
if not r:
raise HTTPException(status_code=500, detail='没有更新!')
return {'code':200,'totals':r,'detail':'更新成功'}
@StudentAPI.delete('/{stu_id}')
def del_student(stu_id:int,db=Depends(get_db)):
rows=delete_student_dao( stu_id=stu_id,db=db )
if not rows:
raise HTTPException(status_code=500,detail='没有删除')
return {'code':200,'totals':rows,'detail':'删除成功'}
View File
+30 -12
View File
@@ -9,49 +9,65 @@ def select_age(min_age,max_age,session=Depends(get_db)):
raise HTTPException(status_code=400, detail="请至少传入一个参数")
session = session.query(Student_Model)
if min_age is not None:
session = session.filter(Student_Model.age >= min_age)
session = session.filter(Student_Model.age >= min_age,Student_Model.delete_status == 0)
if max_age is not None:
session = session.filter(Student_Model.age <= max_age)
session = session.filter(Student_Model.age <= max_age,Student_Model.delete_status == 0)
return session.all()
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=500,detail="查询异常")
def delete_student_dao(stu_id,db):
def add_student_dao(o,db=Depends(get_db)):
try:
rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id).delete()
r = db.query(Student_Model).filter(Student_Model.stu_id == o['stu_id'],Student_Model.delete_status == 0).all()
if not r:
o1 = Student_Model( **o)
db.add(o1)
else:
raise ValueError
except:
db.rollback()
return False
else:
db.commit()
return True
def delete_student_dao(stu_id,db=Depends(get_db)):
try:
rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0).delete()
Student_Model.delete_status = 1
db.commit()
except:
db.rollback()
rows = 0
else:
db.commit()
finally:
return rows
def update_student_dao(stu_id,update_data,db):
def update_student_dao(stu_id,update_data,db=Depends(get_db)):
try:
rows = db.query( Student_Model ).filter( Student_Model.stu_id == stu_id ).update( update_data )
rows = db.query( Student_Model ).filter( Student_Model.stu_id == stu_id,Student_Model.delete_status == 0).update( update_data )
except:
db.rollback()
return False
else:
db.commit()
return rows
def get_student_dao(stu_id:Optional[int]
,stu_name:Optional[str]
,class_id:Optional[int]
,db: Session
,page: int
,page_size: int
,db=Depends(get_db)
) -> tuple[List[Dict[str, Any]], int]:
q = db.query(Student_Model)
if stu_id:
q = q.filter(Student_Model.stu_id == stu_id)
q = q.filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)
if stu_name:
q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%"))
q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%"),Student_Model.delete_status == 0)
if class_id:
q= q.filter(Student_Model.class_id == class_id)
q= q.filter(Student_Model.class_id == class_id,Student_Model.delete_status == 0)
total = q.count()
r = q.offset((page - 1) * page_size).limit(page_size).all()
data = []
@@ -63,3 +79,5 @@ def get_student_dao(stu_id:Optional[int]
,'update_date': i.update_date
})
return data, total
+5 -4
View File
@@ -4,19 +4,20 @@ from pydantic.v1 import BaseModel
from datetime import datetime
class Teacher_RequestModel(BaseModel):
teac_id: int
teac_name:str
teac_gender:str
teac_age:int
teac_position:str
status=int
create_date:datetime
update_date:datetime
delete_status:int
class Teacher_ResponseModel(BaseModel):
code:int=200
detail:str='OK!'
teac_name:str
totals:int=0