#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/2/11 19:25
# @Author  : fanhui
# @File    : getcompanyinfofromES.py
# @Software: PyCharm

import os, sys
from flask_cors import *
import json
from flask import Flask
import oss2,time,datetime
from elasticsearch import Elasticsearch



server = Flask(__name__)
interface_path = os.path.dirname(__file__)
sys.path.insert(0, interface_path)  # 将当前文件的父目录加入临时系统变量
server.config['JSON_AS_ASCII'] = False
CORS(server, supports_credentials=True)  # 跨域请求



# 工商数据接口
def GetComanyinfoFromES(companyname):
    esip = "43.247.184.94"
    esport = 9200
    esuser = "admines"
    espassword = "adminGSBes"
    es = Elasticsearch([esip], http_auth=(esuser, espassword), port=esport, timeout=15000)
    companyinfo = es.search(index="bigdata_ic_gsb_company_op",
                            body={"query": {"bool": {"must": {"term": {"company_name.raw": companyname}}}}})
    companylist = {}
    if companyinfo["hits"]["hits"]:
        for row in companyinfo["hits"]["hits"]:
            companyname = row['_source']["company_name"]  # 公司名称
            legal_person = row['_source']["legal_person"]  # 法定代表人
            if legal_person is None:
                legal_person = "_"
            cred_code = row['_source']["credit_code"]  # 统一社会信用代码
            if cred_code is None:
                cred_code = "_"
            gsb_province = row['_source']["base"]  # 所属省份
            if gsb_province is None:
                gsb_province = "_"
            gsb_city = row['_source']["gsb_city"] #城市
            if gsb_city is None:
                gsb_city="_"
            reg_location = row['_source']["reg_location"]  # 企业地址
            if reg_location is None:
                reg_location = "_"
            establish_time = time.strftime("%Y-%m-%d", time.localtime(row["_source"]["estiblish_time"]))  # 成立日期
            if establish_time is None:
                establish_time = "_"
            company_cate_1 = row['_source']["gsb_company_cate_1"]  # 所属行业
            if company_cate_1 is None:
                company_cate_1 = "_"
            company_org_type = row['_source']["company_org_type"]  # 企业类型
            if company_org_type is None:
                company_org_type = "_"
            reg_institute = row['_source']["reg_institute"]  # 登记机关
            if reg_institute is None:
                reg_institute = "_"
            reg_status = row['_source']["reg_status"]  # 营业状态
            if reg_status is None:
                reg_status = "_"
            reg_capital = row['_source']["reg_capital"]  # 注册资本
            reg_unit=row['_source']["reg_unit"]  # 注册资本单位
            if reg_capital is None:
                reg_capital = "_"
            else:
                if reg_unit=="万人民币":
                    reg_capital = str(reg_capital) + "万元"
                else:
                    reg_capital = str(reg_capital)
            from_time = time.strftime("%Y-%m-%d", time.localtime(row["_source"]["from_time"]))  # 营业期限开始
            if from_time is None:
                from_time = "_"
            to_time = time.strftime("%Y-%m-%d", time.localtime(row["_source"]["to_time"]))  # 营业期限结束
            if to_time is None:
                to_time = "_"
            approved_time = time.strftime("%Y-%m-%d", time.localtime(row["_source"]["approved_time"]))  # 核准日期
            if approved_time is None:
                approved_time = "_"
            business_scope = row['_source']["business_scope"]  # 经营范围
            if business_scope is None:
                business_scope = "_"
            companylist["companyname"] = companyname
            companylist["legal_person"] = legal_person
            companylist["cred_code"] = cred_code
            companylist["province"] = gsb_province
            companylist["city"] = gsb_city
            companylist["reg_location"] = reg_location
            companylist["establish_time"] = establish_time
            companylist["company_cate_1"] = company_cate_1
            companylist["company_org_type"] = company_org_type
            companylist["reg_institute"] = reg_institute
            companylist["reg_capital"] = reg_capital
            companylist["reg_status"] = reg_status
            companylist["from_time"] = from_time
            companylist["to_time"] = to_time
            companylist["approved_time"] = approved_time
            companylist["business_scope"] = business_scope
        return companylist
# a=GetComanyinfoFromES("用友网络科技股份有限公司")
# print(a)