import requests,random,time,json,re,pymysql
import pandas as pd
from bs4 import BeautifulSoup

cnx = pymysql.connect(host='114.115.159.144',user='caiji', password='zzsn9988', db='clb_project', charset='utf8mb4')
cnx_in = pymysql.connect(host='114.116.44.11',user='caiji', password='f7s0&7qqtK', db='clb_project', charset='utf8mb4')

list_com = []

with cnx_in.cursor() as cursor:
    sql_sel = '''select securities_code,social_credit_code,exchange,securities_short_name from sys_base_enterprise_ipo where category = 1 '''
    cursor.execute(sql_sel)
    selects = cursor.fetchall()

    for rows in selects:
        try:
            com_code = rows[0]
            social_code = rows[1]
            com_type = rows[2]
            short_name = rows[3]

            if com_type == 1:
                com_code = 'BJ'+str(com_code)
            elif com_type == 2:
                com_code = 'SH'+str(com_code)
            elif com_type == 3:
                com_code = 'SZ'+str(com_code)
            else:
                print(f'{com_code}:!!!!!!!!!!!!!!!!!')
            sql_sel2 = '''select name from sys_base_enterprise where social_credit_code = %s '''
            cursor.execute(sql_sel2,social_code)
            select = cursor.fetchone()
            com_name = select[0]

            list_com.append([com_code,social_code,com_name,short_name])
        except:
            continue
        # print(com_code)


for com_num in range(len(list_com)):
    com = list_com[com_num]
    com_code = com[0]
    print(com_num)
    print(com_code)
    code = com_code[2:]
    social_code = com[1]
    com_name = com[2]
    short_name = com[3]
    url = f'https://emweb.securities.eastmoney.com/PC_HSF10/CapitalStockStructure/PageAjax?code={com_code}'

    res = requests.get(url).json()
    list_all = res['lngbbd']

    for one_info in list_all:
        total_share_capital = one_info['TOTAL_SHARES']
        timeline = one_info['END_DATE'][:10]
        change_reason = one_info['CHANGE_REASON']
        listed_circulating_a_shares = one_info['LISTED_A_SHARES']
        unit = '万股'
        create_by = 'TangYuHang'
        create_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

        with cnx_in.cursor() as cursor:
            sql_sel = '''select id from ranklist_market_equity where social_credit_code = %s and timeline = %s '''
            cursor.execute(sql_sel,(social_code,timeline))
            select = cursor.fetchone()
            if select:
                break
            else:
                tuple_info = (social_code,code,com_name,short_name,code,
                             total_share_capital,timeline,change_reason,
                             listed_circulating_a_shares,unit,create_by,
                             create_time)
                sql_in = '''insert into ranklist_market_equity(social_credit_code,stock_code,enterprise_name,securities_abbreviation,securities_code,total_share_capital,timeline,change_reason,listed_circulating_a_shares,unit,create_by,create_time) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'''
                cursor.execute(sql_in,tuple_info)
                cnx_in.commit()

