import pymysql
import datetime
import json
import redis
import pandas as pd
from sqlalchemy import create_engine


def get_mysql_data(host,port,user,passwd,db,sql):
    db = pymysql.connect(host=host, port=port, user=user, passwd=passwd,db=db)
    cursor = db.cursor()
    cursor.execute(sql)
    result = cursor.fetchall()
    db.close()
    return result


def get_user():
    sql = "select user_id,phone,name,city_id,channel,auth_type from api_userextra"
    df = pd.DataFrame(list(get_mysql_data(host,port,user,passwd,db,sql)))
    df = df.rename(columns={0: "user_id", 1: "phone",2:"name",3:"city_id",4:"channel",5:"auth_type"})
    print(df.shape)
    l = 0
    r = 2000
    length = len(df)
    while l < length:
        pd.io.sql.to_sql(df[l:r], "user", yconnect, schema='jerry_test', if_exists='append', index=False)
        l += 2000
        r += 2000
        print("insert done")


def get_device():
    sql = "select device_id,platform,version,model,screen,channel,idfv from statistic_device"
    df = pd.DataFrame(list(get_mysql_data(host, port, user, passwd, db, sql)))
    df = df.rename(columns={0: "device_id", 1: "platform", 2: "version", 3: "model", 4: "screen",
                            5: "channel",6:"idfv"})
    print(df.shape)
    l = 0
    r = 10000
    length = len(df)
    while l < length:
        pd.io.sql.to_sql(df[l:r], "device", yconnect, schema='jerry_test', if_exists='append', index=False)
        l += 10000
        r += 10000
        print("insert done")


def get_du():
    sql = "select device_id,user_id from statistic_device_user"
    df = pd.DataFrame(list(get_mysql_data(host, port, user, passwd, db, sql)))
    df = df.rename(columns={0: "device_id", 1: "user_id"})
    print(df.shape)
    l = 0
    r = 10000
    length = len(df)
    while l < length:
        pd.io.sql.to_sql(df[l:r], "device_user", yconnect, schema='jerry_test', if_exists='append', index=False)
        l += 10000
        r += 10000
        print("insert done")


def get_order():
    sql = "select user_id,service_id,created_time from api_order"
    df = pd.DataFrame(list(get_mysql_data(host, port, user, passwd, db, sql)))
    df = df.rename(columns={0: "user_id", 1: "service_id",2:"created_time"})
    print(df.shape)
    l = 0
    r = 10000
    length = len(df)
    while l < length:
        pd.io.sql.to_sql(df[l:r], "meigou_order", yconnect, schema='jerry_test', if_exists='append', index=False)
        l += 10000
        r += 10000
        print("insert done")

def meigou_to_csv():
    sql = "select device_id,service_id,created_time from meigou_order"
    df = pd.DataFrame(list(get_mysql_data(host, port, user, passwd, db, sql)))
    df = df.rename(columns={0: "user_id", 1: "service_id", 2: "created_time"})
    df.to_csv('/tmp/csv/meigou_order.csv', index=False)

def user_device_to_csv():
    sql = "select device_id,user_id from device_user"
    df = pd.DataFrame(list(get_mysql_data(host, port, user, passwd, db, sql)))
    df = df.rename(columns={0: "device_id", 1: "user_id"})
    df.to_csv('/tmp/csv/device_user.csv', index=False)

def devcie_to_csv():
    sql = "select device_id,platform,version,model,screen,channel,idfv from device"
    df = pd.DataFrame(list(get_mysql_data(host, port, user, passwd, db, sql)))
    df = df.rename(columns={0: "device_id", 1: "platform", 2: "version", 3: "model", 4: "screen",
                            5: "channel", 6: "idfv"})
    df.to_csv('/tmp/csv/device.csv', index=False)


def user_to_csv():
    sql = "select user_id,phone,name,city_id,channel,auth_type from user"
    df = pd.DataFrame(list(get_mysql_data(host, port, user, passwd, db, sql)))
    df = df.rename(columns={0: "user_id", 1: "phone", 2: "name", 3: "city_id", 4: "channel", 5: "auth_type"})
    df.to_csv('/tmp/csv/user.csv', index=False)


if __name__ == "__main__":
    # host = "172.16.30.141"
    # port = 3306
    # user = "work"
    # passwd = "BJQaT9VzDcuPBqkd"
    # db = "zhengxing"
    # yconnect = create_engine('mysql+pymysql://root:3SYz54LS9#^9sBvC@172.16.40.170:4000/jerry_test?charset=utf8')
    #
    host = "172.16.40.170"
    port = 4000
    user = "root"
    passwd = "3SYz54LS9#^9sBvC"
    db = "jerry_test"
    devcie_to_csv()
    print("user ok")
    user_to_csv()














