#补充剩余核心人员信息
#先采集天眼查id，再通过id采集核心人员信息
import datetime
import json

import requests,time,random
import pandas as pd
from bs4 import BeautifulSoup
import urllib3
from base.BaseCore import BaseCore
from getTycId import getTycIdByXYDM
baseCore = BaseCore()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
log = baseCore.getLogger()
headers = {
    'Cookie':'HWWAFSESID=b6312a4594bea18413c; HWWAFSESTIME=1686818921445; csrfToken=e7sNDKWelJwlcjnm6Rlny887; TYCID=6ff6bc600b5911ee89d35bf79a73a3b1; bannerFlag=true; ssuid=1534238432; refresh_page=0; _ga=GA1.2.1790752229.1688467828; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22307016917%22%2C%22first_id%22%3A%22188be3e337e4bf-0d85716d366e44-26031d51-1049088-188be3e337f19e%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTg4YmUzZTMzN2U0YmYtMGQ4NTcxNmQzNjZlNDQtMjYwMzFkNTEtMTA0OTA4OC0xODhiZTNlMzM3ZjE5ZSIsIiRpZGVudGl0eV9sb2dpbl9pZCI6IjMwNzAxNjkxNyJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22307016917%22%7D%2C%22%24device_id%22%3A%22188be3e337e4bf-0d85716d366e44-26031d51-1049088-188be3e337f19e%22%7D; jsid=SEO-BAIDU-ALL-SY-000001; bdHomeCount=7; Hm_lvt_e92c8d65d92d534b0fc290df538b4758=1693986307; tyc-user-info=%7B%22state%22%3A%220%22%2C%22vipManager%22%3A%220%22%2C%22mobile%22%3A%2213592481839%22%7D; tyc-user-info-save-time=1693986377592; auth_token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzU5MjQ4MTgzOSIsImlhdCI6MTY5Mzk4NjM3NywiZXhwIjoxNjk2NTc4Mzc3fQ.xeK54nMtB5wt7ipdOjhrzdplT1azvezrTuoD1b8i3OguqMB97ZOR1pFbRsP7vsKRdZ3Fsf5Y5ZqlmRKAVHGraA; Hm_lpvt_e92c8d65d92d534b0fc290df538b4758=1693986412',
    # 'Cookie': 'TYCID=82cbe530204b11ed9f23298cecec1c60; ssuid=3927938144; _ga=GA1.2.1842488970.1670638075; jsid=SEO-BAIDU-ALL-SY-000001; tyc-user-info={%22state%22:%220%22%2C%22vipManager%22:%220%22%2C%22mobile%22:%2215565837784%22}; tyc-user-info-save-time=1678953978429; auth_token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTU2NTgzNzc4NCIsImlhdCI6MTY3ODk1Mzk3OCwiZXhwIjoxNjgxNTQ1OTc4fQ.wsNxLWMkZVrtOEvo_CCDPD38R7F23c5yk7dFAdHkwFPkZhEEvmiv0nlt7UD0ZWfo3t8aYxc4qvu4ueEgMubJ5g; tyc-user-phone=%255B%252215565837784%2522%255D; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22284710084%22%2C%22first_id%22%3A%22182b9ca585ead-089598c1d7f7928-26021d51-1327104-182b9ca585f7f1%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfbG9naW5faWQiOiIyODQ3MTAwODQiLCIkaWRlbnRpdHlfY29va2llX2lkIjoiMTgyYjljYTU4NWVhZC0wODk1OThjMWQ3Zjc5MjgtMjYwMjFkNTEtMTMyNzEwNC0xODJiOWNhNTg1ZjdmMSJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22284710084%22%7D%2C%22%24device_id%22%3A%22182b9ca585ead-089598c1d7f7928-26021d51-1327104-182b9ca585f7f1%22%7D; HWWAFSESID=fa776898fa88a6520ea; HWWAFSESTIME=1679899464128; csrfToken=m3cB6mHsznwIuppkT-S8oYc6; Hm_lvt_e92c8d65d92d534b0fc290df538b4758=1679016180,1679471093,1679732923,1679899468; bdHomeCount=28; bannerFlag=true; show_activity_id_92=92; searchSessionId=1679899783.48494979; Hm_lpvt_e92c8d65d92d534b0fc290df538b4758=1679899783',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}
cnx_ = baseCore.cnx
cursor_ = baseCore.cursor
list_all_1 = []
list_all_2 = []
taskType = '天眼查/核心人员'
def doJob():
    while True:
        # 根据从Redis中拿到的社会信用代码,在数据库中获取对应基本信息
        social_code = baseCore.redicPullData('CorPersonEnterprise:gnqy_socialCode')
        # 判断 如果Redis中已经没有数据，则等待
        # social_code = '9135020056842712XB'
        if social_code == None:
            time.sleep(20)
            continue
        start = time.time()
        try:
            data = baseCore.getInfomation(social_code)
            if len(data) != 0:
                pass
            else:
                #数据重新塞入redis
                baseCore.rePutIntoR('CorPersonEnterprise:gnqy_socialCode',social_code)
                continue
            id = data[0]
            xydm = data[2]
            tycid = data[11]
            if tycid == None or tycid == '':
                try:
                    retData = getTycIdByXYDM(xydm)
                    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('CorPersonEnterprise:gnqy_socialCode', social_code)
                        continue
                except:
                    state = 0
                    takeTime = baseCore.getTimeCost(start, time.time())
                    baseCore.recordLog(social_code, taskType, state, takeTime, '', '获取天眼查id失败')
                    baseCore.rePutIntoR('CorPersonEnterprise:gnqy_socialCode', social_code)
                    continue
            count = data[17]
            log.info(f"{id}---{xydm}----{tycid}----开始采集核心人员")
            list_one_info = []
            num = 1
            for page in range(1,2):
                t = int(time.time()*1000)
                       #https://capi.tianyancha.com/cloud-listed-company/listed/getHkNoRepeatSeniorExecutive?_=1692929256462&gid=209370942&pageSize=20&pageNum=1
                url = f'https://capi.tianyancha.com/cloud-listed-company/listed/noRepeatSeniorExecutive?_={t}&gid={tycid}&pageSize=20&pageNum={page}'
                ip = baseCore.get_proxy()
                # res = requests.get(url,headers=headers,proxies=ip)  # ,verify=False
                res = requests.get(url,headers=headers)  # ,verify=False
                time.sleep(1)
                list_all = res.json()['data']['dataList']
                if list_all:
                    for one_info in list_all:
                        name = one_info['name']
                        sex = one_info['sex']
                        education = one_info['education']
                        position = one_info['position']
                        Salary = one_info['salary']
                        #todo:获取当前年份
                        now = datetime.datetime.now()
                        year = now.year
                        try:
                            birthYear = year - int(one_info['age'])
                        except:
                            birthYear = ''
                        StockKeepings = one_info['numberOfShares']
                        currentTerm = one_info['term']
                        personInfo = one_info['resume']

                        try:
                            person_img = one_info['logo']
                        except:
                            person_img = '--'
                        dic_json = {
                            "socialCreditCode":social_code,
                            "name":name,
                            "sex":sex,
                            "education":education,
                            "position":position,
                            "salary":Salary,
                            "birthYear":birthYear,
                            "shareNum":StockKeepings,
                            "shareRatio":'',
                            "benefitShare":'',
                            "currentTerm":currentTerm,
                            "personInfo":personInfo,
                            "sort":str(num)
                        }
                        dic_json_img = {
                            "socialCreditCode":social_code,
                            "name":name,
                            "sex":sex,
                            "education":education,
                            "position":position,
                            "salary":Salary,
                            "birthYear":birthYear,
                            "shareNum":StockKeepings,
                            "shareRatio":'',
                            "benefitShare":'',
                            "currentTerm":currentTerm,
                            "personInfo":personInfo,
                            "头像":person_img,
                            "sort":str(num)
                        }
                        num = num+1
                        list_one_info.append(dic_json)
                        # list_all_2.append(dic_json_img)
                else:
                    t = int(time.time() * 1000)
                    url = f'https://capi.tianyancha.com/cloud-listed-company/listed/getHkNoRepeatSeniorExecutive?_={t}&gid={tycid}&pageSize=20&pageNum={page}'
                    ip = baseCore.get_proxy()
                    # res = requests.get(url, headers=headers, proxies=ip)  # ,verify=False
                    res = requests.get(url, headers=headers)  # ,verify=False
                    time.sleep(1)
                    list_all = res.json()['data']['dataList']
                    if list_all:
                        for one_info in list_all:
                            name = one_info['personal_name']
                            try:
                                sex = one_info['gender2']
                            except:
                                sex = ''
                            education = ''
                            position = one_info['position_name']
                            Salary = ''
                            birthYear = ''
                            personInfo = one_info['resume_cn']
                            dic_json = {
                                "socialCreditCode": social_code,
                                "name": name,
                                "sex": sex,
                                "education": education,
                                "position": position,
                                "salary": Salary,
                                "birthYear": birthYear,
                                "shareNum": '',
                                "shareRatio": '',
                                "benefitShare": '',
                                "currentTerm": '',
                                "personInfo": personInfo,
                                "sort": str(num)
                            }
                            num = num + 1
                            list_one_info.append(dic_json)
                    else:
                        t = int(time.time() * 1000)
                        url = f'https://capi.tianyancha.com/cloud-company-background/company/dim/staff?_={t}&gid={tycid}&pageSize=20&pageNum={page}'
                        ip = baseCore.get_proxy()
                        # res = requests.get(url, headers=headers, proxies=ip)  # ,verify=False
                        res = requests.get(url, headers=headers)  # ,verify=False
                        time.sleep(1)
                        list_all = res.json()['data']['result']
                        # todo:增加一种情况
                        if list_all:
                            for one_info in list_all:
                                name = one_info['name']
                                try:
                                    sex = one_info['sex']
                                except:
                                    sex = ''
                                try:
                                    education = one_info['education']
                                except:
                                    education = ''
                                try:
                                    position = one_info['typeSore']
                                except:
                                    position = ''
                                try:
                                    Salary = one_info['salary']
                                except:
                                    Salary = ''
                                birthYear = ''
                                try:
                                    shareRatio = one_info['percent']
                                except:
                                    shareRatio = ''
                                try:
                                    benefitShare = one_info['finalBenefitShares']
                                except:
                                    benefitShare = ''
                                try:
                                    currentTerm = one_info['term']
                                except:
                                    currentTerm = ''
                                person_id = one_info['id']
                                person_url = f'https://www.tianyancha.com/human/{person_id}-c{tycid}'
                                # person_res = requests.get(person_url, headers=headers, proxies=ip)
                                person_res = requests.get(person_url, headers=headers)
                                person_soup = BeautifulSoup(person_res.content, 'html.parser')
                                try:
                                    personInfo = person_soup.find('span', {'class': '_56d0a'}).text.strip()
                                except:
                                    personInfo = ''
                                try:
                                    person_img = one_info['logo']
                                except:
                                    person_img = '--'
                                dic_json = {
                                    "socialCreditCode": social_code,
                                    "name": name,
                                    "sex": sex,
                                    "education": education,
                                    "position": position,
                                    "salary": Salary,
                                    "birthYear": birthYear,
                                    "shareNum": '',
                                    "shareRatio": shareRatio,
                                    "benefitShare": benefitShare,
                                    "currentTerm": currentTerm,
                                    "personInfo": personInfo,
                                    "sort": str(num)
                                }
                                dic_json_img = {
                                    "socialCreditCode": social_code,
                                    "name": name,
                                    "sex": sex,
                                    "education": education,
                                    "position": position,
                                    "salary": Salary,
                                    "birthYear": birthYear,
                                    "shareNum": '',
                                    "shareRatio": shareRatio,
                                    "benefitShare": benefitShare,
                                    "currentTerm": '',
                                    "personInfo": personInfo,
                                    "头像": person_img,
                                    "sort": str(num)
                                }
                                num = num + 1
                                list_one_info.append(dic_json)

            json_updata = json.dumps(list_one_info)
            if json_updata == '[]':
                continue
            else:
                pass
            response = requests.post('http://114.115.236.206:8088/sync/executive',data=json_updata,timeout=300, verify=False)
            print(response.text)
            log.info('=========成功======')
        except Exception as e:
            log.info(f'==={social_code}=====企业核心人员采集失败===重新放入redis====')
            log.info(e)
            # 重新塞入redis
            baseCore.rePutIntoR('CorPersonEnterprise:gnqy_socialCode', social_code)
            state = 0
            takeTime = baseCore.getTimeCost(start, time.time())
            baseCore.recordLog(social_code, taskType, state, takeTime, '', f'获取企业信息失败--{e}')
            time.sleep(5)
        # break
# df_img = pd.DataFrame(list_all_2)
# df_img.to_excel('企业主要人员-头像.xlsx',index=False)
if __name__ == "__main__":
    doJob()