From bf44ac9450da9976c44e71853b9d94bacef5165b Mon Sep 17 00:00:00 2001 From: maruizhi <3088243241@qq.com> Date: Tue, 22 Sep 2026 14:36:43 +0800 Subject: [PATCH] mrz --- PythonProject/api/employment_api.py | 23 +++++++++---------- PythonProject/api/student_api.py | 2 +- PythonProject/dao/employment_dao.py | 27 ++++++----------------- PythonProject/schema/employment_schema.py | 15 +++++++++++++ 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/PythonProject/api/employment_api.py b/PythonProject/api/employment_api.py index a3aa6d7..fd6f2aa 100644 --- a/PythonProject/api/employment_api.py +++ b/PythonProject/api/employment_api.py @@ -1,7 +1,7 @@ from fastapi import APIRouter, Query, Depends, HTTPException, Path,Form from dao import employment_dao from dao.employment_dao import add_emp_dao, update_emp_dao, delete_emp_dao -from schema.employment_schema import EmploymentRequest,EmploymentResponse +from schema.employment_schema import EmploymentRequest,UpdateEmpRequest,EmploymentResponse from util.database import get_db from typing import Optional emp_api = APIRouter(tags=['学生就业管理模块']) @@ -22,8 +22,8 @@ def get_emp_info2(class_id: Optional[int]=Path(description='班级编号') return e2 raise HTTPException(status_code=404, detail='该班级就业信息不存在') -@emp_api.get('/employment',response_model=EmploymentResponse,summary='按照学⽣编号,公司名字,⼯资范围查询学⽣就业信息') -def get_emp_info3(stu_id:int|None = Query(None, description='学生编号') +@emp_api.get('/employment/class/{stu_id}',response_model=list[EmploymentResponse],summary='按照学⽣编号,公司名字,⼯资范围查询学⽣就业信息') +def get_emp_info3(stu_id:int = Path(description='学生编号') ,company_name: str|None = Query(None,description='公司名称') ,min_salary: float|None = Query(None,ge=0,description='最低工资') ,max_salary: float|None = Query(None,ge=0,description='最高工资') @@ -31,11 +31,11 @@ def get_emp_info3(stu_id:int|None = Query(None, description='学生编号') if min_salary and max_salary: if min_salary > max_salary: raise HTTPException(status_code=404, detail='最低工资不能大于最高工资') - return [employment_dao.get_emp_dao(stu_id=stu_id - ,company_name=company_name - ,min_salary=min_salary - ,max_salary=max_salary - ,db=db)] + l1 = employment_dao.get_emp_dao(db, stu_id=stu_id + , company_name=company_name + , min_salary=min_salary + , max_salary=max_salary) + return l1 @emp_api.post('/employment/students/{stu_id}',response_model=EmploymentResponse,summary='新增学生就业信息') def create_emp_info(emp:EmploymentRequest=Form(),db=Depends(get_db)): @@ -45,10 +45,9 @@ def create_emp_info(emp:EmploymentRequest=Form(),db=Depends(get_db)): raise HTTPException(status_code=500,detail='服务器繁忙,请稍后添加!') return r -@emp_api.put('/employment/students/{emp_id}',response_model=EmploymentResponse,summary='更新学生就业信息') -def update_emp_info(emp_id:int,emp:EmploymentRequest,db=Depends(get_db)): - d = emp.model_dump(exclude_unset=True) - r = update_emp_dao(emp_id=emp_id,db=db) +@emp_api.put('/employment/students/{emp_id}',summary='更新学生就业信息') +def update_emp_info(emp_id:int,emp:UpdateEmpRequest,db=Depends(get_db)): + r = update_emp_dao(emp_id,emp.model_dump(exclude_unset=True),db) if not r: raise HTTPException(status_code=500,detail='没有更新!') return {'code':200,'totals':r,'detail':'更新成功!'} diff --git a/PythonProject/api/student_api.py b/PythonProject/api/student_api.py index b48fdc1..3b1bb2b 100644 --- a/PythonProject/api/student_api.py +++ b/PythonProject/api/student_api.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter,HTTPException +from fastapi import APIRouter,HTTPException,Depends from dao.student_dao import * from schema.student_schema import * from util.database import get_db diff --git a/PythonProject/dao/employment_dao.py b/PythonProject/dao/employment_dao.py index 6168d87..2a7e1fc 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 from datetime import datetime -def get_emp_dao(stu_id=None,class_id=None,company_name=None,min_salary=None,max_salary=None,db=None): +def get_emp_dao(db,stu_id=None,class_id=None,company_name=None,min_salary=None,max_salary=None): q = db.query(Employment_info.emp_id ,Employment_info.stu_id ,Student_Model.stu_name @@ -33,24 +33,11 @@ def get_emp_dao(stu_id=None,class_id=None,company_name=None,min_salary=None,max_ r = q.all() if not r: raise HTTPException(status_code=500,detail='查询异常,没有结果!') - return [ - {'emp_id':i.emp_id - ,'stu_id': i.stu_id - ,'stu_name':i.stu_name - ,'class_id':i.class_id - ,'email':i.email - ,'employment_opening_date':i.employment_opening_date - ,'offer_issuance_date':i.offer_issuance_date - ,'company_name':i.company_name - ,'company_address':i.company_address - ,'salary':i.salary - } - for i in r - ] + return r -def add_emp_dao(o:dict,db): +def add_emp_dao(o,db): try: stu = db.query(Student_Model).filter(Student_Model.stu_id == o['stu_id'] , Student_Model.delete_status == 0).first() @@ -70,11 +57,11 @@ def add_emp_dao(o:dict,db): db.rollback() raise HTTPException(status_code=500, detail=f'新增就业信息失败: {e}') -def update_emp_dao(emp_id:int,db): +def update_emp_dao(emp_id,emp,db): try: - rows = db.query(Employment_info).filter(Employment_info.emp_id == emp_id - ,Employment_info.delete_status == 0).first() - + rows = db.query(Employment_info)\ + .filter(Employment_info.emp_id == emp_id,Employment_info.delete_status == 0)\ + .update(emp) db.commit() return rows except: diff --git a/PythonProject/schema/employment_schema.py b/PythonProject/schema/employment_schema.py index 4c5dd52..1505868 100644 --- a/PythonProject/schema/employment_schema.py +++ b/PythonProject/schema/employment_schema.py @@ -18,6 +18,21 @@ class EmploymentRequest(BaseModel): raise ValueError('offer下发日期不能早于就业开放日期') return self +class UpdateEmpRequest(BaseModel): + email: Optional[str] = Field(None, description='邮箱') + employment_opening_date:Optional[date] = Field(None, description='就业开放日期') + offer_issuance_date:Optional[date] = Field(None, description='offer下发日期') + company_name:Optional[str] = Field(None, description='公司名称') + company_address:Optional[str] = Field(None, description='公司地址') + salary:Optional[float] = Field(None, description='薪资') + + @model_validator(mode='after') + def check_date(self): + if self.employment_opening_date and self.offer_issuance_date: + if self.employment_opening_date > self.offer_issuance_date: + raise ValueError('offer下发日期不能早于就业开放日期') + return self + class EmploymentResponse(BaseModel): code:int = 200 detail:str = 'OK'