import json
from config import simplemodel
from template import xuanxiang
with open('nantong_rule', 'r') as f:
    content = f.read()
import json
obj=json.loads(content)
from fallback import fallback
qiangdu={"role": "user", "content": " 特殊处理要求：不需考虑工作内容中的混凝土强度等级与选项中的混凝土强度等级的差异"}
xiang={"role": "user", "content": " 重要提示：选项指的是给定的A、B、C之类的选项，不是指的工作内容中的可能的1、2、3这样罗列的特征"}
shanchu={"role": "user", "content": " 重要提示：除特殊处理要求提及的内容外，不需考虑选项内容与工作内容是否符合(匹配)，不得擅自根据符合与否去除选项，只需要根据特殊处理要求做出处理"}
ceng={"role": "user", "content": " 重要提示：混凝土找平层与混凝土面层是不同的概念，不得混淆。混凝土整体面层不属于混凝土找平层"}
def aifilter4(A, #options
       B, #data
       aiclient,
             qwclient,
             sfclient,
             dw):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(A)):
        options.append("给定选项" + letters[i]+"，内容为"+A[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="Qwen/Qwen3-14B",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": " 特殊处理要求：如果选项中有混凝土垫层选项，但是工作内容中明确描述垫层做法是碎石垫层，则去除选项中的混凝土垫层选项"},
            {"role": "user", "content": " 特殊处理要求：如果选项中有混凝土垫层选项，但是工作内容中不涉及垫层，则去除选项中的混凝土垫层选项"},
            {"role": "user", "content": " 特殊处理要求：如果选项中有细石混凝土（细石砼）找平层选项(细石混凝土整体面层不属于找平层)，但是工作内容没有明确表述细石混凝土找平层，则去除选项中的细石混凝土找平层选项"},
            {"role": "user", "content": " 特殊处理要求：抱框柱是柱的一种，与门框不相同。如果选项中有门框选项，但是工作内容中没有明确提出门框，则去除选项中的门框选项"},
            qiangdu,xiang,shanchu,ceng,
            {"role": "user", "content": "问题描述： 给定一段工作内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请根据处理要求做出处理，写出推理过程，并返回结果,删除选项必须对应到明确的特殊处理要求，不要擅自删除选项。例如，如果处理完后剩余A,B,C三个选项，请返回[A,B,C]"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string:
        answer.append(A[0])
    if 'B' in json_string:
        answer.append(A[1])
    if 'C' in json_string:
        answer.append(A[2])
    if 'D' in json_string:
        answer.append(A[3])
    if 'E' in json_string:
        answer.append(A[4])
    if 'F' in json_string:
        answer.append(A[5])
    if 'G' in json_string:
        answer.append(A[6])
    if 'H' in json_string:
        answer.append(A[7])
    if 'I' in json_string:
        answer.append(A[8])
    if 'J' in json_string:
        answer.append(A[9])
    return answer
def aifilter2(A, #options
       B, #data
       aiclient,
             qwclient,
             sfclient,
             dw):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(A)):
        options.append("给定选项" + letters[i]+"，内容为"+A[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="Qwen/Qwen3-14B",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": " 特殊处理要求：如果选项中有檩条选项，但是工作内容中没有涉及檩条，则去除选项中的檩条选项"},
            {"role": "user", "content": " 特殊处理要求：如果工作内容中没有明确提及素水泥浆(提到水泥浆不等于提到素水泥浆)，则去除选项中的素水泥浆选项"},
             qiangdu,xiang,shanchu,ceng,
            {"role": "user", "content": "问题描述： 给定一段工作内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请根据处理要求做出处理，写出推理过程，并返回结果,删除选项必须对应到明确的特殊处理要求，不要擅自删除选项。例如，如果处理完后剩余A,B,C三个选项，请返回[A,B,C]"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string:
        answer.append(A[0])
    if 'B' in json_string:
        answer.append(A[1])
    if 'C' in json_string:
        answer.append(A[2])
    if 'D' in json_string:
        answer.append(A[3])
    if 'E' in json_string:
        answer.append(A[4])
    if 'F' in json_string:
        answer.append(A[5])
    if 'G' in json_string:
        answer.append(A[6])
    if 'H' in json_string:
        answer.append(A[7])
    if 'I' in json_string:
        answer.append(A[8])
    if 'J' in json_string:
        answer.append(A[9])
    return answer
def aifilter3(A, #options
       B, #data
       aiclient,
             qwclient,
             sfclient,
             dw):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(A)):
        options.append("给定选项" + letters[i]+"，内容为"+A[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="Qwen/Qwen3-14B",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": " 特殊处理要求：如果选项中有模板工程选项，但是工作内容中没有明确提出包含模板工程，则去除选项中的模板工程选项"},
            {"role": "user", "content": " 特殊处理要求：如果选项中有锯缝选项，但是工作内容中没有明确提出需要锯缝（道路伸缩缝），则去除选项中的锯缝选项"},
            qiangdu,xiang,shanchu,ceng,
            {"role": "user", "content": "问题描述： 给定一段工作内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请根据处理要求做出处理，写出推理过程，并返回结果,删除选项必须对应到明确的特殊处理要求，不要擅自删除选项。例如，如果处理完后剩余A,B,C三个选项，请返回[A,B,C]"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string:
        answer.append(A[0])
    if 'B' in json_string:
        answer.append(A[1])
    if 'C' in json_string:
        answer.append(A[2])
    if 'D' in json_string:
        answer.append(A[3])
    if 'E' in json_string:
        answer.append(A[4])
    if 'F' in json_string:
        answer.append(A[5])
    if 'G' in json_string:
        answer.append(A[6])
    if 'H' in json_string:
        answer.append(A[7])
    if 'I' in json_string:
        answer.append(A[8])
    if 'J' in json_string:
        answer.append(A[9])
    return answer
def aifilter1_2(A, #options
       B, #data
       aiclient,
             qwclient,
             sfclient,
             dw):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(A)):
        options.append("给定选项" + letters[i]+"，内容为"+A[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="Qwen/Qwen3-14B",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": " 特殊处理要求：钢盖板，钢篦子一般指不锈钢材质。如果选项中同时出现不锈钢盖板选项和铸铁盖板选项，则二者只能选一种(并删除另外一个)，且优先选择与工作内容描述更接近的选项"},
            {"role": "user", "content": " 特殊处理要求：如果选项中同时出现混凝土散水选项和混凝土（砼）整体面层选项(加浆抹光选项虽然有整体面层字样，但是不属于整体面层选项)，则二者只能选一个(并删除另一个)，且优先选择出现更早的选项"},
            qiangdu,xiang,shanchu,ceng,
            {"role": "user", "content": "问题描述： 给定一段工作内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请根据处理要求做出处理，写出推理过程，并返回结果,删除选项必须对应到明确的特殊处理要求，不要擅自删除选项。例如，如果处理完后剩余A,B,C三个选项，请返回[A,B,C]"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string:
        answer.append(A[0])
    if 'B' in json_string:
        answer.append(A[1])
    if 'C' in json_string:
        answer.append(A[2])
    if 'D' in json_string:
        answer.append(A[3])
    if 'E' in json_string:
        answer.append(A[4])
    if 'F' in json_string:
        answer.append(A[5])
    if 'G' in json_string:
        answer.append(A[6])
    if 'H' in json_string:
        answer.append(A[7])
    if 'I' in json_string:
        answer.append(A[8])
    if 'J' in json_string:
        answer.append(A[9])
    return answer
def aifilter1_1(A, #options
       B, #data
       aiclient,
             qwclient,
             sfclient,
             dw):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(A)):
        options.append("给定选项" + letters[i]+"，内容为"+A[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="Qwen/Qwen3-14B",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": " 特殊处理要求：如果选项中同时出现现浇板式雨棚（雨蓬）选项和现浇水平板(平板)选项，则二者只能选一个(并删除另一个)，且优先选择出现更早的选项"},
            {"role": "user", "content": " 特殊处理要求：如果选项中同时出现多个钢丝网选项，则只能选一个(并删除另一个)，且优先选择出现更早的选项"},
            qiangdu,xiang,shanchu,ceng,
            {"role": "user", "content": "问题描述： 给定一段工作内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请根据处理要求做出处理，写出推理过程，并返回结果,删除选项必须对应到明确的特殊处理要求，不要擅自删除选项。例如，如果处理完后剩余A,B,C三个选项，请返回[A,B,C]"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string:
        answer.append(A[0])
    if 'B' in json_string:
        answer.append(A[1])
    if 'C' in json_string:
        answer.append(A[2])
    if 'D' in json_string:
        answer.append(A[3])
    if 'E' in json_string:
        answer.append(A[4])
    if 'F' in json_string:
        answer.append(A[5])
    if 'G' in json_string:
        answer.append(A[6])
    if 'H' in json_string:
        answer.append(A[7])
    if 'I' in json_string:
        answer.append(A[8])
    if 'J' in json_string:
        answer.append(A[9])
    return answer
def postprocess0105(selected, data, aiclient, qwclient, sfclient, label_name, name_dw):
    name=[label_name[x] for x in selected]
    dw=[name_dw[x] for x in name]
    if len(selected) == 1:
        t1 = selected
    else:   
        t1 = aifilter1_1(selected, data, aiclient, qwclient, sfclient, dw)
        t1 = aifilter1_2(t1, data, aiclient, qwclient, sfclient, dw)
        t1 = aifilter3(t1, data, aiclient, qwclient, sfclient, dw)
        t1 = aifilter2(t1, data, aiclient, qwclient, sfclient, dw)
        t1 = aifilter4(t1, data, aiclient, qwclient, sfclient, dw)
    #t2 = fallback(t1, data, aiclient, qwclient)
    result = []
    if '套' in data['dw']:
        for entry in t1:
            if entry in obj['7.8']:##钢盖板
                result.append('第十二章  厂区道路及排水工程 12.5  排水系统中钢筋混凝土井、池、其它 铸铁盖板安装')
            elif '成品不锈钢盖板安装' in entry:
                result.append('第十二章  厂区道路及排水工程 12.5  排水系统中钢筋混凝土井、池、其它 铸铁盖板安装')
            else:
                result.append(entry)
    else:    
        for entry in t1:
            if entry in obj['7.8']:##钢盖板
                result.append(obj['nantong7.8'][0])
            else:
                result.append(entry)
    return result
