Files

72 lines
2.2 KiB
Python
Raw Permalink 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
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-22 15:38:53 +08:00
def delete_student_dao(stu_id, db):
2026-09-21 11:43:34 +08:00
try:
2026-09-22 15:38:53 +08:00
rows = (db.query(Student_Model)
.filter(Student_Model.stu_id == stu_id,
Student_Model.delete_status == 0)
.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
return rows
2026-09-22 15:38:53 +08:00
except Exception:
db.rollback()
raise
2026-09-20 15:53:36 +08:00
2026-09-22 15:38:53 +08:00
def update_student_dao(stu_id, update_data, db):
if not update_data:
return 0
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-22 15:38:53 +08:00
q = db.query(Student_Model).filter(Student_Model.delete_status == 0)
if stu_id:
q = q.filter(Student_Model.stu_id == stu_id)
if stu_name and stu_name.strip() != "":
q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%"))
if class_id:
q = q.filter(Student_Model.class_id == class_id)
total = q.count()
r = q.offset((page - 1) * page_size).limit(page_size).all()
return r, total