From d63798aee59ecd345f2b95816081b19bef11704a Mon Sep 17 00:00:00 2001 From: zxl <2290782958@qq.com> Date: Sat, 12 Sep 2026 16:30:43 +0800 Subject: [PATCH] first --- .idea/misc.xml | 7 +++ .idea/wl_student_manager_system.iml | 10 ++++ api/wl_student_api.py | 45 ++++++++++++++++++ dao/wl_student_dao.py | 74 +++++++++++++++++++++++++++++ main.py | 19 ++++++++ model/__init__.py | 1 + model/wl_advisor_model.py | 29 +++++++++++ model/wl_class_model.py | 10 ++++ model/wl_emp_model.py | 36 ++++++++++++++ model/wl_score_model.py | 14 ++++++ model/wl_student_model.py | 30 ++++++++++++ model/wl_teacher_model.py | 23 +++++++++ scheme/wl_student_scheme.py | 48 +++++++++++++++++++ 13 files changed, 346 insertions(+) create mode 100644 .idea/misc.xml create mode 100644 .idea/wl_student_manager_system.iml create mode 100644 api/wl_student_api.py create mode 100644 dao/wl_student_dao.py create mode 100644 main.py create mode 100644 model/__init__.py create mode 100644 model/wl_advisor_model.py create mode 100644 model/wl_class_model.py create mode 100644 model/wl_emp_model.py create mode 100644 model/wl_score_model.py create mode 100644 model/wl_student_model.py create mode 100644 model/wl_teacher_model.py create mode 100644 scheme/wl_student_scheme.py diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..af2232e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/wl_student_manager_system.iml b/.idea/wl_student_manager_system.iml new file mode 100644 index 0000000..a448293 --- /dev/null +++ b/.idea/wl_student_manager_system.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/api/wl_student_api.py b/api/wl_student_api.py new file mode 100644 index 0000000..9a2f5cb --- /dev/null +++ b/api/wl_student_api.py @@ -0,0 +1,45 @@ +from fastapi import APIRouter, Depends, HTTPException, Query +from sqlalchemy.orm import Session +from database import get_db +from scheme.wl_student_scheme import StudentCreate, StudentUpdate, StudentOut +from dao import wl_student_dao + +router = APIRouter(prefix="/students", tags=["学生管理"]) + + +@router.get("", response_model=list[StudentOut]) +def list_students( + stu_no: str = Query(None), + stu_name: str = Query(None), + class_id: int = Query(None), + db: Session = Depends(get_db), +): + return wl_student_dao.search_students(db, stu_no, stu_name, class_id) + + +@router.post("", response_model=StudentOut) +def create_student(data: StudentCreate, db: Session = Depends(get_db)): + return wl_student_dao.create_student(db, data) + + +@router.get("/{stu_no}", response_model=StudentOut) +def get_student(stu_no: str, db: Session = Depends(get_db)): + stu = wl_student_dao.get_student_by_no(db, stu_no) + if not stu: + raise HTTPException(404, "学生不存在") + return stu + + +@router.put("/{stu_no}", response_model=StudentOut) +def update_student(stu_no: str, data: StudentUpdate, db: Session = Depends(get_db)): + stu = wl_student_dao.update_student(db, stu_no, data) + if not stu: + raise HTTPException(404, "学生不存在") + return stu + + +@router.delete("/{stu_no}") +def delete_student(stu_no: str, db: Session = Depends(get_db)): + if not wl_student_dao.delete_student(db, stu_no): + raise HTTPException(404, "学生不存在") + return {"msg": "删除成功"} diff --git a/dao/wl_student_dao.py b/dao/wl_student_dao.py new file mode 100644 index 0000000..6e9bba3 --- /dev/null +++ b/dao/wl_student_dao.py @@ -0,0 +1,74 @@ +from sqlalchemy.orm import Session +from sqlalchemy import func +from model.wl_student_model import Student +# from model.class_ 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") + if seq is None: + 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: + stu_no = generate_stu_no(db, data.class_id) + stu = Student(stu_no=stu_no, **data.model_dump()) + db.add(stu) + db.commit() + db.refresh(stu) + return stu + + +# ---- 查(多条件)---- +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) # 若加了逻辑删除字段 + if 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}%")) + if class_id: + q = q.filter(Student.class_id == class_id) + if advisor_id: + q = q.filter(Student.advisor_id == advisor_id) + if 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 + ).first() + + +# ---- 改 ---- +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 + for k, v in data.model_dump(exclude_unset=True).items(): + setattr(stu, k, v) + db.commit() + db.refresh(stu) + return stu + + +# ---- 逻辑删 ---- +def delete_student(db: Session, stu_no: str) -> bool: + stu = get_student_by_no(db, stu_no) + if not stu: + return False + stu.is_deleted = 1 + db.commit() + return True \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..e11b2ee --- /dev/null +++ b/main.py @@ -0,0 +1,19 @@ +#项目初始化入口 +from fastapi import FastAPI +from api import wl_student_api # 周学灵的路由 +from database import Base, engine +from model import wl_student_model as student_model + + +Base.metadata.create_all(bind=engine) + +app = FastAPI(title="沃林学生管理系统") +app.include_router(wl_student_api.router) + + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="127.0.0.1", port=8000) + + + diff --git a/model/__init__.py b/model/__init__.py new file mode 100644 index 0000000..cd87425 --- /dev/null +++ b/model/__init__.py @@ -0,0 +1 @@ +from model import wl_student_model \ No newline at end of file diff --git a/model/wl_advisor_model.py b/model/wl_advisor_model.py new file mode 100644 index 0000000..63a9d4c --- /dev/null +++ b/model/wl_advisor_model.py @@ -0,0 +1,29 @@ +from sqlalchemy import ( Column,Date,DateTime, + ForeignKey, + Integer, + SmallInteger, + String, + func, +) +from sqlalchemy.orm import relationship + +from database import Base + + +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="负责区域/招生渠道") + 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",) + + diff --git a/model/wl_class_model.py b/model/wl_class_model.py new file mode 100644 index 0000000..4283562 --- /dev/null +++ b/model/wl_class_model.py @@ -0,0 +1,10 @@ +#班级管理 熊浩钦 +from sqlalchemy import Column, Integer, DateTime +from sqlalchemy.orm import relationship +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 diff --git a/model/wl_emp_model.py b/model/wl_emp_model.py new file mode 100644 index 0000000..8084bef --- /dev/null +++ b/model/wl_emp_model.py @@ -0,0 +1,36 @@ +#学生就业 赵康宁 +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): + __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) + salary = Column(Integer, nullable=False, default=15000) + + student = relationship("wl_student", back_populates="wl_emp") + + 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 new file mode 100644 index 0000000..48753f1 --- /dev/null +++ b/model/wl_score_model.py @@ -0,0 +1,14 @@ +#学生考核成绩 圣国伟 +from sqlalchemy import Column, Integer, Float + +from database import Base +class StudentScore(Base): + __tablename__ = "student_score" + + stu_id = Column(Integer, primary_key=True, comment="学生编号") + exam_order = Column(Integer, primary_key=True, comment="考核序次") + score = Column(Float, nullable=False, comment="成绩") + + + + diff --git a/model/wl_student_model.py b/model/wl_student_model.py new file mode 100644 index 0000000..bbdd7c3 --- /dev/null +++ b/model/wl_student_model.py @@ -0,0 +1,30 @@ +#学生基本信息 周学灵 +from database import Base +from sqlalchemy import Column, Integer, String, Date, ForeignKey +from sqlalchemy.orm import relationship + + +class Student(Base): + __tablename__ = 'wl_student' + # ---------- 内部主键(隐藏,不对外)---------- + 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') + stu_name = Column(String(30), nullable=True, comment='学生姓名') + native_place = Column(String(50), nullable=True, comment='籍贯') + graduate_school=Column(String(128), nullable=True, comment='毕业学校') + major= Column(String(64), nullable=True, comment='专业') + 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') + stu_age = Column(Integer, nullable=True, comment='年龄') + stu_gender = Column(String(8), nullable=True, comment='性别') + + # class_ = relationship("Class", back_populates="students") + # advisor = relationship("Advisor", back_populates="students") + + is_deleted = Column(Integer, default=0, comment='逻辑删除: 0正常 1删除') + def __repr__(self): + return f"" \ No newline at end of file diff --git a/model/wl_teacher_model.py b/model/wl_teacher_model.py new file mode 100644 index 0000000..0ff0454 --- /dev/null +++ b/model/wl_teacher_model.py @@ -0,0 +1,23 @@ +#老师管理模块 刘盼 +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_name = Column(String(50), nullable=False) + t_phone = Column(String(20), unique=True, nullable=True) + t_email = Column(String(20), unique=False) + class_id = Column(Integer, ForeignKey("wl_class.id"), nullable=False) + role = Column(String(50), nullable=False) + is_deleted = Column(Integer, default=0) + + def generate_teacher_no(db: Session, hire_date: date): + """ + 工号规则:T + 入职日期(YYYYMMDD) + 3位当天序号 + 例如:T20250912001、T20250912002 + """ + date_str = hire_date.strftime("%Y%m%d") + prefix = f"T{date_str}" + diff --git a/scheme/wl_student_scheme.py b/scheme/wl_student_scheme.py new file mode 100644 index 0000000..ef96bc9 --- /dev/null +++ b/scheme/wl_student_scheme.py @@ -0,0 +1,48 @@ +from pydantic import BaseModel +from datetime import date +from typing import Optional +class StudentCreate(BaseModel): + class_id: Optional[int] = None + advisor_id: Optional[int] = None + stu_name: Optional[str] = None + native_place: Optional[str] = None + graduate_school: Optional[str] = None + major: Optional[str] = None + in_time: Optional[date] = None + out_time: Optional[date] = None + edu: Optional[str] = None + stu_age: Optional[int] = None + stu_gender: Optional[str] = None + # status: Optional[str] = None + +class StudentUpdate(BaseModel): + class_id: Optional[int] = None + advisor_id: Optional[int] = None + stu_name: Optional[str] = None + native_place: Optional[str] = None + graduate_school: Optional[str] = None + major: Optional[str] = None + in_time: Optional[date] = None + out_time: Optional[date] = None + edu: Optional[str] = None + stu_age: Optional[int] = None + stu_gender: Optional[str] = None + # status: Optional[str] = None + +class StudentOut(BaseModel): + stu_no: str + stu_name: Optional[str] = None + class_id: Optional[int] = None + advisor_id: Optional[int] = None + native_place: Optional[str] = None + graduate_school: Optional[str] = None + major: Optional[str] = None + in_time: Optional[date] = None + out_time: Optional[date] = None + edu: Optional[str] = None + stu_age: Optional[int] = None + stu_gender: Optional[str] = None + # status: Optional[str] = None + + class Config: + from_attributes = True \ No newline at end of file