# encoding: utf8
import sys
import os
import pandas as pd
import numpy as np

reload(sys)
sys.setdefaultencoding("utf-8")

from sqlalchemy import create_engine

pd.options.mode.chained_assignment = None
engine_new_transaction = create_engine(
    'mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/new_transaction?charset=utf8', echo=True)
# engine_new_transaction = create_engine('mysql+mysqldb://yulong:yulong@172.16.3.201:3306/temp?charset=utf8',echo=True)
engine_pay_cent = create_engine(
    'mysql+mysqldb://xyqb_query:Uiwb@o^fR&nHOa5t@172.16.1.103:3306/payment_center?charset=utf8', echo=True)
update_repay_record = """ update repay_record_online set  
 amount=%s,poundage=%s,bill_time=%s where pay_approach=%s AND order_id=%s """
select_repay_record_online = """ 
 select order_id from repay_record_online 
 WHERE bill_time>='%s' 
 AND bill_time<'%s' 
 AND enable=1 
 AND pay_approach= %s """

start_bill_time = '2017-08-01'
end_bill_time = '2017-09-01'


# 录入易宝白条数据
def yeepay_baitiao():
    # 易宝白条7月份数据
    df = pd.read_excel(u"E:/baitiao/repay_online/2017-09/易宝众信利民白条9月.xls", converters={u'商户订单号': str})
    transact_yepay(df)


def transact_yepay(df):
    df.rename(columns={u'商户订单号': 'order_id'}, inplace=True)
    df.rename(columns={u'手续费': 'poundage'}, inplace=True)
    df.rename(columns={u'入账时间': 'bill_time'}, inplace=True)
    df.rename(columns={u'收入': 'amount'}, inplace=True)
    df.rename(columns={u'业务类型': 'biz_type'}, inplace=True)
    df = df.loc[df["biz_type"] == u"收款"]
    df['pay_approach'] = 22
    df['pay_approach'] = df['pay_approach'].astype(int)
    df['order_id'] = df['order_id'].astype(long)
    df['order_id'] = df['order_id'].astype(str)
    df_update = pd.read_sql(select_repay_record_online % (start_bill_time, end_bill_time, 22),
                            con=engine_new_transaction)
    df_not_in = df.ix[~df['order_id'].isin(df_update['order_id'])]
    df_in = df.ix[df['order_id'].isin(df_update['order_id'])]
    if len(df_not_in) > 0:
        df_out = df_not_in[['amount', 'poundage', 'bill_time', 'pay_approach', 'order_id']]
        df_out['is_ref'] = 0
        df_out.to_sql('repay_record_online', con=engine_new_transaction, chunksize=10000, if_exists='append',
                      index=None)
    if len(df_in) > 0:
        list1 = df_in[['amount', 'poundage', 'bill_time', 'pay_approach', 'order_id']].values.tolist()
        engine_new_transaction.execute(update_repay_record, list1)


# 录入联动优势白条数据
def um_baitiao():
    # 2017-06月份数据
    # file_path = u'E:/baitiao/repay_online/2017-09/umpay/baitiao/20171001/'
    file_path = u'E:/baitiao/repay_online/2017-09/umpay_new/umpay/baitiao/20171001/'
    df = pd.DataFrame()
    for file in os.listdir(file_path):
        df_1 = pd.read_table(file_path + file, delimiter=',', header=None, skipfooter=1, skiprows=1,
                             names=['no_use1', 'unkown', 'phone', 'order_id', 'date01', 'bill_time', 'bill_money',
                                    'no_use2', 'no_use3', 'no_use4', 'no_use5',
                                    'no_use6', 'no_use7', 'no_use8', 'no_use9', 'no_use10'],
                             converters={'phone': str,
                                         'order_id': str,
                                         'bill_time': str
                                         }
                             , encoding='utf8')
        df_t = df_1[['phone', 'order_id', 'bill_time', 'bill_money']]
        df = df.append(df_t)
    transact_um(df)


