Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
058e053554 | ||
|
|
5c6cb0c7c4 | ||
|
|
cebdb127e6 | ||
|
|
76cbc9f75e | ||
|
|
b923a28afb | ||
|
|
01c289c102 | ||
|
|
05def16ec4 | ||
|
|
6a08bdfdf2 | ||
|
|
44c8d68ada | ||
|
|
8d5c1749c4 | ||
|
|
f7f3f13479 | ||
|
|
23df163ca6 | ||
|
|
0cd4a1fa45 | ||
|
|
c0e08bd905 |
+8
-8
@@ -1,11 +1,11 @@
|
||||
from fastapi import APIRouter,Depends,HTTPException,Query
|
||||
from dao.class_dao import add_class_dao, update_class_dao, delete_class_dao, hard_delete_class_dao,class_no_exist,get_class_list_dao,get_class_by_id_dao
|
||||
from schemas.class_request import ClassCreate,ClassResponse,ClassUpdate
|
||||
from schema.class_schema import ClassCreate,ClassResponse,ClassUpdate
|
||||
from database import get_db
|
||||
from typing import Optional
|
||||
from sqlalchemy.orm import Session
|
||||
class_app = APIRouter()
|
||||
@class_app.post('',response_model=ClassResponse)
|
||||
class_router = APIRouter()
|
||||
@class_router.post('',response_model=ClassResponse,summary='添加班级信息')
|
||||
def add_class(c:ClassCreate,db=Depends(get_db)):
|
||||
if class_no_exist(c.class_id,db):
|
||||
raise HTTPException(status_code=404,detail='该编号已经存在')
|
||||
@@ -13,7 +13,7 @@ def add_class(c:ClassCreate,db=Depends(get_db)):
|
||||
if not ok:
|
||||
raise HTTPException(status_code=500,detail='服务器繁忙或指定的老师不存在')
|
||||
return ClassResponse(totals=1,data=result)
|
||||
@class_app.put('{id}')
|
||||
@class_router.put('/{id}',summary='更新班级信息')
|
||||
def update_class(id:int,c:ClassUpdate,db=Depends(get_db)):
|
||||
d = c.model_dump()
|
||||
r = update_class_dao(id=id ,update_data=d,db=db)
|
||||
@@ -28,7 +28,7 @@ def get_class(id:int,db=Depends(get_db),page:int=1,page_size:int=10):
|
||||
return rows
|
||||
raise HTTPException(status_code=404,detail='该班级不存在')
|
||||
'''
|
||||
@class_app.get('')
|
||||
@class_router.get('',summary='查询班级信息')
|
||||
def get_classes(
|
||||
id: Optional[int] = Query(None, description="班级ID"),
|
||||
class_id: Optional[str] = Query(None, description="班级编号"),
|
||||
@@ -49,7 +49,7 @@ def get_classes(
|
||||
# 多条件列表查询
|
||||
total, items = get_class_list_dao(
|
||||
db=db,
|
||||
class_no=class_id,
|
||||
class_id=class_id,
|
||||
class_name=class_name,
|
||||
ht_id=ht_id,
|
||||
t_id=t_id,
|
||||
@@ -63,14 +63,14 @@ def get_classes(
|
||||
"page_size": page_size,
|
||||
"items": items
|
||||
}
|
||||
@class_app.delete('/{id}') #软删除
|
||||
@class_router.delete('/{id}',summary='软删除') #软删除
|
||||
def delete_class(id:int,db=Depends(get_db)):
|
||||
rows = delete_class_dao(id=id,db=db)
|
||||
if not rows:
|
||||
raise HTTPException(status_code=404,detail='该班级不存在或已删除')
|
||||
return{'code':200,'total':rows,'detail':'删除成功'}
|
||||
|
||||
@class_app.delete('{id}/hard')
|
||||
@class_router.delete('{id}/hard',summary='删除班级信息')
|
||||
def hard_delete_class(id:int,db=Depends(get_db)):
|
||||
rows = hard_delete_class_dao(id=id,db=db)
|
||||
if not rows:
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
from fastapi import APIRouter,HTTPException,Depends
|
||||
from database import Session
|
||||
from model.e_model import Employment
|
||||
from schema.e_schema import EmploymentRequest
|
||||
from dao.e_dao import get_employment_dao,wq_employment_dao,upd_employment_dao,del_employment_dao
|
||||
employment_api = APIRouter()
|
||||
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@employment_api.get('/',summary='查询就业信息')
|
||||
def get_employment(db=Depends(get_db)):
|
||||
r = get_employment_dao(db)
|
||||
return {'code':200,'detail':'查询成功!','data':r}
|
||||
|
||||
|
||||
@employment_api.post('/',summary='新增就业信息')
|
||||
def add_employment(e:EmploymentRequest,db=Depends(get_db)):
|
||||
d= e.model_dump()
|
||||
r = wq_employment_dao(d,db)
|
||||
return {'code':200,'detail':'添加成功!','total':r}
|
||||
#
|
||||
#
|
||||
@employment_api.put('/',summary='更新就业信息')
|
||||
def update_employment(e:EmploymentRequest,db=Depends(get_db)):
|
||||
e1 = e.model_dump()
|
||||
r = upd_employment_dao(e1,db)
|
||||
return {'code':200,'msg':'更新成功','total':r}
|
||||
#
|
||||
@employment_api.delete('/',summary='删除就业信息')
|
||||
def delete_employment(stu_id:str,db=Depends(get_db)):
|
||||
r =del_employment_dao(stu_id,db)
|
||||
return {'code':200,'totals':r,'detail':'删除成功!'}
|
||||
@@ -0,0 +1,35 @@
|
||||
from fastapi import APIRouter,Depends,HTTPException
|
||||
from database import *
|
||||
from schema.epy_schema import EmploymentRequest
|
||||
from dao.epy_dao import get_employment_dao,wq_employment_dao,upd_employment_dao,del_employment_dao
|
||||
epy_router = APIRouter()
|
||||
|
||||
|
||||
@epy_router.get('',summary='查询就业信息')
|
||||
def get_employment(stu_id:str,company:str,salary:int,db=Depends(get_db)): # get变动
|
||||
try:
|
||||
rows = get_employment_dao(db, stu_id, company, salary)
|
||||
if not rows: # dao层返回的是q.() 是列表
|
||||
return {'code':404,'detail':'数据不存在','total':[]} # 数据不存在返回报错信息
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f'查询失败:{str(e)}')
|
||||
return {'code':200,'detail':'查询成功','date':[{'stu_id':i.stu_id,'company':i.company,'salary':i.salary} for i in rows]}
|
||||
|
||||
|
||||
@epy_router.post('',summary='新增就业信息')
|
||||
def add_employment(e:EmploymentRequest,db=Depends(get_db)):
|
||||
d= e.model_dump()
|
||||
r = wq_employment_dao(d,db)
|
||||
return {'code':200,'detail':'添加成功!','total':r}
|
||||
#
|
||||
#
|
||||
@epy_router.put('',summary='更新就业信息')
|
||||
def update_employment(e:EmploymentRequest,db=Depends(get_db)):
|
||||
e1 = e.model_dump()
|
||||
r = upd_employment_dao(e1,db)
|
||||
return {'code':200,'msg':'更新成功','total':r}
|
||||
#
|
||||
@epy_router.delete('',summary='删除就业信息')
|
||||
def delete_employment(stu_id:str,db=Depends(get_db)):
|
||||
r =del_employment_dao(stu_id,db)
|
||||
return {'code':200,'totals':r,'detail':'删除成功!'}
|
||||
@@ -1,43 +1,43 @@
|
||||
from dao import get_db
|
||||
from schema import Test
|
||||
from database import get_db
|
||||
from fastapi import APIRouter,Depends
|
||||
from dao import a,a1,a2,a3,a4,a5
|
||||
from dao.sc_dao import a,a1,a2,a3,a4,a5
|
||||
from schema.sc_schema import Test
|
||||
|
||||
ScoreAPI=APIRouter()
|
||||
ScoreAPI1=APIRouter()
|
||||
@ScoreAPI.post('/scores',tags=['增'])
|
||||
score_router=APIRouter()
|
||||
@score_router.post('',summary='添加成绩信息')
|
||||
def scores(s:Test,db=Depends(get_db)):
|
||||
a(s,db)
|
||||
d = s.model_dump()
|
||||
a(d,db)
|
||||
return {'code':200,'detail':'添加成功!',}
|
||||
@ScoreAPI.delete('/scores',tags=['删'])
|
||||
@score_router.delete('',summary='删除成绩信息')
|
||||
def scores1(stu_id:int,db=Depends(get_db)):
|
||||
r = a1(stu_id,db)
|
||||
if r==0:
|
||||
return '没有该学生'
|
||||
else:
|
||||
return f'已删除学生编号为{stu_id}的学生'
|
||||
@ScoreAPI.put('/scores',tags=['改'])
|
||||
@score_router.put('',summary='更新成绩信息')
|
||||
def scores2(stu_id:int,exam_seq:int,s:Test,db=Depends(get_db)):
|
||||
r=a2(stu_id,exam_seq,s,db)
|
||||
if r!=0:
|
||||
return '更新成功'
|
||||
else:
|
||||
return '没有该数据'
|
||||
@ScoreAPI.get('/scores',tags=['查'])
|
||||
def Scores3(stu_id:int,exam_seq:int,db=Depends(get_db)):
|
||||
@score_router.get('',summary='通过学号查询成绩信息')
|
||||
def scores3(stu_id:int,exam_seq:int,db=Depends(get_db)):
|
||||
r=a3(stu_id,exam_seq,db)
|
||||
try:
|
||||
return {'成绩编号':r.id,'学生编号':r.stu_id,f'第{exam_seq}次成绩:':r.score,'创建日期':r.create_date,'更新日期':r.update_date}
|
||||
except:
|
||||
return '没有该学生'
|
||||
@ScoreAPI1.put('/scores/{stu_id}',tags=['软删除'])
|
||||
@score_router.delete('/{stu_id}',summary='软删除')
|
||||
def scores4(stu_id:int,exam_seq:int,db=Depends(get_db)):
|
||||
r=a4(stu_id, exam_seq, db)
|
||||
if r ==0:
|
||||
if r == 1:
|
||||
return '没有该数据'
|
||||
else:
|
||||
return '已删除'
|
||||
@ScoreAPI1.get('/scores/{stu_id}',tags=['总分,平均分,最大值,最小值'])
|
||||
@score_router.get('/{stu_id}',summary='计算总分、平均分、最大值、最小值')
|
||||
def scores5(stu_id:int,db=Depends(get_db)):
|
||||
s,r,a=a5(stu_id,db)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from database import get_db
|
||||
from dao.stu_dao import create_student, get_student_by_id, get_student_list, update_student, delete_student_logic
|
||||
from schema.stu_schema import (
|
||||
StudentCreateRequest,
|
||||
StudentUpdateRequest,
|
||||
StudentQuery,
|
||||
StudentResponse,
|
||||
StudentPageResponse
|
||||
)
|
||||
|
||||
# 创建路由对象 API Router1
|
||||
stu_router = APIRouter()
|
||||
@stu_router.post("", response_model=StudentResponse, summary="新增学生")
|
||||
def add_student(
|
||||
student_req: StudentCreateRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
db_stu = create_student(db, student_req)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=500, detail="新增学生失败,数据库异常")
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
|
||||
|
||||
@stu_router.get("", response_model=StudentPageResponse, summary="根据学号姓名班级查询")
|
||||
def get_student_page(
|
||||
query: StudentQuery = Depends(),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
total, db_stu_list = get_student_list(
|
||||
db,
|
||||
stu_id=query.stu_id,
|
||||
stu_name=query.stu_name,
|
||||
class_id=query.class_id,
|
||||
page=query.page,
|
||||
page_size=query.page_size
|
||||
)
|
||||
if total == 0:
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
|
||||
item_list = [StudentResponse.model_validate(s) for s in db_stu_list]
|
||||
|
||||
return StudentPageResponse(
|
||||
total=total,
|
||||
page=query.page,
|
||||
page_size=query.page_size,
|
||||
items=item_list
|
||||
)
|
||||
|
||||
|
||||
@stu_router.get("/{stu_id}", response_model=StudentResponse, summary="根据学号查询")
|
||||
def get_one_student(
|
||||
stu_id: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
db_stu = get_student_by_id(db, stu_id)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
|
||||
@stu_router.put("/{stu_id}", response_model=StudentResponse, summary="修改学生信息")
|
||||
def edit_student(
|
||||
stu_id: str,
|
||||
update_req: StudentUpdateRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
exist = get_student_by_id(db, stu_id)
|
||||
if exist is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
|
||||
db_stu = update_student(db, stu_id, update_req)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=500, detail="修改学生失败,数据库异常")
|
||||
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
|
||||
|
||||
@stu_router.delete("/{stu_id}", response_model=StudentResponse, summary="删除学生信息")
|
||||
def remove_student(
|
||||
stu_id: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
exist = get_student_by_id(db, stu_id)
|
||||
if exist is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
|
||||
db_stu = delete_student_logic(db, stu_id)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=500, detail="删除学生失败,数据库异常")
|
||||
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
@@ -1,168 +0,0 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from database import get_db
|
||||
from dao.student_dao import create_student, get_student_by_id, get_student_list, update_student, delete_student_logic
|
||||
from schema.student_schema import (
|
||||
StudentCreateRequest,
|
||||
StudentUpdateRequest,
|
||||
StudentQuery,
|
||||
StudentResponse,
|
||||
StudentPageResponse
|
||||
)
|
||||
|
||||
# 创建路由对象 API Router1
|
||||
router = APIRouter( tags=["学生管理模块"])
|
||||
@router.post("/", response_model=StudentResponse,summary="新增学生")
|
||||
def add_student(
|
||||
student_req: StudentCreateRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
db_stu = create_student(db, student_req)
|
||||
# 手动转字典:model的stu_id映射响应体的id
|
||||
stu_dict = {
|
||||
"id": db_stu.id,
|
||||
"stu_id": db_stu.stu_id,
|
||||
"class_id": db_stu.class_id,
|
||||
"stu_name": db_stu.stu_name,
|
||||
"native_place": db_stu.native_place,
|
||||
"graduate_school": db_stu.graduate_school,
|
||||
"major": db_stu.major,
|
||||
"enroll_date": db_stu.enroll_date,
|
||||
"graduate_date": db_stu.graduate_date,
|
||||
"education": db_stu.education,
|
||||
"advisor_no": db_stu.advisor_no,
|
||||
"age": db_stu.age,
|
||||
"gender": db_stu.gender,
|
||||
"is_deleted": db_stu.is_deleted
|
||||
}
|
||||
return StudentResponse(**stu_dict)
|
||||
|
||||
|
||||
@router.get("/", response_model=StudentPageResponse,summary="根据学号姓名班级查询")
|
||||
def get_student_page(
|
||||
query: StudentQuery = Depends(),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
|
||||
total, db_stu_list = get_student_list(
|
||||
db,
|
||||
stu_id=query.stu_id,
|
||||
stu_name=query.stu_name,
|
||||
class_id=query.class_id,
|
||||
page=query.page,
|
||||
page_size=query.page_size
|
||||
)
|
||||
item_list = []
|
||||
for db_stu in db_stu_list:
|
||||
stu_dict = {
|
||||
"id": db_stu.id,
|
||||
"stu_id": db_stu.stu_id,
|
||||
"class_id": db_stu.class_id,
|
||||
"stu_name": db_stu.stu_name,
|
||||
"native_place": db_stu.native_place,
|
||||
"graduate_school": db_stu.graduate_school,
|
||||
"major": db_stu.major,
|
||||
"enroll_date": db_stu.enroll_date,
|
||||
"graduate_date": db_stu.graduate_date,
|
||||
"education": db_stu.education,
|
||||
"advisor_no": db_stu.advisor_no,
|
||||
"age": db_stu.age,
|
||||
"gender": db_stu.gender,
|
||||
"is_deleted": db_stu.is_deleted
|
||||
}
|
||||
item_list.append(StudentResponse(**stu_dict))
|
||||
|
||||
return StudentPageResponse(
|
||||
total=total,
|
||||
page=query.page,
|
||||
page_size=query.page_size,
|
||||
items=item_list
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{stu_id}", response_model=StudentResponse,summary="根据id查询")
|
||||
def get_one_student(
|
||||
stu_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
#根据主键stu_id查询单个学生
|
||||
db_stu = get_student_by_id(db, stu_id)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
|
||||
stu_dict = {
|
||||
"id": db_stu.id,
|
||||
"stu_id": db_stu.stu_id,
|
||||
"class_id": db_stu.class_id,
|
||||
"stu_name": db_stu.stu_name,
|
||||
"native_place": db_stu.native_place,
|
||||
"graduate_school": db_stu.graduate_school,
|
||||
"major": db_stu.major,
|
||||
"enroll_date": db_stu.enroll_date,
|
||||
"graduate_date": db_stu.graduate_date,
|
||||
"education": db_stu.education,
|
||||
"advisor_no": db_stu.advisor_no,
|
||||
"age": db_stu.age,
|
||||
"gender": db_stu.gender,
|
||||
"is_deleted": db_stu.is_deleted
|
||||
}
|
||||
return StudentResponse(**stu_dict)
|
||||
|
||||
|
||||
@router.put("/{stu_id}", response_model=StudentResponse,summary="修改学生信息")
|
||||
def edit_student(
|
||||
stu_id: int,
|
||||
update_req: StudentUpdateRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
#修改学生信息
|
||||
db_stu = update_student(db, stu_id, update_req)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
|
||||
stu_dict = {
|
||||
"id": db_stu.id,
|
||||
"stu_id": db_stu.stu_id,
|
||||
"class_id": db_stu.class_id,
|
||||
"stu_name": db_stu.stu_name,
|
||||
"native_place": db_stu.native_place,
|
||||
"graduate_school": db_stu.graduate_school,
|
||||
"major": db_stu.major,
|
||||
"enroll_date": db_stu.enroll_date,
|
||||
"graduate_date": db_stu.graduate_date,
|
||||
"education": db_stu.education,
|
||||
"advisor_no": db_stu.advisor_no,
|
||||
"age": db_stu.age,
|
||||
"gender": db_stu.gender,
|
||||
"is_deleted": db_stu.is_deleted
|
||||
}
|
||||
return StudentResponse(**stu_dict)
|
||||
|
||||
|
||||
@router.delete("/{stu_id}", response_model=StudentResponse,summary="删除学生信息")
|
||||
def remove_student(
|
||||
stu_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
#逻辑删除学生
|
||||
db_stu = delete_student_logic(db, stu_id)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
|
||||
stu_dict = {
|
||||
"id": db_stu.id,
|
||||
"stu_id": db_stu.stu_id,
|
||||
"stu_name": db_stu.stu_name,
|
||||
"class_id": db_stu.class_id,
|
||||
"native_place": db_stu.native_place,
|
||||
"graduate_school": db_stu.graduate_school,
|
||||
"major": db_stu.major,
|
||||
"enroll_date": db_stu.enroll_date,
|
||||
"graduate_date": db_stu.graduate_date,
|
||||
"education": db_stu.education,
|
||||
"advisor_no": db_stu.advisor_no,
|
||||
"age": db_stu.age,
|
||||
"gender": db_stu.gender,
|
||||
"is_deleted": db_stu.is_deleted
|
||||
}
|
||||
return StudentResponse(**stu_dict)
|
||||
@@ -1,49 +1,41 @@
|
||||
from fastapi import APIRouter,Depends,UploadFile,File,Query
|
||||
from model.teaModel import Session
|
||||
from dao.teaDao import insert_teachers_dao,update_teachers_dao,delete_teachers_dao,get_teachers_limit_dao,get_teachers_name_dao
|
||||
from schema.teaSchema import RequestModel, ResponseModel
|
||||
from dao.th_dao import insert_teachers_dao,update_teachers_dao,delete_teachers_dao,get_teachers_limit_dao,get_teachers_name_dao
|
||||
from schema.th_schema import RequestModel, ResponseModel
|
||||
from database import get_db
|
||||
|
||||
t_router = APIRouter()
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@t_router.get("/teachers",tags=['教师接口信息'],summary='分页查询教师接口')
|
||||
@t_router.get("",summary='分页查询教师接口')
|
||||
def get_teachers(p:int=Query(default=0,ge=0,description='页码,0表示查询全部'),
|
||||
n:int=Query(default=0,ge=0,description='每页条数,0表示查询全部'),
|
||||
db=Depends(get_db)):
|
||||
db_teachers = get_teachers_limit_dao(p,n,db)
|
||||
return ResponseModel(data=db_teachers,total=len(db_teachers))
|
||||
|
||||
@t_router.get("/teachers/{name}",tags=['教师接口信息'],summary='分页查询教师接口')
|
||||
@t_router.get("/{name}",summary='通过名称查询教师接口')
|
||||
def get_name_teachers(name:str,db=Depends(get_db)):
|
||||
db_teachers = get_teachers_name_dao(name,db)
|
||||
return ResponseModel(data=db_teachers,total=len(db_teachers))
|
||||
|
||||
@t_router.post("/teachers",tags=['教师接口信息'],summary='新增教师接口')
|
||||
@t_router.post("",summary='新增教师接口')
|
||||
def insert_teachers(req:RequestModel,db=Depends(get_db)):
|
||||
rq= req.model_dump()
|
||||
n,m = insert_teachers_dao(rq,db)
|
||||
# print('打印获取的值:',n,m)
|
||||
return ResponseModel(total=n,msg=m)
|
||||
|
||||
@t_router.put("/teachers/{t_id}",tags=['教师接口信息'],summary='更新教师接口')
|
||||
def update_teachers(req:RequestModel,t_id:int,db=Depends(get_db),):
|
||||
@t_router.put("",summary='更新教师接口')
|
||||
def update_teachers(req:RequestModel,db=Depends(get_db)):
|
||||
rq= req.model_dump()
|
||||
n,m = update_teachers_dao(rq,db,t_id)
|
||||
n,m = update_teachers_dao(rq,db)
|
||||
return ResponseModel(total=n,msg=m)
|
||||
|
||||
@t_router.delete("/teachers/{t_id}",tags=['教师接口信息'],summary='删除教师接口')
|
||||
@t_router.delete("/{t_id}",summary='删除教师接口')
|
||||
def delete_teachers(t_id:int,db=Depends(get_db)):
|
||||
n,m =delete_teachers_dao(t_id,db)
|
||||
return ResponseModel(msg=m,total=n)
|
||||
|
||||
@t_router.post('/files',tags=['文件接口信息'],summary='上传文件接口')
|
||||
@t_router.post('/files',summary='上传文件接口')
|
||||
async def upload_files(f:UploadFile=File(),db=Depends(get_db)):
|
||||
# print('UploadFile上传文件,读取该对象:',f)
|
||||
text =await f.read()
|
||||
@@ -52,12 +44,12 @@ async def upload_files(f:UploadFile=File(),db=Depends(get_db)):
|
||||
dt = []
|
||||
print('长度为',lg,l)
|
||||
for i in l:
|
||||
if '名字' in i:
|
||||
if '教师编号' in i:
|
||||
continue
|
||||
p = i.split()
|
||||
if not p: # 判断如果p为[],跳过
|
||||
continue
|
||||
l2 = ['t_name','t_age','t_phone','t_sex','class_id']
|
||||
l2 = ['t_id','t_name','t_age','t_phone','t_sex','class_id']
|
||||
d =dict()
|
||||
for j in range(len(p)):
|
||||
d[l2[j]]=p[j]
|
||||
@@ -1,56 +0,0 @@
|
||||
from fastapi import APIRouter, Depends,FastAPI
|
||||
from model.teaModel import Session
|
||||
from dao.dao1 import fun1,fun2,fun3,fun4,fun5,fun6, fun7 ,fun8
|
||||
|
||||
StatsAPI = APIRouter(prefix='/stats', tags=['统计分析'])
|
||||
app = FastAPI()
|
||||
app.include_router(StatsAPI)
|
||||
def get_db():
|
||||
db=Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@StatsAPI.get('/age-over-30', summary='超过30岁学员')
|
||||
def stat_age(db= Depends(get_db)):
|
||||
rows = fun1(db)
|
||||
return {'code': 200, 'data':rows }
|
||||
|
||||
@StatsAPI.get('/class-gender', summary='各班人数及男女比')
|
||||
def stat_class_gender(db= Depends(get_db)):
|
||||
rows = fun2(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/all-pass-80', summary='每次考试都≥80分')
|
||||
def stat_pass80(db= Depends(get_db)):
|
||||
rows = fun3(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/two-fail', summary='两次以上不及格')
|
||||
def stat_fail(db= Depends(get_db)):
|
||||
rows = fun4(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/avg-score', summary='各班每次考试平均分')
|
||||
def stat_avg(db= Depends(get_db)):
|
||||
rows = fun5(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/top5-salary', summary='薪资前五名')
|
||||
def stat_top5(db= Depends(get_db)):
|
||||
rows = fun6(db)
|
||||
return rows
|
||||
@StatsAPI.get('/duration', summary='每个学生就业时长')
|
||||
def stat_duration(db= Depends(get_db)):
|
||||
rows = fun7(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
@StatsAPI.get('/avg-duration', summary='各班平均就业时长')
|
||||
def stat_avg_duration(db= Depends(get_db)):
|
||||
rows = fun8(db)
|
||||
return {'code': 200, 'data': rows}
|
||||
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run('cxjk:app',host='127.0.0.1',port=9999)
|
||||
+2
-8
@@ -1,14 +1,8 @@
|
||||
from pymysql import IntegrityError
|
||||
from models.class_model import Class,Teacher
|
||||
from model.class_model import Class
|
||||
from model.th_model import Teacher
|
||||
from sqlalchemy.orm import Session
|
||||
def add_class_dao(c,db):
|
||||
try:
|
||||
head_teacher = db.query(Teacher).filter(Teacher.id == c['ht_id']).first()
|
||||
if head_teacher is None:
|
||||
return False, '指定的班主任不存在'
|
||||
class_teacher = db.query(Teacher).filter(Teacher.id == c['t_id']).first()
|
||||
if class_teacher is None:
|
||||
return False, '指定的授课老师不存在'
|
||||
if db.query(Class).filter(
|
||||
Class.class_id == c['class_id'],#如果请求体传进来的classid在表里找的到
|
||||
Class.is_deleted==0 #且没有处于软删除状态
|
||||
|
||||
@@ -1,17 +1,30 @@
|
||||
from http.client import HTTPException
|
||||
|
||||
from model.e_model import Employment
|
||||
from fastapi import HTTPException
|
||||
from model.epy_model import Employment
|
||||
from fastapi import HTTPException,Depends
|
||||
from database import *
|
||||
|
||||
def get_employment_dao(db):
|
||||
try:
|
||||
r = db.query(Employment).all()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f'查询失败:str(e)')
|
||||
else:
|
||||
db.commit()
|
||||
return [{'stu_id':i.stu_id,'class_name':i.class_name} for i in r ]
|
||||
|
||||
def get_employment_dao(db, stu_id:str, company:str, salary:int): # get变动
|
||||
q = db.query(Employment).filter(Employment.is_deleted == False)
|
||||
if stu_id:
|
||||
q = q.filter(Employment.stu_id == stu_id)
|
||||
if company:
|
||||
q = q.filter(Employment.company == company)
|
||||
if salary is not None:
|
||||
q = q.filter(Employment.salary == salary)
|
||||
return q.all()
|
||||
|
||||
|
||||
# def get_employment_dao(db):
|
||||
# try:
|
||||
# r = db.query(Employment).all()
|
||||
# except Exception as e:
|
||||
# db.rollback()
|
||||
# raise HTTPException(status_code=500, detail=f'查询失败:str(e)')
|
||||
# else:
|
||||
# db.commit()
|
||||
# return [{'stu_id':i.stu_id,'class_name':i.class_name,'company':i.company,'salary':i.salary} for i in r ]
|
||||
|
||||
|
||||
def wq_employment_dao(o,db):
|
||||
@@ -41,8 +54,8 @@ def del_employment_dao(stu_id,db):
|
||||
if not row:
|
||||
raise HTTPException(status_code=404,detail='记录不存在')
|
||||
for i in row:
|
||||
if i.deleted == 0:
|
||||
db.query(Employment).filter(Employment.stu_id == i.stu_id).update({'deleted':1})
|
||||
if i.is_deleted == 0:
|
||||
db.query(Employment).filter(Employment.stu_id == i.stu_id).update({'is_deleted':1})
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}')
|
||||
@@ -1,38 +1,37 @@
|
||||
from model import Session
|
||||
from model import Scores
|
||||
from database import Session
|
||||
from model.sc_model import Scores
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
# @ScoreAPI.post('/scores')
|
||||
def a(s,db):
|
||||
d = s.model_dump()
|
||||
|
||||
def a(d,db):
|
||||
o = Scores(**d)
|
||||
db.add(o)
|
||||
db.commit()
|
||||
# @ScoreAPI.delete('/scores')
|
||||
|
||||
def a1(stu_id,db):
|
||||
r=db.query(Scores).filter(Scores.stu_id==stu_id).delete()
|
||||
db.commit()
|
||||
return r
|
||||
# @ScoreAPI.put('/scores')
|
||||
|
||||
def a2(stu_id,exam_seq,s,db):
|
||||
r = db.query(Scores).filter((Scores.stu_id == stu_id) & (Scores.exam_seq == exam_seq)).update(
|
||||
s.model_dump(exclude={'stu_id', 'exam_seq'}))
|
||||
db.commit()
|
||||
return r
|
||||
#@ScoreAPI.get('/scores')
|
||||
|
||||
def a3(stu_id,exam_seq,db):
|
||||
r=db.query(Scores).filter((Scores.stu_id==stu_id)&(Scores.exam_seq==exam_seq)).first()
|
||||
return r
|
||||
# @ScoreAPI1.put('/scores/{stu_id}',tags=['软删除'])
|
||||
|
||||
def a4(stu_id, exam_seq, db):
|
||||
r=db.query(Scores).filter((Scores.stu_id==stu_id)&(Scores.exam_seq==exam_seq)).update({Scores.deleted:0})
|
||||
r=db.query(Scores).filter((Scores.stu_id==stu_id)&(Scores.exam_seq==exam_seq)).update({Scores.deleted:1})
|
||||
db.commit()
|
||||
return r
|
||||
# @ScoreAPI1.get('/scores/{stu_id}',tags=['总分,平均分,最大值,最小值'])
|
||||
|
||||
def a5(stu_id,db):
|
||||
r = db.query(Scores).filter((Scores.stu_id == stu_id)).all()
|
||||
s = 0
|
||||
+34
-36
@@ -1,44 +1,43 @@
|
||||
from sqlalchemy import func, desc
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from model.Employment import Employment
|
||||
from model.epy_model import Employment
|
||||
from fastapi import HTTPException
|
||||
from model.Students import Student
|
||||
from model.Score import Scores
|
||||
from model.stu_model import Student
|
||||
from model.sc_model import Scores
|
||||
|
||||
# ① 超30岁学员
|
||||
def fun1(db):
|
||||
def get_stu_over_30(db):
|
||||
try:
|
||||
rows = db.query(Student).filter(Student.age>30).all()
|
||||
return [{'id': i.stu_id, 'name': i.name} for i in rows]
|
||||
rows = db.query(Student).filter(Student.age>30,Student.is_deleted == 0).all()
|
||||
return [{'id': i.stu_id, 'name': i.stu_name} for i in rows]
|
||||
except SQLAlchemyError as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询超三十岁学员信息失败:{str(e)}')
|
||||
|
||||
# ② 各班人数 + 男女比
|
||||
def fun2(db):
|
||||
def get_class_gender_count(db):
|
||||
try:
|
||||
rows = db.query(
|
||||
Student.stu_class,
|
||||
Student.class_id,
|
||||
Student.gender,
|
||||
func.count(1).label('num')
|
||||
).group_by(Student.stu_class, Student.gender).all()
|
||||
return [{'班级': r.stu_class, '性别': r.gender, '人数':r.num } for r in rows]
|
||||
).group_by(Student.class_id, Student.gender).all()
|
||||
return [{'班级': r.class_id, '性别': r.gender, '人数':r.num } for r in rows]
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400,detail=f'查询各班人数与男女比例失败:{str(e)}')
|
||||
|
||||
# ③ 每次考试都≥80分的学生
|
||||
def fun3(db):
|
||||
def get_stu_all_pass_80(db):
|
||||
try:
|
||||
print('1111111111-----')
|
||||
rows = db.query(
|
||||
Scores.stu_id,
|
||||
Student.name,
|
||||
Student.stu_name,
|
||||
func.min(Scores.score).label('min_score')
|
||||
).join(Student, Student.stu_id == Scores.stu_id) \
|
||||
.group_by(Scores.stu_id, Student.name) \
|
||||
.group_by(Scores.stu_id, Student.stu_name) \
|
||||
.having(func.min(Scores.score) >= 80).all()
|
||||
t = ['stu_id', 'name', 'min_score']
|
||||
t = ['stu_id', 'stu_name', 'min_score']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
@@ -46,16 +45,16 @@ def fun3(db):
|
||||
raise HTTPException(status_code=400,detail=f'查询成绩大于80的学生失败:{str(e)}')
|
||||
|
||||
# ④ 两次以上不及格的学生
|
||||
def fun4(db):
|
||||
def get_stu_two_fail(db):
|
||||
try:
|
||||
rows = db.query(
|
||||
Student.name,
|
||||
Student.stu_class,
|
||||
Student.stu_name,
|
||||
Student.class_id,
|
||||
).join(Scores, Scores.stu_id == Student.stu_id) \
|
||||
.filter(Scores.score < 60) \
|
||||
.group_by(Scores.stu_id,Student.name,Student.stu_class) \
|
||||
.group_by(Scores.stu_id,Student.stu_name,Student.class_id) \
|
||||
.having(func.count(Scores.id) >= 2).all()
|
||||
t = ['name', 'stu_class']
|
||||
t = ['stu_name', 'class_id']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
@@ -63,17 +62,16 @@ def fun4(db):
|
||||
raise HTTPException(status_code=400,detail=f'查询两次不及格的学生失败:{str(e)}')
|
||||
|
||||
# ⑤ 每次考试各班平均分(降序)
|
||||
def fun5(db):
|
||||
def get_class_avg_sc(db):
|
||||
try:
|
||||
print('5555555555')
|
||||
rows = db.query(
|
||||
Scores.exam_seq,
|
||||
Student.stu_class,
|
||||
Student.class_id,
|
||||
func.avg(Scores.score).label('avg_score')
|
||||
).join(Student, Student.stu_id == Scores.stu_id) \
|
||||
.group_by(Scores.exam_seq, Student.stu_class) \
|
||||
.group_by(Scores.exam_seq, Student.class_id) \
|
||||
.order_by(desc('avg_score')).all()
|
||||
t = ['exam_seq', 'stu_class','avg_score']
|
||||
t = ['exam_seq', 'class_id','avg_score']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
@@ -81,10 +79,10 @@ def fun5(db):
|
||||
raise HTTPException(status_code=400, detail=f'查询各班平均分失败:{str(e)}')
|
||||
|
||||
# ⑥ 薪资前五名
|
||||
def fun6(db):
|
||||
def get_top5_salary(db):
|
||||
try:
|
||||
rows = db.query(Student.name, Student.stu_class,Employment.work_date,Employment.company,Employment.salary).join(Student, Student.stu_id == Employment.s_id).order_by(Employment.salary).limit(5).all()
|
||||
t = ['s_name','stu_class','work_date','company','salary']
|
||||
rows = db.query(Student.stu_name, Student.class_id,Employment.work_date,Employment.company,Employment.salary).join(Student, Student.stu_id == Employment.stu_id).order_by(Employment.salary.desc()).limit(5).all()
|
||||
t = ['stu_name','class_id','work_date','company','salary']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
print(l)
|
||||
return {'code': 200, 'data': l}
|
||||
@@ -93,10 +91,10 @@ def fun6(db):
|
||||
raise HTTPException(status_code=400,detail=f'查询薪资前五名失败失败:{str(e)}')
|
||||
|
||||
# ⑦ 每个学生就业时长
|
||||
def fun7(db):
|
||||
def get_stu_duration(db):
|
||||
try:
|
||||
rows = db.query(Employment.s_id,func.datediff(Employment.offer_date,Employment.open_date).label('offer_s')).group_by(Employment.s_id,Employment.offer_date,Employment.open_date).all()
|
||||
t = ['s_id', 'offer_s']
|
||||
rows = db.query(Employment.stu_id,func.datediff(Employment.offer_date,Employment.open_date).label('offer_diff')).group_by(Employment.stu_id,Employment.offer_date,Employment.open_date).all()
|
||||
t = ['s_id', 'offer_diff']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return {'code': 200, 'data': l}
|
||||
except Exception as e:
|
||||
@@ -104,15 +102,15 @@ def fun7(db):
|
||||
raise HTTPException(status_code=400,detail=f'查询每个学生的就业时长失败:{str(e)}')
|
||||
|
||||
# ⑧ 各班平均就业时长
|
||||
def fun8(db):
|
||||
def get_class_avg_duration(db):
|
||||
try:
|
||||
rows = db.query(
|
||||
Student.stu_class,
|
||||
Student.class_id,
|
||||
func.avg(func.datediff(Employment.offer_date, Employment.open_date)).label('avg_duration')
|
||||
).join(Student, Student.stu_id == Employment.s_id).filter(
|
||||
).join(Student, Student.stu_id == Employment.stu_id).filter(
|
||||
Employment.open_date.isnot(None)
|
||||
).group_by(Student.stu_class).all()
|
||||
t = ['stu_class', 'avg_duration']
|
||||
).group_by(Student.class_id).all()
|
||||
t = ['class_id', 'avg_duration']
|
||||
l = [dict(zip(t, row)) for row in rows]
|
||||
return l
|
||||
except Exception as e:
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from model.stu_model import Student
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from schema.stu_schema import StudentCreateRequest,StudentUpdateRequest
|
||||
def create_student(db: Session, student_create: StudentCreateRequest):
|
||||
|
||||
try:
|
||||
db_student = Student(**student_create.model_dump())
|
||||
db.add(db_student)
|
||||
db.commit()
|
||||
except SQLAlchemyError:
|
||||
db.rollback()
|
||||
return None
|
||||
new_student = db.query(Student).filter(
|
||||
Student.stu_id == student_create.stu_id,
|
||||
Student.is_deleted == False).first()
|
||||
return new_student
|
||||
|
||||
|
||||
def get_student_by_id(db: Session, stu_id: str):
|
||||
query = db.query(Student).filter(
|
||||
Student.stu_id == stu_id,
|
||||
Student.is_deleted == False)
|
||||
return query.first()
|
||||
|
||||
|
||||
def get_student_list(db: Session, stu_id: str | None = None,
|
||||
class_id: str = None, page: int = 1, page_size: int = 10, stu_name: str = None):
|
||||
page = max(1, page)
|
||||
page_size = max(1, min(100, page_size))
|
||||
query = db.query(Student).filter(Student.is_deleted == False)
|
||||
if stu_id:
|
||||
query = query.filter(Student.stu_id == stu_id)
|
||||
if stu_name:
|
||||
query = query.filter(Student.stu_name.like(f"%{stu_name}%"))
|
||||
if class_id:
|
||||
query = query.filter(Student.class_id == class_id)
|
||||
|
||||
total = query.count()
|
||||
offset = (page - 1) * page_size
|
||||
db_student_list = query.offset(offset).limit(page_size).all()
|
||||
return total, db_student_list
|
||||
|
||||
|
||||
def update_student(db: Session, stu_id: str, student_update: StudentUpdateRequest):
|
||||
try:
|
||||
db_student = get_student_by_id(db=db, stu_id=stu_id)
|
||||
if not db_student:
|
||||
return None
|
||||
update_data = student_update.model_dump(exclude_unset=True)
|
||||
for k, v in update_data.items():
|
||||
setattr(db_student, k, v)
|
||||
db.commit()
|
||||
except SQLAlchemyError:
|
||||
db.rollback()
|
||||
return None
|
||||
return db_student
|
||||
|
||||
def delete_student_logic(db: Session, stu_id: str):
|
||||
try:
|
||||
db_student = get_student_by_id(db, stu_id)
|
||||
if not db_student:
|
||||
return None
|
||||
db_student.is_deleted = True
|
||||
db.commit()
|
||||
except SQLAlchemyError:
|
||||
db.rollback()
|
||||
return None
|
||||
return db_student
|
||||
@@ -1,57 +0,0 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from model.student_model import Student
|
||||
from schema.student_schema import StudentCreateRequest,StudentUpdateRequest
|
||||
def create_student(db: Session, student_create: StudentCreateRequest):
|
||||
"""
|
||||
新增学生
|
||||
: db: 数据库会话
|
||||
:student_c reate: 前端传来的新增请求体
|
||||
"""
|
||||
db_student = Student(**student_create.model_dump())#请求体转为字典,再解包创建实体对象
|
||||
db.add(db_student)#添加
|
||||
db.commit()#提交事务
|
||||
new_student=db.query(Student).filter(
|
||||
Student.stu_id==student_create.stu_id,
|
||||
Student.is_deleted==False).first()
|
||||
#查询 Student 表,取第一条,没有则返回 None
|
||||
return new_student
|
||||
def get_student_by_id(db: Session, stu_id: int):#根据stu_id查询单个学生
|
||||
query = db.query(Student).filter(
|
||||
Student.stu_id == stu_id,
|
||||
Student.is_deleted == False)
|
||||
return query.first()
|
||||
def get_student_list(db: Session, stu_id:None,
|
||||
class_id:str=None, page:int=1, page_size:int=10, stu_name:str=None):#查询学生列表,支持按照编号/姓名/班级删选
|
||||
# 基础查询:只查未逻辑删除的学生
|
||||
query = db.query(Student).filter(Student.is_deleted == False)
|
||||
if stu_id:
|
||||
query = query.filter(Student.stu_id == stu_id)
|
||||
if stu_name:
|
||||
query = query.filter(Student.stu_name == stu_name)
|
||||
if class_id:
|
||||
query = query.filter(Student.class_id == class_id)
|
||||
total = query.count()#统计满足条件的记录
|
||||
offset = (page - 1) * page_size# 计算分页偏移量:跳过前面 (page-1)*page_size 条
|
||||
db_student_list = query.offset(offset).limit(page_size).all()# 执行分页查询:跳过offset条,取page_size条
|
||||
return total, db_student_list
|
||||
def update_student(db: Session, stu_id:int,student_update: StudentUpdateRequest):
|
||||
"""修改学生信息
|
||||
db: 数据库会话stu_id: 要修改的学生主键student_update: 前端传来的修改请求体:
|
||||
"""
|
||||
db_student = get_student_by_id(db=db,stu_id=stu_id)
|
||||
if not db_student:
|
||||
return None#先查学生,查不到报错
|
||||
update_data=student_update.model_dump(exclude_unset=True)#只解析前端输入的键值对,默认值不会解析
|
||||
for k,v in update_data.items():
|
||||
setattr(db_student,k,v)#把 value 赋给 db_student 的 key 属性。
|
||||
db.commit()
|
||||
updated_student=get_student_by_id(db, stu_id)
|
||||
return updated_student
|
||||
def delete_student_logic(db: Session, stu_id:int):
|
||||
db_student = get_student_by_id(db, stu_id)
|
||||
if not db_student:
|
||||
return None
|
||||
db_student.is_deleted = True#把逻辑删除标记改成True
|
||||
db.commit()#重新查询,返回标记已删除的这条记录
|
||||
deleted_student = db.query(Student).filter(Student.stu_id == stu_id).first()
|
||||
return deleted_student
|
||||
@@ -1,63 +0,0 @@
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from fastapi import HTTPException
|
||||
from model.teaModel import Teacher
|
||||
|
||||
def get_teachers_limit_dao(p,n,db):
|
||||
if p == 0 and n == 0:
|
||||
r = db.query(Teacher).all()
|
||||
else:
|
||||
if p < 1:
|
||||
p = 1
|
||||
r = db.query(Teacher).offset((p-1)*n).limit(n).all()
|
||||
return [ {'id':i.id,'t_name':i.t_name,'t_sex':i.t_sex,'t_age':i.t_age} for i in r]
|
||||
|
||||
|
||||
def get_teachers_name_dao(name,db):
|
||||
try:
|
||||
r = db.query(Teacher).filter(Teacher.t_name==name).all()
|
||||
except SQLAlchemyError as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f'查询失败:{str(e)}')
|
||||
else:
|
||||
return [ {'id':i.id,'t_name':i.t_name,'t_sex':i.t_sex,'t_age':i.t_age} for i in r]
|
||||
|
||||
def insert_teachers_dao(req,db):
|
||||
try:
|
||||
exist = db.query(Teacher).filter(Teacher.t_phone==req['t_phone']).first()
|
||||
if exist:
|
||||
raise HTTPException(status_code=400,detail='该手机号码已存在,不可重复使用')
|
||||
t = Teacher(**req)
|
||||
db.add(t)
|
||||
except SQLAlchemyError as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500,detail=f'添加失败:{str(e)}')
|
||||
else:
|
||||
db.commit()
|
||||
return True,'添加成功'
|
||||
|
||||
def update_teachers_dao(req,db,t_id):
|
||||
try:
|
||||
db.query(Teacher).filter(Teacher.id == t_id).update(req)
|
||||
except SQLAlchemyError as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500,detail=f'更新失败:{str(e)}')
|
||||
else:
|
||||
db.commit()
|
||||
return True,'更新成功'
|
||||
|
||||
def delete_teachers_dao(t_id,db):
|
||||
try:
|
||||
r = db.query(Teacher).filter(Teacher.id == t_id).all()
|
||||
if not r:
|
||||
raise HTTPException(status_code=404,detail='记录不存在')
|
||||
for i in r:
|
||||
if i.is_delete == 0:
|
||||
db.query(Teacher).filter(Teacher.id == i.id).update({'is_delete':True})
|
||||
else:
|
||||
raise HTTPException(status_code=400,detail='该记录已删除,不可重复删除')
|
||||
except SQLAlchemyError as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}')
|
||||
else:
|
||||
db.commit()
|
||||
return True,'删除成功'
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker,declarative_base
|
||||
|
||||
|
||||
tea_url ='mysql+pymysql://root:123456@127.0.0.1:3306/ai0824_stu?charset=utf8'
|
||||
|
||||
engine = create_engine(tea_url,pool_size=100, echo=False)
|
||||
Base=declarative_base()
|
||||
Session = sessionmaker(bind=engine,autoflush=False,autocommit=False)
|
||||
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -1,5 +1,21 @@
|
||||
from fastapi import FastAPI
|
||||
from api.teaApi import t_router
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(t_router)
|
||||
from fastapi import FastAPI
|
||||
from api.th_api import t_router
|
||||
from api.epy_api import epy_router
|
||||
from api.stu_api import stu_router
|
||||
from api.sc_api import score_router
|
||||
from api.class_api import class_router
|
||||
from api.tj_api import StatsAPI
|
||||
from database import *
|
||||
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
app.include_router(epy_router,prefix='/employment',tags=['就业信息管理模块'])
|
||||
app.include_router(stu_router,prefix='/students',tags=['学生信息管理模块'])
|
||||
app.include_router(score_router,prefix='/scores',tags=['成绩管理模块'])
|
||||
app.include_router(class_router,prefix='/classes',tags=['班级管理模块'])
|
||||
app.include_router(StatsAPI,prefix='/statistics',tags=['统计管理模块'])
|
||||
app.include_router(t_router,prefix='/teachers',tags=['教师管理模块'])
|
||||
|
||||
+5
-10
@@ -1,7 +1,6 @@
|
||||
from database import DATETIME,Base,Column,Integer,String,ForeignKey,Boolean,Enum as SQLEnum,DateTime,DATETIME
|
||||
from sqlalchemy import *
|
||||
from datetime import datetime
|
||||
from database import engine
|
||||
from enum import Enum
|
||||
from database import Base
|
||||
class Class(Base):
|
||||
'''
|
||||
班级信息数据库类模型
|
||||
@@ -15,21 +14,17 @@ class Class(Base):
|
||||
class_id = Column(String(32),
|
||||
nullable=False,
|
||||
index=True,
|
||||
unique=True,
|
||||
comment='班级编号')
|
||||
class_name = Column(String(32),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment='班级名字')
|
||||
ht_id = Column(Integer,
|
||||
ForeignKey('s_teacher.id'),
|
||||
nullable=False,
|
||||
ht_id = Column(Integer,nullable=False,
|
||||
comment='班主任ID')
|
||||
t_id = Column(Integer,
|
||||
ForeignKey('s_teacher.id'),
|
||||
nullable=False,
|
||||
t_id = Column(Integer,nullable=False,
|
||||
comment='授课老师ID')
|
||||
start_time = Column(DATETIME,default=datetime.now,comment='开课时间')
|
||||
update_time = Column(DATETIME,default=datetime.now,onupdate=datetime.now)
|
||||
is_deleted = Column(Integer,default=0,index=True)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
from sqlalchemy import *
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
|
||||
db_url = "mysql+pymysql://root:123456@127.0.0.1:3306/ai0824_stu?charset=utf8"
|
||||
|
||||
engine = create_engine(db_url, pool_size=100,echo=False)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
from database import *
|
||||
class Employment(Base):
|
||||
__tablename__ = 's_employment'
|
||||
id = Column(Integer
|
||||
, primary_key=True
|
||||
, autoincrement=True
|
||||
, comment='编号,自增主键')
|
||||
stu_id = Column(String(20)
|
||||
# , ForeingKey('s_student.stu_id')
|
||||
stu_id = Column(String(20),
|
||||
ForeignKey('s_student.stu_id')
|
||||
, unique=True
|
||||
, nullable=True
|
||||
, comment='学生学号,唯一')
|
||||
@@ -30,16 +23,6 @@ class Employment(Base):
|
||||
work_date = Column(DATETIME
|
||||
, default=datetime.now()
|
||||
, onupdate=datetime.now)
|
||||
is_delete = Column(Integer
|
||||
is_deleted = Column(Integer
|
||||
, default=0
|
||||
)
|
||||
|
||||
|
||||
Session = sessionmaker( bind = engine, autoflush = False, autocommit = False )
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -1,9 +1,7 @@
|
||||
from sqlalchemy import *
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
db_url='mysql+pymysql://root:123456@127.0.0.1:3306/ai0824?charset=utf8'
|
||||
engine=create_engine(db_url , pool_size=50,echo=True)
|
||||
Base=declarative_base()
|
||||
from database import *
|
||||
|
||||
class Scores(Base):
|
||||
__tablename__ = 's_scores'
|
||||
id=Column( Integer
|
||||
@@ -11,7 +9,8 @@ class Scores(Base):
|
||||
,autoincrement=True
|
||||
,comment='成绩编号'
|
||||
)
|
||||
stu_id=Column(Integer
|
||||
stu_id=Column(String(50)
|
||||
, ForeignKey('s_student.stu_id')
|
||||
,comment='学生编号'
|
||||
, nullable=False
|
||||
)
|
||||
@@ -36,10 +35,6 @@ class Scores(Base):
|
||||
,default=1
|
||||
, comment='软删除'
|
||||
)
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker( bind=engine
|
||||
,autoflush=False
|
||||
,autocommit = False
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from database import Base
|
||||
from sqlalchemy import Column,Integer,String,Date,Boolean, Enum as SQLEnum
|
||||
from sqlalchemy import Column,Integer,String,Date,Boolean, Enum as SQLEnum,ForeignKey
|
||||
from enum import Enum
|
||||
class Sex(Enum):
|
||||
m= "男"
|
||||
@@ -8,7 +8,7 @@ class Student(Base):
|
||||
__tablename__ = 's_student'
|
||||
id=Column(Integer, primary_key=True,autoincrement=True,comment="数据库主键id")
|
||||
stu_id=Column(String(50),unique=True,nullable=False,comment='学生id')
|
||||
class_id=Column(String(50),#ForeignKey=("s_class.class_id"),
|
||||
class_id=Column(String(50),ForeignKey("s_class.class_id"),
|
||||
comment='学生班级')
|
||||
stu_name=Column(String(30),nullable=False,comment='学生姓名')
|
||||
native_place = Column(String(100), comment="籍贯")
|
||||
@@ -21,5 +21,5 @@ class Student(Base):
|
||||
age = Column(Integer, comment="年龄")
|
||||
gender = Column(
|
||||
SQLEnum(Sex, values_callable=lambda x: [e.value for e in x]),
|
||||
comment="性 别")
|
||||
comment="性别")
|
||||
is_deleted = Column(Boolean, default=False, comment="逻辑删除标记:False未删除,True已删除")
|
||||
@@ -1,28 +1,23 @@
|
||||
from sqlalchemy import Integer, String, Column, DateTime, Enum as SQLEnum,Boolean,create_engine
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
from sqlalchemy import Integer, String, Column, DateTime, Enum as SQLEnum, Boolean, ForeignKey
|
||||
from database import *
|
||||
|
||||
from enum import Enum
|
||||
from datetime import datetime
|
||||
|
||||
tea_url ='mysql+pymysql://root:123456@127.0.0.1:3306/ai0824_stu?charset=utf8'
|
||||
|
||||
engine = create_engine(tea_url,pool_size=100, echo=False)
|
||||
Base = declarative_base()
|
||||
class Sex(Enum):
|
||||
w = '女'
|
||||
m = '男'
|
||||
|
||||
class Teacher(Base):
|
||||
__tablename__ = 's_teacher'
|
||||
id =Column(Integer, primary_key=True,autoincrement=True)
|
||||
id = Column(Integer, primary_key=True,autoincrement=True)
|
||||
t_id =Column(Integer,unique=True,nullable=False)
|
||||
t_name = Column(String(100),comment='教师名称')
|
||||
t_age = Column(Integer,comment='年龄')
|
||||
t_phone = Column(String(20),unique=True,comment='电话')
|
||||
t_sex = Column(SQLEnum(Sex,values_callable=lambda x: [e.value for e in x]),comment='性别')
|
||||
class_id = Column(String(100),comment='带班编号')
|
||||
class_id = Column(String(100),ForeignKey('s_class.class_id'),comment='带班编号')
|
||||
create_time = Column(DateTime,default=datetime.now,comment='创建时间')
|
||||
update_time = Column(DateTime,default=datetime.now,onupdate=datetime.now,comment='更新时间')
|
||||
is_delete = Column(Boolean,default=False,comment='判断是否删除')
|
||||
is_deleted = Column(Boolean,default=False,comment='判断是否删除')
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine,autoflush=False,autocommit=False)
|
||||
@@ -1,8 +1,8 @@
|
||||
from typing import Optional,List
|
||||
from pydantic import BaseModel,Field
|
||||
from datetime import date
|
||||
from model.student_model import Sex
|
||||
##================ =====请求体===============
|
||||
from model.stu_model import Sex
|
||||
##=====================请求体===============
|
||||
class StudentCreateRequest(BaseModel):
|
||||
stu_id:str=Field(...,description="学生id")
|
||||
class_id:Optional[str]=Field(None,description="学生班级id")
|
||||
@@ -37,20 +37,24 @@ class StudentQuery(BaseModel):
|
||||
page_size: int = Field(10, ge=1, le=100, description="每页条数,默认10条")
|
||||
##=====================响应体===============
|
||||
class StudentResponse(BaseModel):
|
||||
stu_id: str# 学生编号
|
||||
class_id: Optional[str]# 学生班级
|
||||
stu_name: str# 学生姓名
|
||||
native_place: Optional[str] # 籍贯
|
||||
graduate_school: Optional[str] # 毕业院校
|
||||
major: Optional[str]# 专业
|
||||
enroll_date: Optional[date] # 入学时间
|
||||
graduate_date: Optional[date] # 毕业时间
|
||||
education: Optional[str] # 学历
|
||||
advisor_no: Optional[str]# 顾问编号
|
||||
age: Optional[int]# 年龄
|
||||
gender: Optional[Sex] = Field(None, description="性别")# 性别
|
||||
is_deleted: bool# 逻辑删除标记
|
||||
# 分页列表响应体:查询学生列表时返回
|
||||
id: int
|
||||
stu_id: str
|
||||
class_id: Optional[str]
|
||||
stu_name: str
|
||||
native_place: Optional[str]
|
||||
graduate_school: Optional[str]
|
||||
major: Optional[str]
|
||||
enroll_date: Optional[date]
|
||||
graduate_date: Optional[date]
|
||||
education: Optional[str]
|
||||
advisor_no: Optional[str]
|
||||
age: Optional[int]
|
||||
gender: Optional[Sex] = Field(None, description="性别")
|
||||
is_deleted: bool
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
class StudentPageResponse(BaseModel):
|
||||
total: int# 符合条件的总记录数
|
||||
page: int# 当前页码
|
||||
@@ -1,12 +1,13 @@
|
||||
from pydantic import BaseModel,Field
|
||||
from model.teaModel import Sex
|
||||
from model.th_model import Sex
|
||||
|
||||
|
||||
class RequestModel(BaseModel):
|
||||
t_id : int
|
||||
t_name: str=Field(min_length=2,max_length=20)
|
||||
t_sex:Sex
|
||||
t_age: int=Field(gt=0)
|
||||
t_phone: str=Field(min_length=11)
|
||||
t_age: int=Field(gt=0,description='年龄大于0')
|
||||
t_phone: str=Field(min_length=11,description='电话号码')
|
||||
class_id: str
|
||||
|
||||
class ResponseModel(BaseModel):
|
||||
Reference in New Issue
Block a user