#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-05-16 17:30
# @Author  : hj
# @File    : zhengce-select_es_result.py
# @Software: PyCharm


from flask import request, jsonify
from elasticsearch import Elasticsearch
from service.company_basic import CompanyBase
import json

companybase = CompanyBase()


class HX:
    def __init__(self):
        self.esip = "43.247.184.94"
        self.esport = 9200
        # self.index_name = "company_bigdata_hx"
        self.index_name = "company_bigdata_risk"
        self.index_type = "_doc"
        self.esuser = "admines"
        self.espassword = "adminGSBes。"
        self.es = Elasticsearch([self.esip], http_auth=(self.esuser, self.espassword), port=self.esport)

    def hxsearch(self, re_item):
        querybody = {
            "query": {
                "regexp": {
                    "must": [
                    ]
                }
            }
        }
        if 'qiyeguimo' in re_item:
            dsl = {
                "terms": {
                    "qiyeguimo": re_item['qiyeguimo']
                }
            }
            querybody['query']['bool']['must'].append(dsl)
        if 'qiyeleixing' in re_item:
            dsl = {
                "terms": {
                    "qiyeleixing": re_item['qiyeleixing']
                }}
            querybody['query']['bool']['must'].append(dsl)

        if 'chenglinianxian' in re_item:
            years_value = re_item['years_value']
            for years_item in years_value:
                if years_item[0:4] in ['青年公司', '长青公司', '成熟公司', '长寿公司']:
                    re_years_value = years_item[0:4]
                else:
                    re_years_value = years_item[0:3]
                dsl = {
                    "wildcard": {
                        "chenglinianxian": re_years_value + '*'
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

        if 'hangyeleibie' in re_item:
            dsl = {
                "terms": {
                    "hangyeleibie": re_item['hangyeleibie']
                }
            }
            querybody['query']['bool']['must'].append(dsl)
        if 'jingyingzhuangtai' in re_item:
            dsl = {
                "terms": {
                    "jingyingzhuangtai": re_item['jingyingzhuangtai']
                }
            }
            querybody['query']['bool']['must'].append(dsl)

        if 'jingyingdiqu' != "":
            dsl = {
                "term": {
                    "jingyingdiqu": re_item['jingyingdiqu']
                }
            }
            querybody['query']['bool']['must'].append(dsl)

        if 'fengongsishuliang' in re_item:
            branches = re_item['fengongsishuliang']
            for branches_item in branches:
                if branches_item[0:4] in ['枝繁叶茂', '草木稀疏', '一枝独秀']:
                    re_branches = branches_item[0:4]
                    dsl = {
                        "wildcard": {
                            "fengongsishuliang": re_branches + '*'
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

        if 'zhengzhao' in re_item:
            dsl = {
                "terms": {
                    "zhengzhao": re_item['zhengzhao']
                }
            }
            querybody['query']['bool']['must'].append(dsl)

        if 'zhuanli' in re_item:
            patent_nu = re_item['zhuanli']
            for patent_item in patent_nu:
                if patent_item[0:2] == '专利':
                    re_patent_nu = patent_nu[0:2]
                    dsl = {
                        "wildcard": {
                            'zhuanli': re_patent_nu + '*'
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

        if 'shangbiao' in re_item:
            trademark = re_item['shangbiao']
            for trademark_item in trademark:
                if trademark_item[0:2] == '商标':
                    re_trademark = trademark_item[0:2]
                    dsl = {
                        "wildcard": {
                            "shangbiao": re_trademark + '*'
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

        if 'zuopinzhuzuoquan' in re_item:
            work_nu = re_item['shangbiao']
            for work_item in work_nu:
                if work_item[0:4] == '作品著作':
                    re_work_nu = work_nu[0:4]
                    dsl = {
                        "wildcard": {
                            "zuopinzhuzuoquan": re_work_nu + '*'
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

        if 'suozaidiqu' in re_item:
            company_location = re_item['suozaidiqu']
            for location in company_location:
                if location[0:2] in ['北京', '天津', '山西', '河北','广东', '广西', '海南', '香港', '澳门', '河南', '湖北', '湖南',
                                     '上海', '江苏', '浙江', '安徽', '福建', '江西', '山东', '台湾','吉林', '辽宁', '陕西', '甘肃',
                                     '青海', '宁夏', '新疆', '四川', '贵州', '云南', '重庆', '西藏']:
                    re_location = location[0:2]
                else:
                    re_location = location[0:3]
                dsl = {
                    "wildcard": {
                        "suozaidiqu": re_location + '*'
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

        if 'ruanjianzhuzuoquan' in re_item:
            soft_ware = re_item['ruanjianzhuzuoquan']
            for soft_ware_item in soft_ware:
                if soft_ware_item[0:4] == '软件著作':
                    re_soft_ware = soft_ware_item[0:4]
                    dsl = {
                        "wildcard": {
                            "ruanjianzhuzuoquan": re_soft_ware + '*'
                        }
                    }
                    querybody['query']['bool']['must'].append(dsl)

        if 'chanpinxinxi' in re_item:
            product_info =  re_item['chanpinxinxi']
            for product_item in product_info:
                dsl = {
                    "wildcard": {
                        "chanpinxinxi":  '*' + product_item + '*'
                    }
                }
                querybody['query']['bool']['must'].append(dsl)

        if 'fengxian' in re_item:
            dsl = {
                "terms": {
                    "fengxian": re_item['fengxian']
                }
            }
            querybody['query']['bool']['must'].append(dsl)

        if 'chanpindalei' in re_item:
            dsl = {
                "terms": {
                    "chanpindalei": re_item['chanpindalei']
                }
            }
            querybody['query']['bool']['must'].append(dsl)

        if 'jingyinghangye' in re_item:
            dsl = {
                "terms": {
                    "jingyinghangye": re_item['jingyinghangye']
                }
            }
            querybody['query']['bool']['must'].append(dsl)

        print('查询语法：', querybody)
        searched = self.es.search(index=self.index_name, doc_type=self.index_type, body=querybody, request_timeout=1000)

        company_hx_list = []
        if (searched["hits"]["total"] > 0):
            for row in searched["hits"]["hits"]:
                company_name = row['_source']["company_name"]
                company_hx_list.append(company_name)
        return company_hx_list


# hx = HX()


# 画像检索接口
def hx_company_dict(server):
    @server.route('/gsb/api/hx_dict', methods=['POST'])
    def hx_dict_select():
        print(22)
        if request.method == 'POST':
            requestdata = request.json
            company_name = requestdata.get('company_name')
            print(type(company_name), 0000)
            try:
                base_result = companybase.companyinfo(company_name)
                base_result['logo'] = 'xxx'
            except:
                base_result = {
                    "company_name": "北京码农村科技有限公司",
                    "business_scope": "技术开发、技术转让、技术咨询、技术推广、技术服务;计算机系统服务;软件开发;电脑动画设计;经济贸易咨询;企业策划;企业管理咨询;销售自行开发后的产品。(企业依法自主选择经营项目,开展经营活动;依法须经批准的项目,经相关部门批准后依批准的内容开展经营活动;不得从事本市产业政策禁止和限制类项目的经营活动。)",
                    "reg_unit": "100.000",
                    "establish_time": "2016-07-06",
                    "legal_person_name": "白冰",
                    "reg_location": "北京市海淀区高里掌路1号院6号楼104-165",
                    "logo": "xxx"
                }
            return {"status": 200, "msg": "成功", "data": base_result}


def hx_company_list(server):
    @server.route('/gsb/api/hx_list', methods=['POST'])
    def hx_select_main():
        if request.method == 'POST':
            requestdata = request.json
            print(requestdata, 1121212)
            base_result = ""
            re_item = {}
            try:
                for item in requestdata:
                    name = item['name']
                    list = item['list']
                    re_item[name] = list
                    company_hx_list = HX.hxsearch(re_item)
                    base_result_list = []
                    for company_name in company_hx_list:
                        base_result = companybase.companyinfo(company_name)
                        base_result_list.append(base_result)
            except:
                base_result = [{
                    "company_name": "北京晟软在线科技有限公司",
                    "business_scope": "技术开发、技术转让、技术咨询、技术服务;计算机系统服务;应用软件服务;软件开发。(企业依法自主选择经营项目,开展经营活动;依法须经批准的项目,经相关部门批准后依批准的内容开展经营活动;不得从事本市产业政策禁止和限制类项目的经营活动。)",
                    "reg_unit": "100.000",
                    "establish_time": "2010-11-03",
                    "legal_person_name": "赵学军",
                    "reg_location": "北京市海淀区中关村软件园11号楼3层330",
                    "logo": "xxx"
                }, {
                    "company_name": "北京游逗科技有限公司",
                    "business_scope": "技术推广服务;票务代理(不含航空机票销售代理);旅游信息咨询(不含中介服务);酒店管理;风景名胜区规划;企业形象策划;企业管理咨询(不含中介服务);应用软件服务(不含医用软件服务);销售计算机、软件及辅助设备、电子产品、通讯设备;计算机系统服务。(企业依法自主选择经营项目,开展经营活动;依法须经批准的项目,经相关部门批准后依批准的内容开展经营活动;不得从事本市产业政策禁止和限制类项目的经营活动。)",
                    "reg_unit": "200.000",
                    "establish_time": "2017-11-27",
                    "legal_person_name": "王京",
                    "reg_location": "北京市丰台区程庄北里13号楼二层207室",
                    "logo": "xxx"
                }, {
                    "company_name": "北京码农村科技有限公司",
                    "business_scope": "技术开发、技术转让、技术咨询、技术推广、技术服务;计算机系统服务;软件开发;电脑动画设计;经济贸易咨询;企业策划;企业管理咨询;销售自行开发后的产品。(企业依法自主选择经营项目,开展经营活动;依法须经批准的项目,经相关部门批准后依批准的内容开展经营活动;不得从事本市产业政策禁止和限制类项目的经营活动。)",
                    "reg_unit": "100.000",
                    "establish_time": "2016-07-06",
                    "legal_person_name": "白冰",
                    "reg_location": "北京市海淀区高里掌路1号院6号楼104-165",
                    "logo": "xxx"
                }]
            print(base_result, 111)
            return {"status": 200, "msg": "成功", "data": base_result}
