Files
student_manage_system/stu_jiuye/dao.py
T

127 lines
5.9 KiB
Python
Raw Normal View History

2026-09-21 15:07:50 +08:00
from model import Employments,Address,Company
2026-09-20 21:20:12 +08:00
2026-09-21 17:05:10 +08:00
def add_employment_dao(o,db): #新增就业信息函数
2026-09-20 21:20:12 +08:00
try:
2026-09-21 17:05:10 +08:00
# r = db.query(Employments).filter(Employments.id == o['id'] ).all() #
o.pop('id', None) #摘除id字段,自增字段无需手动添加
o1 = Employments( **o) #实例化参数
db.add(o1) #添加数据进入表
2026-09-20 21:20:12 +08:00
2026-09-21 17:05:10 +08:00
except Exception as e: #失败返回
2026-09-20 21:20:12 +08:00
print(f'添加失败,{e}')
2026-09-21 17:05:10 +08:00
db.rollback() #回滚数据
2026-09-20 21:20:12 +08:00
return False
else:
2026-09-21 17:05:10 +08:00
db.commit() #提交事务,永久保存
2026-09-20 21:20:12 +08:00
return True
2026-09-21 17:05:10 +08:00
def add_company_dao(o,db): #添加公司信息函数
try:
2026-09-21 17:05:10 +08:00
o.pop('id', None) #摘除id字段,自增字段无需手动添加
o1 = Company( **o) #实例化参数
db.add(o1) #添加数据进入表
except Exception as e: #失败返回
print(f'添加失败,{e}')
2026-09-21 17:05:10 +08:00
db.rollback() #回滚数据
return False
else:
2026-09-21 17:05:10 +08:00
db.commit() #提交事务,永久保存
return True
2026-09-21 17:05:10 +08:00
def add_address_dao(o,db): #添加地址区域信息函数
try:
2026-09-21 17:05:10 +08:00
o.pop('id', None) #摘除id字段,自增字段无需手动添加
o1 = Address( **o) #实例化参数
db.add(o1) #添加数据进入表
except Exception as e: #失败返回
print(f'添加失败,{e}')
2026-09-21 17:05:10 +08:00
db.rollback() #回滚数据
return False
else:
2026-09-21 17:05:10 +08:00
db.commit() #提交事务,永久保存
return True
2026-09-20 21:20:12 +08:00
2026-09-21 17:05:10 +08:00
def update_emp_dao(id,update_data,db): #更新数据函数
try:
rows = db.query( Employments )\
.filter( Employments.id == id )\
.update( update_data ) #更新条件匹配id
2026-09-21 15:07:50 +08:00
except Exception as e:
2026-09-21 17:05:10 +08:00
print(f'更新失败,{e}') #返回失败信息
db.rollback() #数据回滚
2026-09-20 21:20:12 +08:00
return False
else:
2026-09-21 17:05:10 +08:00
db.commit() #提交事务,永久保存
2026-09-20 21:20:12 +08:00
return rows
2026-09-21 17:05:10 +08:00
def get_emp_dao( #获取学生就业信息函数
2026-09-20 21:20:12 +08:00
# stuid,
2026-09-21 17:24:14 +08:00
stuname
,stuclass
,compname
,min_salary
,max_salary
,somewhere
,employment_open_date
,offer_date
,db
2026-09-20 21:20:12 +08:00
# ,page
# ,page_size
2026-09-21 17:05:10 +08:00
): #根据选择条件获取信息
2026-09-21 15:07:50 +08:00
q = db.query(Employments,Company.compname,Address.somewhere)\
.join(Company,Employments.company_id==Company.id)\
2026-09-21 15:07:50 +08:00
.join(Address,Company.address_id==Address.id)\
2026-09-21 17:05:10 +08:00
.filter(Employments.is_deleted == 0) #连接公司表和地址表,筛选未被删除信息
2026-09-20 21:20:12 +08:00
# if stuid:
# q = q.filter( Employments.id == stuid )
2026-09-21 17:05:10 +08:00
if stuname: #根据学生姓名筛选
2026-09-21 15:07:50 +08:00
q = q.filter( Employments.stuname == stuname )
2026-09-21 17:24:14 +08:00
if stuclass:
q = q.filter(Employments.stuclass == stuclass)
if somewhere:
q = q.filter(Address.somewhere.like(f'%{somewhere}%'))
2026-09-21 17:05:10 +08:00
if compname: #根据公司姓名筛选
2026-09-21 15:07:50 +08:00
q=q.filter(Company.compname.like(f'%{compname}%'))
2026-09-20 21:20:12 +08:00
# r = q.offset( (page-1)*page_size ).limit( page_size ).all()
2026-09-21 17:05:10 +08:00
if min_salary is not None: #根据最小薪资筛选
2026-09-20 21:20:12 +08:00
q = q.filter(Employments.salary >= min_salary)
2026-09-21 17:05:10 +08:00
if max_salary is not None: ##根据最大薪资筛选
2026-09-20 21:20:12 +08:00
q = q.filter(Employments.salary <= max_salary)
2026-09-21 17:24:14 +08:00
if employment_open_date:
q = q.filter(Employments.employment_open_date == employment_open_date)
if offer_date:
q = q.filter(Employments.offer_date == offer_date)
2026-09-20 21:20:12 +08:00
r= q.all()
if r:
return [ {"id": i.id,
2026-09-21 17:24:14 +08:00
'学生姓名':i.stuname,
'学生班级':i.stuclass,
"公司": compname,
"公司区域":somewhere,
2026-09-21 15:07:50 +08:00
# "company_id": i.company_id,
2026-09-21 17:24:14 +08:00
"薪资":i.salary,
2026-09-21 15:07:50 +08:00
# "address_id": i.address_id,
2026-09-21 17:24:14 +08:00
"就业开放时间":i.employment_open_date,
"offer下发时间":i.offer_date,
2026-09-20 21:20:12 +08:00
"created_date": i.created_date,
"updated_date": i.updated_date,
2026-09-21 17:05:10 +08:00
"is_deleted":i.is_deleted } for i,compname,somewhere in r ] #返回信息列表
2026-09-20 21:20:12 +08:00
2026-09-21 17:05:10 +08:00
def delete_emp_dao(id,db): #删除函数
try:
rows = db.query(Employments)\
.filter(Employments.id == id)\
.update( {'is_deleted':1} ) #软删除,is_deleted返回1
2026-09-21 12:22:26 +08:00
2026-09-21 17:05:10 +08:00
except Exception as e: #失败返回信息
2026-09-20 21:20:12 +08:00
print(f'删除失败,{e}')
2026-09-21 17:05:10 +08:00
db.rollback() #数据回滚
rows = 0 #返回0
2026-09-20 21:20:12 +08:00
else:
2026-09-21 17:05:10 +08:00
db.commit() #提交事务,永久保存
2026-09-20 21:20:12 +08:00
return rows