from template import xuanxiang
from config import simplemodel
import time
tuliaos=[
'第十七章  油漆、涂料、裱糊工程 17.1  油漆、涂料 17.1.2  金属面油漆 17.1.2.3  防火涂料 金属面防火涂料 薄型 0.5小时',
'第十七章  油漆、涂料、裱糊工程 17.1  油漆、涂料 17.1.2  金属面油漆 17.1.2.3  防火涂料 金属面防火涂料 薄型 1小时',
'第十七章  油漆、涂料、裱糊工程 17.1  油漆、涂料 17.1.2  金属面油漆 17.1.2.3  防火涂料 金属面防火涂料 薄型 1.5小时',
'第十七章  油漆、涂料、裱糊工程 17.1  油漆、涂料 17.1.2  金属面油漆 17.1.2.3  防火涂料 金属面防火涂料 薄型 2小时',
'第十七章  油漆、涂料、裱糊工程 17.1  油漆、涂料 17.1.2  金属面油漆 17.1.2.3  防火涂料 金属面防火涂料 厚型 2小时',
'第十七章  油漆、涂料、裱糊工程 17.1  油漆、涂料 17.1.2  金属面油漆 17.1.2.3  防火涂料 金属面防火涂料 厚型 2.5小时',
'第十七章  油漆、涂料、裱糊工程 17.1  油漆、涂料 17.1.2  金属面油漆 17.1.2.3  防火涂料 金属面防火涂料 厚型 3小时',
]
tuliao_options=[]
tuliao_letters='ABCDEFG'
for i in range(len(tuliaos)):
    tuliao_options.append('给定选项'+tuliao_letters[i]+', 内容为' + tuliaos[i])
import json
with open('nantong_rule', 'r') as f:
    content = f.read()
