更新 dao/epy_dao.py

This commit is contained in:
2026-09-22 15:30:05 +08:00
parent d2eb86730c
commit f2b311519b
+52 -52
View File
@@ -1,52 +1,52 @@
from http.client import HTTPException from http.client import HTTPException
from model.e_model import Employment from model.e_model import Employment
from fastapi import HTTPException from fastapi import HTTPException
def get_employment_dao(db): def get_employment_dao(db):
try: try:
r = db.query(Employment).all() r = db.query(Employment).all()
except Exception as e: except Exception as e:
db.rollback() db.rollback()
raise HTTPException(status_code=500, detail=f'查询失败:str(e)') raise HTTPException(status_code=500, detail=f'查询失败:str(e)')
else: else:
db.commit() db.commit()
return [{'stu_id':i.stu_id,'class_name':i.class_name} for i in r ] return [{'stu_id':i.stu_id,'class_name':i.class_name} for i in r ]
def wq_employment_dao(o,db): def wq_employment_dao(o,db):
try: try:
o1 = Employment(**o) o1 = Employment(**o)
db.add(o1) db.add(o1)
except Exception as e: except Exception as e:
db.rollback() db.rollback()
raise HTTPException(status_code=500, detail=f'添加失败:str(e)') raise HTTPException(status_code=500, detail=f'添加失败:str(e)')
else: else:
db.commit() db.commit()
return 1 return 1
def upd_employment_dao(e,db): def upd_employment_dao(e,db):
try: try:
rows = db.query(Employment).filter(Employment.stu_id == e['stu_id']).update(e) rows = db.query(Employment).filter(Employment.stu_id == e['stu_id']).update(e)
except: except:
db.rollback() db.rollback()
raise HTTPException(status_code=500, detail='更新异常,请稍后再执行!') raise HTTPException(status_code=500, detail='更新异常,请稍后再执行!')
else: else:
db.commit() db.commit()
return 1 return 1
def del_employment_dao(stu_id,db): def del_employment_dao(stu_id,db):
try: try:
row = db.query(Employment).filter(Employment.stu_id == stu_id).all() row = db.query(Employment).filter(Employment.stu_id == stu_id).all()
if not row: if not row:
raise HTTPException(status_code=404,detail='记录不存在') raise HTTPException(status_code=404,detail='记录不存在')
for i in row: for i in row:
if i.deleted == 0: if i.deleted == 0:
db.query(Employment).filter(Employment.stu_id == i.stu_id).update({'deleted':1}) db.query(Employment).filter(Employment.stu_id == i.stu_id).update({'deleted':1})
except Exception as e: except Exception as e:
db.rollback() db.rollback()
raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}') raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}')
else: else:
db.commit() db.commit()
return 1 return 1