# -*- coding: utf-8 -*-
import sys
import pandas as pd
from sqlalchemy import create_engine
reload(sys)
sys.setdefaultencoding('utf8')
import numpy as np
import datetime


engine_new_transaction = create_engine(
    'mysql+mysqldb://yulong_rw:TouBStYwN8wkdxVt@172.16.3.201:3306/all_back_0430?charset=utf8',
    echo=True)

sql_plan='''
select DATE(deadline) day_hk, sum(principle+interest_new+funding_service_fee/4) '应还资金方金额'
FROM new_ms_repayment_plan
GROUP by 1
'''

sql_real='''
SELECT DATE(repay_date) day_hk,SUM(repay_principle+repay_intrest+fund_service_fee) '实还资金方金额' FROM new_ms_transaction
GROUP BY 1
'''

df_plan=pd.read_sql(sql_plan,engine_new_transaction)
df_real=pd.read_sql(sql_real,engine_new_transaction)
df_plan.day_hk=pd.to_datetime(df_plan.day_hk).dt.date
df_real.day_hk=pd.to_datetime(df_real.day_hk).dt.date

df=pd.merge(df_plan,df_real,on='day_hk',how='outer')
df.fillna(0,inplace=True)
df['赔付']=np.round(df['应还资金方金额']-df['实还资金方金额'],2)
df.day_hk=pd.to_datetime(df.day_hk).dt.date
df=df.ix[df.day_hk < datetime.date(2017,1,1)]
df.to_excel('E:/peifu/xms_peifu.xlsx', index=None, columns=df.columns, encoding='utf8')
