#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import logging
import traceback
import base64
from libs.tools import tzlc, getMd5Digest
from libs.es import ESPerform
from libs.cache import redis_client
import json
from django.conf import settings
from trans2es.models import wordresemble
from trans2es.commons.words_utils import QueryWordAttr, get_tips_word_type
from trans2es.commons.commons import get_tips_suggest_list
from trans2es.utils.doctor_transfer import DoctorTransfer
from trans2es.utils.itemwiki_transfer import ItemWikiTransfer
from trans2es.utils.tag_transfer import TagTransfer


class WordResemble(object):

    @classmethod
    def get_word_resemble_list(cls, keyword):
        try:
            query_sql_item = wordresemble.WordRel.objects.filter(keyword=keyword)

            temp_list = list()
            for sql_obj in query_sql_item:
                temp_list.extend(list(sql_obj.all_resembles.all().values_list('word', flat=True)))

            resemble_list = list()
            for item in temp_list:
                resemble_list.extend(item.split("、"))

            return resemble_list
        except:
            logging.error("catch exception,err_msg:%s" % traceback.format_exc())
            return list()

    @classmethod
    def get_resemble_list(cls, instance):
        try:
            try:
                ret_list = list()

                item_dict = dict()
                item_dict["id"] = getMd5Digest(str(instance.keyword))
                item_dict["ori_name"] = instance.keyword
                item_dict["is_online"] = True
                item_dict["order_weight"] = QueryWordAttr.get_project_query_word_weight(instance.keyword)
                item_dict["results_num"] = QueryWordAttr.get_query_results_num(instance.keyword)
                item_dict["type_flag"] = get_tips_word_type(instance.keyword)
                item_dict["offline_score"] = 0.0
                item_dict["tips_name_type"] = 4
                ret_list.append(item_dict)
                suggest_list = get_tips_suggest_list(instance.keyword)

                return (item_dict, suggest_list)
            except:
                logging.error("catch exception,err_msg:%s" % traceback.format_exc())
                return ([], [])
        except:
            logging.error("catch exception,err_msg:%s" % traceback.format_exc())
            return list()

    @classmethod
    def get_all_data_name_mapping_results_to_redis(cls, instance):
        try:
            total_count = 0
            instance.name = instance.keyword
            # 获取百科的
            ItemWikiTransfer.get_wiki_data_name_mapping_results_to_redis(instance)

            # 获取美购的
            TagTransfer.get_tag_data_name_mapping_results_to_redis(instance)

            # 获取医生医院的
            DoctorTransfer.get_doctor_data_name_mapping_results_to_redis(instance)

            # 获取日记的
            WordResemble.get_diary_data_name_mapping_results_to_redis(instance)

        except:
            logging.error("catch exception,err_msg:%s" % traceback.format_exc())

    @classmethod
    def get_diary_data_name_mapping_results_to_redis(cls, instance):
        try:

            tips_num_redis_key_prefix = "search_tips:tips_mapping_num"

            tag_name = instance.name.strip()
            q = dict()
            if tag_name:
                multi_fields = {
                    'tags': 8,
                    'doctor.name': 4,
                    'doctor.hospital.name': 3,
                    'doctor.hospital.officer_name': 3,
                    'user.last_name': 2,
                    'service.name': 1,
                    "title": 2}
                query_fields = ['^'.join((k, str(v))) for (k, v) in multi_fields.items()]
                multi_match = {
                    'query': tag_name,
                    'type': 'cross_fields',
                    'operator': 'and',
                    'fields': query_fields,
                }
                q['query'] = {
                    'bool': {
                        "should": [
                            {'multi_match': multi_match}
                        ],
                        "must": [
                            {"term": {"is_online": True}}
                        ],
                        "minimum_should_match": 1
                    }
                }

                result_dict = ESPerform.get_search_results(ESPerform.get_cli(settings.GM_ORI_ES_INFO_LIST),
                                                           sub_index_name="diary", doc_type="diary", query_body=q,
                                                           offset=0, size=0)

                doctor_results = result_dict["total_count"]

                redis_data = redis_client.hget(tips_num_redis_key_prefix, tag_name)

                redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}

                total_count = doctor_results

                if 't' in redis_val_dict:
                    total_count += int(redis_val_dict['t'])

                redis_val_dict['t'] = total_count
                redis_val_dict['r'] = doctor_results

                redis_client.hset(tips_num_redis_key_prefix, tag_name, json.dumps(redis_val_dict))
        except:
            logging.error("catch exception,err_msg:%s" % traceback.format_exc())

    @classmethod
    def set_data_to_redis(cls, instance):
        try:

            QUERY_KEY = "query:{}:search_tip"
            tips_num_redis_key_prefix = "search_tips:tips_mapping_num"

            # result = wordresemble.WordRel.objects.all().values("keyword", "id")
            # for wordrm in result:
            query_sql_item = wordresemble.WordRel.objects.filter(keyword=instance.keyword)
            for sql_obj in query_sql_item:
                words = list(sql_obj.all_resembles.all().values_list('word', flat=True))
                for items in words:
                    # 先存储本体词
                    query_base64 = base64.b64encode(instance.keyword.encode('utf8')).decode('utf8')
                    key = QUERY_KEY.format(query_base64)
                    value_name = base64.b64encode(items.encode('utf8')).decode('utf8')

                    # 获取本体词的结果
                    redis_data = redis_client.hget(tips_num_redis_key_prefix, items)
                    redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
                    total_count = 0
                    if 't' in redis_val_dict:
                        total_count = int(redis_val_dict['t'])

                    # 存储到Redis
                    value = value_name + str(":" + str(total_count))
                    redis_client.sadd(key, value)

                    # 存储近义词
                    # query_base64 = base64.b64encode(items.encode('utf8')).decode('utf8')
                    key = QUERY_KEY.format(value_name)
                    # value_name = base64.b64encode(wordrm["keyword"].encode('utf8')).decode('utf8')
                    value_names = query_base64
                    # 获取结果
                    redis_data = redis_client.hget(tips_num_redis_key_prefix, instance.keyword)
                    redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
                    total_count = 0
                    if 't' in redis_val_dict:
                        total_count = int(redis_val_dict['t'])
                    # 存储结果
                    value = value_names + str(":" + str(total_count))
                    redis_client.sadd(key, value)
                    if words:
                        for w in words:
                            value_name_w = base64.b64encode(w.encode('utf8')).decode('utf8')
                            redis_data = redis_client.hget(tips_num_redis_key_prefix, w)
                            redis_val_dict = json.loads(str(redis_data, encoding="utf-8")) if redis_data else {}
                            total_count = 0
                            if 't' in redis_val_dict:
                                total_count = int(redis_val_dict['t'])
                            value = value_name_w + str(":" + str(total_count))
                            redis_client.sadd(key, value)

        except:
            logging.error("catch exception,err_msg:%s" % traceback.format_exc())
