from dao.treasure_name_es import NameEs
from collections import Counter
from dao.literal_meaning_es import ExplainEs
from service import preference_selecter

# url = 'https://entprofile.gongsibao.com/gsb/api/ImageStatusSearch'  # 获取信息接口
# headers = {"Content-Type": "application/json", "ak": "YLc6GsgLtuRGaVA5Om848x18NxLtHlyA"}
# companyname = {"companyname":companyname}# 接口访问参数
# companyInfo = requests.post(url, data=json.dumps(json_info), headers=headers)  # 访问接口返回的信息


all_size = 20
find_size = 15

def statistics(name_type_dict,name_size_dict,json_result):
    name_type_list = []
    name_size_list = []
    for like_name in json_result:
        name_type_list.append(name_type_dict[like_name])
        name_size_list.append(name_size_dict[like_name])
    return name_type_list,name_size_list,len(json_result)

def precent_get(num_list,all_len):
    size_result_dict = {}
    for name_size in num_list.keys():
        size_result_dict[name_size] = num_list[name_size] / float(all_len)
    return size_result_dict

def  name_create(json_result):
    name_type_dict ,name_size_dict = preference_selecter.preference_meaning(json_result['preference'])
    name_type_list, name_size_list, all_len = statistics(name_type_dict,name_size_dict,json_result['preference'])

    name_type_num_dict = Counter(name_type_list)
    name_size_num_list = Counter(name_size_list)

    name_size_num_list = precent_get(name_size_num_list,all_len)
    name_type_num_list = precent_get(name_type_num_dict,all_len)
    namees = NameEs()
    result = {}
    all_num = 0
    name_list = []
    for item in name_type_num_list:
        if item not in result:
            result[item] = {}
        for name in name_size_num_list:
            if name not in result[item]:
                result[item][name] = ""
            result[item][name] = round(name_type_num_list[item] * name_size_num_list[name] * find_size)
            all_num += result[item][name]
            name_search_result = namees.select_name(item, name,json_result['city'],json_result['cate'], result[item][name])
            for search_name in name_search_result["hits"]['hits']:
                name_list.append(search_name["_source"]['company_name'])

    surplus_num = all_size - len(name_list)
    if surplus_num > 0:
        null_result = namees.select_null(surplus_num)
        for search_name in null_result["hits"]['hits']:
            print(search_name)
            name_list.append(search_name["_source"]['company_name'])

    return name_list

def word_explain(name_list,json_result):
    explaines = ExplainEs()
    result = []
    for name in name_list:
        name_dict = {"name":"","explain":[],"company_name":""}
        name_dict["company_name"] = json_result['city'].replace("市","") + name + json_result['cate'] + "有限公司"
        name_dict['name'] = name
        for word in name:
            word_dict = {}

            explain_result = explaines.select_explain(word)
            if explain_result['hits']['hits']:

                word_dict[word] = explain_result['hits']['hits'][0]['_source']['explain']

            if not explain_result['hits']['hits']:
                print("nohave",word)
                word_dict[word] = "暂无释义"
            name_dict['explain'].append(word_dict)
        result.append(name_dict)
    return result


def company_name_explain(json_result):
    result_dict = {}
    name_list = name_create(json_result)
    result = word_explain(name_list,json_result)
    result_dict["orderId"]=json_result['orderId']
    result_dict["result_name"] = result
    return result_dict