def transact_um(df):
    df['order_id'] = df['order_id'].astype(str)
    df['bill_money'] = df['bill_money'].astype(int)
    df['amount'] = df['bill_money'] / 100
    df['pay_approach'] = 23
    df_update = pd.read_sql(select_repay_record_online % (start_bill_time, end_bill_time, 23),
                            con=engine_new_transaction)
    df_not_in = df.ix[~df['order_id'].isin(df_update['order_id'])]
    df_in = df.ix[df['order_id'].isin(df_update['order_id'])]
    if len(df_not_in) > 0:
        df_out = df_not_in[['amount', 'bill_time', 'pay_approach', 'order_id']]
        df_out['is_ref'] = 0
        df_out.to_sql('repay_record_online', con=engine_new_transaction, chunksize=10000, if_exists='append',
                      index=None)
    if len(df_in) > 0:
        list1 = df_in[['amount', 0.0, 'bill_time', 'pay_approach', 'order_id']].values.tolist()
        engine_new_transaction.execute(update_repay_record, list1)


# 录入白条直投
def zt_baitiao():
    file_path = u'E:/baitiao/repay_online/2017-09/wechat/cash/20171001/'
    df = pd.DataFrame()
    for file in os.listdir(file_path):
        df_1 = pd.read_table(file_path + file, skipfooter=2,
                             skiprows=1, header=None,
                             names=['bill_time',
                                    u'公众账号ID', u'商户号', u'子商户号', u'设备号', u'微信订单号',
                                    'order_id',
                                    u'用户标识', u'交易类型',
                                    'transaction_status',
                                    u'付款银行', u'货币种类',
                                    'amount',
                                    u'企业红包金额', u'微信退款单号', u'商户退款单号',
                                    'refund_amount',
                                    u'企业红包退款金额',
                                    u'退款类型',
                                    'refund_status',
                                    'product_name', u'商户数据包',
                                    'poundage',
                                    u'费率'],
                             delimiter=',', converters={
                'order_id': str,
                'transaction_status': str
            },
                             encoding='utf8')
        df_t = df_1[
            ['bill_time', 'order_id', 'transaction_status', 'amount', 'refund_amount',
             'refund_status', 'product_name', 'poundage']]
        df = df.append(df_t)
    transact_zt(df)


def transact_zt(df):
    df['order_id'] = df['order_id'].astype(str)
    df['pay_approach'] = 24
    df_app = df.ix[df['transaction_status'] == 'SUCCESS']
    df_refund = df.ix[df['transaction_status'] == 'REFUND']
    if len(df_refund) > 0:
        df_refund = df_refund.ix[df_refund['refund_status'] == 'SUCCESS']
        df_refund['amount'] = 0 - df_refund['refund_amount']
    df = pd.concat([df_app, df_refund])
    df_update = pd.read_sql(select_repay_record_online % (start_bill_time, end_bill_time, 24),
                            con=engine_new_transaction)
    df_not_in = df.ix[~df['order_id'].isin(df_update['order_id'])]
    df_in = df.ix[df['order_id'].isin(df_update['order_id'])]
    if len(df_not_in) > 0:
        df_out = df_not_in[['amount', 'poundage', 'bill_time', 'pay_approach', 'order_id']]
        df_out['is_ref'] = 0
        df_out.to_sql('repay_record_online', con=engine_new_transaction, chunksize=10000, if_exists='append',
                      index=None)
    if len(df_in) > 0:
        list1 = df_in[['amount', 'poundage', 'bill_time', 'pay_approach', 'order_id']].values.tolist()
        engine_new_transaction.execute(update_repay_record, list1)


# 录入微信白条数据
def wechat_baitiao():
    file_path = u'E:/baitiao/repay_online/2017-09/wechat/baitiao/20171001/'
    df = pd.DataFrame()
    for file in os.listdir(file_path):
        df_1 = pd.read_table(file_path + file, skipfooter=2,
                             skiprows=1, header=None,
                             names=['bill_time',
                                    u'公众账号ID', u'商户号', u'子商户号', u'设备号', u'微信订单号',
                                    'order_id',
                                    u'用户标识', u'交易类型',
                                    'transaction_status',
                                    u'付款银行', u'货币种类',
                                    'amount',
                                    u'企业红包金额', u'微信退款单号', u'商户退款单号', u'退款金额', u'企业红包退款金额',
                                    u'退款类型', u'退款状态', u'商品名称', u'商户数据包',
                                    'poundage',
                                    u'费率'],
                             delimiter=',', converters={
                'order_id': str,
                'transaction_status': str
            }, encoding='utf8')
        df_t = df_1[['bill_time', 'order_id', 'transaction_status', 'amount', 'poundage']]
        df = df.append(df_t)
    transact_wechat(df)


