# 根据信用代码获取天眼查id
import json
import random
import sys
import time

import pymysql
import requests
from retry import retry

from base import BaseCore
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
requests.adapters.DEFAULT_RETRIES = 5
baseCore = BaseCore.BaseCore()
log = baseCore.getLogger()
# headers = {
#         'Accept': 'application/json, text/plain, */*',
#         'Accept-Language': 'zh-CN,zh;q=0.9',
#         'Connection': 'keep-alive',
#         'Content-Length': '32',
#         'Content-Type': 'application/json',
#         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
#         'version': 'TYC-Web'
# }

headers = {
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Content-Type': 'application/json',
    'Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'version': 'TYC-Web'
}

header = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br, zstd',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Content-Length': '93',
    'Content-Type': 'application/json',
    'Host': 'capi.tianyancha.com',
    'Origin': 'https://www.tianyancha.com',
    'Referer': 'https://www.tianyancha.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
    'X-AUTH-TOKEN': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzYzNjcxMTc0NiIsImlhdCI6MTcxNDk1Njg3MywiZXhwIjoxNzE3NTQ4ODczfQ.qMEvtETT7RS3Rhwq9idu5H2AKMxc2cjtr5bDDW6C6yOFKR-ErgDwT4SOBX9PB2LWDexAG2hNaeAvn6swr-n6VA',
    'X-TYCID': 'dad485900fcc11ee8c0de34479b5b939',
    'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'version': 'TYC-Web'
}

# cnx = pymysql.connect(host='114.116.44.11', user='caiji', password='f7s0&7qqtK', db='dbScore', charset='utf8mb4')
# cursor= cnx.cursor()
cnx_ = baseCore.cnx
cursor_ = baseCore.cursor
taskType = '天眼查企业id/天眼查'


# 根据信用代码获取天眼查id 企业名字等信息

@retry(tries=5, delay=3)
def getTycIdByXYDM(com_name, s):
    retData = {'state': False, 'tycData': None, 'reput': True}
    url = f"https://capi.tianyancha.com/cloud-tempest/search/suggest/v3?_={baseCore.getNowTime(3)}"
    # url=f"https://capi.tianyancha.com/cloud-tempest/search/suggest/v3"
    # ip = baseCore.get_proxy()
    paramJsonData = {'keyword': com_name}

    # response = requests.post(url=url, json=paramJsonData, headers=header, verify=False, proxies=ip)
    response = s.post(url, json=paramJsonData, headers=headers)
    time.sleep(random.randint(3, 5))
    retJsonData = json.loads(response.content.decode('utf-8'))
    if retJsonData['data'] and retJsonData['state'] == 'ok':
        pass
    else:
        log.error(f"---{com_name}-未查询到该企业---")
        retData['reput'] = False
        return retData
    matchType = retJsonData['data'][0]['matchType']
    if matchType == '信用代码匹配' or matchType == '公司名称匹配':
        retData['state'] = True
        retData['tycData'] = retJsonData['data'][0]
        response.close()
        return retData
    else:
        log.error(f"{com_name}------{retJsonData}")
        response.close()
        return retData


# 更新天眼查企业基本信息
def updateTycInfo():
    while True:
        # 根据从Redis中拿到的社会信用代码,在数据库中获取对应基本信息
        social_code = baseCore.redicPullData('NewsEnterprise:gnqy_socialCode')
        # social_code = '9111000066990444XF'
        # 判断 如果Redis中已经没有数据，则等待
        if social_code == None:
            time.sleep(20)
            continue
        start = time.time()

        data = baseCore.getInfomation(social_code)
        if len(data) != 0:
            pass
        else:
            # 数据重新塞入redis
            baseCore.rePutIntoR('NewsEnterprise:gnqy_socialCode', social_code)
            continue

        xydm = data[2]
        tycid = data[11]
        if tycid == None or tycid == '':
            try:
                retData = getTycIdByXYDM(xydm)
                if retData['tycData'] and retData['reput']:
                    tycid = retData['id']
                    # todo:写入数据库
                    updateSql = f"update EnterpriseInfo set TYCID = '{tycid}' where SocialCode = '{xydm}'"
                    cursor_.execute(updateSql)
                    cnx_.commit()
                elif not retData['tycData'] and retData['reput']:
                    state = 0
                    takeTime = baseCore.getTimeCost(start, time.time())
                    baseCore.recordLog(social_code, taskType, state, takeTime, '', '获取天眼查id失败')
                    log.info(f'======={social_code}====重新放入redis====')
                    baseCore.rePutIntoR('NewsEnterprise:gnqy_socialCode', social_code)
                    continue
                elif not retData['reput'] and not retData['tycData']:
                    continue
            except Exception as e:
                log.error(e)
                state = 0
                takeTime = baseCore.getTimeCost(start, time.time())
                baseCore.recordLog(social_code, taskType, state, takeTime, '', '获取天眼查id失败')
                baseCore.rePutIntoR('NewsEnterprise:gnqy_socialCode', social_code)
                continue


# 从数据里获取天眼查id
def getTycIdByDB(social_code, cursor, start, item, s):

    data = baseCore.getInfomation(social_code)
    if len(data) != 0:
        id = data[0]
        com_name = data[1]
        xydm = data[2]
        tycid = data[11]
        count = data[17]
    else:
        # 数据重新塞入redis
        # log.info(f'数据库中无该企业{social_code}')
        sql = f"SELECT * FROM sys_base_enterprise WHERE social_credit_code = '{social_code}'"
        cursor.execute(sql)
        data = cursor.fetchone()
        if data:
            pass
        else:
            # 数据库中并没有该企业 需要新增
            pass
        id = data[0]
        com_name = data[3]
        xydm = data[1]
        conut = 0
        # 写入数据库
        insert = "INSERT INTO EnterpriseInfo(CompanyName, SocialCode) VALUES (%s, %s)"
        cursor_.execute(insert, (com_name, xydm))
        cnx_.commit()
        tycid = ''
    if tycid == None or tycid == '':
        try:
            retData = getTycIdByXYDM(com_name, s)
            if retData['state']:
                tycid = retData['tycData']['id']
                # # todo:写入数据库
                updateSql = f"update EnterpriseInfo set TYCID = '{tycid}' where SocialCode = '{xydm}'"
                cursor_.execute(updateSql)
                cnx_.commit()
            else:
                state = 0
                takeTime = baseCore.getTimeCost(start, time.time())
                baseCore.recordLog(social_code, taskType, state, takeTime, '', '获取天眼查id失败')
                log.info(f'======={social_code}====重新放入redis====')
                baseCore.rePutIntoR('CorPersonEnterpriseError:gnqy_socialCode', item)
                return None
        except:
            state = 0
            takeTime = baseCore.getTimeCost(start, time.time())
            baseCore.recordLog(social_code, taskType, state, takeTime, '', '获取天眼查id失败')
            baseCore.rePutIntoR('CorPersonEnterpriseError:gnqy_socialCode', item)
            return None

    return tycid


if __name__ == '__main__':
    updateTycInfo()
