from dao.tm_treasure_name_es import NameEs
from dao.tm_treasure_name_dao import NameDao

name_es = NameEs()
name_dao = NameDao()
def name_treasure(result_json):
    if "admiring_word" not in result_json:
        result_json['admiring_word'] = ''
    if "word_site" not in result_json:
        result_json['word_site'] = ''
    name_result_list  = []
    ncl_one_list = result_json['ncl_one_list']
    name_size = result_json['name_size']

    include_word = result_json['admiring_word']
    word_type = result_json['word_site']
    name_result = name_es.select_name(ncl_one_list,name_size,include_word,word_type)
    if name_result:
        if name_result['hits']['hits']:

            for item in name_result['hits']['hits']:
                if word_type == "首位" and include_word != '':
                    if  item['_source']['tm_name'][0] == include_word:
                       name_result_list.append(item['_source']['tm_name'])
                elif word_type == "末位" and include_word != '':
                    if  item['_source']['tm_name'][-1] == include_word:
                       name_result_list.append(item['_source']['tm_name'])
                elif word_type == "中位":
                    if  item['_source']['tm_name'][-1] != include_word and item['_source']['tm_name'][0] != include_word :
                       name_result_list.append(item['_source']['tm_name'])
                elif word_type == "随机" and include_word != '':
                    if include_word in item['_source']['tm_name']:
                        name_result_list.append(item['_source']['tm_name'])
                else :
                    name_result_list.append(item['_source']['tm_name'])
    #es挂了解决方案
    if not name_result:
        name_result = name_dao.select_name_dao(ncl_one_list, name_size, include_word, word_type)
        for item in name_result:
            if word_type == "首位" and include_word != '':
                if item['tm_name'][0] == include_word :
                    name_result_list.append(item['tm_name'])
            elif word_type == "末位" and include_word != '':
                if item['tm_name'][-1] == include_word:
                    name_result_list.append(item['tm_name'])
            elif word_type == "中位":
                if item['tm_name'][0] != include_word and item['tm_name'][-1] != include_word:
                    name_result_list.append(item['tm_name'])
            elif word_type == "随机" and include_word != '':
                if include_word in item['tm_name']:
                    name_result_list.append(item['tm_name'])
            else:
                name_result_list.append(item['tm_name'])

    result_list = list(set(name_result_list))
    return result_list


