# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
import pandas as pd
import sys


reload(sys)
sys.setdefaultencoding('utf8')


'''
1、问财务要支付宝流水记录
2、根据提供的数据录入到new_transaction.offline_alipay_record 表中
'''
#--------------------配置变量开始------------------
#支付宝数据
# filename=u'E:/核对账单/支付宝还款核对/6月份/201706.xlsx'
engine_audit = create_engine('mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/new_transaction?charset=utf8', echo=True)
#--------------------配置变量结束------------------

df=pd.read_clipboard()
df.rename(columns={'入账时间':'transfer_time','支付宝交易号':'business_flow','支付宝流水号':'financial_flow','账务类型':'business_type',
                   '收入（+元）':'income','支出（-元）':'outcome','账户余额（元）':'balance','对方账户':'alipay_account','对方名称':'user_name',
                   '备注':'remark'},inplace=True)

df=df[['transfer_time','business_flow','financial_flow','business_type','income','outcome','balance','alipay_account','user_name','remark']]
df['transfer_account']=df['alipay_account']
df['transfer_channel']=u'支付宝'
df['is_ref']= 0
df['enable']=1
# 1-正常交易，2-当月充值、提现，-1-还款退款, 4-服务费
df['flag'] = 1
df.ix[df.business_type == u'服务费','flag']=4
df.ix[df.business_type == u'提现','flag'] = 2

#outcome = -outcome
df.outcome=df.outcome.apply(lambda x:str(x).replace(' ',''))
df.outcome=df.outcome.apply(lambda x:str(x).replace('-',''))
df.ix[~(df.outcome == ''),'outcome']=df.ix[~(df.outcome=='')].apply(lambda x:'-'+x.outcome,axis=1)
#数据录入
df.to_sql(name='offline_alipay_record',con=engine_audit,if_exists='append',index=None)



