# init_test_data.py # 一次性灌入测试数据,运行前确保已建表且数据库连接正常 import random from datetime import date, timedelta from database import SessionLocal from model.teacher_model import Teacher from model.advisor_model import AdvisorInfo from model.cls_mgmt_model import ClsMgmt from model.stu_model import StuInfo from model.stu_score_model import StuScore from model.employ_model import StudentEmployManage random.seed(42) # 保证每次生成的数据一致 db = SessionLocal() # ---------- 1. 教师 6 人 ---------- teachers = [ Teacher(id="T001", name="张伟", phone="13800000001", type="班主任", is_deleted=0), Teacher(id="T002", name="李娜", phone="13800000002", type="班主任", is_deleted=0), Teacher(id="T003", name="王强", phone="13800000003", type="主讲", is_deleted=0), Teacher(id="T004", name="赵敏", phone="13800000004", type="主讲", is_deleted=0), Teacher(id="T005", name="刘洋", phone="13800000005", type="主讲", is_deleted=0), Teacher(id="T006", name="陈静", phone="13800000006", type="班主任", is_deleted=0), ] db.add_all(teachers) # ---------- 2. 顾问 8 人 ---------- advisors = [ AdvisorInfo(id="A001", name="孙悦", phone="13900000001", is_deleted=0), AdvisorInfo(id="A002", name="周杰", phone="13900000002", is_deleted=0), AdvisorInfo(id="A003", name="吴倩", phone="13900000003", is_deleted=0), AdvisorInfo(id="A004", name="郑凯", phone="13900000004", is_deleted=0), AdvisorInfo(id="A005", name="冯雪", phone="13900000005", is_deleted=0), AdvisorInfo(id="A006", name="韩磊", phone="13900000006", is_deleted=0), AdvisorInfo(id="A007", name="许晴", phone="13900000007", is_deleted=0), AdvisorInfo(id="A008", name="何勇", phone="13900000008", is_deleted=0), ] db.add_all(advisors) # ---------- 3. 班级 5 个 ---------- classes = [ ClsMgmt(id="C001", cls_start_date=date(2024, 3, 1), head_tea_id="T001", lecturer_id="T003", is_deleted=0), ClsMgmt(id="C002", cls_start_date=date(2024, 4, 1), head_tea_id="T002", lecturer_id="T004", is_deleted=0), ClsMgmt(id="C003", cls_start_date=date(2024, 5, 1), head_tea_id="T006", lecturer_id="T005", is_deleted=0), ClsMgmt(id="C004", cls_start_date=date(2024, 6, 1), head_tea_id="T001", lecturer_id="T003", is_deleted=0), ClsMgmt(id="C005", cls_start_date=date(2024, 7, 1), head_tea_id="T002", lecturer_id="T004", is_deleted=0), ] db.add_all(classes) # ---------- 4. 学生 45 人 ---------- surnames = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜" given_names = ["伟", "娜", "强", "敏", "洋", "静", "磊", "婷", "超", "丽", "军", "梅", "杰", "雪", "鹏", "芳", "浩", "娟", "宇", "艳"] grad_schools = ["北京大学", "清华大学", "复旦大学", "浙江大学", "武汉大学", "四川大学", "中山大学", "南京大学", "华中科技大学", "西安交通大学"] majors = ["计算机科学与技术", "软件工程", "人工智能", "数据科学", "信息管理"] educations = ["本科", "硕士", "大专"] hometowns = ["北京", "上海", "广州", "深圳", "杭州", "成都", "武汉", "西安"] students = [] for i in range(1, 46): name = random.choice(surnames) + random.choice(given_names) stu = StuInfo( id=f"S{i:04d}", cls_id=random.choice(["C001", "C002", "C003", "C004", "C005"]), name=name, gender=random.choice(["男", "女"]), age=random.randint(20, 28), hometown=random.choice(hometowns), grad_school=random.choice(grad_schools), major=random.choice(majors), education=random.choice(educations), enr_date=date(2023, 9, 1) + timedelta(days=random.randint(0, 300)), grad_date=date(2025, 6, 30) + timedelta(days=random.randint(0, 180)), advisor_id=f"A{random.randint(1, 8):03d}", state=random.choice(["在读", "在读", "在读", "毕业"]), # 在读占多数 is_deleted=0, ) students.append(stu) db.add_all(students) # ---------- 5. 成绩(每人 1~2 次考试) ---------- levels = ["优秀", "良好", "普通", "及格", "不及格"] scores = [] for stu in students: for attempt in range(1, random.randint(2, 3)): # 1 或 2 次 scores.append(StuScore( stu_id=stu.id, exam_attempt=attempt, exam_score=round(random.uniform(45, 100), 2), score_level=random.choice(levels), is_deleted=False, )) db.add_all(scores) # ---------- 6. 就业(约一半学生) ---------- companies = ["字节跳动", "腾讯", "阿里巴巴", "美团", "京东", "百度", "网易", "小米", "华为", "滴滴"] emp_list = [] for stu in random.sample(students, 25): # 25 人已就业 open_time = date(2025, 1, 1) + timedelta(days=random.randint(0, 120)) offer_time = open_time + timedelta(days=random.randint(10, 60)) emp_list.append(StudentEmployManage( stu_id=stu.id, emp_open_time=open_time, send_offer_time=offer_time, emp_company=random.choice(companies), salary=round(random.uniform(6000, 25000), 2), is_deleted=0, )) db.add_all(emp_list) # ---------- 提交 ---------- try: db.commit() print("✅ 测试数据导入成功") print(f"教师 {len(teachers)} 人, 顾问 {len(advisors)} 人, 班级 {len(classes)} 个, " f"学生 {len(students)} 人, 成绩 {len(scores)} 条, 就业 {len(emp_list)} 条") except Exception as e: db.rollback() print("❌ 导入失败:", e) finally: db.close()