#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug  7 10:28:30 2020

@author: chencong
"""

import requests
import hashlib
import pandas as pd
import time
from urllib import parse
import datetime
import csv
import math
import pymysql
from apscheduler.schedulers.blocking import BlockingScheduler


def md5value(s):
    md5 = hashlib.md5() 
    md5.update(s.encode("utf-8"))
    return md5.hexdigest()

def get_huasheng_order(st,et):
    apikey_list = [
                 
                   ['4747764','f350d7edd89bb7f6170b970f071ecb36','趣程20期']]
    url = 'https://vip.rlcps.cn/api/getMerchants'


    for key in apikey_list: #获取每个vip账号下的channel_id
        apiKEY=key[0]
        apiSecurity=key[1]
        stage = key[2]
        timestamp = str(int(time.time()))
        sign = md5value(apiKEY+timestamp+apiSecurity).upper()
        data = {'apiKey':apiKEY,
                'apiSecurity':apiSecurity,
                'timestamp':timestamp,
                'sign':sign
                }
        list0 = requests.post(url,data).json()
        t  = ()
        
        for merchant in list0['data']:
            merchant_id = merchant['merchant_id']
            merchant_name = merchant['merchant_name']
            url1 = 'https://vip.rlcps.cn/api/orderList'
            start_time = st
            if merchant_name in ('墨霜书楼','秋水文阁','问天书楼'):
                for i in range((et-st)//86400):
                
                    page = 1
                    date = time.strftime("%Y-%m-%d",time.localtime(start_time))
                    sign = md5value(apiKEY+date+str(merchant_id)+timestamp+apiSecurity).upper()
                    data1 = {'apiKey':apiKEY,
                'apiSecurity':apiSecurity,
                'timestamp':timestamp,
                'date':date,
                'merchant_id':merchant_id,
                'sign':sign,
                'page':page}
                    list1 = requests.post(url1,data1).json()
                
                    if 'data' in list1.keys() and len(list1['data'])>0 :    
                            
                        for i in range(int(math.ceil(list1['count']/500))):
                            
                            data2= {'apiKey':apiKEY,
                                'apiSecurity':apiSecurity,
                                'timestamp':timestamp,
                                'date':date,
                                'merchant_id':merchant_id,
                                'sign':sign,
                                'page':page}
                            list2 = requests.post(url1,data2).json()

                            for x in list2['data']:
                                if x['order_status']==1:
                                    
                                    x['date'] = start_time
                                    x['channel'] = merchant_name
                                    x['channel_id'] = merchant_id                    
                                    x['platform'] = '花生'
                                    x['stage'] = stage                                                                       
                                    x= sorted(x.items(), key=lambda item:item[0])
                                    
                                    x = dict(x)
                                    
                                    x = tuple(x.values())
                    
                                    t = t+((x),)
                            page=page+1

                    else:
                        print(list1)
                    start_time = start_time+86400

        if len(t)>0:
            mysql_insert_huasheng_order(t)
    print(stage,merchant_name,len(t))
    

def mysql_insert_huasheng_order(data):
    db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com','superc','Cc719199895','quchen_text')
    cursor = db.cursor()   
    sql = 'insert ignore into quchen_text.huasheng_text (amount,book_id,book_name,channel,channel_id,charge_count,date,join_at,openid,order_num,order_status,pay_at,platform,request_at,spread_name,stage,subscribe_at,trans_id,user_id,user_name) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
    try:
        cursor.executemany(sql,data)
        db.commit()
        print('access insert huasheng_text',len(data))
    except:
        db.rollback()
        print('defeat')
def start_huasheng_job():
    st_unix = int((time.time()+8*3600)//86400*86400-8*3600-86400)
    et_unix = int((time.time()+8*3600)//86400*86400-8*3600)
    # et_unix = et_unix-1
    get_huasheng_order(st_unix,et_unix)
    
start_order_time = '2020-09-30 03:30:00'

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