import time

from base import BaseCore
from apscheduler.schedulers.blocking import BlockingScheduler

basecore = BaseCore.BaseCore()
log = basecore.getLogger()
cnx = basecore.cnx
cursor = basecore.cursor
r = basecore.r

# # 连接到Redis
# r = redis.Redis(host="114.115.236.206", port=6379, password='clbzzsn', db=6)
#
# cnx = pymysql.connect(host='114.115.159.144', user='root', password='zzsn9988', db='caiji',
#                            charset='utf8mb4')
# cursor = cnx.cursor()

# def pullDateFromSql():
#     gn_query = "select SocialCode from EnterpriseInfo where Place = '1' "
#     cursor.execute(gn_query)
#     gn_result = cursor.fetchall()
#
#     gw_query = "select SocialCode from EnterpriseInfo where Place = '2' "
#     cursor.execute(gw_query)
#     gw_result = cursor.fetchall()
#
#     gw_social_list = [item[0] for item in gw_result]
#     gn_social_list = [item[0] for item in gn_result]
#     return gn_social_list,gw_social_list

def NewsEnterprise():
    #获取国内企业
    gn_query = "select SocialCode from EnterpriseInfo where Place = '1'"
    cursor.execute(gn_query)
    gn_result = cursor.fetchall()
    #获取国外企业
    gw_query = "select SocialCode from EnterpriseInfo where Place = '2'"
    cursor.execute(gw_query)
    gw_result = cursor.fetchall()

    gw_social_list = [item[0] for item in gw_result]
    gn_social_list = [item[0] for item in gn_result]
    # return gn_social_list, gw_social_list
    print('=======')
    # gn_social_list,gw_social_list = pullDateFromSql()
    #将数据插入到redis中
    for item in gn_social_list:
        r.rpush('NewsEnterprise:gnqy_socialCode', item)

    for item in gw_social_list:
        r.rpush('NewsEnterprise:gwqy_socialCode', item)

def NewsEnterprise_task():
    # 实例化一个调度器
    scheduler = BlockingScheduler()
    # 每天执行一次
    scheduler.add_job(NewsEnterprise, 'cron', hour=12,minute=0,max_instances=2)
    try:
        # redisPushData  # 定时开始前执行一次
        scheduler.start()
    except Exception as e:
        print('定时采集异常', e)
        pass

def NoticeEnterprise():
    # 获取国内企业
    gn_query = "select SocialCode from EnterpriseInfo where Place = '1' and SecuritiesCode is not null limit 1 "
    cursor.execute(gn_query)
    gn_result = cursor.fetchall()
    gn_social_list = [item[0] for item in gn_result]
    print('=======')
    for item in gn_social_list:
        r.rpush('NoticeEnterprise:gnqy_socialCode', item)

def NoticeEnterprise_task():
    # 实例化一个调度器
    scheduler = BlockingScheduler()
    # 每天执行一次
    scheduler.add_job(NoticeEnterprise, 'cron', hour=12,minute=0)
    try:
        # redisPushData  # 定时开始前执行一次
        scheduler.start()
    except Exception as e:
        print('定时采集异常', e)
        pass

def AnnualEnterprise():
    # 获取国内企业
    gn_query = "select SocialCode from EnterpriseInfo where Place = '1' and SecuritiesCode is not null"
    cursor.execute(gn_query)
    gn_result = cursor.fetchall()
    gn_social_list = [item[0] for item in gn_result]
    print('=======')
    for item in gn_social_list:
        r.rpush('AnnualEnterprise:gnqy_socialCode', item)

def AnnualEnterprise_task():
    # 实例化一个调度器
    scheduler = BlockingScheduler()
    # 每年执行一次
    scheduler.add_job(AnnualEnterprise, 'cron', second='*/10')
    try:
        # redisPushData  # 定时开始前执行一次
        scheduler.start()
    except Exception as e:
        print('定时采集异常', e)
        pass

def BaseInfoEnterprise():
    # 获取国内企业
    gn_query = "select SocialCode from EnterpriseInfo where Place = '1' limit 1 "
    cursor.execute(gn_query)
    gn_result = cursor.fetchall()
    gn_social_list = [item[0] for item in gn_result]
    print('=======')
    for item in gn_social_list:
        r.rpush('BaseInfoEnterprise:gnqy_socialCode', item)

#企业基本信息
def BaseInfoEnterprise_task():
    # 实例化一个调度器
    scheduler = BlockingScheduler()
    # 每年执行一次
    scheduler.add_job(BaseInfoEnterprise, 'cron', second='*/10')
    try:
        # redisPushData  # 定时开始前执行一次
        scheduler.start()
    except Exception as e:
        print('定时采集异常', e)
        pass


if __name__ == "__main__":
    start = time.time()
    # NewsEnterprise_task()
    # NoticeEnterprise_task()
    AnnualEnterprise_task()
    log.info(f'====={basecore.getNowTime(1)}=====添加数据成功======耗时：{basecore.getTimeCost(start,time.time())}===')
    # cnx.close()
    # cursor.close()
    # basecore.close()
