diff --git a/PythonProject/api/cla_api.py b/PythonProject/api/cla_api.py index e7d4da1..a57ae60 100644 --- a/PythonProject/api/cla_api.py +++ b/PythonProject/api/cla_api.py @@ -10,8 +10,8 @@ def add_class(data: claRequest, Session = Depends(get_db)): return add_class_dao(data, Session) @claAPI.get("/list", summary="查询班级") -def list_class(Session = Depends(get_db)): - return get_class_dao(Session) +def list_class(n:int,m:int,Session = Depends(get_db)): + return get_class_dao(n,m,Session) @claAPI.put("/{class_id}", summary="更新班级信息") def update_class(class_id: int, data: claRequest, Session = Depends(get_db)): diff --git a/PythonProject/api/employment_api.py b/PythonProject/api/employment_api.py index 58a14d3..6c022bf 100644 --- a/PythonProject/api/employment_api.py +++ b/PythonProject/api/employment_api.py @@ -1,25 +1,53 @@ -from fastapi import APIRouter,Depends,HTTPException +from fastapi import APIRouter,Query,Depends,HTTPException +from util.database import get_db from dao import employment_dao -from dao.employment_dao import get_employment_dao -from schema.employment_schema import EmploymentResponse -from model.all_model import ClassManagement,Employment_info +from dao.employment_dao import get_emp_dao, add_emp_dao, check_stu_dao, update_emp_dao, delete_emp_dao +from schema.employment_schema import EmploymentRequest,EmploymentResponse from util.database import get_db +from typing import Optional emp_api = APIRouter(tags=['学生就业管理系统']) @emp_api.get('/employment/students/{stu_id}',response_model=EmploymentResponse,summary='获取学生就业信息') -def get_emp_info1(stu_id: int,db=Depends(get_db)): - if stu_id: - return employment_dao.get_employment_dao(stu_id=stu_id,db=db) +def get_emp_info1(stu_id: Optional[int]=Query(None,description='学生编号') + ,company_name: Optional[str]=Query(None,description='公司名称') + ,min_salary: Optional[float]=Query(None,description='最低工资') + ,max_salary: Optional[float]=Query(None,description='最高工资') + ,db=Depends(get_db)): + if stu_id or company_name or min_salary or max_salary: + return [employment_dao.get_emp_dao(stu_id=stu_id,db=db)] raise HTTPException(status_code=404, detail='该学生就业信息不存在') @emp_api.get('/employment/class/{class_id}',response_model=EmploymentResponse,summary='获取班级学生就业信息') -def get_emp_info2(class_id: int,db=Depends(get_db)): +def get_emp_info2(class_id: Optional[int],db=Depends(get_db)): if class_id: - return employment_dao.get_employment_dao(class_id=class_id,db=db) + return employment_dao.get_emp_dao(class_id=class_id,db=db) raise HTTPException(status_code=404, detail='该班级就业信息不存在') -# @emp_api.post('/employment/students/{stu_id}') -# def create_emp_info(stu_id: int,db=Depends(get_db),response_model=EmploymentResponse): -# if stu_id: -# -# return employment_dao. +@emp_api.post('/employment/students/{stu_id}',response_model=EmploymentResponse,summary='新增学生就业信息') +def create_emp_info(stu_id:int,emp:EmploymentRequest,db=Depends(get_db)): + if not check_stu_dao(stu_id,db): + raise HTTPException(status_code=404, detail='该学生不存在,无法添加就业信息') + emp.stu_id = stu_id + exist = db.query(employment_dao.Employment_info).filter(employment_dao.Employment_info.stu_id == stu_id,employment_dao.Employment_info.delete_status == 0).first() + if exist: + raise HTTPException(status_code=409,detail='该学生已有就业信息,请勿重复增加!') + d=emp.model_dump(exclude_unset=False) + r=add_emp_dao(d,db) + if not r: + raise HTTPException(status_code=500,detail='服务器繁忙,请稍后添加!') + return r + +@emp_api.put('/employment/students/{stu_id}',response_model=EmploymentResponse,summary='更新学生就业信息') +def update_emp_info(stu_id:int,emp:EmploymentRequest,db=Depends(get_db)): + d = emp.model_dump(exclude_unset=True) + r = update_emp_dao(stu_id,d,db) + if not r: + raise HTTPException(status_code=500,detail='更新失败!') + return {'code':200,'totals':r,'detail':'更新成功!'} + +@emp_api.delete('/employment/{stu_id}',summary='删除学生就业信息') +def delete_emp_info(stu_id:int,db=Depends(get_db)): + rows = delete_emp_dao(stu_id, db) + if not rows: + raise HTTPException(status_code=500, detail='没有删除!') + return {'code':200,'detail':'删除成功!'} diff --git a/PythonProject/api/grade_api.py b/PythonProject/api/grade_api.py index f8ab18b..be2612f 100644 --- a/PythonProject/api/grade_api.py +++ b/PythonProject/api/grade_api.py @@ -6,11 +6,11 @@ from schema.grade_schema import * gradeAPI = APIRouter(tags=['学生考核成绩']) -@gradeAPI.get("/grade/{stu_id}",summary="根据学生id查询成绩") +@gradeAPI.get("/grade/{stu_id}",response_model=list[GradeResponse],summary="根据学生id查询成绩") def get_grade(stu_id: int,db=Depends(get_db)): try: res=get_grade_dao(stu_id,db) - return {'code': 200,'msg':'查询成功','data':res} + return res except Exception as e: raise HTTPException(status_code=500,detail=f'数据库查询异常:{str(e)}') @@ -23,14 +23,36 @@ def post_grade(o:GradeRequest=Form(),db=Depends(get_db)): return o @gradeAPI.put('/{score_id}',summary='成绩修改') -def put_grade(score_id: int,o:GradeUpdate,db=Depends(get_db)): - d = o.model_update(exclude_unset=True) - r = update_grade_dao(score_id=score_id - ,update_data=d - ,db=db) - if not r: - raise HTTPException(status_code=500,detail='没有更新') - return {'code':200,'totals':r,'detail':'更新成功'} +def put_grade( + score_id: int + ,o:GradeUpdate=Form() + ,db=Depends(get_db) +): + old_obj = get_score_by_id_dao(score_id=score_id, db=db) + if not old_obj: + raise HTTPException(status_code=404, detail="成绩记录不存在(已删除)") + + d = o.model_dump(exclude_unset=True) + if not d: + raise HTTPException(status_code=400,detail='请传入需要修改的字段') + if "exam_order" in d: + exist_obj = get_score_by_stu_exam_order_dao( + stu_id=old_obj.stu_id, + exam_order=d["exam_order"], + db=db + ) + if exist_obj and exist_obj.id != score_id: + raise HTTPException(status_code=409, detail="修改后的考核序次,该学生已有成绩!") + + try: + r = update_grade_dao(score_id=score_id, update_data=d, db=db) + except Exception: + raise HTTPException(status_code=409, detail="数据冲突,重复数据") + + if r == 0: + return {"code": 200, "totals": r, "detail": "数据未发生变化"} + + return {"code": 200, "totals": r, "detail": "更新成功"} @gradeAPI.delete('/{score_id}',summary='删除成绩') def delete_grade(score_id:int,db=Depends(get_db)): diff --git a/PythonProject/api/student_api.py b/PythonProject/api/student_api.py index 3341a3a..2295009 100644 --- a/PythonProject/api/student_api.py +++ b/PythonProject/api/student_api.py @@ -6,7 +6,7 @@ from util.database import get_db StudentAPI = APIRouter() -@StudentAPI.get('/students',response_model=list[StudentPageResponse],tags=['学⽣基本信息管理模块'],summary='学生信息查询接口',description='查询学生信息') +@StudentAPI.get('/students',response_model=StudentPageResponse,tags=['学⽣基本信息管理模块'],summary='学生信息查询接口',description='查询学生信息') def get_students(stu_id:int|None=None ,stu_name:str|None=None ,class_id:int|None=None @@ -22,12 +22,10 @@ def get_students(stu_id:int|None=None if not r: raise HTTPException(status_code=404,detail='学生不存在') - return {'code': 200 - , 'detail': 'ok' - , 'page': page - , 'page_size': page_size - , 'total': total - , 'data': r} + return StudentPageResponse(page=page, + page_size=page_size, + total=total, + data=r) @StudentAPI.put('/{stu_id}',tags=['学⽣基本信息管理模块'],summary='学生信息更新接口',description='更新学生信息') def update_students(s:StudentRequest diff --git a/PythonProject/dao/cla_dao.py b/PythonProject/dao/cla_dao.py index 12bc50c..cd53254 100644 --- a/PythonProject/dao/cla_dao.py +++ b/PythonProject/dao/cla_dao.py @@ -15,9 +15,9 @@ def add_class_dao(c:claRequest,session): session.commit() return '添加成功!' -def get_class_dao(session): +def get_class_dao(n,m,session): try: - class_list=session.query(ClassManagement).filter(ClassManagement.delete_status==0).all() + class_list=session.query(ClassManagement).filter(ClassManagement.delete_status==0).offset((n-1)*m).limit(m).all() return class_list except: raise HTTPException(status_code=406,detail="输入查询信息有误,请重新输入!") diff --git a/PythonProject/dao/employment_dao.py b/PythonProject/dao/employment_dao.py index e7a27c0..5ce81a0 100644 --- a/PythonProject/dao/employment_dao.py +++ b/PythonProject/dao/employment_dao.py @@ -1,9 +1,8 @@ from fastapi import HTTPException from model.all_model import Employment_info, ClassManagement, Student_Model -from util.database import get_db -from schema.employment_schema import EmploymentRequest -def get_employment_dao(stu_id,class_id,db): + +def get_emp_dao(stu_id=None,class_id=None,company_name=None,min_salary=None,max_salary=None,db=None): try: q = db.query(Employment_info).\ join(Student_Model,Employment_info.stu_id == Student_Model.stu_id).\ @@ -11,16 +10,24 @@ def get_employment_dao(stu_id,class_id,db): filter(Employment_info.delete_status == 0).\ filter(Student_Model.delete_status == 0).\ filter(ClassManagement.delete_status == 0) - if stu_id is not None: + if stu_id: q = q.filter(Employment_info.stu_id == stu_id) - if class_id is not None: + if class_id: q = q.filter(ClassManagement.class_id == class_id) + if company_name: + q = q.filter(Employment_info.company_name == company_name) + if min_salary: + q = q.filter(Employment_info.salary >= min_salary) + if max_salary: + q = q.filter(Employment_info.salary <= max_salary) + r = q.all() + if r: return [{'emp_id':i.emp_id ,'stu_id': i.stu_id - ,'stu_name':i.stu_name - ,'class_id':i.class_id + ,'stu_name':i.Student_Model.stu_name + ,'class_id':i.Student_Model.class_id ,'email':i.email ,'employment_opening_date':i.employment_opening_date ,'offer_issuance_date':i.offer_issuance_date @@ -32,14 +39,36 @@ def get_employment_dao(stu_id,class_id,db): except: raise HTTPException(status_code=404,detail='信息不存在!') -def add_employment_dao(e:EmploymentRequest,db): +def add_emp_dao(o,db): try: - d = e.model_dump() - e1 = Employment_info(**d) + e1 = Employment_info(**o) db.add(e1) - except: - db.rollback() - raise HTTPException(status_code=404, detail='信息不存在!') - else: db.commit() return e1 + except Exception as e: + db.rollback() + raise HTTPException(status_code=500, detail=f'新增就业信息失败: {e}') + +def check_stu_dao(stu_id,db): + stu = db.query(Student_Model).filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0).first() + return stu + +def update_emp_dao(stu_id,o,db): + try: + rows = db.query(Employment_info).filter(Employment_info.stu_id == stu_id,Employment_info.delete_status == 0).first() + db.commit() + return rows + except: + db.rollback() + raise HTTPException(status_code=404,detail='该就业记录已存在') + + +def delete_emp_dao(stu_id,db): + try: + rows = db.query(Employment_info).filter(Employment_info.stu_id == stu_id,Employment_info.delete_status == 0).delete() + db.commit() + except: + db.rollback() + rows = 0 + finally: + return rows diff --git a/PythonProject/dao/grade_dao.py b/PythonProject/dao/grade_dao.py index a30c5e2..683ec95 100644 --- a/PythonProject/dao/grade_dao.py +++ b/PythonProject/dao/grade_dao.py @@ -4,11 +4,22 @@ from util.database import get_db from model.all_model import * +# 根据id查询单条有效成绩 +def get_score_by_id_dao(score_id:int, db): + return db.query(WlScore).filter( + WlScore.id == score_id + ,WlScore.delete_status==1 + ).first() + +# 根据stu_id+exam_order查重(新增、修改时校验重复) +def get_score_by_stu_exam_order_dao(stu_id:str,exam_order:int,db): + return db.query(WlScore).filter(WlScore.stu_id==stu_id,WlScore.exam_order==exam_order,WlScore.delete_status==0).first() def get_grade_dao( stu_id:int , db): r1 =db.query(WlScore).filter(WlScore.stu_id == stu_id,WlScore.delete_status == 0).all() if r1: + return r1 else: raise HTTPException(status_code=404,detail="学生不存在") @@ -21,15 +32,15 @@ def add_grade_dao(g,db): return o1 except Exception: db.rollback() - return None + raise HTTPException(status_code=500,detail="学生添加失败") def update_grade_dao(score_id:int, update_data, db): try: rows = db.query(WlScore).filter( - WlScore.stu_id == score_id, + WlScore.score_id == score_id, WlScore.delete_status == 0 ).update(update_data) - except: + except Exception: db.rollback() return False else: diff --git a/PythonProject/dao/student_dao.py b/PythonProject/dao/student_dao.py index e896579..e1869bf 100644 --- a/PythonProject/dao/student_dao.py +++ b/PythonProject/dao/student_dao.py @@ -2,22 +2,23 @@ from fastapi import Depends from util.database import get_db from model.all_model import Student_Model from typing import List, Optional, Dict, Any +from datetime import datetime def add_student_dao(o,db): try: o1 = Student_Model( **o) db.add(o1) + db.commit() + stu_id=o1.stu_id + return db.query(Student_Model).filter(Student_Model.stu_id == stu_id).first() except: db.rollback() - return False - else: - db.commit() - return True + return None def delete_student_dao(stu_id,db): try: rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)\ - .update({'delete_status':1}) + .update({'delete_status':1,'delete_time': datetime.now()}) db.commit() except: db.rollback() @@ -52,7 +53,7 @@ def get_student_dao(stu_id:Optional[int] q= q.filter(Student_Model.class_id == class_id,Student_Model.delete_status == 0) total = q.count() r = q.offset((page - 1) * page_size).limit(page_size).all() - return total,r + return r,total except: db.rollback() return [],0 diff --git a/PythonProject/model/all_model.py b/PythonProject/model/all_model.py index 422cb09..45c78ea 100644 --- a/PythonProject/model/all_model.py +++ b/PythonProject/model/all_model.py @@ -184,4 +184,4 @@ class WlScore(Base): delete_time = Column(DATETIME,default=datetime.now,onupdate=datetime.now,comment='删除时间') -Base.metadata.create_all(engine) + diff --git a/PythonProject/schema/grade_schema.py b/PythonProject/schema/grade_schema.py index d669265..e3461ed 100644 --- a/PythonProject/schema/grade_schema.py +++ b/PythonProject/schema/grade_schema.py @@ -1,20 +1,21 @@ + from model.all_model import * from pydantic import BaseModel, Field, field_validator -from fastapi import HTTPException +from fastapi import HTTPException,Form class GradeRequest(BaseModel): - stu_id:str|None = Field(description="学生编号") - exam_order:int = Field(description='考核序次') - score:float = Field(description='成绩') - -class GradeUpdate(BaseModel):#修改请求体 - exam_order: int = Field(description='考核序次') - score: float = Field(description='成绩') + stu_id:int = Form(description="学生编号") + exam_order:int = Form(description='考核序次') + score:float = Form(description='成绩') class GradeResponse(BaseModel): code: int = 200 detail: str = 'OK' - stu_id:str + stu_id:int exam_order:int - score:float \ No newline at end of file + score:float + +class GradeUpdate(BaseModel): + exam_order: int | None = Form( description="考核序次") + score: float | None = Form( description="成绩") diff --git a/PythonProject/schema/student_schema.py b/PythonProject/schema/student_schema.py index 2d9c631..8a16c95 100644 --- a/PythonProject/schema/student_schema.py +++ b/PythonProject/schema/student_schema.py @@ -6,7 +6,7 @@ from pydantic import BaseModel, field_serializer, field_validator,model_validato class StudentRequest(BaseModel): class_id:int |None = None - stu_name:str |None = None + stu_name:str age:int |None = None gender:str |None = None native_place:str |None = None @@ -78,5 +78,5 @@ class StudentPageResponse(BaseModel): detail: str = "ok" page: int page_size: int - total: int + totals: int data: List[StugetResponse] \ No newline at end of file