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


path='E:/peifu/2015_20160630/'


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

sql_pay='''
select t1.ref_id,t2.class,t1.loan_id,funding_code,contract_loan_amount loan_amount,date(loan_paid_at) loan_paid_at 
from all_back_0630.loan_manifest t1 
left JOIN temp.loan_class t2 on t1.ref_id = t2.ref_id
where funding_code = 160 AND is_active in (1,-2)
'''
df_pay=pd.read_sql(sql_pay,engine_new_transaction)
df_pay.loan_paid_at=pd.to_datetime(df_pay.loan_paid_at).dt.date
df_pay.loan_id=df_pay.loan_id.astype(int)
sql_plan='''
select real_loan_id,term_no,date(deadline) deadline,principle+interest fund_prin_inst from all_back_0630.user_repayment_plan 
where fund_code = 160 
'''
df_plan=pd.read_sql(sql_plan,engine_new_transaction)
df_plan.deadline=pd.to_datetime(df_plan.deadline).dt.date
df_plan['fund_code'] = 160
df_plan.real_loan_id=df_plan.real_loan_id.astype(int)
df_plan=pd.merge(df_plan,df_pay[['loan_id','class']],left_on='real_loan_id',right_on='loan_id',how='left')

sql_repay='''
select loan_id,term_no,date(repaid_at) repaid_at,repay_principle+repay_intrest+fund_service_fee real_prin_inst,
qg_service_fee real_service_fee,penalty_fee real_punish 
from test_8_2.new_ms_transaction_with_term_details
'''
df_repay=pd.read_sql(sql_repay,engine_new_transaction)
df_repay.repaid_at = pd.to_datetime(df_repay.repaid_at).dt.date
df_repay.loan_id=df_repay.loan_id.astype(int)
df_repay=pd.merge(df_repay,df_pay[['loan_id','class']],on='loan_id',how='left')
df_repay.loan_id=df_repay.loan_id.astype(int)
df_repay.term_no=df_repay.term_no.astype(int)
df_plan.real_loan_id=df_plan.real_loan_id.astype(int)
df_plan.term_no=df_plan.term_no.astype(int)
df_repay = pd.merge(df_repay,df_plan[['real_loan_id','term_no','deadline']],left_on=['loan_id','term_no'],right_on=['real_loan_id','term_no'],how='left')
df_repay['fund_code']  = 160
#放款金额
df_pay_gp=df_pay.groupby(['class','funding_code','loan_paid_at'])['loan_amount'].sum().\
    reset_index().rename(columns={'funding_code':'fund_code','loan_paid_at':'day_hk','loan_amount':'放款金额'})
#应还
df_plan_gp=df_plan.groupby(['class','fund_code','deadline'])['fund_prin_inst'].sum().reset_index().rename(columns={
    'deadline':'day_hk','fund_prin_inst':'资金方应还本息'
})

#实还可用本息
df_1=(df_repay.ix[df_repay.repaid_at <= df_repay.deadline]).groupby(['class','fund_code','deadline'])['real_prin_inst'].sum().reset_index().rename(columns={
    'deadline':'day_hk','real_prin_inst':'实还可用本息'
})
#逾期本息
df_2=(df_repay.ix[df_repay.repaid_at > df_repay.deadline]).groupby(['class','fund_code','repaid_at'])['real_prin_inst'].sum().reset_index().rename(columns={
    'repaid_at':'day_hk','real_prin_inst':'逾期本息'
})
#实还服务费，罚息
df_3=(df_repay.ix[df_repay.repaid_at > df_repay.deadline]).groupby(['class','fund_code','repaid_at'])['real_service_fee','real_punish'].sum().reset_index().rename(columns={
    'repaid_at':'day_hk','real_service_fee':'实还服务费','real_punish':'实还罚息'
})
df=pd.merge(df_pay_gp,df_plan_gp,on=['class','fund_code','day_hk'],how='outer')\
    .merge(df_1,on=['class','fund_code','day_hk'],how='outer')\
    .merge(df_2,on=['class','fund_code','day_hk'],how='outer')\
    .merge(df_3,on=['class','fund_code','day_hk'],how='outer')

df.to_csv(path+'new_ms_peifu.csv')