#encoding:UTF8

from BaseDataMaintenance.dataSource.setttings import *
import requests
import json
import pymysql
import pymongo
import tablestore
import oss2
import redis

def solrQuery(collection,args):
    if collection in solr_collections:
        _arg = ""
        for k,v in args.items():
            _arg += "&%s=%s"%(k,v)
        _arg = _arg[1:]
        url = "%s%s/select?%s"%(solr_collections[collection],collection,_arg)
        resp = requests.get(url)
        if resp.status_code==200:
            return json.loads(resp.content.decode())
        return None

def solrQuery_url(url):
    resp = requests.get(url)
    if resp.status_code==200:
        return json.loads(resp.content.decode())
    return None

def getConnection_mysql(db=None):
    if db is None:
        db = mysql_db
    connect = pymysql.Connect(host=mysql_host, port=mysql_port, db=db, user=mysql_user, passwd=mysql_pass)
    return connect

def getConnection_testmysql(db=None):
    if db is None:
        db = test_mysql_db
    connect = pymysql.Connect(host=test_mysql_host, port=test_mysql_port, db=db, user=test_mysql_user, passwd=test_mysql_pass)
    return connect

def getConnection_oracle():
    import cx_Oracle
    connect = cx_Oracle.connect(oracle_user,oracle_pass,'%s:%s/%s'%(oracle_host,oracle_port,oracle_db), encoding = "UTF-8", nencoding = "UTF-8")
    # connect = cx_Oracle.connect('%s/%s@%s:%s/%s'%(oracle_user,oracle_pass,oracle_host,oracle_port,oracle_db))
    return connect

def getConnect_mongodb():
    client = pymongo.MongoClient(mongo_host,mongo_port)
    db = client[mongo_db]
    db.authenticate(mongo_user,mongo_pass)
    return db

def make_elasticSearch(query):
    resp = requests.post(elasticSearch_url,json=query)
    if resp.status_code==200:
        return json.loads(resp.content.decode())
    return None

def getConnect_neo4j():
    from py2neo import Graph,NodeMatcher
    graph = Graph(host=neo4j_host,auth=(neo4j_user,neo4j_pass))
    return graph
    # finded = graph.run("MATCH (n:Organization)-[R:ZhaoBiaoRelation]->(p:Project) where n.name='昆山市周市基础建设开发有限公司的昆山市恒迪服装辅料公司'  RETURN p LIMIT 25")
    # print(json.loads(json.dumps(finded.data())))
    # print(finded)


from urllib import request
import os
def check_net(testserver):
    try:
        response = os.system("ping -c 1 " + testserver)
        if response == 0:
            return True
    except:
        return False
    return False

import platform

is_internal = False
if platform.system()=="Windows":
    OTS_URL = "https://bxkc-ots.cn-hangzhou.ots.aliyuncs.com"
    OTS_URL = "https://bxkc-ots.cn-hangzhou.vpc.tablestore.aliyuncs.com"
else:
    OTS_URL = "https://bxkc-ots.cn-hangzhou.vpc.tablestore.aliyuncs.com"
    check_url = "oss-cn-hangzhou-internal.aliyuncs.com"
    is_internal = check_net(check_url)
    if not is_internal:
        is_internal = False
        OTS_URL = "https://bxkc-ots.cn-hangzhou.ots.aliyuncs.com"



print(OTS_URL)
# if platform.system()=="Windows":
#     OTS_URL = "https://bxkc-ots.cn-hangzhou.ots.aliyuncs.com"
# else:
#     OTS_URL = "https://bxkc-ots.cn-hangzhou.vpc.tablestore.aliyuncs.com"

def getConnect_ots():
    ots_client = tablestore.client.OTSClient(OTS_URL, ots_AccessKeyId, ots_AccessKeySecret,
                                             'bxkc-ots', logger_name = 'table_store.log',
                                             retry_policy = tablestore.WriteRetryPolicy(),socket_timeout=120)
    return ots_client

def getConnect_ots_capacity():
    ots_client = tablestore.client.OTSClient(OTS_URL, ots_AccessKeyId, ots_AccessKeySecret,
                                             'bxkc-capacity', logger_name = 'table_store.log',
                                             retry_policy = tablestore.WriteRetryPolicy(),socket_timeout=120)
    return ots_client

def getConnect_gdb():
    from gremlin_python.driver import client
    client = client.Client('ws://gds-bp130d7rgd9m7n61150070pub.graphdb.rds.aliyuncs.com:3734/gremlin', 'g', username="bxkc", password="k0n1bxkc!0K^Em%j")
    callback = client.submitAsync("g.V('北京赛克德利科贸有限公司').outE('ZhongBiaoRelation').inV().inE('ZhaoBiaoRelation').outV()")
    for result in callback.result():
        for item in result:
            print(item.id)
    return client

def getConnection_postgres():
    import psycopg2
    conn = psycopg2.connect(dbname=attach_postgres_db,user=attach_postgres_user,password=attach_postgres_pswd,host=attach_postgres_host,port=attach_postgres_port)
    return conn

def getAuth():
    auth = oss2.Auth(ots_AccessKeyId, ots_AccessKeySecret)
    return auth


def getConnect_activateMQ():
    import stomp
    conn = stomp.Connection(host_and_ports=[(activateMQ_host, activateMQ_port)])
    conn.connect(login=activateMQ_user, passcode=activateMQ_pswd)
    return conn

def getConnect_activateMQ_ali():
    import stomp
    conn = stomp.Connection(host_and_ports=[(activateMQ_ali_host, activateMQ_ali_port)])
    conn.connect(login=activateMQ_ali_user, passcode=activateMQ_ali_pswd)
    return conn


def getConnect_redis_baseline():
    db = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,
                           db=6,password=REDIS_PASS)
    return db

def getConnect_redis_doc():
    db = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,
                           db=7,password=REDIS_PASS)
    return db

def getConnect_redis_product():
    db = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,
                           db=9,password=REDIS_PASS)
    return db

def getConnect_redis_product_pool():
    pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT,
                                db=9,password=REDIS_PASS,max_connections=40)
    return pool

if __name__=="__main__":
    # solrQuery("document",{"q":"*:*"})
    # getConnect_mongodb()
    # data = solrQuery_url('http://47.97.221.63:8983/solr/document/select?fq=(publishtime:[2020-01-01T00:00:00Z%20TO%202020-08-12T23:59:59Z])&q=dochtmlcon:"防盗门"')
    # data = solrQuery("document",{"q":'dochtmlcon:"防盗门"',"fq":'(publishtime:[2020-01-01T00:00:00Z%20TO%202020-08-12T23:59:59Z])',"fl":"city","rows":1})
    # data = make_elasticSearch({"query":{"bool":{"must":[{"wildcard":{"nicknames.keyword":"*服装*"}}],"must_not":[],"should":[]}},"from":0,"size":10,"sort":[],"aggs":{}})
    # print(data)
    # getConnect_neo4j()
    # conn = getConnection_oracle()
    # cursor = conn.cursor()
    # getConnect_gdb()
    import sys,os
    import platform
    print(platform.system())
    print(sys.platform)