import json
obj=json.loads(content)
from fallback import fallback
def fanghuotuliao(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": "问题描述： " + ','.join(options) + "。请问选项中是否有防火涂料相关选项，请回答是或者否"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    if len(json_string) < 4:
        if '是' in json_string:
            return True
        else:
            return False
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=[
            {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
            {"role": "user", "content": " 给你一段文字如下， " + json_string + ",其中给出了一个是或者否的判断作为结果，请将该最终结果输出"},                       
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if '否' in json_string:
        return False
    return True
def fanghuo(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": "问题描述： 给定一段工作内容: " + B['label'] + " " +  B['mc'] + " " + B['tz']  + "。请问工作内容是否提及防火要求，请回答是或者否"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    if len(json_string) < 4:
        if '是' in json_string:
            return True
        else:
            return False
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=[
            {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
            {"role": "user", "content": " 给你一段文字如下， " + json_string + ",其中给出了一个是或者否的判断作为结果，请将该最终结果输出"},                       
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if '否' in json_string:
        return False
    return True
def tuliaofilter(A, #options
       B, #data
       aiclient,
             qwclient,
            sfclient,
             dw):

    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": "问题描述： 给定一段涉及防火涂料的工作内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(tuliao_options) + "。请选出最恰当的选项。请返回类似A或者B或者C这样的结果"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    if len(json_string) < 4:
        if 'A' in json_string:
            return tuliaos[0]
        if 'B' in json_string:
            return tuliaos[1]
        if 'C' in json_string:
            return tuliaos[2]
        if 'D' in json_string:
            return tuliaos[3]
        if 'E' in json_string:
            return tuliaos[4]
        if 'F' in json_string:
            return tuliaos[5]
        if 'G' in json_string:
            return tuliaos[6]
    completion = sfclient.chat.completions.create(
        model=simplemodel(),
        messages=[
            {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
            {"role": "user", "content": " 给你一段文字如下， " + json_string + ",其中给出了一个类似于A或者B的字母作为结果，请将该最终结果输出"},                       
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    if 'A' in json_string:
        return tuliaos[0]
    if 'B' in json_string:
        return tuliaos[1]
    if 'C' in json_string:
        return tuliaos[2]
    if 'D' in json_string:
        return tuliaos[3]
    if 'E' in json_string:
        return tuliaos[4]
    if 'F' in json_string:
        return tuliaos[5]
    if 'G' in json_string:
        return tuliaos[6]
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] )
    time.sleep(5)
    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": "问题描述： 给定一段工作内容: " + 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 and len(A) > 0:
        answer.append(A[0])
    if 'B' in json_string and len(A) > 1:
        answer.append(A[1])
    if 'C' in json_string and len(A) > 2:
        answer.append(A[2])
    if 'D' in json_string and len(A) > 3:
        answer.append(A[3])
    if 'E' in json_string and len(A) > 4:
        answer.append(A[4])
    if 'F' in json_string and len(A) > 5:
        answer.append(A[5])
    if 'G' in json_string and len(A) > 6:
        answer.append(A[6])
    if 'H' in json_string and len(A) > 7:
        answer.append(A[7])
    if 'I' in json_string and len(A) > 8:
        answer.append(A[8])
    if 'J' in json_string and len(A) > 9:
        answer.append(A[9])
    return answer
def aifilter1(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": " 重要提示：除特殊处理要求提及的内容外，不需考虑选项内容与工作内容是否符合，只需要根据特殊处理要求做出处理"},
            {"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 aifilter5(A, #options
       B, #data
       aiclient,
             qwclient,
              sfclient,
             dw):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(A)):
        options.append("给定选项" + letters[i]+"，内容为"+A[i] )
    time.sleep(5)
    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": "问题描述： 给定一段工作内容: " + 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)
    if len(json_string) < 4:
        
        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
    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 aifilter4(A, #options
       B, #data
       aiclient,
             qwclient,
              sfclient,
             dw):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(A)):
        options.append("给定选项" + letters[i]+"，内容为"+A[i] )
    time.sleep(5)
    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": " 特殊处理要求：如果选项中有含有“铁件制作”字样的选项，并且选项中有其他具体的钢构件选项（例如CZ轻钢檩条），则去除选项中的含有“铁件制作”字样的选项"},
            {"role": "user", "content": " 重要提示：除特殊处理要求提及的内容外，不需考虑选项内容与工作内容是否符合，只需要根据特殊处理要求做出处理"},
            {"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)
    if len(json_string) < 4:
        
        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
    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] )
    time.sleep(5)
    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": " 特殊处理要求：如果选项中有含有“零星钢构件制作”字样的选项，并且选项中有其他具体的钢构件选项（例如CZ轻钢檩条），则去除选项中的含有“零星钢构件制作”字样的选项"},
            {"role": "user", "content": " 重要提示：除特殊处理要求提及的内容外，不需考虑选项内容与工作内容是否符合，只需要根据特殊处理要求做出处理"},
            {"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)
    if len(json_string) < 4:
        
        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
    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 postprocess0106_(selected, data, aiclient, qwclient, sfclient, label_name, name_dw):
    if len(selected) == 1:
        return selected
    return aifilter5(aifilter4(aifilter3(aifilter2(aifilter1(selected, data, aiclient, qwclient, sfclient, name_dw), data, aiclient, qwclient, sfclient, name_dw), data, aiclient, qwclient, sfclient, name_dw),data,aiclient,qwclient,sfclient,name_dw),data,aiclient,qwclient,sfclient,name_dw)
def postprocess0106(selected, data, aiclient, qwclient, sfclient, label_name, name_dw,yqspecial):
    selected = [x for x in selected if '金属面油漆' not in x]
    selected = selected + yqspecial
    if len([x for x in selected if '氟碳漆' in x]) > 0:
        selected = [x for x in selected if '红丹防锈漆' not in x and '调和漆' not in x and '银粉漆' not in x]
    if len([x for x in selected if '环氧富锌漆' in x]) > 0:
        selected = [x for x in selected if '红丹防锈漆' not in x ]
    selected =  postprocess0106_(selected, data, aiclient, qwclient, sfclient, label_name, name_dw)
    hit = fanghuo(selected, data, aiclient, qwclient, sfclient, name_dw)
    if hit:
        tuliao = fanghuotuliao(selected, data, aiclient, qwclient, sfclient, name_dw)
        if not tuliao:
            selected.append(tuliaofilter(selected, data, aiclient, qwclient, sfclient, name_dw))
        return selected
    else:
        return selected
