Files
max_code_test_gitea_2/dao/employ_dao.py
T
2026-09-14 15:23:26 +08:00

218 lines
8.9 KiB
Python

# dao/employ_dao.py
# 本文件封装 stu_emp_mgmt 表的所有数据库操作(增、删、改、查)
# author:王博
from datetime import date
from sqlalchemy import and_
from sqlalchemy.orm import Session
from model.employ_model import StudentEmployManage as ETable
from scheme.employ_scheme import EmployStatusCreate as EModel, EmployStatusQuery as QModel,EmployStatusDelete as DModel
from model.stu_model import StuInfo
from typing import List
class StudentEmployManageDAO:
"""
封装stu_emp_mgmt 表的所有数据库操作(增、删、改、查)
"""
@staticmethod
def add_emp_reco(db:Session,emp_sta_add:EModel):
"""
登记学生就业信息
:param db: 数据库会话
:param emp_sta_add: 参数,对象,类型属于 学生就业管理表 模型类
:return: 添加的stu_emp_info对象
"""
d_stu = emp_sta_add.model_dump() # 将请求体参数对象 ,转为字典
if d_stu['emp_open_time']:
d_stu['emp_open_time'] = date(**d_stu['emp_open_time']) # 就业开放时间
if d_stu['send_offer_time']:
d_stu['send_offer_time'] = date(**d_stu['send_offer_time']) # offer下发时间
db_emp = ETable(**d_stu)
db.add(db_emp)
db.commit()
db.refresh(db_emp)
return db_emp
@staticmethod
def is_in_stu(db: Session, emp_sta_in: EModel):
"""
判断 参数中 学生id 是否在 学生表StuInfo 中存在
根据 学生id、is_deleted 过滤
:return: 存在 返回True;不存在返回False
"""
stu = db.query(StuInfo).filter(and_(StuInfo.id==emp_sta_in.stu_id,
StuInfo.is_deleted==0)).first()
if stu: # 能找到学号,并且 是否 删除状态为 0
return True
return False
@staticmethod
def is_exist_visible(db: Session, emp_sta_exist:List[DModel] |EModel | QModel | str):
"""
判断学生就业信息是否在 学生就业信息表中 存在
is_deleted 0,用来判断可见、可查询
:return: 存在、可见 返回True;否则 返回False
"""
emp = None
emp_info = db.query(ETable)
if isinstance(emp_sta_exist,str):
emp = emp_info.filter(and_(ETable.stu_id == emp_sta_exist,
ETable.is_deleted == 0)).first()
elif isinstance(emp_sta_exist, list):
for i in emp_sta_exist:
emp = emp_info.filter(and_(ETable.stu_id == i.stu_id,
ETable.is_deleted == 0)).first()
if not emp:
emp = False
break
else:
emp = emp_info.filter(and_(ETable.stu_id == emp_sta_exist.stu_id,
ETable.is_deleted == 0)).first()
if emp:
return True
return False
@staticmethod
def is_exist_invisible(db: Session, emp_sta_exist: EModel):
"""
判断学生就业信息是否在 学生就业信息表中 存在
is_deleted 1,用来判断不可见、不可查询
:return: 存在、不可见 返回True;否则 返回False
"""
emp_info = db.query(ETable).filter(and_(ETable.stu_id == emp_sta_exist.stu_id,
ETable.is_deleted == 1)).first()
if emp_info:
return True
return False
@staticmethod
def set_stu_state(db: Session, emp_sta_set: EModel):
"""
根据就业开放时间、offer下发时间 设置 学生就业状态
就业未开放 ---》在读
就业开放&offer未下发 ---》进入就业
就业开放&offer下发 ---》已就业
:return: 在读、进入就业、已就业
"""
stu = db.query(StuInfo).filter(StuInfo.id == emp_sta_set.stu_id).first()
if emp_sta_set.emp_open_time is None: # 就业开放时间为 None
stu.state = '在读'
db.commit()
db.refresh(stu)
return stu.state
elif emp_sta_set.send_offer_time is None:# 有就业开放时间,offer下发时间为 None
stu.state = '进入就业'
db.commit()
db.refresh(stu)
return stu.state
else: # 有offer下发时间
stu.state = '已就业'
db.commit()
db.refresh(stu)
return stu.state
@staticmethod
def set_emp_info(db: Session, emp_sta_update: EModel):
"""
根据请求体参数 修改 学生 就业信息
:return: 修改条数0或1;0代表 修改失败 ;1代表 修改成功
"""
d_stu = emp_sta_update.model_dump() # 将请求体参数对象 ,转为字典
if d_stu['emp_open_time']:
d_stu['emp_open_time'] = date(**d_stu['emp_open_time']) # 转化成 就业开放时间
if d_stu['send_offer_time']:
d_stu['send_offer_time'] = date(**d_stu['send_offer_time']) # 转化成 offer下发时间
row_count = db.query(ETable).filter(ETable.stu_id == emp_sta_update.stu_id).update(d_stu) #修改条数0或1
db.commit()
return row_count
@staticmethod
def batch_set_emp_info(db: Session, emp_sta_update:EModel):
"""
根据请求体参数列表 批量修改 学生 就业信息
:return: 修改条数0或1;0代表 修改失败 ;1代表 修改成功
"""
d_stu = emp_sta_update.model_dump() # 将请求体参数对象 ,转为字典
if d_stu['emp_open_time']:
d_stu['emp_open_time'] = date(**d_stu['emp_open_time']) # 转化成 就业开放时间
if d_stu['send_offer_time']:
d_stu['send_offer_time'] = date(**d_stu['send_offer_time']) # 转化成 offer下发时间
row_count = db.query(ETable).filter(ETable.stu_id == emp_sta_update.stu_id).update(d_stu) # 修改条数0或1
return row_count
@staticmethod
def is_deleted(db: Session, emp_info):
if emp_info:
emp_info.is_deleted = 1
db.commit()
db.refresh(emp_info)
@staticmethod
def del_emp_info(db: Session, emp_sta_del: List[DModel] | str):
"""
根据请求体参数 逻辑删除 学生就业信息
:return: 返回逻辑删除的 学生就业信息
"""
emp_info = db.query(ETable)
if isinstance(emp_sta_del, str):
emp = emp_info.filter(and_(ETable.stu_id == emp_sta_del,
ETable.is_deleted == 0)).first()
StudentEmployManageDAO.is_deleted(db,emp)
db.commit()
return True
if isinstance(emp_sta_del, list):
emp_copy = emp_sta_del.copy()
for i in emp_copy:
emp = emp_info.filter(and_(ETable.stu_id == i.stu_id,
ETable.is_deleted ==0)).first()
StudentEmployManageDAO.is_deleted(db, emp)
db.commit()
return True
return False
@staticmethod
def recover_emp_info(db: Session, emp_sta_recover: EModel):
"""
根据请求体参数 逻辑删除 学生就业信息
:return: 返回逻辑删除的 学生就业信息
"""
emp_info = db.query(ETable).filter(and_(ETable.stu_id == emp_sta_recover.stu_id,
ETable.is_deleted ==0)).first()
if emp_info:
emp_info.is_deleted = 0
db.commit()
db.refresh(emp_info)
return emp_info
@staticmethod
def query_emp_info(db: Session, emp_sta_query: QModel):
"""
请求体传入学生编号(可选)、就业公司(可选)、薪资范围(可选) 可多条件查询 学生就业信息
:return: 返回查询到的 所有 学生就业信息
"""
e_all = db.query(ETable)
if emp_sta_query.stu_id is not None: # 根据 学生编号 筛选出 学生就业信息
e_all = e_all.filter(ETable.stu_id == emp_sta_query.stu_id)
if emp_sta_query.emp_company is not None: # 根据 就业公司名称 模糊筛选出 学生就业信息
e_all = e_all.filter(ETable.emp_company.like(f"%{emp_sta_query.emp_company}%"))
if emp_sta_query.min_salary is not None: # 根据 最小薪资范围 筛选出 大于 最小薪资范围的 学生就业信息
e_all = e_all.filter(ETable.salary >= emp_sta_query.min_salary)
if emp_sta_query.max_salary is not None: # 根据 最大薪资范围 筛选出 小于 最大薪资范围的 学生就业信息
e_all = e_all.filter(ETable.salary <= emp_sta_query.max_salary)
total = e_all.count()
e_all = e_all.offset((emp_sta_query.skip-1)*emp_sta_query.limit).limit(emp_sta_query.limit).all()
return e_all,total