def transact_wechat(df):
    df = df.ix[df['transaction_status'] == 'SUCCESS']
    df['pay_approach'] = 7
    df_update = pd.read_sql(select_repay_record_online % (start_bill_time, end_bill_time, 24),
                            con=engine_new_transaction)
    df_not_in = df.ix[~df['order_id'].isin(df_update['order_id'])]
    df_in = df.ix[df['order_id'].isin(df_update['order_id'])]
    if len(df_not_in) > 0:
        df_out = df_not_in[['amount', 'poundage', 'bill_time', 'pay_approach', 'order_id']]
        df_out['is_ref'] = 0
        df_out.to_sql('repay_record_online', con=engine_new_transaction, chunksize=10000, if_exists='append',
                      index=None)
    if len(df_in) > 0:
        list1 = df_in[['amount', 'poundage', 'bill_time', 'pay_approach', 'order_id']].values.tolist()
        engine_new_transaction.execute(update_repay_record, list1)


# 录入易联白条退款数据
def insert_refund():
    df = pd.read_excel(u'E:/baitiao/易联易宝退款/易联退款.xlsx')
    df.rename(columns={u'收/付': 'inOrOut'}, inplace=True)
    df.rename(columns={u'日期': 'bill_time'}, inplace=True)
    df.rename(columns={u'支出': 'bill_money'}, inplace=True)
    df.rename(columns={u'摘要': 'mark'}, inplace=True)
    df['bill_money'] = df['bill_money'].astype(float)
    df['bill_money'] = df['bill_money'].apply(lambda t: (0 - t))
    df = df.loc[df["inOrOut"] == u"付"]
    df = df.loc[df["mark"] == u"多还款"]
    if len(df) > 0:
        values_list = df[['bill_money', 'bill_time']].values.tolist()
        insert_sql = """ insert into repay_record_online(status,amount,bill_time,enable,pay_approach) VALUES (5,%s,%s,1,1)"""
        engine_new_transaction.execute(insert_sql, values_list)


# 录入易宝退款数据
def insert_yepay_refund():
    # 现金贷部分
    # df_1 = pd.read_excel(u'E:/baitiao/易联易宝退款/易宝现金贷/20160706新马上第一次退款.xls',skip_footer=1)
    # df_1['pay_approach'] = 2
    # xjd_refund(df_1)
    # df_2 = pd.read_excel(u'E:/baitiao/易联易宝退款/易宝现金贷/20160719新马上第二次退款（事故）.xls', skip_footer=1)
    # df_2['pay_approach'] = 2
    # xjd_refund(df_2)
    # df_3 = pd.read_excel(u'E:/baitiao/易联易宝退款/易宝现金贷/20160720新马上第三次退款.xls', skip_footer=1)
    # df_3['pay_approach'] = 2
    # xjd_refund(df_3)
    # 白条部分
    df_bt_1 = pd.read_excel(u'E:/baitiao/易联易宝退款/易宝白条/20161122白条话费事故第一次退款.xls', skip_footer=1)
    df_bt_1['pay_approach'] = 3
    xjd_refund(df_bt_1)
    df_bt_2 = pd.read_excel(u'E:/baitiao/易联易宝退款/易宝白条/20161206白条话费事故第二次退款.xls', skip_footer=1)
    df_bt_2['pay_approach'] = 3
    xjd_refund(df_bt_2)


