2026-09-21 12:36:57 +08:00
|
|
|
from schemas.student import *
|
|
|
|
|
from core.database import get_db
|
|
|
|
|
from models.student import *
|
2026-09-21 17:51:33 +08:00
|
|
|
from sqlalchemy.orm import Session
|
2026-09-21 12:36:57 +08:00
|
|
|
|
|
|
|
|
|
2026-09-21 19:28:15 +08:00
|
|
|
def create_students(d: dict, db: Session):
|
|
|
|
|
stu = Student(**d)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
db.add(stu)
|
2026-09-21 20:42:53 +08:00
|
|
|
# 把 SQL 发给数据库执行
|
2026-09-21 19:28:15 +08:00
|
|
|
db.flush()
|
2026-09-21 20:42:53 +08:00
|
|
|
# 根据 id 生成学生编号
|
2026-09-21 19:28:15 +08:00
|
|
|
if not stu.student_no:
|
|
|
|
|
stu.student_no = f"STU{stu.id:04d}"
|
|
|
|
|
db.commit()
|
|
|
|
|
db.refresh(stu)
|
|
|
|
|
return stu
|
|
|
|
|
except Exception as e:
|
|
|
|
|
db.rollback()
|
|
|
|
|
print(f"插入学生失败: {e}")
|
2026-09-21 20:42:53 +08:00
|
|
|
return None
|
|
|
|
|
|
2026-09-21 21:09:38 +08:00
|
|
|
# def updet_students():
|