#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/5/14 0014 11:04
# @Author  : guopeng
# @File    : change_message.py
# @Software: PyCharm Community Edition

from flask import request
import json
from common.re_companyName import strQ2B
from dao.change_message_es import ChangeMessage
from common.crm_change_standard import getStandardName


# 内网IP
# esip = "172.16.18.110"
# esprot = "9201"

# esip = "43.247.184.94"
# esprot = "9200"
# esuser = "admines"
# espassword = "adminGSB."

# def qingxi(ustring):
#     """全角转半角"""
#     if ustring:
#         rstring = ""
#         for uchar in ustring:
#             inside_code = ord(uchar)
#             if inside_code == 12288:  # 全角空格直接转换
#                 inside_code = 32
#             elif (inside_code >= 65281 and inside_code <= 65374):  # 全角字符（除空格）根据关系转化
#                 inside_code -= 65248
#             rstring += chr(inside_code)
#         for line in list(rstring):
#             if line in ['·', '(', ')']:
#                 return rstring
#         # if '·' or '(' or ')' in rstring:
#         #    return  rstring
#         data = re.sub('([\W,_]+)', '', rstring)
#         return data

class Doelastic(object):
    def SearchCompany(self,factor):
        ChangeMessageInfo = ChangeMessage()
        results = ChangeMessageInfo.select_branch(factor)
        sp=[]
        res = results["hits"]["hits"]
        print(res)
        for i in range(len(res)):
            res[i]['_source']["num"]=i+1
            if res[i]['_source']["change_item"] == None:
                res[i]['_source']["change_item"]='-'
            sp.append(res[i]['_source'])
        return sp
    def getCompnayByName(self,company_name):
        factor = ['gsb_company_name.raw',company_name]
        sss = self.SearchCompany(factor)
        return sss
def companyChage(server):
    @server.route("/api/changeAPI", methods=['POST'])
    def APIChange():
        try:
            el = Doelastic()
            mycompanyname = request.get_json().get("mycompanyname")  # 得到问题
            # re_mycompanyname = strQ2B(mycompanyname)
            print('re_mycompanyname',mycompanyname)
            standard_company_name = getStandardName(mycompanyname)
            print('standard_company_name',standard_company_name)
            r_1 = el.getCompnayByName(standard_company_name)
            if r_1:
                return json.dumps({"status": 1, "msg":"成功","data": r_1}, ensure_ascii=False)
            else:
                return json.dumps({"status": -1, "msg":"失败","data": 0}, ensure_ascii=False)
        except:
            return json.dumps({"status": -1, "msg":"调用服务出错","data": {}}, ensure_ascii=False)
#
# if __name__ == "__main__":
#     el = Doelastic()
#     el.getCompnayByName('青岛永创信息技术有限公司')
# if __name__ == '__main__':
#     app.run(host='127.0.0.1', port=5000, debug=True)
    # app.run(host=host'172.16.18.120', port=9500, debug=True)


