Files
stu_system/dao/employments.py
T
2026-09-21 17:39:16 +08:00

88 lines
3.1 KiB
Python

from typing import Optional
from sqlalchemy.orm import Session
from model.employments import Employments
from model.student_info import Student
from schemas.employments import Emp_Class,Emp_add_update
from sqlalchemy import and_
from fastapi import HTTPException
#根据学生id查看信息
def get_student_id(db: Session, student_id: str):
return db.query(Employments).filter(and_(Employments.id == student_id,Employments.is_deleted==True)).first()
#根据学生id、公司名称、工资范围多条件查询
def get_student_multi_condition(
db: Session,
id: Optional[str] = None,
name: Optional[str] = None,
min_sal: Optional[float] = None,
max_sal: Optional[float] = None,
):
# LEFT JOIN student + student_employment
q = db.query(Student, Employments)\
.outerjoin(Employments, Student.id == Employments.id)\
.filter(Employments.is_deleted==True)
# if min_sal>max_sal:
# raise HTTPException(status_code=404, detail="学生不存在,请先创建学生基础信息")
# 条件动态拼接
if id:
q = q.filter(Student.id.like(f"%{id}%"))#like模糊查询
if name:
q = q.filter(Employments.emp.like(f"%{name}%"))
if min_sal is not None:
q = q.filter(Employments.sal >= min_sal)
if max_sal is not None:
q = q.filter(Employments.sal <= max_sal)
return q.all()
#根据学生班级查看信息
def get_class_id(db:Session,class_id:str):
return (db.query(Student,Employments)
.outerjoin(Student,Student.id==Employments.id))\
.filter(Student.class_name==class_id).all()
#增加修改学生信息
#首先判断是否是已存在的学生,如果不是就增加,反之就修改
def add_update_employment(db: Session,student_id:str,emp_data: Emp_add_update,student:Student ):
emp = get_student_id(db, student_id)
if emp:
# 更新
emp.offer_open = emp_data.offer_open
emp.offer_review = emp_data.offer_review
emp.emp = emp_data.emp
emp.sal = emp_data.sal
else:
# 新增
stu = db.query(Student).filter(Student.id == student_id)
if stu:
emp = Employments(
id=student_id,
offer_open=emp_data.offer_open,
offer_review=emp_data.offer_review,
emp=emp_data.emp,
sal=emp_data.sal
)
db.add(emp)
else:
raise HTTPException(status_code=404, detail="学生不存在,请先创建学生基础信息")
# 业务状态更新 offer优先级更高
if emp_data.offer_review is not None:
student.status = "已就业"
elif emp_data.offer_open is not None:
student.status = "就业开放"
db.commit()
db.refresh(emp)
return emp
def delete_employment(db: Session,student_id:str ):
emp = get_student_id(db, student_id)
if emp:
# 删除
emp.is_deleted = False
db.commit()
db.refresh(emp)
# else:
# raise HTTPException(status_code=404, detail="学生不存在,请先创建学生基础信息")
return 1