import requests,random,time,json,re,pymysql,datetime
import pandas as pd


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_all_info = []
list_code = []
list_code_no = []

now = datetime.datetime.now()
yes1 = now + datetime.timedelta(days = -1)
date_yes = yes1.strftime('%Y-%m-%d')

print(date_yes)
for page in range(21,200):
    print(page)
    url = f'https://datacenter-web.eastmoney.com/api/data/v1/get?reportName=RPTA_WEB_RZRQ_GGMX&columns=ALL&source=WEB&pageNumber={page}&pageSize=50&sortColumns=RZJME&sortTypes=-1&filter=(date%3D%27{date_yes}%27)'

    res = requests.get(url).json()
    try:
        list_all = res['result']['data']
    except:
        print('无数据')
        break

    for one_com in list_all:
        com_code = one_com['SCODE']
        list_code.append(com_code)
        with cnx.cursor() as cursor:
            sql_sel = '''select social_credit_code from sys_base_enterprise_ipo where securities_code = %s '''
            cursor.execute(sql_sel,com_code)
            select = cursor.fetchone()
            if select:
                com_social = select[0]
            else:
                print(f'数据库中无此股票代码：{com_code}')
                list_code_no.append(com_code)
                com_social = ''
        if com_social:
            with cnx.cursor() as cursor:
                sql_sel = '''select name from sys_base_enterprise where social_credit_code = %s '''
                cursor.execute(sql_sel,com_social)
                select = cursor.fetchone()
                if select:
                    com_name = select[0]
                else:
                    print(f'数据库中无此信用代码：{com_social}')
                    com_name = ''
        else:
            com_name = ''

        com_short = one_com['SECNAME']
        securities_code = com_code
        closing_price = str(one_com['SPJ'])
        fluctuation_range = str(one_com['ZDF'])
        financing_balance = str(one_com['RZYE'])
        financing_balance_circulating_ratio = str(one_com['RZYEZB'])
        financing_balance_purchase_amount = str(one_com['RZMRE'])
        financing_balance_refund = str(one_com['RZCHE'])
        financing_balance_net_purchases = str(one_com['RZJME'])
        short_sale_balance = str(one_com['RQYE'])
        short_sale_margin = str(one_com['RQYL'])
        short_sale_selling_volume = str(one_com['RQMCL'])
        short_sale_repayment_amount = str(one_com['RQCHL'])
        short_sale_net_sales = str(one_com['RQJMG'])
        financing_short_sale_balance = str(one_com['RZRQYE'])
        financing_short_sale_balance_difference = str(one_com['RZRQYECZ'])
        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_value where stock_code = %s and data_time = %s '''
            cursor.execute(sql_sel,(com_code,date_yes))
            select = cursor.fetchone()
            if select:
                continue

        tuple_info = (com_social,com_code,com_name,com_short,securities_code,
                     closing_price,fluctuation_range,financing_balance,
                     financing_balance_circulating_ratio,financing_balance_purchase_amount,
                     financing_balance_refund,financing_balance_net_purchases,
                     short_sale_balance,short_sale_margin,short_sale_selling_volume,
                     short_sale_repayment_amount,short_sale_net_sales,
                     financing_short_sale_balance,financing_short_sale_balance_difference,
                     create_by,create_time,date_yes)

        list_all_info.append(tuple_info)

with cnx_in.cursor() as cursor:
    sql_in = '''insert into ranklist_market_value(social_credit_code,stock_code,enterprise_name,securities_abbreviation,securities_code,closing_price,fluctuation_range,financing_balance,financing_balance_circulating_ratio,financing_balance_purchase_amount,financing_balance_refund,financing_balance_net_purchases,short_sale_balance,short_sale_margin,short_sale_selling_volume,short_sale_repayment_amount,short_sale_net_sales,financing_short_sale_balance,financing_short_sale_balance_difference,create_by,create_time,data_time) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'''
    cursor.executemany(sql_in,list_all_info)
    cnx_in.commit()
