forked from wolin_ck666_999/StuManagerSys
119 lines
4.2 KiB
Python
119 lines
4.2 KiB
Python
from pymysql import IntegrityError
|
|||
|
|
from models.class_model import Class,Teacher
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
def add_class_dao(c,db):
|
||
|
|
try:
|
||
|
|
head_teacher = db.query(Teacher).filter(Teacher.id == c['ht_id']).first()
|
||
|
|
if head_teacher is None:
|
||
|
|
return False, '指定的班主任不存在'
|
||
|
|
class_teacher = db.query(Teacher).filter(Teacher.id == c['t_id']).first()
|
||
|
|
if class_teacher is None:
|
||
|
|
return False, '指定的授课老师不存在'
|
||
|
|
if db.query(Class).filter(
|
||
|
|
Class.class_id == c['class_id'],#如果请求体传进来的classid在表里找的到
|
||
|
|
Class.is_deleted==0 #且没有处于软删除状态
|
||
|
|
).first():
|
||
|
|
return False, '班级编号已存在'
|
||
|
|
new_class = Class(**c)
|
||
|
|
db.add(new_class)
|
||
|
|
db.commit()
|
||
|
|
return True,new_class
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
print('创建失败:',repr(e))
|
||
|
|
return False,str(e)
|
||
|
|
def update_class_dao(id,update_data,db):
|
||
|
|
try:
|
||
|
|
rows = db.query(Class).filter(Class.id == id).update(update_data)
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
print('更新失败:', repr(e))
|
||
|
|
return False, str(e)
|
||
|
|
else:
|
||
|
|
db.commit()
|
||
|
|
return rows
|
||
|
|
'''
|
||
|
|
def get_class_dao(id,db,page,page_size):
|
||
|
|
q = db.query(Class)
|
||
|
|
if id:
|
||
|
|
q = q.filter(Class.id == id,Class.is_deleted==0)
|
||
|
|
rows = q.offset((page-1)*page_size ).limit(page_size).all()
|
||
|
|
if rows:
|
||
|
|
return[{'class_id':i.id,'class_no':i.class_no,'class_name':i.class_name,'ht_id':i.ht_id,'t_id':i.t_id,'start_time':i.start_time}for i in rows]
|
||
|
|
'''
|
||
|
|
def get_class_list_dao(db: Session, page: int, page_size: int, **filters):
|
||
|
|
#软删除过滤
|
||
|
|
q = db.query(Class).filter(Class.is_deleted == 0)
|
||
|
|
|
||
|
|
# 精确匹配字段
|
||
|
|
if filters.get("class_no"):
|
||
|
|
q = q.filter(Class.class_id == filters["class_id"])
|
||
|
|
if filters.get("ht_id") is not None:
|
||
|
|
q = q.filter(Class.ht_id == filters["ht_id"])
|
||
|
|
if filters.get("t_id") is not None:
|
||
|
|
q = q.filter(Class.t_id == filters["t_id"])
|
||
|
|
|
||
|
|
# 模糊匹配字段(例如班级名称)
|
||
|
|
if filters.get("class_name"):
|
||
|
|
q = q.filter(Class.class_name.like(f"%{filters['class_name']}%"))
|
||
|
|
|
||
|
|
# 获取满足条件的总记录数(分页必备)
|
||
|
|
total = q.count()
|
||
|
|
|
||
|
|
# 分页查询
|
||
|
|
rows = q.offset((page - 1) * page_size).limit(page_size).all()
|
||
|
|
|
||
|
|
items = [ #如果匹配不到,则会返回空列表
|
||
|
|
{
|
||
|
|
'id': i.id,
|
||
|
|
'class_id': i.class_id,
|
||
|
|
'class_name': i.class_name,
|
||
|
|
'ht_id': i.ht_id,
|
||
|
|
't_id': i.t_id,
|
||
|
|
'start_time': i.start_time
|
||
|
|
}
|
||
|
|
for i in rows
|
||
|
|
]
|
||
|
|
return total, items
|
||
|
|
def get_class_by_id_dao(id: int, db: Session):
|
||
|
|
i = db.query(Class).filter(Class.id == id, Class.is_deleted == 0).first()
|
||
|
|
if not i:
|
||
|
|
return None
|
||
|
|
return {
|
||
|
|
'id': i.id,
|
||
|
|
'class_id': i.class_id,
|
||
|
|
'class_name': i.class_name,
|
||
|
|
'ht_id': i.ht_id,
|
||
|
|
't_id': i.t_id,
|
||
|
|
'start_time': i.start_time
|
||
|
|
}
|
||
|
|
def delete_class_dao(id,db): #软删除
|
||
|
|
try:
|
||
|
|
rows = (db.query(Class)
|
||
|
|
.filter(Class.id == id,Class.is_deleted==0)#只匹配还没处于软删除状态的行
|
||
|
|
.update({Class.is_deleted:1},synchronize_session=False))#软删除就是把这个字段改成True
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
print('删除失败:', repr(e))
|
||
|
|
return False, str(e)
|
||
|
|
else:
|
||
|
|
db.commit()
|
||
|
|
return rows
|
||
|
|
def hard_delete_class_dao(id,db): #物理删除
|
||
|
|
try:
|
||
|
|
rows = (db.query(Class).filter(Class.id == id).delete(synchronize_session=False))
|
||
|
|
db.commit()
|
||
|
|
return rows
|
||
|
|
except IntegrityError:
|
||
|
|
db.rollback()
|
||
|
|
return False, '该班级被其他数据引用,无法彻底删除'
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
print('物理删除失败', repr(e))
|
||
|
|
return False, str(e)
|
||
|
|
def class_no_exist(class_id,db):
|
||
|
|
return (db.query(Class)
|
||
|
|
.filter(Class.class_id == class_id,Class.is_deleted==0)
|
||
|
|
.first() is not None
|
||
|
|
)
|