dm/python/teacher_func.py

285 lines
9.9 KiB
Python
Raw Normal View History

2024-06-19 13:13:34 +08:00
import dmPython
import json
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import base64
import datetime
2024-06-19 19:23:01 +08:00
db = dmPython.connect(user='SYSDBA', password='dameng!!', host="36.138.114.105", port="32522")
2024-06-19 13:13:34 +08:00
#获取题目
def choice_question_func(ID):
question_cursor = db.cursor()
#查询表中字段,与所选两张表中字段相同的信息
question_cursor.execute(f'SELECT CHOICE_QUESTION_BANK.* FROM TEACHER_MIDDLE_SUBJECT INNER JOIN '
f'CHOICE_QUESTION_BANK ON TEACHER_MIDDLE_SUBJECT.SUBJECT = CHOICE_QUESTION_BANK.SUBJECT '
f'WHERE TEACHER_MIDDLE_SUBJECT.TEACHER_ID = (?)',(ID,))
choice_question_result = question_cursor.fetchall()
choice_list=[]
for i in choice_question_result:
choice_list.append(list(i))
question_cursor.close()
return choice_list
def completion_question_func(ID):
question_cursor = db.cursor()
question_cursor.execute(f'SELECT COMPLETION_QUESTION_BANK.* FROM TEACHER_MIDDLE_SUBJECT INNER JOIN '
f'COMPLETION_QUESTION_BANK ON TEACHER_MIDDLE_SUBJECT.SUBJECT = COMPLETION_QUESTION_BANK.SUBJECT '
f'WHERE TEACHER_MIDDLE_SUBJECT.TEACHER_ID = (?)',(ID,))
completion_question_result = question_cursor.fetchall()
completion_list=[]
for i in completion_question_result:
completion_list.append(list(i))
question_cursor.close()
return completion_list
def t_or_f_question_func(ID):
question_cursor = db.cursor()
question_cursor.execute(f'SELECT T_OR_F_QUESTION_BANK.* FROM TEACHER_MIDDLE_SUBJECT INNER JOIN '
f'T_OR_F_QUESTION_BANK ON TEACHER_MIDDLE_SUBJECT.SUBJECT = T_OR_F_QUESTION_BANK.SUBJECT '
f'WHERE TEACHER_MIDDLE_SUBJECT.TEACHER_ID = (?)',(ID,))
t_or_f_question_result = question_cursor.fetchall()
t_or_f_list=[]
for i in t_or_f_question_result:
t_or_f_list.append(list(i))
question_cursor.close()
return t_or_f_list
def find_classboss_succeed_func(teacher_id):
cursor = db.cursor()
cursor.execute(f'SELECT CLASS FROM TEACHER_MIDDLE_CLASS WHERE TEACHER_ID={teacher_id}')
class_list=cursor.fetchall()[0][0]
class_list=eval(class_list)
class_succeed_dic={}
for i in class_list:
cursor.execute(f"SELECT CLASS_MIDDLE_HEADTEACHER.HEADTEACHER FROM STUDENT_MIDDLE_CLASS INNER JOIN "
f"CLASS_MIDDLE_HEADTEACHER ON STUDENT_MIDDLE_CLASS.CLASS=CLASS_MIDDLE_HEADTEACHER.CLASS WHERE "
f"STUDENT_MIDDLE_CLASS.CLASS='{i}'")
list=cursor.fetchall()
list = [item[0] for item in list]
class_succeed_dic[i]=[list[0],len(list)]
cursor.close()
return class_succeed_dic
# find_classboss_succeed_func('111111')
def find_student_succeed_func(ID):
cursor=db.cursor()
cursor.execute(f'SELECT * FROM STUDENT WHERE ID={ID}')
student_succeed=cursor.fetchall()[0]
student_succeed_list=list(student_succeed)
student_succeed_list.pop(2)
if student_succeed_list[5]:
pass
else:
student_succeed_list[5]='未绑定电话号码'
cursor.close()
return student_succeed_list
# find_student_succeed_func('20240101')
def find_class_student(Class):
students_list=[]
cursor = db.cursor()
cursor.execute(f'SELECT STUDENT_ID FROM STUDENT_MIDDLE_CLASS WHERE CLASS=(?) ',(Class))
students=cursor.fetchall()
for i in students:
students_list.append(i[0])
cursor.close()
return students_list
def fetch_teacher_name(id):
cursor = db.cursor()
cursor.execute(f'SELECT TEACHER_NAME FROM TEACHER WHERE TEACHER_ID=(?)',(id,))
teacher_name=cursor.fetchall()[0][0]
cursor.close()
return teacher_name
def fetch_subject(id):
cursor = db.cursor()
cursor.execute(f'SELECT SUBJECT FROM TEACHER_MIDDLE_SUBJECT WHERE TEACHER_ID=(?)',(id,))
subject=cursor.fetchall()[0][0]
cursor.close()
return subject
#存储到TEST_BANK数据表里面
def save_test(choice_list,completion_list,judge_list,hour,min,stop_time,class_list,ID):
choice_list=json.dumps(choice_list)
completion_list=json.dumps(completion_list)
judge_list=json.dumps(judge_list)
class_list=json.dumps(class_list)
now_time = datetime.datetime.now().replace(microsecond=0)
end_time = now_time + datetime.timedelta(days=int(stop_time))
end_time=end_time.replace(microsecond=0)
subject=fetch_subject(ID)
cursor=db.cursor()
cursor.execute(f'INSERT INTO TEST_BANK (CHOICE_ID,COMPLE_ID,T_OR_F_ID,HOUR,MIN,RELEASETIME,STOPTIME,CLASS,SUBJECT,TEACHER_ID) VALUES(?,?,?,?,?,?,?,?,?,?)',
(choice_list,completion_list,judge_list,hour,min,now_time,end_time,class_list,subject,ID))
db.commit()
# 查找TEST_BANK里面的试卷ID
cursor.execute(f'SELECT ID FROM TEST_BANK WHERE RELEASETIME=?',(now_time,))
test_id=cursor.fetchall()[0][0]
print(test_id)
# 查找学生
student_list=[]
for i in eval(class_list):
for j in find_class_student(i):
student_list.append(j)
print(student_list)
end='false'
# 将试卷分配到每一个学生
for i in student_list:
cursor.execute("INSERT INTO STUDENT_TEST (STUDENT_ID,CLASS,SUBJECT,TEST_ID,TF) VALUES (?,?,?,?,?)",(i,i[0:6],subject,test_id,end))
db.commit()
cursor.close()
return '发布成功'
def find_default_class_func(class_ID,teacher_ID):
cursor = db.cursor()
cursor.execute(f'select * from TEST_BANK WHERE TEACHER_ID=?',(teacher_ID))
class_test_set=cursor.fetchall()
class_test_list=[]
class_test_dic={}
new_dic={}
subject=fetch_subject(teacher_ID)
for i in class_test_set:
class_test_list.append(list(i))
for i in class_test_list:
class_test_dic[i[8]]=i[5],i[6]
for key in class_test_dic:
cursor.execute(f'SELECT * FROM STUDENT_TEST WHERE SUBJECT=? AND CLASS=? AND TEST_ID=?',(subject,class_ID,key))
message_set=cursor.fetchall()
sum=0
number=0
for i in message_set:
if i[5]!=None:
sum+=int(i[5])
number+=1
try:
sum=sum/number
except ZeroDivisionError:
sum=0
new_dic[key]=class_test_dic[key],sum,number
return new_dic
# find_default_class_func('202401', '111111')
def detailed_class(testID, classID):
cursor = db.cursor()
cursor.execute(f'SELECT * FROM STUDENT_TEST WHERE TEST_ID=? AND CLASS=?',(testID,classID))
data=cursor.fetchall()
students=[]
StudentName=[]
for i in data:
students.append([i[0]])
students = [item for sublist in students for item in sublist]
for i in students:
cursor.execute(f'SELECT NAME FROM STUDENT WHERE ID=?',(i,))
name=cursor.fetchall()
StudentName.append(name[0][0])
data=[(*tup,name) for tup,name in zip(data,StudentName)]
cursor.close()
return data
# detailed_class('33','202401')
def train_question():
Train={}
cursor = db.cursor()
cursor.execute(f'SELECT * FROM CHOICE_QUESTION_BANK WHERE SUBJECT=(?)',('Train'))
data=cursor.fetchall()
Train['choice'] = data
cursor.execute(f'SELECT * FROM COMPLETION_QUESTION_BANK WHERE SUBJECT=(?)',('Train'))
data=cursor.fetchall()
Train['completion'] = data
cursor.execute(f'SELECT * FROM T_OR_F_QUESTION_BANK WHERE SUBJECT=(?)',('Train'))
data=cursor.fetchall()
Train['judge'] = data
# print(Train)
return Train
# train_question()
def SendTrainTestFunc(TrainChoice,TrainCompletion,TrainJudge,Hour,Min,StopTime,Class,Train,teacher_id):
TrainChoice = json.dumps(TrainChoice)
TrainCompletion = json.dumps(TrainCompletion)
TrainJudge = json.dumps(TrainJudge)
Class=json.dumps(Class)
Train=json.dumps(Train)
# 获取当前时间
now_time = datetime.datetime.now().replace(microsecond=0)
end_time = now_time + datetime.timedelta(days=int(StopTime))
end_time = end_time.replace(microsecond=0)
cursor = db.cursor()
cursor.execute(f'INSERT INTO TRAINTEST (CHOICE,COMPLE,JUDGE,HOUR,MIN,RELEASETIME,STOPTIME,CLASS,SUBJECT,TEACHER_ID,Train) VALUES'
f'(?,?,?,?,?,?,?,?,?,?,?)',(TrainChoice,TrainCompletion,TrainJudge,Hour,Min,now_time,end_time,Class,'Train',teacher_id,Train))
db.commit()
print('添加成功')
cursor.execute(f'SELECT ID FROM TRAINTEST WHERE RELEASETIME=?', (now_time,))
test_id = cursor.fetchall()[0][0]
# 查找学生
student_list = []
for i in eval(Class):
for j in find_class_student(i):
student_list.append(j)
end = 'false'
# 将试卷分配到每一个学生
for i in student_list:
cursor.execute("INSERT INTO TrainScore (STUDENT_ID,CLASS,TEST_ID,TF) VALUES (?,?,?,?)",
(i, i[0:6], test_id, end))
print('分配成功')
db.commit()
cursor.close()
return '发布成功'
2024-06-25 14:49:31 +08:00
2024-06-25 18:44:33 +08:00
# def Find_dic(dic):
# # 获取字典的键,并按照从大到小的顺序排序
# keys_sorted = sorted(dic.keys(), reverse=True)
#
# # 取最后三个键
# last_three_keys = keys_sorted[:3]
#
# # 创建一个新的字典,仅包含排序后的最后三个键及其对应的值
# dic_last_three = {key: dic[key] for key in last_three_keys}
#
# return dic_last_three # 返回一个新字典,该字典仅包含排序后的最后三个键及其对应的值
2024-06-26 11:24:33 +08:00
def Find_details_Func(ID):
2024-06-25 18:44:33 +08:00
dic = {}
2024-06-25 14:49:31 +08:00
cursor = db.cursor()
2024-06-25 18:44:33 +08:00
cursor.execute(f"SELECT * FROM STUDENT_TEST WHERE STUDENT_ID=? AND TF='true' ", (ID))
data = cursor.fetchall()
2024-06-25 14:49:31 +08:00
for i in data:
2024-06-25 18:44:33 +08:00
dic[i[3]] = i
2024-06-25 14:49:31 +08:00
cursor.close()
2024-06-25 18:44:33 +08:00
# 调用Find_dic函数并传入dic字典作为参数
2024-06-25 14:49:31 +08:00
return dic
2024-06-25 18:44:33 +08:00
# print(Find_details('20240101'))