from db.mod_db import EsBase
index = "tm_treasure_name"
import datetime,time,os,sys

class NameEs:
    def __init__(self, es=None):
        if es is None:
            self.esBase = EsBase()
        else:
            self.esBase = es
    def select_name(self,ncl_one_list,name_size,include_word,word_type):
        if word_type == "首位":
            include_word_type = include_word + "*"
        if word_type == "末位":
            include_word_type = "*" + include_word
        if word_type == "中位" or word_type == "随机" or word_type == '':
            include_word_type = "*" + include_word + "*"
        query = {
            "from": 0,
            "size": 120,
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "name_size": name_size
                            }

                        },

                        {
                            "match": {
                                "whether_register": 1
                            }
                        },
                        {
                            "wildcard": {
                                "tm_name": include_word_type
                            }
                        },
                        {
                            "terms": {
                                "ncl_one": ncl_one_list
                            }
                        }

                    ],
                }
            },
            "sort": {
                "_script": {
                    "script": "Math.random()",
                    "type": "number",
                    "order": "asc"
                }
            }
        }


        return self.esBase.select(index=index,body=query)
