# -*- encoding: utf8 -*-
import sys
import datetime
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from dateutil.relativedelta import relativedelta
reload(sys)
sys.setdefaultencoding("utf-8")
pd.options.mode.chained_assignment = None


'''
1、读取财务提供的文件，
2、统计2017.1-6月的实还本金，实还金额
'''
engine_new_transaction = create_engine('mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/qunaer_new?charset=utf8', echo=False).connect()

df_repay=pd.read_excel(u'E:/2016-12-31白条-去哪儿-在贷本金余额及2017.1-6回款.xlsx')
product_nos=df_repay.product_no.astype(str).tolist()

sql_repay_2017='''
SELECT product_no,SUM(repay_amount) '2017年1-6月实还金额',SUM(repay_principle) '2017年1-6月实还本金'
FROM `repay_detail`
WHERE repay_type != 'REFUND_REPAY' AND repay_status = 1 AND repay_time >= '2017-01-01' AND product_no IN %s
GROUP by 1
''' % str(tuple(product_nos))
df_2017=pd.read_sql(sql_repay_2017,engine_new_transaction)

df=pd.merge(df_repay,df_2017,on='product_no',how='left')
df.fillna(0,inplace=True)
df.to_excel(u'E:/2016-12-31白条-去哪儿-在贷本金余额及2017.1-6回款2.xlsx',index=None)

