From a2688dd41c17778784acf657eb23cc722c29d571 Mon Sep 17 00:00:00 2001 From: wolin_chenyulin001 <123@163.com> Date: Mon, 21 Sep 2026 23:23:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=B1=E4=B8=9Aget=E6=AC=A1=E6=98=82?= =?UTF-8?q?=E7=AE=A1=E7=9A=84dao=E5=B1=82=E5=92=8Capi=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PythonProject/api/employment_api.py | 25 +++++++++++++++++-------- PythonProject/dao/employment_dao.py | 17 +++++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/PythonProject/api/employment_api.py b/PythonProject/api/employment_api.py index acb8e61..2b827c3 100644 --- a/PythonProject/api/employment_api.py +++ b/PythonProject/api/employment_api.py @@ -1,23 +1,30 @@ -from fastapi import APIRouter,Depends,HTTPException +from fastapi import APIRouter,Query,Depends,HTTPException +from starlette.middleware.sessions import Session + from dao import employment_dao 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: +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:Session=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:Session=Depends(get_db)): if class_id: 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}',response_model=EmploymentResponse,summary='新增学生就业信息') -def create_emp_info(stu_id:int,emp:EmploymentRequest,db=Depends(get_db)): +def create_emp_info(stu_id:int,emp:EmploymentRequest,db:Session=Depends(get_db)): if not check_stu_dao(stu_id,db): raise HTTPException(status_code=404, detail='该学生不存在,无法添加就业信息') emp.stu_id = stu_id @@ -31,7 +38,7 @@ def create_emp_info(stu_id:int,emp:EmploymentRequest,db=Depends(get_db)): 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)): +def update_emp_info(stu_id:int,emp:EmploymentRequest,db:Session=Depends(get_db)): d = emp.model_dump(exclude_unset=True) r = update_emp_dao(stu_id,d,db) if not r: @@ -39,6 +46,8 @@ def update_emp_info(stu_id:int,emp:EmploymentRequest,db=Depends(get_db)): return {'code':200,'totals':r,'detail':'更新成功!'} @emp_api.delete('/employment/{stu_id}',summary='删除学生就业信息') -def delete_emp_info(stu_id:int,db=Depends(get_db)): - delete_emp_dao(stu_id,db) +def delete_emp_info(stu_id:int,db:Session=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/dao/employment_dao.py b/PythonProject/dao/employment_dao.py index dc7cacc..5ce81a0 100644 --- a/PythonProject/dao/employment_dao.py +++ b/PythonProject/dao/employment_dao.py @@ -2,7 +2,7 @@ from fastapi import HTTPException from model.all_model import Employment_info, ClassManagement, Student_Model -def get_emp_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).\ @@ -10,11 +10,19 @@ def get_emp_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 @@ -45,9 +53,6 @@ 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 check_emp_dao(stu_id=): - - 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()