import os,re,json

outputfile = "临时文件/已做题目.txt"
classid = "2023届高三02班"
new_dir = r"C:\Users\weiye\Documents\wwy sync\23届\四月错题重做"

old_dirs = [
r"C:\Users\weiye\Documents\wwy sync\23届\暑假概率初步续",   
r"C:\Users\weiye\Documents\wwy sync\23届\上学期测验卷",
r"C:\Users\weiye\Documents\wwy sync\23届\上学期周末卷",
r"C:\Users\weiye\Documents\wwy sync\23届\第一轮复习讲义",
r"C:\Users\weiye\Documents\wwy sync\23届\其他讲义与试卷",
r"C:\Users\weiye\Documents\wwy sync\23届\赋能",
r"C:\Users\weiye\Documents\wwy sync\23届\正态分布及成对数据新课",
r"C:\Users\weiye\Documents\wwy sync\23届\下学期测验卷",
r"C:\Users\weiye\Documents\wwy sync\23届\下学期周末卷",
r"C:\Users\weiye\Documents\wwy sync\23届\第二轮复习讲义"

]

def id_related(id,prodict,idrespdict):
    for rid in prodict[id]["related"]:
        if rid in idrespdict:
            return True
    return False

with open(r"../题库0.3/Problems.json","r",encoding="u8") as f:
    data = f.read()
pro_dict = json.loads(data)

# 收集数据构建已做过的题目的id与试卷-题号-题序对应字典
id_resp_dict = {}

for dir in old_dirs:
    for texfile in os.listdir(dir):
        if ".tex" in texfile:
            with open(os.path.join(dir,texfile),"r",encoding="u8") as f:
                data = f.read()
            ids = re.findall(r"\((\d{6})\)",data)
            for id in ids:
                if not id in id_resp_dict:
                    id_resp_dict[id] = [os.path.join(os.path.split(dir)[1],texfile[:-4])+"--题序"+str(ids.index(id)+1)]
                else:
                    id_resp_dict[id].append(os.path.join(os.path.split(dir)[1],texfile[:-4])+"--题序"+str(ids.index(id)+1))

new_texfiles = [f for f in os.listdir(new_dir) if ".tex" in f]

output = ""

for texfile in new_texfiles:
    with open(os.path.join(new_dir,texfile),"r",encoding="u8") as f:
        data = f.read()
    ids = re.findall(r"\((\d{6})\)",data)
    output += texfile[:-4] + "\n"
    print(texfile[:-4])
    output += ",".join(ids) + "\n"
    print(",".join(ids))
    for id in ids:
        if id in id_resp_dict:
            results = "\n".join([(id + "\t" + r) for r in pro_dict[id]["usages"] if classid in r])
            print("(%s)\t(已做题)\t%s"%(id,",".join(id_resp_dict[id])))
            print(results)
            output += "(%s)\t(已做题)\t%s\n"%(id,",".join(id_resp_dict[id]))
            output += results + "\n"
        elif id_related:
            rel_ids = pro_dict[id]["related"]
            origins = []
            for id1 in rel_ids:
                if id1 in id_resp_dict:
                    origins += id_resp_dict[id1]
            print("(%s)\t(关联题)\t%s"%(id,",".join(origins)))
            output += "(%s)\t(关联题)\t%s\n"%(id,",".join(origins))
            for id1 in rel_ids:
                results = "\n".join([(id1 + "\t" + r) for r in pro_dict[id1]["usages"] if classid in r])
                if len(results) > 2:
                    print(results)
                    output += results + "\n"
        else:
            print("(%s)\t(无关题)")
            output += "(%s)\t(无关题)\n"
            results = "\n".join([(id + "\t" + r) for r in pro_dict[id]["usages"] if classid in r])
            print(results)
            output += results + "\n"
    print("")
    output += "\n"

with open(outputfile,"w",encoding="u8") as f:
    f.write(output)
            


