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

"""
__title__ = '每日凌晨空闲时检查本地数据库中的订单数据是否和平台昨天总订单一致'

@Time    : 2020/9/26 19:44
@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 platform_config_util
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 = date_util.getYesterdayStartTime()
    et_unix = date_util.getTodayStartTime()

    account_list_yuewen = platform_config_util.get_yuewen_account_list()
    account_list_zhangzhongyun = platform_config_util.get_zhangzhongyun_account_list()
    account_list_huasheng = platform_config_util.get_huasheng_account_list()
    account_list_youshuge = platform_config_util.get_youshuge_account_list()
    account_list_zhangdu = platform_config_util.get_zhangdu_account_list()
    # st_unix = 1601136000  # 2020/9/27 0:0:0
    # et_unix = 1601308800  # 2020/9/29 0: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_youshuge_order(st_unix, et_unix)
    # db_order_util.batch_save_order(order_list)
    #
    # exit_flag = True
    # if exit_flag:
    #     exit()  #这里是为了测试，不让代码继续执行
    ########################################## 测试开关 ##########################################

    platform_order_num_list = db_order_util.get_platform_order_count(date_util.getYesterdayStartTime())
    if len(platform_order_num_list) == 0:
        print('本地库中没有任何数据，现在全平台补全')
        db_order_util.batch_save_order(platform_order_api_util.get_zhangzhongyun_order(st_unix, et_unix,account_list_zhangzhongyun))
        db_order_util.batch_save_order(platform_order_api_util.get_yuewen_order(st_unix, et_unix,account_list_yuewen))
        db_order_util.batch_save_order(platform_order_api_util.get_huasheng_order(st_unix, et_unix,account_list_huasheng))
        db_order_util.batch_save_order(platform_order_api_util.get_youshuge_order(st_unix, et_unix,account_list_youshuge))
        db_order_util.batch_save_order(platform_order_api_util.get_zhangdu_order(st_unix, et_unix,account_list_zhangdu))
    else:
        platform_list = ['阅文', '悠书阁', '掌读', '掌中云', '花生']
        for platform_order_num in platform_order_num_list:
            platform = str(platform_order_num['platform'])
            num = int(platform_order_num['num'])

            if platform not in platform_list:
                print('目前不支持【{platform}】的订单数据同步。'.format(platform=platform))
                continue

            platform_list.remove(platform)

            if platform == '阅文':
                order_list = platform_order_api_util.get_yuewen_order(st_unix, et_unix,account_list_yuewen)
                if len(order_list) != num:
                    print('阅文数据实际订单和已经入库数据差异：', len(order_list) - num)
                    db_order_util.batch_save_order(order_list)
            elif platform == '悠书阁':
                order_list = platform_order_api_util.get_youshuge_order(st_unix, et_unix,account_list_youshuge)
                if len(order_list) != num:
                    print('悠书阁数据实际订单和已经入库数据差异：', len(order_list) - num)
                    db_order_util.batch_save_order(order_list)
            elif platform == '掌读':
                order_list = platform_order_api_util.get_zhangdu_order(st_unix, et_unix,account_list_zhangdu)
                if len(order_list) != num:
                    print('掌读数据实际订单和已经入库数据差异：', len(order_list) - num)
                    db_order_util.batch_save_order(order_list)
            elif platform == '掌中云':
                order_list = platform_order_api_util.get_zhangzhongyun_order(st_unix, et_unix,account_list_zhangzhongyun)
                if len(order_list) != num:
                    print('掌中云数据实际订单和已经入库数据差异：', len(order_list) - num)
                    db_order_util.batch_save_order(order_list)
            elif platform == '花生':
                order_list = platform_order_api_util.get_huasheng_order(st_unix, et_unix,account_list_huasheng)
                if len(order_list) != num:
                    print('花生数据实际订单和已经入库数据差异：', len(order_list) - num)
                    db_order_util.batch_save_order(order_list)
            else:
                print('平台【{platform}】数据没有同步！'.format(platform=platform))

        for platform in platform_list:
            if platform == '阅文':
                print('阅文没有数据')
                db_order_util.batch_save_order(platform_order_api_util.get_yuewen_order(st_unix, et_unix,account_list_yuewen))
            elif platform == '悠书阁':
                print('悠书阁没有数据')
                db_order_util.batch_save_order(platform_order_api_util.get_youshuge_order(st_unix, et_unix,account_list_youshuge))
            elif platform == '掌读':
                print('掌读没有数据')
                db_order_util.batch_save_order(platform_order_api_util.get_zhangdu_order(st_unix, et_unix,account_list_zhangdu))
            elif platform == '掌中云':
                print('掌中云没有数据')
                db_order_util.batch_save_order(platform_order_api_util.get_zhangzhongyun_order(st_unix, et_unix,account_list_zhangzhongyun))
            elif platform == '花生':
                print('花生没有数据')
                db_order_util.batch_save_order(platform_order_api_util.get_huasheng_order(st_unix, et_unix,account_list_huasheng))
            else:
                print('平台【{platform}】数据没有同步！'.format(platform=platform))

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


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

    scheduler = BlockingScheduler()
    #每天凌晨3点到4点的30分钟都执行一次
    scheduler.add_job(start_order_job, 'cron', max_instances=10, hour='3-4', minute='35')
    scheduler.start()
