# -*- encoding: utf8 -*-
import sys
import os
import datetime
import numpy as np
import pandas as pd
from dateutil.relativedelta import relativedelta
from sqlalchemy import create_engine

reload(sys)
sys.setdefaultencoding("utf-8")
pd.options.mode.chained_assignment = None



engine_qunaer = create_engine('mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/qunaer_new?charset=utf8', echo=False).connect()
'''
@author linfang
计算去哪儿的在贷明细
product_no 的明细
包括观察点时的在贷和观察点之后的贷后表现
其中需要剔除 loan_amount > 0 & 剩余 < 0 的情况---这些是因为 还款计划拆分的问题造成的。
16年0930 的版本以这个为准
'''


year=2017
month=10

listen_time=datetime.datetime(year,month,1,0,0,0)

sql_loan='''
select product_no,date(loan_time) loan_time,  IF(is_ptf=0,loan_amount,0) loan_amount
from qunaer_new.pay_detail
where loan_time < '%s' AND fund_code = 1 
''' % listen_time
sql_loan_2='''
select product_no,date(loan_time) loan_time,loan_amount 
from qunaer_new.baoli_pay_detail 
WHERE trans_time < '%s'
''' % listen_time

#在listen_time 内发生转分期的数据
# sql_not_loan='''
# select t1.product_no
# from qunaer_new.pay_detail t1
# JOIN qunaer_new.trans_ref t2 on t1.product_no = t2.product_no
# WHERE t1.trans_time < '%s'
# ''' % listen_time


sql_pre_refund='''
select product_no,-sum(refund_principle) pre_refund_principle from 
qunaer_new.refund_detail
where refund_time < '%s' AND fund_code=1 AND flow_id is NULL 
GROUP by 1
''' % listen_time

sql_pre_repay='''
select product_no,sum(repay_principle)  pre_repay_principle from 
qunaer_new.repay_detail
where repay_time < '%s' AND fund_code = 1
GROUP by 1
''' % listen_time

sql_pre_repay2='''
select product_no,-sum(refund_principle)  pre_repay_principle from 
qunaer_new.refund_detail
where refund_time < '%s' AND fund_code = 1 and flow_id is not NULL 
GROUP by 1
''' % listen_time

sql_after_refund='''
select t1.product_no ,-sum(refund_principle) after_refund_principle from 
qunaer_new.refund_detail t1
JOIN qunaer_new.pay_detail t2 on t1.product_no = t2.product_no
where t2.loan_time < '%s' AND t1.refund_time >= '%s' AND t1.fund_code = 1 AND t1.flow_id is NULL 
GROUP by 1
''' % (listen_time,listen_time)


sql_after_repay='''
select t1.product_no ,sum(repay_principle) after_repay_principle from 
qunaer_new.repay_detail t1
JOIN qunaer_new.pay_detail t2 on t1.product_no = t2.product_no
where t2.loan_time < '%s' AND t1.repay_time >= '%s' AND t1.fund_code = 1
GROUP by 1
''' % (listen_time,listen_time)

sql_after_repay2='''
select t1.product_no ,-sum(refund_principle) after_repay_principle from 
qunaer_new.refund_detail t1
JOIN qunaer_new.pay_detail t2 on t1.product_no = t2.product_no
where t2.loan_time < '%s' AND t1.refund_time >= '%s' AND t1.fund_code = 1 AND t1.flow_id is not NULL 
GROUP by 1
''' % (listen_time,listen_time)


sql_after_deadline='''
select t1.product_no,sum(principle) after_deadline
from qunaer_new.repayment_plan_2016 t1
JOIN qunaer_new.pay_detail t2 on t1.product_no = t2.product_no 
where t2.loan_time < '%s' AND t1.status = 1 AND t1.deadline >= '%s' AND t2.fund_code = 1
GROUP by 1
''' % (listen_time,listen_time)

sql_after_deadline_2017='''
select t1.product_no,sum(principle) after_deadline
from qunaer_new.repayment_plan_2017 t1
JOIN qunaer_new.pay_detail t2 on t1.product_no = t2.product_no 
where t2.loan_time < '%s' AND t1.status = 1 AND t1.deadline >= '%s' AND t1.fund_code = 1
GROUP by 1
''' % (listen_time,listen_time)

sql_after_deadline_baoli='''
select t1.product_no,sum(principle) after_deadline
from qunaer_new.baoli_repayment_plan t1
JOIN qunaer_new.pay_detail t2 on t1.product_no = t2.product_no 
where t2.trans_time < '%s' AND t1.status = 1 AND t1.deadline >= '%s' AND t1.fund_code = 1
GROUP by 1
''' % (listen_time,listen_time)


df_loan=pd.read_sql(sql_loan,engine_qunaer)
df_loan_1=pd.read_sql(sql_loan_2,engine_qunaer)
df_loan = pd.concat([df_loan,df_loan_1],ignore_index=True)
# df_not_loan=pd.read_sql(sql_not_loan,engine_qunaer)
df_pre_refund=pd.read_sql(sql_pre_refund,engine_qunaer)
df_pre_repay=pd.read_sql(sql_pre_repay,engine_qunaer)
df_pre_repay2=pd.read_sql(sql_pre_repay2,engine_qunaer)
df_pre_repay = pd.concat([df_pre_repay,df_pre_repay2],ignore_index=True)

df_after_refund=pd.read_sql(sql_after_refund,engine_qunaer)
df_after_repay=pd.read_sql(sql_after_repay,engine_qunaer)
df_after_repay2=pd.read_sql(sql_after_repay2,engine_qunaer)
df_after_repay=pd.concat([df_after_repay,df_after_repay2],ignore_index=True)

df_after_deadline=pd.read_sql(sql_after_deadline,engine_qunaer)
df_after_deadline_2017=pd.read_sql(sql_after_deadline_2017,engine_qunaer)
df_after_deadline_baoli=pd.read_sql(sql_after_deadline_baoli,engine_qunaer)
df_after_deadline=pd.concat([df_after_deadline,df_after_deadline_2017,df_after_deadline_baoli],ignore_index=True)
df_after_deadline=df_after_deadline.groupby(['product_no'])['after_deadline'].sum().reset_index()
#剔除发生转分期的订单
# df_loan=df_loan.ix[~df_loan.product_no.isin(df_not_loan.product_no)]
df=pd.merge(df_loan,df_pre_refund,on='product_no',how='left')
df=pd.merge(df,df_pre_repay,on='product_no',how='left')
df=pd.merge(df,df_after_refund,on='product_no',how='left')
df=pd.merge(df,df_after_repay,on='product_no',how='left')
df=pd.merge(df,df_after_deadline,on='product_no',how='left')

df.fillna(0,inplace=True)
df['在贷']=np.round(df['loan_amount']-df['pre_refund_principle']-df['pre_repay_principle'],2)
df['贷后还款']=np.round(df['after_refund_principle'] +df['after_repay_principle']+df['after_deadline'],2)
df['剩余']=np.round(df['loan_amount']-df['pre_refund_principle']-df['pre_repay_principle']-df['after_refund_principle'] -df['after_repay_principle']-df['after_deadline'],2)
# df=df.ix[df['剩余']>=0]
df.rename(columns={'product_no':'产品编号','loan_time':'放款时间'},inplace=True)

df.to_csv(u'E:/去哪儿在贷明细_%s_%s.csv' % (year,month),encoding='UTF-8',index=None)