def xjd_refund(df):
    df.rename(columns={u'订单号': 'third_order_id'}, inplace=True)
    df.rename(columns={u'实付': 'bill_money'}, inplace=True)
    df.rename(columns={u'状态': 'refund_status'}, inplace=True)
    df.rename(columns={u'处理时间': 'bill_time'}, inplace=True)
    df = df.loc[df["refund_status"] == u"已汇出"]
    if len(df) > 0:
        values_list = df[['bill_money', 'bill_time', 'third_order_id', 'pay_approach']].values.tolist()
        insert_sql = """ insert into repay_record_online(status,amount,bill_time,third_order_id,enable,pay_approach) VALUES (5,%s,%s,%s,1,%s)"""
        engine_new_transaction.execute(insert_sql, values_list)


# 用于录入每月退款数据！！！
def baitiao_alipay_refund():
    df = pd.read_excel(u'E:/baitiao/repay_online/2017-09/alipay201709.xlsx', header=None,skiprows=1,
                       names=['transfer_time',
                              'business_flow', 'financial_flow', u'财务类型',
                              'income', 'outcome',
                              u'账户余额（元）',
                              'service_fee',
                              'alipay_account', 'user_name',
                              'remark'],
                       converters={u'对方账户': str, u'支付宝交易号': str, u'支付宝流水号': str})
    df['income'].fillna(0, inplace=True)
    df['outcome'].fillna(0, inplace=True)
    df['outcome'] = df['outcome'].astype(float)
    df['income'] = df['income'].astype(float)
    df['outcome'] = df['outcome'].apply(lambda x: np.round(x, 2))
    df['income'] = df['income'].apply(lambda x: np.round(x, 2))
    df['outcome'] = 0 - df['outcome']
    if len(df) > 0:
        df = df[['user_name', 'alipay_account', 'transfer_time', 'income', 'outcome', 'financial_flow',
                 'business_flow', 'remark']]
        df['flag'] = -1
        df['is_ref'] = 0
        df.to_sql(name='offline_alipay_record', con=engine_new_transaction, if_exists='append', index=None)


# yeepay_baitiao()
# um_baitiao()


# wechat_baitiao()
# zt_baitiao()


baitiao_alipay_refund()  # 执行时间为2017-10-02 15:50:00


# insert_refund()
# insert_yepay_refund()



def update_wechat_cash():
    file_path = u'E:/baitiao/repay_online/2017-08/wechat/cash/update/'
    df = pd.DataFrame()
    for file in os.listdir(file_path):
        df_1 = pd.read_table(file_path + file, delimiter=',', header=None, skipfooter=1, skiprows=1,
                             names=['no_use1', 'unkown', 'phone', 'order_id', 'date01', 'bill_time', 'amount',
                                    'no_use2', 'no_use3', 'no_use4', 'no_use5',
                                    'no_use6', 'no_use7', 'no_use8', 'no_use9', 'no_use10'],
                             converters={'phone': str,
                                         'order_id': str,
                                         'bill_time': str
                                         }
                             , encoding='utf8')
        df_t = df_1[['phone', 'order_id', 'bill_time', 'amount']]
        df = df.append(df_t)
    df['order_id'] = df['order_id'].astype(str)
    # df['bill_money'] = df['bill_money'].apply(lambda x: np.round(x, 2))
    # df['refund_amount'] = df['refund_amount'].apply(lambda x: np.round(x, 2))
    df['order_id'] = df['order_id'].astype(str)
    df['amount'] = df['amount'].astype(int)
    df['bill_money'] = df['amount'] / 100
    df['pay_approach'] = 5
    df_update = pd.read_sql(select_repay_record_online % (start_bill_time, end_bill_time, 5),
                            con=engine_new_transaction)
    df_not_in = df.ix[~df['order_id'].isin(df_update['order_id'])]
    df_in = df.ix[df['order_id'].isin(df_update['order_id'])]
    if len(df_not_in) > 0:
        df_out = df_not_in[['bill_money', 'poundage', 'bill_time', 'pay_approach', 'order_id']]
        df_out['is_ref'] = 0
        df_out.to_sql('repay_record_online', con=engine_new_transaction, chunksize=10000, if_exists='append',
                      index=None)
    if len(df_in) > 0:
        list1 = df_in[['bill_money', 'bill_time', 'pay_approach', 'order_id']].values.tolist()
        engine_new_transaction.execute(update_repay_record, list1)
