from model.DateUtils import DateUtils
import logging
from model.DingTalkUtils import DingTalkUtils
from app.etl.data_stat_task import *
from app.etl.sync_to_ck_task import *
from app.etl.dm.dm_pitcher_daily_overview import dm_pitcher_daily_overview
from app.etl.dw.dw_channel_daily import dw_channel_daily
from app.etl.dw.dw_pitcher_daily import dw_pitcher_trend
from app.etl.src.src_book_info import src_book_info
from app.etl.dw.dw_book_trend import book_trend
from app.etl.src import book_annual_expect_profit

du = DateUtils()
logger = logging.getLogger("")


# logger.setLevel(logging.ERROR)
# logging.getLogger().setLevel(logging.WARNING)


def do_order(st, et):
    logging.info('订单数据同步到ck,开始')
    for i in du.getDateLists(st, et):
        logging.info('订单:' + str(i))
        order_sync_ck(i)
    logging.info('订单数据同步到ck,结束')



def do_cost(st, et):
    logging.info('消耗数据处理,开始')
    for i in du.getDateLists(st, et):
        logging.info("消耗:" + str(i))
        channel_by_account_daily(i)
        channel_info_daily(i)
        dw_daily_channel_cost(i)
    logging.info('消耗数据处理,结束')


def main(st, et):
    try:
        do_order(st, et)
        do_cost(st, et)
        src_book_info()  # 书籍卡点信息
        # book_annual_expect_profit.run() # 年预期收益
        dw_channel_daily()
        dw_pitcher_trend()
        book_trend()
        dm_pitcher_daily_overview()
    except Exception as e:
        logging.error(e)
        DingTalkUtils().send("hourlyRun fail!! " + str(e), '15168342316')


def hourly():
    thedate = du.getTodayOrYestoday()
    main(thedate, thedate)


def daily():
    "往前跑10天"
    st = du.get_n_days(-10)
    et = du.get_n_days(-1)
    do_order(st, et)
    do_cost(st, et)


if __name__ == '__main__':
    hourly()
    # daily()
    # daily('2021-06-30','2021-07-04')

    # do_order('2021-05-30', '2021-06-29')