79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
# dao/employ_dao.py
|
|
# 学生就业数据访问层(与学生一对一)
|
|
|
|
from typing import List, Optional, Tuple
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from dao.exceptions import ConflictError, NotFoundError
|
|
from dao.pagination import paginate
|
|
from dao.stu_dao import assert_stu_alive
|
|
from model.employ_model import StudentEmployManage
|
|
|
|
|
|
def assert_employ_alive(db: Session, stu_id: str, detail: Optional[str] = None) -> StudentEmployManage:
|
|
"""确认就业记录存在且未删除,否则抛 NotFoundError。"""
|
|
emp = (
|
|
db.query(StudentEmployManage)
|
|
.filter(StudentEmployManage.stu_id == stu_id, StudentEmployManage.is_deleted == 0)
|
|
.first()
|
|
)
|
|
if not emp:
|
|
raise NotFoundError(detail or "就业记录不存在或已删除")
|
|
return emp
|
|
|
|
|
|
def list_employ(
|
|
db: Session,
|
|
page: int,
|
|
size: int,
|
|
emp_company: Optional[str] = None,
|
|
employed: Optional[bool] = None,
|
|
) -> Tuple[int, List[StudentEmployManage]]:
|
|
"""分页查询就业记录,支持按公司名模糊、按是否已就业过滤。"""
|
|
query = db.query(StudentEmployManage).filter(StudentEmployManage.is_deleted == 0)
|
|
if emp_company:
|
|
query = query.filter(StudentEmployManage.emp_company.like(f"%{emp_company}%"))
|
|
if employed is True:
|
|
query = query.filter(StudentEmployManage.emp_company.isnot(None))
|
|
elif employed is False:
|
|
query = query.filter(StudentEmployManage.emp_company.is_(None))
|
|
return paginate(query, page, size, order_by=StudentEmployManage.stu_id)
|
|
|
|
|
|
def create_employ(db: Session, data: dict) -> StudentEmployManage:
|
|
"""新增就业记录:校验学生存在,且该学生尚无有效就业记录(一对一)。"""
|
|
assert_stu_alive(db, data["stu_id"])
|
|
|
|
exists = (
|
|
db.query(StudentEmployManage)
|
|
.filter(StudentEmployManage.stu_id == data["stu_id"], StudentEmployManage.is_deleted == 0)
|
|
.first()
|
|
)
|
|
if exists:
|
|
raise ConflictError("该学生已存在就业记录")
|
|
|
|
emp = StudentEmployManage(**data)
|
|
db.add(emp)
|
|
db.commit()
|
|
db.refresh(emp)
|
|
return emp
|
|
|
|
|
|
def update_employ(db: Session, stu_id: str, data: dict) -> StudentEmployManage:
|
|
"""更新就业信息。"""
|
|
emp = assert_employ_alive(db, stu_id)
|
|
|
|
for field, value in data.items():
|
|
setattr(emp, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(emp)
|
|
return emp
|
|
|
|
|
|
def soft_delete_employ(db: Session, stu_id: str) -> None:
|
|
"""软删除就业记录。"""
|
|
emp = assert_employ_alive(db, stu_id)
|
|
emp.is_deleted = 1
|
|
db.commit() |