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,UpdateEmpRequest,EmploymentResponse from util.database import get_db from typing import Optional emp_api = APIRouter(tags=['学生就业管理模块']) @emp_api.get('/employment/class/{class_id}',response_model=list[EmploymentResponse],summary='获取班级学生就业信息') def get_emp_info2(class_id: Optional[int]=Path(description='班级编号') ,db=Depends(get_db)): e2 = employment_dao.get_emp_dao(class_id=class_id, db=db) if e2: return e2 raise HTTPException(status_code=404, detail='该班级就业信息不存在') @emp_api.get('/employment/students/{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='最高工资') ,db=Depends(get_db)): if min_salary and max_salary: if min_salary > max_salary: raise HTTPException(status_code=404, detail='最低工资不能大于最高工资') 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)): 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/{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':'更新成功!'} @emp_api.delete('/employment/{emp_id}',summary='删除学生就业信息') def delete_emp_info(emp_id:int=Path(description='序号'),db=Depends(get_db)): rows = delete_emp_dao(emp_id, db) if not rows: raise HTTPException(status_code=500, detail='没有删除!') return {'code':200,'detail':'删除成功!'}