import time

from base import BaseCore
from apscheduler.schedulers.blocking import BlockingScheduler
import pymysql
basecore = BaseCore.BaseCore()
log = basecore.getLogger()
r = basecore.r


def conn11():
    conn = pymysql.Connect(host='114.116.44.11', port=3306, user='caiji', passwd='f7s0&7qqtK', db='clb_project',
                           charset='utf8')
    cursor = conn.cursor()
    return conn,cursor

#企业公告
def shizhiCodeFromSql():
    conn,cursor=conn11()
    try:
        gn_query = "select securities_code from sys_base_enterprise_ipo where category in ('4','5','6') "
        cursor.execute(gn_query)
        gn_result = cursor.fetchall()
        gn_social_list = [item[0] for item in gn_result]
        print('shizhiCodeFromSql开始将股票代码放入redis=======')
        for item in gn_social_list:
            r.rpush('NoticeEnterprise:shizhi_code', item)
        print('shizhiCodeFromSql将股票代码放入redis结束')
    except Exception as e:
        log.info("数据查询异常")
    finally:
        cursor.close()
        conn.close()
#企业公告
def yahooCodeFromSql():
    conn,cursor=conn11()
    try:
        gn_query = "select securities_code from sys_base_enterprise_ipo where category in ('4','5','6') "
        cursor.execute(gn_query)
        gn_result = cursor.fetchall()
        gn_social_list = [item[0] for item in gn_result]
        print('yahooCodeFromSql开始将股票代码放入redis=======')
        for item in gn_social_list:
            r.rpush('NoticeEnterprise:securities_code', item)
        print('yahooCodeFromSql将股票代码放入redis结束')
    except Exception as e:
        log.info("数据查询异常")
    finally:
        cursor.close()
        conn.close()

def yahooCode_task():
    # 实例化一个调度器
    scheduler = BlockingScheduler()
    # 每天执行一次
    # scheduler.add_job(yahooCodeFromSql, 'cron', hour=0,minute=0)
    #3天执行一次
    scheduler.add_job(yahooCodeFromSql, 'cron', day='*/3', hour=0, minute=0)
    # 每天执行一次
    scheduler.add_job(shizhiCodeFromSql, 'cron', hour=10,minute=0)
    try:
        yahooCodeFromSql()  # 定时开始前执行一次
        shizhiCodeFromSql()  # 定时开始前执行一次
        scheduler.start()
    except Exception as e:
        print('定时采集异常', e)
        pass



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