Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cdaa0f86e | ||
|
|
746eaa9822 | ||
|
|
b7ecd90d22 | ||
|
|
b589557207 | ||
|
|
468b2968c5 | ||
|
|
1a14b19a85 | ||
|
|
fb943b42fc | ||
|
|
e86607c6a9 | ||
|
|
c52f92944f | ||
|
|
5581dd8c7d | ||
|
|
ed85ae2826 | ||
|
|
7cc06e6015 | ||
|
|
fe03827aa3 | ||
|
|
f2b311519b | ||
|
|
d2eb86730c | ||
|
|
a67b66702c | ||
|
|
5f5ae2a1f3 | ||
|
|
ede43a477a | ||
|
|
7f37679969 | ||
|
|
ac165923ec | ||
|
|
d50af7ecc3 | ||
|
|
42130285a3 | ||
|
|
0984210387 | ||
|
|
8a554ad4af | ||
|
|
3fd89f8ba5 | ||
|
|
643ef4ae3d | ||
|
|
a4487eb02c | ||
|
|
e59535422a | ||
|
|
00e5d068b8 | ||
|
|
e307bad1c7 | ||
|
|
00984d5c10 | ||
|
|
a34158bd15 | ||
|
|
22583badc2 | ||
|
|
dcb39957f5 |
@@ -0,0 +1,37 @@
|
|||||||
|
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,45 @@
|
|||||||
|
from dao import get_db
|
||||||
|
from schema import Test
|
||||||
|
from fastapi import APIRouter,Depends
|
||||||
|
from dao import a,a1,a2,a3,a4,a5
|
||||||
|
|
||||||
|
ScoreAPI=APIRouter()
|
||||||
|
ScoreAPI1=APIRouter()
|
||||||
|
@ScoreAPI.post('/scores',tags=['增'])
|
||||||
|
def scores(s:Test,db=Depends(get_db)):
|
||||||
|
a(s,db)
|
||||||
|
return {'code':200,'detail':'添加成功!',}
|
||||||
|
@ScoreAPI.delete('/scores',tags=['删'])
|
||||||
|
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=['改'])
|
||||||
|
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)):
|
||||||
|
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=['软删除'])
|
||||||
|
def scores4(stu_id:int,exam_seq:int,db=Depends(get_db)):
|
||||||
|
r=a4(stu_id, exam_seq, db)
|
||||||
|
if r ==0:
|
||||||
|
return '没有该数据'
|
||||||
|
else:
|
||||||
|
return '已删除'
|
||||||
|
@ScoreAPI1.get('/scores/{stu_id}',tags=['总分,平均分,最大值,最小值'])
|
||||||
|
def scores5(stu_id:int,db=Depends(get_db)):
|
||||||
|
s,r,a=a5(stu_id,db)
|
||||||
|
|
||||||
|
return f'总分:{s},平均分:{s/len(r)},最大值:{max(a)},最小值:{min(a)}'
|
||||||
|
|
||||||
+168
-168
@@ -1,168 +1,168 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from database import get_db
|
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 dao.student_dao import create_student, get_student_by_id, get_student_list, update_student, delete_student_logic
|
||||||
from schema.student_schema import (
|
from schema.student_schema import (
|
||||||
StudentCreateRequest,
|
StudentCreateRequest,
|
||||||
StudentUpdateRequest,
|
StudentUpdateRequest,
|
||||||
StudentQuery,
|
StudentQuery,
|
||||||
StudentResponse,
|
StudentResponse,
|
||||||
StudentPageResponse
|
StudentPageResponse
|
||||||
)
|
)
|
||||||
|
|
||||||
# 创建路由对象 API Router1
|
# 创建路由对象 API Router1
|
||||||
router = APIRouter( tags=["学生管理模块"])
|
router = APIRouter( tags=["学生管理模块"])
|
||||||
@router.post("/", response_model=StudentResponse,summary="新增学生")
|
@router.post("/", response_model=StudentResponse,summary="新增学生")
|
||||||
def add_student(
|
def add_student(
|
||||||
student_req: StudentCreateRequest,
|
student_req: StudentCreateRequest,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
db_stu = create_student(db, student_req)
|
db_stu = create_student(db, student_req)
|
||||||
# 手动转字典:model的stu_id映射响应体的id
|
# 手动转字典:model的stu_id映射响应体的id
|
||||||
stu_dict = {
|
stu_dict = {
|
||||||
"id": db_stu.id,
|
"id": db_stu.id,
|
||||||
"stu_id": db_stu.stu_id,
|
"stu_id": db_stu.stu_id,
|
||||||
"class_id": db_stu.class_id,
|
"class_id": db_stu.class_id,
|
||||||
"stu_name": db_stu.stu_name,
|
"stu_name": db_stu.stu_name,
|
||||||
"native_place": db_stu.native_place,
|
"native_place": db_stu.native_place,
|
||||||
"graduate_school": db_stu.graduate_school,
|
"graduate_school": db_stu.graduate_school,
|
||||||
"major": db_stu.major,
|
"major": db_stu.major,
|
||||||
"enroll_date": db_stu.enroll_date,
|
"enroll_date": db_stu.enroll_date,
|
||||||
"graduate_date": db_stu.graduate_date,
|
"graduate_date": db_stu.graduate_date,
|
||||||
"education": db_stu.education,
|
"education": db_stu.education,
|
||||||
"advisor_no": db_stu.advisor_no,
|
"advisor_no": db_stu.advisor_no,
|
||||||
"age": db_stu.age,
|
"age": db_stu.age,
|
||||||
"gender": db_stu.gender,
|
"gender": db_stu.gender,
|
||||||
"is_deleted": db_stu.is_deleted
|
"is_deleted": db_stu.is_deleted
|
||||||
}
|
}
|
||||||
return StudentResponse(**stu_dict)
|
return StudentResponse(**stu_dict)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/", response_model=StudentPageResponse,summary="根据学号姓名班级查询")
|
@router.get("/", response_model=StudentPageResponse,summary="根据学号姓名班级查询")
|
||||||
def get_student_page(
|
def get_student_page(
|
||||||
query: StudentQuery = Depends(),
|
query: StudentQuery = Depends(),
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
|
|
||||||
total, db_stu_list = get_student_list(
|
total, db_stu_list = get_student_list(
|
||||||
db,
|
db,
|
||||||
stu_id=query.stu_id,
|
stu_id=query.stu_id,
|
||||||
stu_name=query.stu_name,
|
stu_name=query.stu_name,
|
||||||
class_id=query.class_id,
|
class_id=query.class_id,
|
||||||
page=query.page,
|
page=query.page,
|
||||||
page_size=query.page_size
|
page_size=query.page_size
|
||||||
)
|
)
|
||||||
item_list = []
|
item_list = []
|
||||||
for db_stu in db_stu_list:
|
for db_stu in db_stu_list:
|
||||||
stu_dict = {
|
stu_dict = {
|
||||||
"id": db_stu.id,
|
"id": db_stu.id,
|
||||||
"stu_id": db_stu.stu_id,
|
"stu_id": db_stu.stu_id,
|
||||||
"class_id": db_stu.class_id,
|
"class_id": db_stu.class_id,
|
||||||
"stu_name": db_stu.stu_name,
|
"stu_name": db_stu.stu_name,
|
||||||
"native_place": db_stu.native_place,
|
"native_place": db_stu.native_place,
|
||||||
"graduate_school": db_stu.graduate_school,
|
"graduate_school": db_stu.graduate_school,
|
||||||
"major": db_stu.major,
|
"major": db_stu.major,
|
||||||
"enroll_date": db_stu.enroll_date,
|
"enroll_date": db_stu.enroll_date,
|
||||||
"graduate_date": db_stu.graduate_date,
|
"graduate_date": db_stu.graduate_date,
|
||||||
"education": db_stu.education,
|
"education": db_stu.education,
|
||||||
"advisor_no": db_stu.advisor_no,
|
"advisor_no": db_stu.advisor_no,
|
||||||
"age": db_stu.age,
|
"age": db_stu.age,
|
||||||
"gender": db_stu.gender,
|
"gender": db_stu.gender,
|
||||||
"is_deleted": db_stu.is_deleted
|
"is_deleted": db_stu.is_deleted
|
||||||
}
|
}
|
||||||
item_list.append(StudentResponse(**stu_dict))
|
item_list.append(StudentResponse(**stu_dict))
|
||||||
|
|
||||||
return StudentPageResponse(
|
return StudentPageResponse(
|
||||||
total=total,
|
total=total,
|
||||||
page=query.page,
|
page=query.page,
|
||||||
page_size=query.page_size,
|
page_size=query.page_size,
|
||||||
items=item_list
|
items=item_list
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{stu_id}", response_model=StudentResponse,summary="根据id查询")
|
@router.get("/{stu_id}", response_model=StudentResponse,summary="根据id查询")
|
||||||
def get_one_student(
|
def get_one_student(
|
||||||
stu_id: int,
|
stu_id: int,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
#根据主键stu_id查询单个学生
|
#根据主键stu_id查询单个学生
|
||||||
db_stu = get_student_by_id(db, stu_id)
|
db_stu = get_student_by_id(db, stu_id)
|
||||||
if db_stu is None:
|
if db_stu is None:
|
||||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||||
|
|
||||||
stu_dict = {
|
stu_dict = {
|
||||||
"id": db_stu.id,
|
"id": db_stu.id,
|
||||||
"stu_id": db_stu.stu_id,
|
"stu_id": db_stu.stu_id,
|
||||||
"class_id": db_stu.class_id,
|
"class_id": db_stu.class_id,
|
||||||
"stu_name": db_stu.stu_name,
|
"stu_name": db_stu.stu_name,
|
||||||
"native_place": db_stu.native_place,
|
"native_place": db_stu.native_place,
|
||||||
"graduate_school": db_stu.graduate_school,
|
"graduate_school": db_stu.graduate_school,
|
||||||
"major": db_stu.major,
|
"major": db_stu.major,
|
||||||
"enroll_date": db_stu.enroll_date,
|
"enroll_date": db_stu.enroll_date,
|
||||||
"graduate_date": db_stu.graduate_date,
|
"graduate_date": db_stu.graduate_date,
|
||||||
"education": db_stu.education,
|
"education": db_stu.education,
|
||||||
"advisor_no": db_stu.advisor_no,
|
"advisor_no": db_stu.advisor_no,
|
||||||
"age": db_stu.age,
|
"age": db_stu.age,
|
||||||
"gender": db_stu.gender,
|
"gender": db_stu.gender,
|
||||||
"is_deleted": db_stu.is_deleted
|
"is_deleted": db_stu.is_deleted
|
||||||
}
|
}
|
||||||
return StudentResponse(**stu_dict)
|
return StudentResponse(**stu_dict)
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{stu_id}", response_model=StudentResponse,summary="修改学生信息")
|
@router.put("/{stu_id}", response_model=StudentResponse,summary="修改学生信息")
|
||||||
def edit_student(
|
def edit_student(
|
||||||
stu_id: int,
|
stu_id: int,
|
||||||
update_req: StudentUpdateRequest,
|
update_req: StudentUpdateRequest,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
#修改学生信息
|
#修改学生信息
|
||||||
db_stu = update_student(db, stu_id, update_req)
|
db_stu = update_student(db, stu_id, update_req)
|
||||||
if db_stu is None:
|
if db_stu is None:
|
||||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||||
|
|
||||||
stu_dict = {
|
stu_dict = {
|
||||||
"id": db_stu.id,
|
"id": db_stu.id,
|
||||||
"stu_id": db_stu.stu_id,
|
"stu_id": db_stu.stu_id,
|
||||||
"class_id": db_stu.class_id,
|
"class_id": db_stu.class_id,
|
||||||
"stu_name": db_stu.stu_name,
|
"stu_name": db_stu.stu_name,
|
||||||
"native_place": db_stu.native_place,
|
"native_place": db_stu.native_place,
|
||||||
"graduate_school": db_stu.graduate_school,
|
"graduate_school": db_stu.graduate_school,
|
||||||
"major": db_stu.major,
|
"major": db_stu.major,
|
||||||
"enroll_date": db_stu.enroll_date,
|
"enroll_date": db_stu.enroll_date,
|
||||||
"graduate_date": db_stu.graduate_date,
|
"graduate_date": db_stu.graduate_date,
|
||||||
"education": db_stu.education,
|
"education": db_stu.education,
|
||||||
"advisor_no": db_stu.advisor_no,
|
"advisor_no": db_stu.advisor_no,
|
||||||
"age": db_stu.age,
|
"age": db_stu.age,
|
||||||
"gender": db_stu.gender,
|
"gender": db_stu.gender,
|
||||||
"is_deleted": db_stu.is_deleted
|
"is_deleted": db_stu.is_deleted
|
||||||
}
|
}
|
||||||
return StudentResponse(**stu_dict)
|
return StudentResponse(**stu_dict)
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{stu_id}", response_model=StudentResponse,summary="删除学生信息")
|
@router.delete("/{stu_id}", response_model=StudentResponse,summary="删除学生信息")
|
||||||
def remove_student(
|
def remove_student(
|
||||||
stu_id: int,
|
stu_id: int,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
#逻辑删除学生
|
#逻辑删除学生
|
||||||
db_stu = delete_student_logic(db, stu_id)
|
db_stu = delete_student_logic(db, stu_id)
|
||||||
if db_stu is None:
|
if db_stu is None:
|
||||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||||
|
|
||||||
stu_dict = {
|
stu_dict = {
|
||||||
"id": db_stu.id,
|
"id": db_stu.id,
|
||||||
"stu_id": db_stu.stu_id,
|
"stu_id": db_stu.stu_id,
|
||||||
"stu_name": db_stu.stu_name,
|
"stu_name": db_stu.stu_name,
|
||||||
"class_id": db_stu.class_id,
|
"class_id": db_stu.class_id,
|
||||||
"native_place": db_stu.native_place,
|
"native_place": db_stu.native_place,
|
||||||
"graduate_school": db_stu.graduate_school,
|
"graduate_school": db_stu.graduate_school,
|
||||||
"major": db_stu.major,
|
"major": db_stu.major,
|
||||||
"enroll_date": db_stu.enroll_date,
|
"enroll_date": db_stu.enroll_date,
|
||||||
"graduate_date": db_stu.graduate_date,
|
"graduate_date": db_stu.graduate_date,
|
||||||
"education": db_stu.education,
|
"education": db_stu.education,
|
||||||
"advisor_no": db_stu.advisor_no,
|
"advisor_no": db_stu.advisor_no,
|
||||||
"age": db_stu.age,
|
"age": db_stu.age,
|
||||||
"gender": db_stu.gender,
|
"gender": db_stu.gender,
|
||||||
"is_deleted": db_stu.is_deleted
|
"is_deleted": db_stu.is_deleted
|
||||||
}
|
}
|
||||||
return StudentResponse(**stu_dict)
|
return StudentResponse(**stu_dict)
|
||||||
@@ -1,67 +1,67 @@
|
|||||||
from fastapi import APIRouter,Depends,UploadFile,File,Query
|
from fastapi import APIRouter,Depends,UploadFile,File,Query
|
||||||
from model.teaModel import Session
|
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 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 schema.teaSchema import RequestModel, ResponseModel
|
||||||
|
|
||||||
t_router = APIRouter()
|
t_router = APIRouter()
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = Session()
|
db = Session()
|
||||||
try:
|
try:
|
||||||
yield db
|
yield db
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@t_router.get("/teachers",tags=['教师接口信息'],summary='分页查询教师接口')
|
@t_router.get("/teachers",tags=['教师接口信息'],summary='分页查询教师接口')
|
||||||
def get_teachers(p:int=Query(default=0,ge=0,description='页码,0表示查询全部'),
|
def get_teachers(p:int=Query(default=0,ge=0,description='页码,0表示查询全部'),
|
||||||
n:int=Query(default=0,ge=0,description='每页条数,0表示查询全部'),
|
n:int=Query(default=0,ge=0,description='每页条数,0表示查询全部'),
|
||||||
db=Depends(get_db)):
|
db=Depends(get_db)):
|
||||||
db_teachers = get_teachers_limit_dao(p,n,db)
|
db_teachers = get_teachers_limit_dao(p,n,db)
|
||||||
return ResponseModel(data=db_teachers,total=len(db_teachers))
|
return ResponseModel(data=db_teachers,total=len(db_teachers))
|
||||||
|
|
||||||
@t_router.get("/teachers/{name}",tags=['教师接口信息'],summary='分页查询教师接口')
|
@t_router.get("/teachers/{name}",tags=['教师接口信息'],summary='分页查询教师接口')
|
||||||
def get_name_teachers(name:str,db=Depends(get_db)):
|
def get_name_teachers(name:str,db=Depends(get_db)):
|
||||||
db_teachers = get_teachers_name_dao(name,db)
|
db_teachers = get_teachers_name_dao(name,db)
|
||||||
return ResponseModel(data=db_teachers,total=len(db_teachers))
|
return ResponseModel(data=db_teachers,total=len(db_teachers))
|
||||||
|
|
||||||
@t_router.post("/teachers",tags=['教师接口信息'],summary='新增教师接口')
|
@t_router.post("/teachers",tags=['教师接口信息'],summary='新增教师接口')
|
||||||
def insert_teachers(req:RequestModel,db=Depends(get_db)):
|
def insert_teachers(req:RequestModel,db=Depends(get_db)):
|
||||||
rq= req.model_dump()
|
rq= req.model_dump()
|
||||||
n,m = insert_teachers_dao(rq,db)
|
n,m = insert_teachers_dao(rq,db)
|
||||||
# print('打印获取的值:',n,m)
|
# print('打印获取的值:',n,m)
|
||||||
return ResponseModel(total=n,msg=m)
|
return ResponseModel(total=n,msg=m)
|
||||||
|
|
||||||
@t_router.put("/teachers/{t_id}",tags=['教师接口信息'],summary='更新教师接口')
|
@t_router.put("/teachers/{t_id}",tags=['教师接口信息'],summary='更新教师接口')
|
||||||
def update_teachers(req:RequestModel,t_id:int,db=Depends(get_db),):
|
def update_teachers(req:RequestModel,t_id:int,db=Depends(get_db),):
|
||||||
rq= req.model_dump()
|
rq= req.model_dump()
|
||||||
n,m = update_teachers_dao(rq,db,t_id)
|
n,m = update_teachers_dao(rq,db,t_id)
|
||||||
return ResponseModel(total=n,msg=m)
|
return ResponseModel(total=n,msg=m)
|
||||||
|
|
||||||
@t_router.delete("/teachers/{t_id}",tags=['教师接口信息'],summary='删除教师接口')
|
@t_router.delete("/teachers/{t_id}",tags=['教师接口信息'],summary='删除教师接口')
|
||||||
def delete_teachers(t_id:int,db=Depends(get_db)):
|
def delete_teachers(t_id:int,db=Depends(get_db)):
|
||||||
n,m =delete_teachers_dao(t_id,db)
|
n,m =delete_teachers_dao(t_id,db)
|
||||||
return ResponseModel(msg=m,total=n)
|
return ResponseModel(msg=m,total=n)
|
||||||
|
|
||||||
@t_router.post('/files',tags=['文件接口信息'],summary='上传文件接口')
|
@t_router.post('/files',tags=['文件接口信息'],summary='上传文件接口')
|
||||||
async def upload_files(f:UploadFile=File(),db=Depends(get_db)):
|
async def upload_files(f:UploadFile=File(),db=Depends(get_db)):
|
||||||
# print('UploadFile上传文件,读取该对象:',f)
|
# print('UploadFile上传文件,读取该对象:',f)
|
||||||
text =await f.read()
|
text =await f.read()
|
||||||
l =text.decode('utf-8').split('\n')
|
l =text.decode('utf-8').split('\n')
|
||||||
lg = 0
|
lg = 0
|
||||||
dt = []
|
dt = []
|
||||||
print('长度为',lg,l)
|
print('长度为',lg,l)
|
||||||
for i in l:
|
for i in l:
|
||||||
if '名字' in i:
|
if '名字' in i:
|
||||||
continue
|
continue
|
||||||
p = i.split()
|
p = i.split()
|
||||||
if not p: # 判断如果p为[],跳过
|
if not p: # 判断如果p为[],跳过
|
||||||
continue
|
continue
|
||||||
l2 = ['t_name','t_age','t_phone','t_sex','class_id']
|
l2 = ['t_name','t_age','t_phone','t_sex','class_id']
|
||||||
d =dict()
|
d =dict()
|
||||||
for j in range(len(p)):
|
for j in range(len(p)):
|
||||||
d[l2[j]]=p[j]
|
d[l2[j]]=p[j]
|
||||||
insert_teachers_dao(d,db)
|
insert_teachers_dao(d,db)
|
||||||
lg = lg + 1
|
lg = lg + 1
|
||||||
dt.append(d)
|
dt.append(d)
|
||||||
return ResponseModel(msg='上传成功',total=lg,data=dt)
|
return ResponseModel(msg='上传成功',total=lg,data=dt)
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
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)
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
from http.client import HTTPException
|
||||||
|
|
||||||
|
from model.e_model import Employment
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
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 wq_employment_dao(o,db):
|
||||||
|
try:
|
||||||
|
o1 = Employment(**o)
|
||||||
|
db.add(o1)
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=500, detail=f'添加失败:str(e)')
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
|
return 1
|
||||||
|
|
||||||
|
def upd_employment_dao(e,db):
|
||||||
|
try:
|
||||||
|
rows = db.query(Employment).filter(Employment.stu_id == e['stu_id']).update(e)
|
||||||
|
except:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=500, detail='更新异常,请稍后再执行!')
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
|
return 1
|
||||||
|
|
||||||
|
def del_employment_dao(stu_id,db):
|
||||||
|
try:
|
||||||
|
row = db.query(Employment).filter(Employment.stu_id == stu_id).all()
|
||||||
|
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})
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}')
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
|
return 1
|
||||||
|
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
from model import Session
|
||||||
|
from 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()
|
||||||
|
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})
|
||||||
|
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
|
||||||
|
a = []
|
||||||
|
for i in r:
|
||||||
|
s += i.score
|
||||||
|
a.append(i.score)
|
||||||
|
return s,r,a
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+123
@@ -0,0 +1,123 @@
|
|||||||
|
from sqlalchemy import func, desc
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from model.Employment import Employment
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from model.Students import Student
|
||||||
|
from model.Score import Scores
|
||||||
|
|
||||||
|
# ① 超30岁学员
|
||||||
|
def fun1(db):
|
||||||
|
try:
|
||||||
|
rows = db.query(Student).filter(Student.age>30).all()
|
||||||
|
return [{'id': i.stu_id, 'name': i.name} for i in rows]
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400,detail=f'查询超三十岁学员信息失败:{str(e)}')
|
||||||
|
|
||||||
|
# ② 各班人数 + 男女比
|
||||||
|
def fun2(db):
|
||||||
|
try:
|
||||||
|
rows = db.query(
|
||||||
|
Student.stu_class,
|
||||||
|
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]
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400,detail=f'查询各班人数与男女比例失败:{str(e)}')
|
||||||
|
|
||||||
|
# ③ 每次考试都≥80分的学生
|
||||||
|
def fun3(db):
|
||||||
|
try:
|
||||||
|
print('1111111111-----')
|
||||||
|
rows = db.query(
|
||||||
|
Scores.stu_id,
|
||||||
|
Student.name,
|
||||||
|
func.min(Scores.score).label('min_score')
|
||||||
|
).join(Student, Student.stu_id == Scores.stu_id) \
|
||||||
|
.group_by(Scores.stu_id, Student.name) \
|
||||||
|
.having(func.min(Scores.score) >= 80).all()
|
||||||
|
t = ['stu_id', 'name', 'min_score']
|
||||||
|
l = [dict(zip(t, row)) for row in rows]
|
||||||
|
return l
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400,detail=f'查询成绩大于80的学生失败:{str(e)}')
|
||||||
|
|
||||||
|
# ④ 两次以上不及格的学生
|
||||||
|
def fun4(db):
|
||||||
|
try:
|
||||||
|
rows = db.query(
|
||||||
|
Student.name,
|
||||||
|
Student.stu_class,
|
||||||
|
).join(Scores, Scores.stu_id == Student.stu_id) \
|
||||||
|
.filter(Scores.score < 60) \
|
||||||
|
.group_by(Scores.stu_id,Student.name,Student.stu_class) \
|
||||||
|
.having(func.count(Scores.id) >= 2).all()
|
||||||
|
t = ['name', 'stu_class']
|
||||||
|
l = [dict(zip(t, row)) for row in rows]
|
||||||
|
return l
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400,detail=f'查询两次不及格的学生失败:{str(e)}')
|
||||||
|
|
||||||
|
# ⑤ 每次考试各班平均分(降序)
|
||||||
|
def fun5(db):
|
||||||
|
try:
|
||||||
|
print('5555555555')
|
||||||
|
rows = db.query(
|
||||||
|
Scores.exam_seq,
|
||||||
|
Student.stu_class,
|
||||||
|
func.avg(Scores.score).label('avg_score')
|
||||||
|
).join(Student, Student.stu_id == Scores.stu_id) \
|
||||||
|
.group_by(Scores.exam_seq, Student.stu_class) \
|
||||||
|
.order_by(desc('avg_score')).all()
|
||||||
|
t = ['exam_seq', 'stu_class','avg_score']
|
||||||
|
l = [dict(zip(t, row)) for row in rows]
|
||||||
|
return l
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400, detail=f'查询各班平均分失败:{str(e)}')
|
||||||
|
|
||||||
|
# ⑥ 薪资前五名
|
||||||
|
def fun6(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']
|
||||||
|
l = [dict(zip(t, row)) for row in rows]
|
||||||
|
print(l)
|
||||||
|
return {'code': 200, 'data': l}
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400,detail=f'查询薪资前五名失败失败:{str(e)}')
|
||||||
|
|
||||||
|
# ⑦ 每个学生就业时长
|
||||||
|
def fun7(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']
|
||||||
|
l = [dict(zip(t, row)) for row in rows]
|
||||||
|
return {'code': 200, 'data': l}
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400,detail=f'查询每个学生的就业时长失败:{str(e)}')
|
||||||
|
|
||||||
|
# ⑧ 各班平均就业时长
|
||||||
|
def fun8(db):
|
||||||
|
try:
|
||||||
|
rows = db.query(
|
||||||
|
Student.stu_class,
|
||||||
|
func.avg(func.datediff(Employment.offer_date, Employment.open_date)).label('avg_duration')
|
||||||
|
).join(Student, Student.stu_id == Employment.s_id).filter(
|
||||||
|
Employment.open_date.isnot(None)
|
||||||
|
).group_by(Student.stu_class).all()
|
||||||
|
t = ['stu_class', 'avg_duration']
|
||||||
|
l = [dict(zip(t, row)) for row in rows]
|
||||||
|
return l
|
||||||
|
except Exception as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=400,detail=f'查询各班平均就业时长失败:{str(e)}')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,57 +1,57 @@
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from model.student_model import Student
|
from model.student_model import Student
|
||||||
from schema.student_schema import StudentCreateRequest,StudentUpdateRequest
|
from schema.student_schema import StudentCreateRequest,StudentUpdateRequest
|
||||||
def create_student(db: Session, student_create: StudentCreateRequest):
|
def create_student(db: Session, student_create: StudentCreateRequest):
|
||||||
"""
|
"""
|
||||||
新增学生
|
新增学生
|
||||||
: db: 数据库会话
|
: db: 数据库会话
|
||||||
:student_c reate: 前端传来的新增请求体
|
:student_c reate: 前端传来的新增请求体
|
||||||
"""
|
"""
|
||||||
db_student = Student(**student_create.model_dump())#请求体转为字典,再解包创建实体对象
|
db_student = Student(**student_create.model_dump())#请求体转为字典,再解包创建实体对象
|
||||||
db.add(db_student)#添加
|
db.add(db_student)#添加
|
||||||
db.commit()#提交事务
|
db.commit()#提交事务
|
||||||
new_student=db.query(Student).filter(
|
new_student=db.query(Student).filter(
|
||||||
Student.stu_id==student_create.stu_id,
|
Student.stu_id==student_create.stu_id,
|
||||||
Student.is_deleted==False).first()
|
Student.is_deleted==False).first()
|
||||||
#查询 Student 表,取第一条,没有则返回 None
|
#查询 Student 表,取第一条,没有则返回 None
|
||||||
return new_student
|
return new_student
|
||||||
def get_student_by_id(db: Session, stu_id: int):#根据stu_id查询单个学生
|
def get_student_by_id(db: Session, stu_id: int):#根据stu_id查询单个学生
|
||||||
query = db.query(Student).filter(
|
query = db.query(Student).filter(
|
||||||
Student.stu_id == stu_id,
|
Student.stu_id == stu_id,
|
||||||
Student.is_deleted == False)
|
Student.is_deleted == False)
|
||||||
return query.first()
|
return query.first()
|
||||||
def get_student_list(db: Session, stu_id:None,
|
def get_student_list(db: Session, stu_id:None,
|
||||||
class_id:str=None, page:int=1, page_size:int=10, stu_name:str=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)
|
query = db.query(Student).filter(Student.is_deleted == False)
|
||||||
if stu_id:
|
if stu_id:
|
||||||
query = query.filter(Student.stu_id == stu_id)
|
query = query.filter(Student.stu_id == stu_id)
|
||||||
if stu_name:
|
if stu_name:
|
||||||
query = query.filter(Student.stu_name == stu_name)
|
query = query.filter(Student.stu_name == stu_name)
|
||||||
if class_id:
|
if class_id:
|
||||||
query = query.filter(Student.class_id == class_id)
|
query = query.filter(Student.class_id == class_id)
|
||||||
total = query.count()#统计满足条件的记录
|
total = query.count()#统计满足条件的记录
|
||||||
offset = (page - 1) * page_size# 计算分页偏移量:跳过前面 (page-1)*page_size 条
|
offset = (page - 1) * page_size# 计算分页偏移量:跳过前面 (page-1)*page_size 条
|
||||||
db_student_list = query.offset(offset).limit(page_size).all()# 执行分页查询:跳过offset条,取page_size条
|
db_student_list = query.offset(offset).limit(page_size).all()# 执行分页查询:跳过offset条,取page_size条
|
||||||
return total, db_student_list
|
return total, db_student_list
|
||||||
def update_student(db: Session, stu_id:int,student_update: StudentUpdateRequest):
|
def update_student(db: Session, stu_id:int,student_update: StudentUpdateRequest):
|
||||||
"""修改学生信息
|
"""修改学生信息
|
||||||
db: 数据库会话stu_id: 要修改的学生主键student_update: 前端传来的修改请求体:
|
db: 数据库会话stu_id: 要修改的学生主键student_update: 前端传来的修改请求体:
|
||||||
"""
|
"""
|
||||||
db_student = get_student_by_id(db=db,stu_id=stu_id)
|
db_student = get_student_by_id(db=db,stu_id=stu_id)
|
||||||
if not db_student:
|
if not db_student:
|
||||||
return None#先查学生,查不到报错
|
return None#先查学生,查不到报错
|
||||||
update_data=student_update.model_dump(exclude_unset=True)#只解析前端输入的键值对,默认值不会解析
|
update_data=student_update.model_dump(exclude_unset=True)#只解析前端输入的键值对,默认值不会解析
|
||||||
for k,v in update_data.items():
|
for k,v in update_data.items():
|
||||||
setattr(db_student,k,v)#把 value 赋给 db_student 的 key 属性。
|
setattr(db_student,k,v)#把 value 赋给 db_student 的 key 属性。
|
||||||
db.commit()
|
db.commit()
|
||||||
updated_student=get_student_by_id(db, stu_id)
|
updated_student=get_student_by_id(db, stu_id)
|
||||||
return updated_student
|
return updated_student
|
||||||
def delete_student_logic(db: Session, stu_id:int):
|
def delete_student_logic(db: Session, stu_id:int):
|
||||||
db_student = get_student_by_id(db, stu_id)
|
db_student = get_student_by_id(db, stu_id)
|
||||||
if not db_student:
|
if not db_student:
|
||||||
return None
|
return None
|
||||||
db_student.is_deleted = True#把逻辑删除标记改成True
|
db_student.is_deleted = True#把逻辑删除标记改成True
|
||||||
db.commit()#重新查询,返回标记已删除的这条记录
|
db.commit()#重新查询,返回标记已删除的这条记录
|
||||||
deleted_student = db.query(Student).filter(Student.stu_id == stu_id).first()
|
deleted_student = db.query(Student).filter(Student.stu_id == stu_id).first()
|
||||||
return deleted_student
|
return deleted_student
|
||||||
@@ -1,63 +1,63 @@
|
|||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from model.teaModel import Teacher
|
from model.teaModel import Teacher
|
||||||
|
|
||||||
def get_teachers_limit_dao(p,n,db):
|
def get_teachers_limit_dao(p,n,db):
|
||||||
if p == 0 and n == 0:
|
if p == 0 and n == 0:
|
||||||
r = db.query(Teacher).all()
|
r = db.query(Teacher).all()
|
||||||
else:
|
else:
|
||||||
if p < 1:
|
if p < 1:
|
||||||
p = 1
|
p = 1
|
||||||
r = db.query(Teacher).offset((p-1)*n).limit(n).all()
|
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]
|
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):
|
def get_teachers_name_dao(name,db):
|
||||||
try:
|
try:
|
||||||
r = db.query(Teacher).filter(Teacher.t_name==name).all()
|
r = db.query(Teacher).filter(Teacher.t_name==name).all()
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(status_code=500, detail=f'查询失败:{str(e)}')
|
raise HTTPException(status_code=500, detail=f'查询失败:{str(e)}')
|
||||||
else:
|
else:
|
||||||
return [ {'id':i.id,'t_name':i.t_name,'t_sex':i.t_sex,'t_age':i.t_age} for i in r]
|
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):
|
def insert_teachers_dao(req,db):
|
||||||
try:
|
try:
|
||||||
exist = db.query(Teacher).filter(Teacher.t_phone==req['t_phone']).first()
|
exist = db.query(Teacher).filter(Teacher.t_phone==req['t_phone']).first()
|
||||||
if exist:
|
if exist:
|
||||||
raise HTTPException(status_code=400,detail='该手机号码已存在,不可重复使用')
|
raise HTTPException(status_code=400,detail='该手机号码已存在,不可重复使用')
|
||||||
t = Teacher(**req)
|
t = Teacher(**req)
|
||||||
db.add(t)
|
db.add(t)
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(status_code=500,detail=f'添加失败:{str(e)}')
|
raise HTTPException(status_code=500,detail=f'添加失败:{str(e)}')
|
||||||
else:
|
else:
|
||||||
db.commit()
|
db.commit()
|
||||||
return True,'添加成功'
|
return True,'添加成功'
|
||||||
|
|
||||||
def update_teachers_dao(req,db,t_id):
|
def update_teachers_dao(req,db,t_id):
|
||||||
try:
|
try:
|
||||||
db.query(Teacher).filter(Teacher.id == t_id).update(req)
|
db.query(Teacher).filter(Teacher.id == t_id).update(req)
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(status_code=500,detail=f'更新失败:{str(e)}')
|
raise HTTPException(status_code=500,detail=f'更新失败:{str(e)}')
|
||||||
else:
|
else:
|
||||||
db.commit()
|
db.commit()
|
||||||
return True,'更新成功'
|
return True,'更新成功'
|
||||||
|
|
||||||
def delete_teachers_dao(t_id,db):
|
def delete_teachers_dao(t_id,db):
|
||||||
try:
|
try:
|
||||||
r = db.query(Teacher).filter(Teacher.id == t_id).all()
|
r = db.query(Teacher).filter(Teacher.id == t_id).all()
|
||||||
if not r:
|
if not r:
|
||||||
raise HTTPException(status_code=404,detail='记录不存在')
|
raise HTTPException(status_code=404,detail='记录不存在')
|
||||||
for i in r:
|
for i in r:
|
||||||
if i.is_delete == 0:
|
if i.is_delete == 0:
|
||||||
db.query(Teacher).filter(Teacher.id == i.id).update({'is_delete':True})
|
db.query(Teacher).filter(Teacher.id == i.id).update({'is_delete':True})
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=400,detail='该记录已删除,不可重复删除')
|
raise HTTPException(status_code=400,detail='该记录已删除,不可重复删除')
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}')
|
raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}')
|
||||||
else:
|
else:
|
||||||
db.commit()
|
db.commit()
|
||||||
return True,'删除成功'
|
return True,'删除成功'
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
Session = sessionmaker(bind=engine,autoflush=False,autocommit=False)
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
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()
|
||||||
|
|
||||||
|
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')
|
||||||
|
, unique=True
|
||||||
|
, nullable=True
|
||||||
|
, comment='学生学号,唯一')
|
||||||
|
class_name = Column(String(32))
|
||||||
|
company = Column(String(100))
|
||||||
|
salary = Column(Integer
|
||||||
|
, default=0)
|
||||||
|
open_date = Column(DATETIME
|
||||||
|
, default=datetime.now())
|
||||||
|
offer_date = Column(DATETIME
|
||||||
|
, default=datetime.now())
|
||||||
|
work_date = Column(DATETIME
|
||||||
|
, default=datetime.now()
|
||||||
|
, onupdate=datetime.now)
|
||||||
|
is_delete = Column(Integer
|
||||||
|
, default=0
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Session = sessionmaker( bind = engine, autoflush = False, autocommit = False )
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = Session()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
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()
|
||||||
|
class Scores(Base):
|
||||||
|
__tablename__ = 's_scores'
|
||||||
|
id=Column( Integer
|
||||||
|
, primary_key=True
|
||||||
|
,autoincrement=True
|
||||||
|
,comment='成绩编号'
|
||||||
|
)
|
||||||
|
stu_id=Column(Integer
|
||||||
|
,comment='学生编号'
|
||||||
|
, nullable=False
|
||||||
|
)
|
||||||
|
exam_seq=Column(Integer
|
||||||
|
|
||||||
|
, nullable=True
|
||||||
|
,comment='考试序次'
|
||||||
|
)
|
||||||
|
score=Column(Float
|
||||||
|
,comment='分数'
|
||||||
|
)
|
||||||
|
create_date=Column(DATETIME
|
||||||
|
, default=datetime.now
|
||||||
|
,comment='创建时间'
|
||||||
|
)
|
||||||
|
update_date=Column(DATETIME
|
||||||
|
, default=datetime.now
|
||||||
|
,onupdate=datetime.now
|
||||||
|
,comment='更新时间'
|
||||||
|
)
|
||||||
|
deleted=Column(Integer
|
||||||
|
,default=1
|
||||||
|
, comment='软删除'
|
||||||
|
)
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
Session = sessionmaker( bind=engine
|
||||||
|
,autoflush=False
|
||||||
|
,autocommit = False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -1,25 +1,25 @@
|
|||||||
from database import Base
|
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
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
class Sex(Enum):
|
class Sex(Enum):
|
||||||
m= "男"
|
m= "男"
|
||||||
w= "女"
|
w= "女"
|
||||||
class Student(Base):
|
class Student(Base):
|
||||||
__tablename__ = 's_student'
|
__tablename__ = 's_student'
|
||||||
id=Column(Integer, primary_key=True,autoincrement=True,comment="数据库主键id")
|
id=Column(Integer, primary_key=True,autoincrement=True,comment="数据库主键id")
|
||||||
stu_id=Column(String(50),unique=True,nullable=False,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='学生班级')
|
comment='学生班级')
|
||||||
stu_name=Column(String(30),nullable=False,comment='学生姓名')
|
stu_name=Column(String(30),nullable=False,comment='学生姓名')
|
||||||
native_place = Column(String(100), comment="籍贯")
|
native_place = Column(String(100), comment="籍贯")
|
||||||
graduate_school = Column(String(100), comment="毕业院校")
|
graduate_school = Column(String(100), comment="毕业院校")
|
||||||
major = Column(String(50), comment="专业")
|
major = Column(String(50), comment="专业")
|
||||||
enroll_date = Column(Date, comment="入学时间")
|
enroll_date = Column(Date, comment="入学时间")
|
||||||
graduate_date = Column(Date, comment="毕业时间")
|
graduate_date = Column(Date, comment="毕业时间")
|
||||||
education = Column(String(30), comment="学历")
|
education = Column(String(30), comment="学历")
|
||||||
advisor_no = Column(String(50), comment="顾问编号")
|
advisor_no = Column(String(50), comment="顾问编号")
|
||||||
age = Column(Integer, comment="年龄")
|
age = Column(Integer, comment="年龄")
|
||||||
gender = Column(
|
gender = Column(
|
||||||
SQLEnum(Sex, values_callable=lambda x: [e.value for e in x]),
|
SQLEnum(Sex, values_callable=lambda x: [e.value for e in x]),
|
||||||
comment="性 别")
|
comment="性 别")
|
||||||
is_deleted = Column(Boolean, default=False, comment="逻辑删除标记:False未删除,True已删除")
|
is_deleted = Column(Boolean, default=False, comment="逻辑删除标记:False未删除,True已删除")
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
from sqlalchemy import Integer, String, Column, DateTime, Enum as SQLEnum,Boolean,create_engine
|
from sqlalchemy import Integer, String, Column, DateTime, Enum as SQLEnum,Boolean,create_engine
|
||||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
tea_url ='mysql+pymysql://root:123456@127.0.0.1:3306/ai0824_stu?charset=utf8'
|
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)
|
engine = create_engine(tea_url,pool_size=100, echo=False)
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
class Sex(Enum):
|
class Sex(Enum):
|
||||||
w = '女'
|
w = '女'
|
||||||
m = '男'
|
m = '男'
|
||||||
|
|
||||||
class Teacher(Base):
|
class Teacher(Base):
|
||||||
__tablename__ = 's_teacher'
|
__tablename__ = 's_teacher'
|
||||||
id =Column(Integer, primary_key=True,autoincrement=True)
|
id =Column(Integer, primary_key=True,autoincrement=True)
|
||||||
t_name = Column(String(100),comment='教师名称')
|
t_name = Column(String(100),comment='教师名称')
|
||||||
t_age = Column(Integer,comment='年龄')
|
t_age = Column(Integer,comment='年龄')
|
||||||
t_phone = Column(String(20),unique=True,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='性别')
|
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),comment='带班编号')
|
||||||
create_time = Column(DateTime,default=datetime.now,comment='创建时间')
|
create_time = Column(DateTime,default=datetime.now,comment='创建时间')
|
||||||
update_time = Column(DateTime,default=datetime.now,onupdate=datetime.now,comment='更新时间')
|
update_time = Column(DateTime,default=datetime.now,onupdate=datetime.now,comment='更新时间')
|
||||||
is_delete = Column(Boolean,default=False,comment='判断是否删除')
|
is_delete = Column(Boolean,default=False,comment='判断是否删除')
|
||||||
|
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
Session = sessionmaker(bind=engine,autoflush=False,autocommit=False)
|
Session = sessionmaker(bind=engine,autoflush=False,autocommit=False)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class EmploymentRequest(BaseModel):
|
||||||
|
stu_id: str|None = None
|
||||||
|
class_name: str|None = None
|
||||||
|
company: Optional[str] = None
|
||||||
|
salary: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
class Test(BaseModel):
|
||||||
|
stu_id:int
|
||||||
|
exam_seq:int
|
||||||
|
score:float
|
||||||
@@ -1,58 +1,58 @@
|
|||||||
from typing import Optional,List
|
from typing import Optional,List
|
||||||
from pydantic import BaseModel,Field
|
from pydantic import BaseModel,Field
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from model.student_model import Sex
|
from model.student_model import Sex
|
||||||
##================ =====请求体===============
|
##================ =====请求体===============
|
||||||
class StudentCreateRequest(BaseModel):
|
class StudentCreateRequest(BaseModel):
|
||||||
stu_id:str=Field(...,description="学生id")
|
stu_id:str=Field(...,description="学生id")
|
||||||
class_id:Optional[str]=Field(None,description="学生班级id")
|
class_id:Optional[str]=Field(None,description="学生班级id")
|
||||||
stu_name: str = Field(..., description="学生姓名")
|
stu_name: str = Field(..., description="学生姓名")
|
||||||
native_place: Optional[str] = Field(None, description="籍贯")
|
native_place: Optional[str] = Field(None, description="籍贯")
|
||||||
graduate_school: Optional[str] = Field(None, description="毕业院校")
|
graduate_school: Optional[str] = Field(None, description="毕业院校")
|
||||||
major: Optional[str] = Field(None, description="专业")
|
major: Optional[str] = Field(None, description="专业")
|
||||||
enroll_date: Optional[date] = Field(None, description="入学时间")
|
enroll_date: Optional[date] = Field(None, description="入学时间")
|
||||||
graduate_date: Optional[date] = Field(None, description="毕业时间")
|
graduate_date: Optional[date] = Field(None, description="毕业时间")
|
||||||
education: Optional[str] = Field(None, description="学历")
|
education: Optional[str] = Field(None, description="学历")
|
||||||
advisor_no: Optional[str] = Field(None, description="顾问编号")
|
advisor_no: Optional[str] = Field(None, description="顾问编号")
|
||||||
age: Optional[int] = Field(None, ge=0, description="年龄,不能负数")
|
age: Optional[int] = Field(None, ge=0, description="年龄,不能负数")
|
||||||
gender: Optional[Sex] = Field(None, description="性别")
|
gender: Optional[Sex] = Field(None, description="性别")
|
||||||
class StudentUpdateRequest(BaseModel):
|
class StudentUpdateRequest(BaseModel):
|
||||||
stu_id:Optional[str]=None#学生id
|
stu_id:Optional[str]=None#学生id
|
||||||
class_id: Optional[str] = None # 学生班级
|
class_id: Optional[str] = None # 学生班级
|
||||||
stu_name: Optional[str] = None # 学生姓名
|
stu_name: Optional[str] = None # 学生姓名
|
||||||
native_place: Optional[str] = None # 籍贯
|
native_place: Optional[str] = None # 籍贯
|
||||||
graduate_school: Optional[str] = None # 毕业院校
|
graduate_school: Optional[str] = None # 毕业院校
|
||||||
major: Optional[str] = None # 专业
|
major: Optional[str] = None # 专业
|
||||||
enroll_date: Optional[date] = None # 入学时间
|
enroll_date: Optional[date] = None # 入学时间
|
||||||
graduate_date: Optional[date] = None # 毕业时间
|
graduate_date: Optional[date] = None # 毕业时间
|
||||||
education: Optional[str] = None # 学历
|
education: Optional[str] = None # 学历
|
||||||
advisor_no: Optional[str] = None # 顾问编号
|
advisor_no: Optional[str] = None # 顾问编号
|
||||||
age: Optional[int] = Field(None, ge=0) # 年龄,不能负数
|
age: Optional[int] = Field(None, ge=0) # 年龄,不能负数
|
||||||
gender: Optional[Sex] = Field(None, description="性别") # 性别'''
|
gender: Optional[Sex] = Field(None, description="性别") # 性别'''
|
||||||
class StudentQuery(BaseModel):
|
class StudentQuery(BaseModel):
|
||||||
stu_id: Optional[str] = None # 学生编号筛选
|
stu_id: Optional[str] = None # 学生编号筛选
|
||||||
stu_name: Optional[str] = None # 学生姓名筛选
|
stu_name: Optional[str] = None # 学生姓名筛选
|
||||||
class_id: Optional[str] = None # 班级筛选
|
class_id: Optional[str] = None # 班级筛选
|
||||||
page: int = Field(1, ge=1, description="页码,默认第1页")
|
page: int = Field(1, ge=1, description="页码,默认第1页")
|
||||||
page_size: int = Field(10, ge=1, le=100, description="每页条数,默认10条")
|
page_size: int = Field(10, ge=1, le=100, description="每页条数,默认10条")
|
||||||
##=====================响应体===============
|
##=====================响应体===============
|
||||||
class StudentResponse(BaseModel):
|
class StudentResponse(BaseModel):
|
||||||
stu_id: str# 学生编号
|
stu_id: str# 学生编号
|
||||||
class_id: Optional[str]# 学生班级
|
class_id: Optional[str]# 学生班级
|
||||||
stu_name: str# 学生姓名
|
stu_name: str# 学生姓名
|
||||||
native_place: Optional[str] # 籍贯
|
native_place: Optional[str] # 籍贯
|
||||||
graduate_school: Optional[str] # 毕业院校
|
graduate_school: Optional[str] # 毕业院校
|
||||||
major: Optional[str]# 专业
|
major: Optional[str]# 专业
|
||||||
enroll_date: Optional[date] # 入学时间
|
enroll_date: Optional[date] # 入学时间
|
||||||
graduate_date: Optional[date] # 毕业时间
|
graduate_date: Optional[date] # 毕业时间
|
||||||
education: Optional[str] # 学历
|
education: Optional[str] # 学历
|
||||||
advisor_no: Optional[str]# 顾问编号
|
advisor_no: Optional[str]# 顾问编号
|
||||||
age: Optional[int]# 年龄
|
age: Optional[int]# 年龄
|
||||||
gender: Optional[Sex] = Field(None, description="性别")# 性别
|
gender: Optional[Sex] = Field(None, description="性别")# 性别
|
||||||
is_deleted: bool# 逻辑删除标记
|
is_deleted: bool# 逻辑删除标记
|
||||||
# 分页列表响应体:查询学生列表时返回
|
# 分页列表响应体:查询学生列表时返回
|
||||||
class StudentPageResponse(BaseModel):
|
class StudentPageResponse(BaseModel):
|
||||||
total: int# 符合条件的总记录数
|
total: int# 符合条件的总记录数
|
||||||
page: int# 当前页码
|
page: int# 当前页码
|
||||||
page_size: int# 每页数据条数
|
page_size: int# 每页数据条数
|
||||||
items: List[StudentResponse] # 当前页学生数据列表
|
items: List[StudentResponse] # 当前页学生数据列表
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
from pydantic import BaseModel,Field
|
from pydantic import BaseModel,Field
|
||||||
from model.teaModel import Sex
|
from model.teaModel import Sex
|
||||||
|
|
||||||
|
|
||||||
class RequestModel(BaseModel):
|
class RequestModel(BaseModel):
|
||||||
t_name: str=Field(min_length=2,max_length=20)
|
t_name: str=Field(min_length=2,max_length=20)
|
||||||
t_sex:Sex
|
t_sex:Sex
|
||||||
t_age: int=Field(gt=0)
|
t_age: int=Field(gt=0)
|
||||||
t_phone: str=Field(min_length=11)
|
t_phone: str=Field(min_length=11)
|
||||||
class_id: str
|
class_id: str
|
||||||
|
|
||||||
class ResponseModel(BaseModel):
|
class ResponseModel(BaseModel):
|
||||||
code: int = 200
|
code: int = 200
|
||||||
msg: str = '操作成功'
|
msg: str = '操作成功'
|
||||||
total: int = 0
|
total: int = 0
|
||||||
data: list=Field(default=[])
|
data: list=Field(default=[])
|
||||||
Reference in New Issue
Block a user