from fastapi import APIRouter, Depends, HTTPException, Query # 导入fastapi组件 from core.database import get_db # 导入获取数据库会话的工具函数 from dao.employment import * # 导入dao层函数 from schemas.employment import * # 导入schema模型 from typing import List, Optional # 导入类型提示 employments_router = APIRouter() @employments_router.post("/employments", response_model=EmploymentResponse, summary="新增就业信息") def create_employment(body: EmploymentCreate # 请求体入参 ,db= Depends(get_db)): # 注入数据库会话 """新增一条学生就业信息,一个学生只能有一条就业记录""" x=body.model_dump() y=create_employment(x,db) if not y: raise HTTPException(status_code=500, detail='服务器繁忙,请稍后添加!') return {"code":200,"detail":"新增信息成功"} @employments_router.get("/employments", response_model=List[EmploymentResponse], summary="就业列表查询") def get_employment_list(company_name: Optional[str] = Query(None, description="公司名称模糊搜索") ,salary_min: Optional[float] = Query(None, description="薪资下限") ,salary_max: Optional[float] = Query(None, description="薪资上限") ,db: Session = Depends(get_db)): """获取就业列表,支持按公司名称、薪资范围过滤""" return get_employment_list(db=db ,company_name=company_name ,salary_min=salary_min ,salary_max=salary_max) @employments_router.get("/employments/students/{student_id}", response_model=EmploymentResponse, summary="查询某个学生的就业信息") def get_student_employment(student_id: int ,db: Session = Depends(get_db)): """根据学生ID获取该学生对应的就业信息""" res = get_employment_by_student_id(db=db, stu_id=student_id) if not res: raise HTTPException(status_code=404, detail="该学生暂无就业信息") return res @employments_router.put("/employments/{emp_id}", response_model=EmploymentResponse, summary="修改就业信息") def update_employment(emp_id: int ,body: EmploymentUpdate ,db: Session = Depends(get_db)): """根据id修改就业记录信息""" res = update_employment(db=db, emp_id=emp_id, E=body) if not res: raise HTTPException(status_code=404, detail="就业记录不存在或已删除") return res @employments_router.delete("/employments/{emp_id}", summary="逻辑删除就业记录") def delete_employment(emp_id: int ,db: Session = Depends(get_db)): """逻辑删除,设置is_deleted=1,不会物理删除数据库数据,删除后普通接口查询不到""" ok = delete_employment_logic(db=db, emp_id=emp_id) if not ok: raise HTTPException(status_code=404, detail="就业记录不存在或已删除") return {"msg": "逻辑删除成功", "id": emp_id}