from http.client import HTTPException from model.epy_model import Employment from fastapi import HTTPException,Depends from database import * def get_employment_dao(db, stu_id:str, company:str, salary:int): # get变动 q = db.query(Employment).filter(Employment.is_deleted == False) if stu_id: q = q.filter(Employment.stu_id == stu_id) if company: q = q.filter(Employment.company == company) if salary is not None: q = q.filter(Employment.salary == salary) return q.all() # def get_employment_dao(db): # try: # r = db.query(Employment).all() # except Exception as e: # db.rollback() # raise HTTPException(status_code=500, detail=f'查询失败:str(e)') # else: # db.commit() # return [{'stu_id':i.stu_id,'class_name':i.class_name,'company':i.company,'salary':i.salary} for i in r ] def wq_employment_dao(o,db): try: o1 = Employment(**o) db.add(o1) except Exception as e: db.rollback() raise HTTPException(status_code=500, detail=f'添加失败:str(e)') else: db.commit() return 1 def upd_employment_dao(e,db): try: rows = db.query(Employment).filter(Employment.stu_id == e['stu_id']).update(e) except: db.rollback() raise HTTPException(status_code=500, detail='更新异常,请稍后再执行!') else: db.commit() return 1 def del_employment_dao(stu_id,db): try: row = db.query(Employment).filter(Employment.stu_id == stu_id).all() if not row: raise HTTPException(status_code=404,detail='记录不存在') for i in row: if i.is_deleted == 0: db.query(Employment).filter(Employment.stu_id == i.stu_id).update({'is_deleted':1}) except Exception as e: db.rollback() raise HTTPException(status_code=500,detail=f'删除失败:{str(e)}') else: db.commit() return 1