import os,re,json

#读取题库json文件并转化为字典
with open(r"../题库0.3/Problems.json","r",encoding = "utf8") as f:
    database = f.read()
pro_dict = json.loads(database)

#读取目标数据库json并转化为字典
with open(r"../题库0.3/LessonObj.json","r",encoding = "utf8") as f:
    database = f.read()
obj_dict = json.loads(database)

#以下是单元题目统计
tags_descr = ["第一单元","第二单元","第三单元","第四单元","第五单元","第六单元","第七单元","第八单元","第九单元","暂无对应"] 
tags_dict = {}
units_string = ""
for t in tags_descr:
    tags_dict[t] = [0,""]
for p in pro_dict:
    for t in tags_descr:
        if t in pro_dict[p]["tags"]:
            tags_dict[t][0] += 1
            tags_dict[t][1] += p + ","
#units_string是简要列表, tags_dict是详细清单
for t in tags_descr:
    units_string += t + ": " + str(tags_dict[t][0]) + "\n" 
#单元统计完成

#以下是课时题目统计
#生成单元课时列表(Kddll)
lessons_descr = [] 
for o in obj_dict:
    if not o[:5] in lessons_descr:
        lessons_descr.append(o[:5])
lessons_dict = {}
lessons_string = ""
for l in lessons_descr:
    lessons_dict[l] = [0,""]
for p in pro_dict:
    for l in lessons_descr:
        for o in pro_dict[p]["objs"]:
            if l in o:
                lessons_dict[l][0] += 1
                lessons_dict[l][1] += p + ","
                break
#lessons_string是简要列表, lessons_dict是详细清单
for l in lessons_descr:
    lessons_string += l + ": " + str(lessons_dict[l][0]) + "\n" 
#课时统计完成

#以下是目标题目统计
objs_dict = {}
objs_string = ""
for o in obj_dict:
    objs_dict[o] = [0,""]
for p in pro_dict:
    for o in obj_dict:
        if o in pro_dict[p]["objs"]:
            objs_dict[o][0] += 1
            objs_dict[o][1] += p + ","
# objs_string是简要列表, objs_dict是详细清单
for o in obj_dict:
    objs_string += o + ": " + str(objs_dict[o][0]) + "\n" 
#课时统计完成

#以下为清点结果输出
with open(r"临时文件\单元课时目标题目数据清点结果.txt","w",encoding = "utf8") as f:
    f.write("---单元清点结果---\n")
    f.write(units_string)
    f.write("\n---课时清点结果---\n")
    f.write(lessons_string)
    f.write("\n---目标清点结果---\n")
    f.write(objs_string)
    f.write("\n"*10)
    f.write("---单元详细清点结果数据库---\n")
    f.write(json.dumps(tags_dict,indent = 4,ensure_ascii = False))
    f.write("\n"*5+"---课时详细清点结果数据库---\n")
    f.write(json.dumps(lessons_dict,indent = 4,ensure_ascii = False))
    f.write("\n"*5+"---目标详细清点结果数据库---\n")
    f.write(json.dumps(objs_dict,indent = 4,ensure_ascii = False))

print("已输出至文件",r"临时文件\单元课时目标题目数据清点结果.txt")