Files
SST/PythonProject/dao/student_dao.py
T

84 lines
2.6 KiB
Python
Raw Normal View History

2026-09-21 14:44:11 +08:00
from model.all_model import Student_Model
2026-09-21 09:39:28 +08:00
from typing import List, Optional, Dict, Any
2026-09-21 22:25:16 +08:00
from datetime import datetime
2026-09-22 14:34:22 +08:00
from sqlalchemy.exc import IntegrityError
2026-09-21 14:44:11 +08:00
2026-09-21 18:04:38 +08:00
def add_student_dao(o,db):
2026-09-22 14:34:22 +08:00
if o.get('id_card'):
conflict = (db.query(Student_Model)
.filter(Student_Model.id_card == o['id_card'],
Student_Model.delete_status == 0)
.first())
if conflict:
return 'conflict'
2026-09-21 09:39:28 +08:00
try:
2026-09-22 14:34:22 +08:00
o2 = Student_Model(**o)
db.add(o2)
2026-09-21 22:25:16 +08:00
db.commit()
2026-09-22 14:34:22 +08:00
db.refresh(o2) # 回填自增的 stu_id
return o2
except IntegrityError:
2026-09-21 11:43:34 +08:00
db.rollback()
2026-09-22 14:34:22 +08:00
return 'conflict'
except Exception:
db.rollback()
return 'error'
2026-09-21 11:43:34 +08:00
2026-09-21 18:04:38 +08:00
def delete_student_dao(stu_id,db):
2026-09-21 11:43:34 +08:00
try:
2026-09-21 12:10:19 +08:00
rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)\
2026-09-21 22:25:16 +08:00
.update({'delete_status':1,'delete_time': datetime.now()})
2026-09-21 11:43:34 +08:00
db.commit()
2026-09-21 09:39:28 +08:00
except:
db.rollback()
rows = 0
finally:
return rows
2026-09-20 15:53:36 +08:00
2026-09-21 18:04:38 +08:00
def update_student_dao(stu_id,update_data,db):
2026-09-22 14:58:51 +08:00
if update_data.get('id_card'):
conflict = (db.query(Student_Model)
.filter(Student_Model.id_card == update_data['id_card'],
Student_Model.stu_id != stu_id,
Student_Model.delete_status == 0)
.first())
if conflict:
return 'conflict'
2026-09-21 09:39:28 +08:00
try:
2026-09-22 14:58:51 +08:00
rows = (db.query(Student_Model)
.filter(Student_Model.stu_id == stu_id,
Student_Model.delete_status == 0)
.update(update_data))
2026-09-22 14:34:22 +08:00
db.commit()
2026-09-22 14:58:51 +08:00
except IntegrityError:
db.rollback()
return 'conflict'
except Exception:
db.rollback()
return 'error'
return rows
2026-09-21 09:39:28 +08:00
def get_student_dao(stu_id:Optional[int]
,stu_name:Optional[str]
,class_id:Optional[int]
,page: int
,page_size: int
2026-09-21 18:04:38 +08:00
,db
2026-09-21 09:39:28 +08:00
) -> tuple[List[Dict[str, Any]], int]:
2026-09-21 18:04:38 +08:00
try:
2026-09-22 09:56:31 +08:00
q = db.query(Student_Model).filter(Student_Model.delete_status == 0)
2026-09-21 18:04:38 +08:00
if stu_id:
2026-09-22 09:56:31 +08:00
q = q.filter(Student_Model.stu_id == stu_id)
2026-09-21 18:04:38 +08:00
if stu_name and stu_name.strip() != "":
2026-09-22 09:56:31 +08:00
q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%"))
2026-09-21 18:04:38 +08:00
if class_id:
2026-09-22 09:56:31 +08:00
q= q.filter(Student_Model.class_id == class_id)
2026-09-21 18:04:38 +08:00
total = q.count()
r = q.offset((page - 1) * page_size).limit(page_size).all()
2026-09-21 22:25:16 +08:00
return r,total
2026-09-21 18:04:38 +08:00
except:
db.rollback()
return [],0