#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug  5 10:32:28 2020

@author: chencong
"""

import datetime
import time
import math
import pandas as pd 
import numpy as np
import hashlib
import requests
from apscheduler.schedulers.blocking import BlockingScheduler
import pymysql
import get_data_hourly


def order_check():
    db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com','superc','Cc719199895','quchen_text')
    cursor = db.cursor()
    sql1 = 'select platform,count(1) from quchen_text.order where date=%s and platform!=%s  group by platform'
    sql2 = 'select platform,count(1) from quchen_text.order_daily where date=%s and platform!=%s group by platform'
    t = int((time.time()+8*3600)//86400*86400-8*3600-86400)
    data = (t,'阳光')
    case1=0
    case2=0
    try:
        cursor.execute(sql1,data)
        db.commit()
        x=cursor.fetchall()
        print('access',x)
    except:
        db.rollback()
        print('defeat order')
    try:
        cursor.execute(sql2,data)
        db.commit()
        y=cursor.fetchall()
        print('access',y)
    except:
        db.rollback()
        print('defeat order_daily')
    if len(x)==len(y) :
        for i in range(len(x)):
            if x[i][1]<y[i][1]:
                case1=1
            elif x[i][1]>y[i][1]:
                case2=1
    elif len(x)>len(y) :
        case2=1
    else:
        case1=1

    return(case1,case2)

def mysql_insert_order_daily(data):
    db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com','superc','Cc719199895','quchen_text')
    cursor = db.cursor() 
    time1 = time.time()
    sql = 'insert ignore into quchen_text.order_daily (amount,channel,channel_id,date,from_novel,order_id,order_time,platform,reg_time,stage,user_id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
    try:
        cursor.executemany(sql,data)
        db.commit()
        cost_time =round((time.time()-time1)/60,1)
        print('insert_order access',len(data),'cost_minutes:',cost_time)
    except:
        db.rollback()
        print('insert_order defeat')


def repair_order():
    repair_unix_time = time.time()
    st_unix = int((repair_unix_time+8*3600)//86400*86400-8*3600-86400)
    et_unix = int((repair_unix_time+8*3600)//86400*86400-8*3600)
    #et_unix = et_unix - 1
    st_dt = time.strftime("%Y-%m-%dT%H:%M:%S",time.localtime(st_unix))+'+08:00'
    et_dt = time.strftime("%Y-%m-%dT%H:%M:%S",time.localtime(et_unix))+'+08:00'
    (a,b)=order_check()
    print(a,b)
    if a==1 :
        get_data_hourly.mysql_insert_order(get_data_hourly.get_zhangdu_order(st_unix,et_unix))
        get_data_hourly.mysql_insert_order(get_data_hourly.get_ysg_order(st_unix,et_unix))
        get_data_hourly.mysql_insert_order(get_data_hourly.get_zzy_order(st_dt,et_dt))
        get_data_hourly.mysql_insert_order(get_data_hourly.get_yuewen_order(st_unix,et_unix))
        ##get_data_hourly.mysql_insert_order(get_data_hourly.get_yuewen_order(st_unix,et_unix-1))
        get_data_hourly.mysql_insert_order(get_data_hourly.get_huasheng_order(st_unix,et_unix))
    if b==1 :
        mysql_insert_order_daily(get_data_hourly.get_zhangdu_order(st_unix,et_unix))
        mysql_insert_order_daily(get_data_hourly.get_ysg_order(st_unix,et_unix))
        mysql_insert_order_daily(get_data_hourly.get_zzy_order(st_dt,et_dt))
        mysql_insert_order_daily(get_data_hourly.get_yuewen_order(st_unix,et_unix))
        #mysql_insert_order_daily(get_data_hourly.get_yuewen_order(st_unix,et_unix-1))
        mysql_insert_order_daily(get_data_hourly.get_huasheng_order(st_unix,et_unix))


start_job_time = '2020-09-30 03:40:00'


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(repair_order, 'interval',days=1,start_date=start_job_time)
    scheduler.start()

