From c4ae803a343fdf3a314e63df457bbd6dd18dd896 Mon Sep 17 00:00:00 2001 From: zxl <2290782958@qq.com> Date: Sat, 12 Sep 2026 20:55:36 +0800 Subject: [PATCH] =?UTF-8?q?test=E4=B8=80=E8=BD=AE=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/wl_student_dao.py | 46 +++++++++++++++++++++------------------ main.py | 4 ++-- model/wl_advisor_model.py | 18 +++++++-------- model/wl_class_model.py | 14 +++++------- model/wl_emp_model.py | 21 ++---------------- model/wl_score_model.py | 8 +++---- model/wl_student_model.py | 4 ++-- model/wl_teacher_model.py | 3 +-- 8 files changed, 50 insertions(+), 68 deletions(-) diff --git a/dao/wl_student_dao.py b/dao/wl_student_dao.py index 6e9bba3..c2aa714 100644 --- a/dao/wl_student_dao.py +++ b/dao/wl_student_dao.py @@ -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 diff --git a/main.py b/main.py index e11b2ee..10e1ed1 100644 --- a/main.py +++ b/main.py @@ -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) diff --git a/model/wl_advisor_model.py b/model/wl_advisor_model.py index 63a9d4c..fca0165 100644 --- a/model/wl_advisor_model.py +++ b/model/wl_advisor_model.py @@ -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",) diff --git a/model/wl_class_model.py b/model/wl_class_model.py index 4283562..678ad65 100644 --- a/model/wl_class_model.py +++ b/model/wl_class_model.py @@ -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已删)") \ No newline at end of file +class Class_(Base): + __tablename__ = "class_info" + + class_id = Column(Integer, primary_key=True, autoincrement=True, comment="班级编号") + start_time = Column(Date, comment="开课时间") \ No newline at end of file diff --git a/model/wl_emp_model.py b/model/wl_emp_model.py index 8084bef..f68f679 100644 --- a/model/wl_emp_model.py +++ b/model/wl_emp_model.py @@ -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() - diff --git a/model/wl_score_model.py b/model/wl_score_model.py index 48753f1..f457173 100644 --- a/model/wl_score_model.py +++ b/model/wl_score_model.py @@ -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): - diff --git a/model/wl_student_model.py b/model/wl_student_model.py index bbdd7c3..3066dd1 100644 --- a/model/wl_student_model.py +++ b/model/wl_student_model.py @@ -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='性别') diff --git a/model/wl_teacher_model.py b/model/wl_teacher_model.py index 0ff0454..e65978b 100644 --- a/model/wl_teacher_model.py +++ b/model/wl_teacher_model.py @@ -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)