import  json

from aidso_geo.utils import robot_utils, bh_utils
from aidso_geo.utils.ai_interface import get_parse_sse_result
from aidso_geo.utils.tos_utils import get_string_from_tos

from aidso_geo.models import  spider_save_tos


def yuanbao_process_original_data(data):
    file_path = f'geo/{data["taskId"]}/{data["platform"]}/original.text'
    url_list = []
    think_content = ""
    response_process = ""
    response_content = ""
    search_keyword = []
    suggestions = []
    is_think = False
    think_bool = False
    response_bool = False
    rich_media_block =[]
    try:
        original_content = get_string_from_tos(file_path)
        content_list = original_content.split("\n\n")

        for i in content_list:

            if i.startswith("data: "):
                try:
                    # 提取并解析JSON数据
                    data_str = i.split("data: ")[1]
                    json_data = json.loads(data_str)

                except (IndexError, json.JSONDecodeError):
                    continue  # 跳过格式错误的数据

                if json_data.get('type') == 'searchGuid':
                    url_list = json_data.get('docs')
                if json_data.get('type') == 'think':
                    think_content+=json_data.get('content')
                if json_data.get('type') == 'text' and json_data.get('msg') is not None:
                    response_content+=json_data.get('msg')
                if json_data.get('type') == 'deepSearch':
                    if json_data.get('contents'):
                        if json_data.get('contents')[0].get('msg'):
                            if json_data.get('contents')[0].get('type') =='text':
                                think_content += json_data.get('contents')[0].get('msg')
                            # if not response_process:
                            #     response_content += json_data.get('contents')[0].get('msg')
                if json_data.get('type') == 'image':
                    response_content = "生成了图片"
        spider_save_tos.process_and_save_files(file_path, search_keyword, url_list, think_content, response_content,
                                               suggestions)
        return (file_path, search_keyword, url_list, think_content, response_content, suggestions)
    except Exception as e:
        parts = file_path.split('/')
        platform = parts[2]
        task_id = parts[1]
        context, quote, suggestion, think, search_word = get_parse_sse_result(platform, task_id)
        if context:
            response_content = context
            url_list = quote
            suggestions = suggestion
            think_content = think
            search_keyword = search_word
            spider_save_tos.process_and_save_files_ai(file_path, search_keyword, url_list, think_content,
                                                      response_content,suggestions)

            return (file_path, search_keyword, url_list, think_content, response_content, suggestions)
        else:
            response_content = "对话信息获取失败:-200"
            robot_utils.feishu_tobot(file_path)

    spider_save_tos.process_and_save_files(file_path, search_keyword, url_list, think_content, response_content,
                                           suggestions)
    return (file_path, search_keyword, url_list, think_content, response_content, suggestions)

if __name__ == '__main__':
    data_list = bh_utils.query_data(f"select * from geo_commit_task  where taskId = '69189ba0-e443-4e7c-a465-cabc07973540' and  platform  = 'TXYB'")

    # # #
    # # #
    # # # # #
    def handle_item(i):
        if i.get('comWordsMap'):
            i['comWordsMap'] = json.loads(i.get('comWordsMap'))
        if i.get('brandWords'):
            i['brandWords'] = json.loads(i.get('brandWords'))
        if i.get('comWords'):
            i['comWords'] = json.loads(i.get('comWords'))
        if i.get('keywords'):
            i['keywords'] = json.loads(i.get('keywords'))
        if i.get('productWordsMap'):
            i['productWordsMap'] = json.loads(i.get('productWordsMap'))

        return yuanbao_process_original_data(i)


    if data_list:
        for i in data_list:
            try:
                handle_item(i)
            except Exception as e:
                ...
