# -*- coding: utf-8 -*-
import datetime
import sys

import pandas as pd
from sqlalchemy import create_engine


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

# 注意修改数据源
engine_new_transaction2016 = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/qunaer?charset=utf8',
    echo=True)
engine_new_transaction2017 = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/qunaer_new?charset=utf8',
    echo=True)
sql='''SELECT product_no,trans_time,SUM(principle) amount  FROM
(SELECT pay.product_no,pay.trans_time,SUM(plan.principle) principle 
FROM qunaer_new.repayment_plan_2016_201703 plan 
JOIN qunaer_new.pay_detail pay ON plan.product_no=pay.product_no 
WHERE plan.`status`=1 AND pay.trans_time<'2017-04-01'
GROUP BY 1
UNION  all
SELECT pay.product_no,pay.trans_time,SUM(plan.principle) principle FROM qunaer_new.repayment_plan_201703 plan 
JOIN qunaer_new.pay_detail pay ON plan.product_no=pay.product_no WHERE plan.`status`=1 AND pay.trans_time<'2017-04-01' 
GROUP BY 1) t1
GROUP BY 1
'''


df=pd.read_sql(sql,con=engine_new_transaction2017)

sql2='''
SELECT r.product_no,SUM(r.repay_principle) repay
FROM `repay_detail` r
JOIN `pay_detail` pay ON r.product_no = pay.`product_no` and pay.trans_time <'2017-04-01'
WHERE
r.`repay_time` <'2017-08-01'  and r.repay_time>= '2017-04-01'
group by 1
'''

df2=pd.read_sql(sql2,con=engine_new_transaction2017)
df3=pd.merge(df,df2,how='left',on='product_no')
# df3.to_csv(u'D:\\data\\20170630去哪白条在贷贷后表现.csv', index=None)
for i in range(0, len(df3), 500000):
    df3[i:i + 500000].to_csv(u'D:\\data\\20170331去哪白条在贷贷后表现_%d.csv' % i,index=None)
print df3['amount'].sum()
print df3['repay'].sum()
