#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-05-28 14:24
# @Author  : zhangyunfei
# @File    : elasticsearch_ic.py
# @Software: PyCharm
import flask, os, sys, time
from flask import request, send_from_directory, jsonify
import time
import json
from elasticsearch import Elasticsearch

interface_path = os.path.dirname(__file__)
sys.path.insert(0, interface_path)  # 将当前文件的父目录加入临时系统变量

server = flask.Flask(__name__)
server.config['JSON_AS_ASCII'] = False


class Elastic:
    def __init__(self, ES_HOST, ES_USER, ES_PWD, ES_PORT):
        self.es = Elasticsearch([ES_HOST], http_auth=(ES_USER, ES_PWD), port=ES_PORT)
        # self.index_name = 'bigdata_ic_gsb_company_04'
        self.index_name = 'tx_ic_bigdata_business_heming_index_01'
        self.index_type = None

    def es_search(self, parameter):
        cityname = parameter['cityname']
        keyword = parameter['keyword']
        btname = parameter['btname']
        orgname = parameter['orgname']
        # setcity = parameter['setcity']
        # pagesize = parameter.get('pagesize')
        # pageindex = parameter.get('pageindex')
        # if pagesize is None:
        #     pagesize = 20
        # if pageindex is None:
        #     pageindex = 1
        #     from_ = 0
        # else:
        #     from_ = (pageindex - 1) * pagesize
        try:
            querybody = {
                "query": {
                    "bool": {
                        "must": [

                            {
                                "query_string": {
                                    "default_field": "company_name",
                                    "query": "\"" + cityname + "\""
                                }
                            },
                            {
                                "query_string": {
                                    "default_field": "company_name",
                                    "query": "\"" + keyword + "\""
                                }
                            },
                            {
                                "bool": {
                                    "should": [
                                        {
                                            "match": {
                                                "company_name": btname
                                            }
                                        }
                                    ]
                                }
                            }
                        ],
                        "must_not": [
                            {
                                "term": {
                                    "reg_status": "注销"
                                }
                            },
                            {
                                "term": {
                                    "company_org_type": "个体工商户"
                                }
                            }
                        ]
                    }
                }
            }
            if orgname != "" and orgname:
                conditiontext = {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "default_field": "company_name",
                                    "query": "\"" + orgname + "\""
                                }
                            }, {
                                "match": {
                                    "company_name": btname
                                }
                            }
                        ]
                    }
                }
                querybody["query"]["bool"]["must"].append(conditiontext)
            else:
                conditiontext = {
                    "bool": {
                        "should": [
                            {
                                "match": {
                                    "company_name": btname
                                }
                            }
                        ]
                    }
                }
                # querybody["query"]["bool"]["must"]["bool"]["should"].append(conditiontext)
            print(json.dumps(querybody, ensure_ascii=False))
            es_result = self.es.search(index=self.index_name, doc_type=self.index_type, body=querybody, size=100,
                                       request_timeout=1000)
            items = es_result['hits']['hits']
            result = {
                "Status": "",
                "Message": "",
                "Paging": {
                    "TotalRecords": 0
                },
                "Result": []
            }
            result["Paging"]["TotalRecords"] = es_result['hits']['total']
            if items:
                result["Status"] = 200
                result["Message"] = "查询成功"
                data = []
                for item in items:
                    Name = item['_source']['company_name']
                    d = {"Name": Name}
                    data.append(d)
                result["Result"] = data
            else:
                result["Status"] = 400
                result["Message"] = "无数据"
                result["Result"] = []
            return result

        except:
            result = {
                "Status": 400,
                "Message": "error",
                "Paging": {
                    "TotalRecords": 0
                },
                "Result": []
            }
            return result
