# encoding: utf8
import time
import pymysql
import random

last_names = "李，王，张，刘，陈，杨，黄，赵，周，吴，徐，孙，朱，马，胡，郭，林，何，高，梁，郑，罗，宋，谢，唐，韩，曹，许，邓，萧，冯，曾，程，蔡，彭，潘，袁，于，董，余，苏，叶，吕，魏，蒋，田，杜，丁，沈，姜，范，江，傅，钟，卢，汪，戴，崔，任，陆，廖，姚，方，金，邱，夏，谭，韦，贾，邹，石，熊，孟，秦，阎，薛，侯，雷，白，龙，段，郝，孔，邵，史，毛，常，万，顾，赖，武，康，贺，严，尹，钱，施，牛，洪，龚"
last_names_list = last_names.split("，")
course_names = "语文、数学、外语、历史、思想政治、地理、 化学、物理、生物、体育、美术、音乐、技术、微积分、体育、毛泽东思想概论、思想道德修养、高等数学、线性代数、概率论与数理统计、离散数学、计算机原理、人工智能、程序设计基础、面向对象程序设计、数字逻辑电路、电路电子技术、数据结构与算法、WEB程序设计、计算机组成与结构、操作系统、数据库系统原理、编译原理、计算机网络"
course_names_list = course_names.split("、")


# 创建mysql连接
conn = pymysql.connect(user="root", password="cyh0110", database="SCT", host="127.0.0.1", port=3306)
# 创建游标
cursor = conn.cursor()

sid_list = []       # 用于存储生成的sid
cid_list = []       # 用于存储生成的cid


def student_random_record():
    """
    # 随机生成10000条学生表数据
    :return:
    """
    year = time.localtime().tm_year     # 获取当前系统时间的年份
    num = 1
    class_num = 1
    class_id = 1
    for i in range(10000):
        sid = "%d" % year + "%04d" % num
        sname = random.choice(last_names_list) + sid
        ssex = random.choice(["男", "女"])
        sage = random.randint(15, 35)
        sclass = "%d" % year + "%02d" % class_id
        sql = f"INSERT INTO student VALUES ('{sid}', '{sname}', '{ssex}', {sage}, '{sclass}')"
        cursor.execute(sql)
        sid_list.append(sid)
        if num == 2500:
            year += 1
            num = 1
            class_id = 1
        if class_num == 30:
            class_id += 1
            class_num = 1
        num += 1
        class_num += 1
    conn.commit()


def course_random_record():
    """
    随机生成1000条课程表数据
    :return:
    """
    hours = [i for i in range(8, 192 + 1, 8)]
    for i in range(1000):
        cid = "%03d" % (i + 1)
        cname = random.choice(course_names_list)
        hour = random.choice(hours)
        credit = round(hour / 16, 2)
        sql = f"INSERT INTO course (cid, cname, credit, chours) VALUES ('{cid}', '{cname}', {credit}, {hour})"
        cursor.execute(sql)
        cid_list.append(cid)
    conn.commit()


def sc_random_record():
    """
    随机生成10000 * 30条以上选课表数据
    :return:
    """
    for sid in sid_list:
        year = int(sid[:4])      # 取sid的前四位学年
        for i in range((year - time.localtime().tm_year + 1) * 16):
            cid = random.choice(cid_list)
            score = random.randint(0, 101)
            sql = f"INSERT INTO sc(sid, cid, score) VALUES ('{sid}', '{cid}', {score})"
            cursor.execute(sql)
    conn.commit()


if __name__ == '__main__':
    student_random_record()
    course_random_record()
    sc_random_record()
    # 关闭数据库连接
    cursor.close()
    conn.close()