test一轮完成的版本
This commit is contained in:
+25
-21
@@ -1,27 +1,31 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func
|
||||
from model.wl_student_model import Student
|
||||
# from model.class_ import Class # 你同学写的
|
||||
from model.wl_student_model import student
|
||||
from model.wl_class_model import Class # 你同学写的
|
||||
from scheme.wl_student_scheme import StudentCreate, StudentUpdate
|
||||
|
||||
|
||||
# ---- 学号生成 ----
|
||||
def generate_stu_no(db: Session, class_id: int, seq: int = None) -> str:
|
||||
cls = db.query(Class).filter_by(id=class_id).first()
|
||||
if not cls or not cls.start_date:
|
||||
raise ValueError("班级不存在或未设置开班时间")
|
||||
date_str = cls.start_date.strftime("%Y%m%d")
|
||||
#非测试就解除下面5行测试,注释date_str = "20260912"
|
||||
# cls = db.query(class_).filter_by(class_id=class_id).first()
|
||||
# if not cls or not cls.start_time:
|
||||
# raise ValueError("班级不存在或未设置开班时间")
|
||||
# date_str = cls.start_time.strftime("%Y%m%d")
|
||||
# 直接写死一个日期字符串用于测试
|
||||
date_str = "20260912"
|
||||
|
||||
if seq is None:
|
||||
count = db.query(func.count(Student.stu_id)) \
|
||||
.filter(Student.class_id == class_id).scalar() or 0
|
||||
count = db.query(func.count(student.stu_id)) \
|
||||
.filter(student.class_id == class_id).scalar() or 0
|
||||
seq = count + 1
|
||||
return f"{date_str}{seq:03d}"
|
||||
|
||||
|
||||
# ---- 增 ----
|
||||
def create_student(db: Session, data: StudentCreate) -> Student:
|
||||
def create_student(db: Session, data: StudentCreate) -> student:
|
||||
stu_no = generate_stu_no(db, data.class_id)
|
||||
stu = Student(stu_no=stu_no, **data.model_dump())
|
||||
stu = student(stu_no=stu_no, **data.model_dump())
|
||||
db.add(stu)
|
||||
db.commit()
|
||||
db.refresh(stu)
|
||||
@@ -31,29 +35,29 @@ def create_student(db: Session, data: StudentCreate) -> Student:
|
||||
# ---- 查(多条件)----
|
||||
def search_students(db: Session, stu_no=None, stu_name=None,
|
||||
class_id=None, advisor_id=None, status=None):
|
||||
q = db.query(Student).filter(Student.is_deleted == 0) # 若加了逻辑删除字段
|
||||
q = db.query(student).filter(student.is_deleted == 0) # 若加了逻辑删除字段
|
||||
if stu_no:
|
||||
q = q.filter(Student.stu_no.like(f"%{stu_no}%"))
|
||||
q = q.filter(student.stu_no.like(f"%{stu_no}%"))
|
||||
if stu_name:
|
||||
q = q.filter(Student.stu_name.like(f"%{stu_name}%"))
|
||||
q = q.filter(student.stu_name.like(f"%{stu_name}%"))
|
||||
if class_id:
|
||||
q = q.filter(Student.class_id == class_id)
|
||||
q = q.filter(student.class_id == class_id)
|
||||
if advisor_id:
|
||||
q = q.filter(Student.advisor_id == advisor_id)
|
||||
q = q.filter(student.advisor_id == advisor_id)
|
||||
if status:
|
||||
q = q.filter(Student.status == status)
|
||||
q = q.filter(student.status == status)
|
||||
return q.all()
|
||||
|
||||
|
||||
def get_student_by_no(db: Session, stu_no: str) -> Student | None:
|
||||
return db.query(Student).filter(
|
||||
Student.stu_no == stu_no,
|
||||
Student.is_deleted == 0
|
||||
def get_student_by_no(db: Session, stu_no: str) -> student | None:
|
||||
return db.query(student).filter(
|
||||
student.stu_no == stu_no,
|
||||
student.is_deleted == 0
|
||||
).first()
|
||||
|
||||
|
||||
# ---- 改 ----
|
||||
def update_student(db: Session, stu_no: str, data: StudentUpdate) -> Student | None:
|
||||
def update_student(db: Session, stu_no: str, data: StudentUpdate) -> student | None:
|
||||
stu = get_student_by_no(db, stu_no)
|
||||
if not stu:
|
||||
return None
|
||||
|
||||
@@ -3,7 +3,7 @@ from fastapi import FastAPI
|
||||
from api import wl_student_api # 周学灵的路由
|
||||
from database import Base, engine
|
||||
from model import wl_student_model as student_model
|
||||
|
||||
from model import wl_class_model,wl_advisor_model
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
@@ -13,7 +13,7 @@ app.include_router(wl_student_api.router)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="127.0.0.1", port=8000)
|
||||
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=8001,reload=True)
|
||||
|
||||
|
||||
|
||||
@@ -14,16 +14,16 @@ class Advisor(Base):
|
||||
|
||||
__tablename__ = "advisor"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True, comment="顾问编号")
|
||||
name = Column(String(50), nullable=False, index=True, comment="顾问姓名")
|
||||
gender = Column(String(8), comment="性别")
|
||||
phone = Column(String(20), index=True, comment="手机号")
|
||||
email = Column(String(64), comment="邮箱")
|
||||
region = Column(String(64), comment="负责区域/招生渠道")
|
||||
adv_id = Column(Integer, primary_key=True, autoincrement=True, comment="顾问编号")
|
||||
adv_name = Column(String(50), nullable=False, index=True, comment="顾问姓名")
|
||||
adv_gender = Column(String(8), comment="性别")
|
||||
adv_phone = Column(String(20), index=True, comment="手机号")
|
||||
adv_email = Column(String(64), comment="邮箱")
|
||||
adv_region = Column(String(64), comment="负责区域/招生渠道")
|
||||
is_deleted = Column(SmallInteger,nullable=False, default=0,server_default="0",
|
||||
index=True,comment="1=已删除",)
|
||||
|
||||
students = relationship("Student",primaryjoin="and_(Advisor.id == Student.advisor_id, Student.is_deleted == 0)",
|
||||
foreign_keys="Student.advisor_id",viewonly=True,lazy="selectin",)
|
||||
#下面的内容临时注释,在main导入时,SQLAlchemy 在配置映射时就会报错,错误大概是:ArgumentError: Could not locate any relevant foreign key columns for primary join condition ...
|
||||
# students = relationship("Student",primaryjoin="and_(Advisor.id == stu_id, Student.is_deleted == 0)",
|
||||
# foreign_keys="stu_id",viewonly=True,lazy="selectin",)
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
#班级管理 熊浩钦
|
||||
from sqlalchemy import Column, Integer, DateTime
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy import Column, Integer, Date, String
|
||||
from database import Base
|
||||
|
||||
class WlClass(Base):
|
||||
__tablename__ = "wl_class"
|
||||
class_id = Column(Integer, primary_key=True, index=True, comment="班级编号")
|
||||
start_time = Column(DateTime, comment="开课时间")
|
||||
is_deleted = Column(Integer, default=0, comment="逻辑删除标记(0正常,1已删)")
|
||||
class Class_(Base):
|
||||
__tablename__ = "class_info"
|
||||
|
||||
class_id = Column(Integer, primary_key=True, autoincrement=True, comment="班级编号")
|
||||
start_time = Column(Date, comment="开课时间")
|
||||
+2
-19
@@ -1,19 +1,12 @@
|
||||
#学生就业 赵康宁
|
||||
from sqlalchemy import *
|
||||
from sqlalchemy.orm import *
|
||||
|
||||
db_url = "sqlalchemy+pymysql://root:123456@localhost/Student"
|
||||
engine = create_engine(db_url, pool_size=8)
|
||||
Base = declarative_base()
|
||||
|
||||
emp_stu = Table('emp_stu',
|
||||
Base.metadata,
|
||||
Column('stu_id', Integer, ForeignKey('stu_id'), primary_key=True))
|
||||
|
||||
class emp(Base):
|
||||
class Emp(Base):
|
||||
__tablename__ = "wl_emp"
|
||||
|
||||
stu_id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
|
||||
emp_open_time = Column(DateTime, default="2026-11-30 18:18:18")
|
||||
offer_time = Column(DateTime, default=func.now())
|
||||
company_name = Column(String(100), nullable=False)
|
||||
@@ -24,13 +17,3 @@ class emp(Base):
|
||||
def __repr__(self):
|
||||
pass
|
||||
|
||||
|
||||
SessionLocal = sessionmaker(bind=engine)
|
||||
db_session = SessionLocal()
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
"""
|
||||
数据增删改查
|
||||
"""
|
||||
db_session.close()
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#学生考核成绩 圣国伟
|
||||
from sqlalchemy import Column, Integer, Float
|
||||
|
||||
from sqlalchemy import Column, Integer, Float
|
||||
from database import Base
|
||||
class StudentScore(Base):
|
||||
|
||||
class Score(Base):
|
||||
__tablename__ = "student_score"
|
||||
|
||||
stu_id = Column(Integer, primary_key=True, comment="学生编号")
|
||||
@@ -11,4 +10,3 @@ class StudentScore(Base):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class Student(Base):
|
||||
stu_id = Column(Integer, primary_key=True, autoincrement=True, comment='内部主键')
|
||||
# ---------- 业务学号(对外,按规则生成)----------
|
||||
stu_no = Column(String(20), unique=True, nullable=False, index=True, comment='学号')
|
||||
# class_id = Column(Integer, ForeignKey('wl_class.id'), nullable=True, comment='班级ID')
|
||||
class_id = Column(Integer, ForeignKey('class_info.class_id'), nullable=True, comment='班级ID')
|
||||
stu_name = Column(String(30), nullable=True, comment='学生姓名')
|
||||
native_place = Column(String(50), nullable=True, comment='籍贯')
|
||||
graduate_school=Column(String(128), nullable=True, comment='毕业学校')
|
||||
@@ -18,7 +18,7 @@ class Student(Base):
|
||||
in_time= Column(Date, nullable=True, comment='入学时间')
|
||||
out_time= Column(Date, nullable=True, comment='毕业时间')
|
||||
edu= Column(String(32), nullable=True, comment='学历')
|
||||
# advisor_id = Column(Integer, ForeignKey('wl_advisor.id'), nullable=True, comment='顾问ID')
|
||||
advisor_id = Column(Integer, ForeignKey('advisor.adv_id'), nullable=True, comment='顾问ID')
|
||||
stu_age = Column(Integer, nullable=True, comment='年龄')
|
||||
stu_gender = Column(String(8), nullable=True, comment='性别')
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
#老师管理模块 刘盼
|
||||
from sqlalchemy import create_engine
|
||||
from database import Base, engine
|
||||
|
||||
class Teacher(Base):
|
||||
__tablename__ = 'wl_teacher'
|
||||
t_id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
teacher_no = Column(String(20), unique=True, nullable=False)
|
||||
t_no = Column(String(20), unique=True, nullable=False)
|
||||
t_name = Column(String(50), nullable=False)
|
||||
t_phone = Column(String(20), unique=True, nullable=True)
|
||||
t_email = Column(String(20), unique=False)
|
||||
|
||||
Reference in New Issue
Block a user