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
                            }
                        }
                    ],

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

        for ncl_one in ncl_one_list:
            query['query']['bool']['should'].append({
                "match_phrase": {
                    "ncl_one":ncl_one
                }
            })
        return self.esBase.select(index=index,body=query)
