# -*- 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/peifu_v6/'


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,t1.loan_id,funding_code,contract_loan_amount loan_amount,date(loan_paid_at) loan_paid_at 
from all_back_0630.loan_manifest t1 
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)
df_class=pd.read_csv(path+'loan_class.csv')
df_class.ref_id=df_class.ref_id.astype(int)
df_pay=pd.merge(df_pay,df_class,on='ref_id',how='left')

sql_plan='''
select loan_id,term_no,date(deadline) deadline,principle+interest_new+funding_service_fee/4 fund_prin_inst from all_back_0630.new_ms_repayment_plan
'''
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.loan_id=df_plan.loan_id.astype(int)
df_plan=pd.merge(df_plan,df_pay[['loan_id','class']],on='loan_id',how='left')

sql_repay='''
select loan_id,term_no,date(repaid_at) repaid_at,funding_principle+funding_interest+funding_service_fee real_prin_inst,
qg_service_fee real_service_fee,qg_overdue_fee real_punish 
from all_back_0630.new_ms_transaction_with_term
'''
df_repay=pd.read_sql(sql_repay,engine_new_transaction)
#时间为空
df_repay['repaid_at']=df_repay['repaid_at'].fillna('2016-12-31')
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.loan_id=df_plan.loan_id.astype(int)
df_plan.term_no=df_plan.term_no.astype(int)
df_repay = pd.merge(df_repay,df_plan[['loan_id','term_no','deadline']],on=['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_time_peifu.csv')