#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__ = '每小时获取各平台的的订单数据'

@Time    : 2020/10/11 15:00
@Author  : Kenny-PC
@Software: PyCharm

# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""
from apscheduler.schedulers.blocking import BlockingScheduler

from util import date_util
from util import db_order_util
from util import platform_order_api_util


def start_order_job():
    start_exec_seconds = date_util.getCurrentSecondTime()

    st_unix, et_unix = date_util.getPreviousHourAndCurrentHourSecondTime(start_exec_seconds)

    # st_unix = 1602313200  # 2020/10/10 15:0:0
    # et_unix = 1602316800  # 2020/10/10 16:0:0

    print('查询开始时间：', st_unix, date_util.getSecondsToDatetime(st_unix))
    print('查询结束时间：', et_unix, date_util.getSecondsToDatetime(et_unix))

    ########################################## 测试开关 ##########################################
    # order_list = platform_order_api_util.get_zhangzhongyun_order(st_unix, et_unix)
    # # print(order_list)
    # db_order_util.batch_save_order(order_list)
    #
    # exit_flag = True
    # if exit_flag:
    #     exit()  #这里是为了测试，不让代码继续执行
    ########################################## 测试开关 ##########################################

    db_order_util.batch_save_order(platform_order_api_util.get_zhangzhongyun_order(st_unix, et_unix))
    db_order_util.batch_save_order(platform_order_api_util.get_yuewen_order(st_unix, et_unix))
    db_order_util.batch_save_order(platform_order_api_util.get_huasheng_order(st_unix, et_unix))
    db_order_util.batch_save_order(platform_order_api_util.get_youshuge_order(st_unix, et_unix))
    db_order_util.batch_save_order(platform_order_api_util.get_zhangdu_order(st_unix, et_unix))

    print('订单同步执行时间（秒）：', date_util.getCurrentSecondTime() - start_exec_seconds)


if __name__ == '__main__':
    # start_order_job()

    scheduler = BlockingScheduler()
    #每小时25分钟就执行一次
    scheduler.add_job(start_order_job, 'cron', max_instances=10, minute='25')
    scheduler.start()
