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

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

file_path=u'E:/量化派/白条减值/'
file_name = u'白条逾期率_%s.xlsx'
engine_qunaer = create_engine('mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/qunaer_new?charset=utf8',
                              echo=False).connect()
# 时间取月末
listen_time='2017-09-30'
user_plan_2016='repayment_plan_2016_201709'
user_plan_2017='repayment_plan_201709'
baoli_plan = 'baoli_repayment_plan_201709'


# 逾期30天上的总额
sql_5='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天上未收回金额
 FROM `repayment_plan_2016` t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (1) AND date(t1.`deadline`) <= '%s'
AND DATEDIFF('%s',date(t1.`deadline`)) > 30
GROUP BY 1,2,3,4,5 
''' % (listen_time,listen_time)

sql_6='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天上未收回金额
 FROM `repayment_plan_2017` t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (1) AND date(t1.`deadline`) <= '%s'
AND DATEDIFF('%s',date(t1.`deadline`)) > 30 AND date(t2.trans_time) <= '%s' AND t2.fund_code=1
GROUP BY 1,2,3,4,5 
''' % (listen_time,listen_time,listen_time)

sql_7='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天上未收回金额
 FROM `baoli_repayment_plan` t1
JOIN `baoli_pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (1) AND date(t1.`deadline`) <= '%s'
AND DATEDIFF('%s',date(t1.`deadline`)) > 30 AND date(t2.trans_time) <= '%s'
GROUP BY 1,2,3,4,5
''' % (listen_time,listen_time,listen_time)

df_5=pd.read_sql(sql_5,engine_qunaer)
df_6=pd.read_sql(sql_6,engine_qunaer)
df_7=pd.read_sql(sql_7,engine_qunaer)
df_q_1=pd.concat([df_5,df_6,df_7],ignore_index=True)
df_q_1=df_q_1.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'逾期30天上未收回金额'].sum().reset_index()

#逾期30天上的逾期收回的金额
# 逾期30天上的总额
sql_5='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天上收回金额
 FROM `repayment_plan_2016` t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (2) AND date(t1.`deadline`) <= '%s'
AND DATEDIFF(date(t1.repay_time),date(t1.`deadline`)) > 30
GROUP BY 1,2,3,4,5 
''' % (listen_time)

sql_6='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天上收回金额
 FROM `repayment_plan_2017` t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (2) AND date(t1.`deadline`) <= '%s'
AND DATEDIFF(date(t1.repay_time),date(t1.`deadline`)) > 30 AND date(t2.trans_time) <= '%s' AND t2.fund_code=1
GROUP BY 1,2,3,4,5 
''' % (listen_time,listen_time)

sql_7='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天上收回金额
 FROM `baoli_repayment_plan` t1
JOIN `baoli_pay_detail` t2 ON t1.`product_no` = t2.`product_no`
JOIN repay_detail t3 on t1.product_no = t3.product_no AND t1.user_current_stage_no=t3.current_stage_no AND t3.fund_code = 1
WHERE t1.`status` IN (2) AND date(t1.`deadline`) <= '%s'
AND DATEDIFF(date(t3.repay_time),date(t1.`deadline`)) > 30 AND date(t2.trans_time) <= '%s'
GROUP BY 1,2,3,4,5
''' % (listen_time,listen_time)

df_5_1=pd.read_sql(sql_5,engine_qunaer)
df_6_1=pd.read_sql(sql_6,engine_qunaer)
df_7_1=pd.read_sql(sql_7,engine_qunaer)
df_q_2=pd.concat([df_5_1,df_6_1,df_7_1],ignore_index=True)
df_q_2=df_q_2.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'逾期30天上收回金额'].sum().reset_index()


#逾期30天上的总额
sql_11='''
SELECT YEAR(t2.loan_paid_at) 放款年,MONTH(t2.loan_paid_at) 放款月, t1.term_no 期数,YEAR(t1.deadline) 应还年,MONTH(t1.deadline) 应还月,
SUM(t1.principle) 逾期30天上未收回金额
FROM new_transaction.user_bt_repayment_plan t1
JOIN baitiao_audit.baitiao_order t2 ON t2.ref_id = t1.ref_id
WHERE 
t1.real_principle+t1.`real_interest`+t1.`real_service_fee`+t1.`real_punish` = 0 AND
 date(t1.deadline) <= '%s'
AND DATEDIFF('%s',date(t1.deadline)) > 30
AND date(t2.loan_paid_at) <= '%s' AND t2.is_active = 1   AND t2.funding_corp_id = 1
GROUP BY 1,2,3,4,5;
''' % (listen_time,listen_time,listen_time)

sql_12='''
SELECT YEAR(t2.loan_paid_at) 放款年,MONTH(t2.loan_paid_at) 放款月, t1.term_no 期数,YEAR(t1.deadline) 应还年,MONTH(t1.deadline) 应还月,
SUM(t1.principle) 逾期30天上未收回金额
FROM new_transaction.user_bt_repayment_plan t1
JOIN baitiao_audit.baitiao_order t2 ON t2.ref_id = t1.ref_id
WHERE 
t1.real_principle+t1.`real_interest`+t1.`real_service_fee`+t1.`real_punish` = 0 AND
 date(t1.deadline) <= '%s'
AND date(t2.loan_paid_at) <= '%s' AND t2.is_active  = -2
AND DATEDIFF('%s',date(t1.`deadline`)) > 30 AND t2.funding_corp_id = 1
GROUP BY 1,2,3,4,5;
''' % (listen_time,listen_time,listen_time)

df_11=pd.read_sql(sql_11,engine_qunaer)
df_12=pd.read_sql(sql_12,engine_qunaer)

df_bt_1=pd.concat([df_11,df_12],ignore_index=True)
df_bt_1=df_bt_1.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'逾期30天上未收回金额'].sum().reset_index()
print df_bt_1.ix[df_bt_1[u'逾期30天上未收回金额'] <0]
df_bt_1=df_bt_1.ix[df_bt_1[u'逾期30天上未收回金额'] >0]

#==
sql_11='''
SELECT YEAR(t2.loan_paid_at) 放款年,MONTH(t2.loan_paid_at) 放款月, t1.term_no 期数,YEAR(t1.deadline) 应还年,MONTH(t1.deadline) 应还月,
SUM(ref.principle) 逾期30天上收回金额
FROM new_transaction.baitiao_repay_plan_repay_record_ref ref 
JOIN new_transaction.baitiao_ref_repay_date ref_date on ref.id = ref_date.bt_ref_id
JOIN new_transaction.user_bt_repayment_plan t1 on ref.plan_id = t1.id
JOIN baitiao_audit.baitiao_order t2 ON t2.ref_id = t1.ref_id
WHERE t1.real_principle+t1.`real_interest`+t1.`real_service_fee`+t1.`real_punish` > 0
AND date(t1.deadline) <= '%s'
AND DATEDIFF(date(ref_date.repaid_at),date(t1.deadline)) > 30
AND date(t2.loan_paid_at) <= '%s' AND t2.is_active = 1   AND t2.funding_corp_id = 1
GROUP BY 1,2,3,4,5;
''' % (listen_time,listen_time)

sql_12='''
SELECT YEAR(t2.loan_paid_at) 放款年,MONTH(t2.loan_paid_at) 放款月, t1.term_no 期数,YEAR(t1.deadline) 应还年,MONTH(t1.deadline) 应还月,
SUM(ref.principle) 逾期30天上收回金额
FROM new_transaction.baitiao_repay_plan_repay_record_ref ref 
JOIN new_transaction.baitiao_ref_repay_date ref_date on ref.id = ref_date.bt_ref_id
JOIN new_transaction.user_bt_repayment_plan t1 on ref.plan_id = t1.id
JOIN baitiao_audit.baitiao_order t2 ON t2.ref_id = t1.ref_id
WHERE t1.real_principle+t1.`real_interest`+t1.`real_service_fee`+t1.`real_punish` > 0 AND
 date(t1.deadline) <= '%s'
AND date(t2.loan_paid_at) <= '%s' AND t2.is_active  = -2
AND DATEDIFF(date(ref_date.repaid_at),date(t1.`deadline`)) > 30 AND t2.funding_corp_id = 1
GROUP BY 1,2,3,4,5;
''' % (listen_time,listen_time)

df_11_1=pd.read_sql(sql_11,engine_qunaer)
df_12_1=pd.read_sql(sql_12,engine_qunaer)

df_bt_4=pd.concat([df_11_1,df_12_1],ignore_index=True)
df_bt_4=df_bt_4.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'逾期30天上收回金额'].sum().reset_index()
print df_bt_4.ix[df_bt_4[u'逾期30天上收回金额'] <0]
df_bt_4=df_bt_4.ix[df_bt_4[u'逾期30天上收回金额'] >0]

# df_a_1=pd.concat([df_q_1,df_bt_1],ignore_index=True)
# df_a_2=pd.concat([df_q_2,df_bt_4],ignore_index=True)
# df_a_3=pd.concat([df_q_3,df_bt_3],ignore_index=True)

df_a=pd.merge(df_bt_1,df_bt_4,on=[u'放款年',u'放款月',u'期数',u'应还年',u'应还月'],how='outer')
# df_a=pd.merge(df_a,df_a_3,on=[u'放款年',u'放款月',u'期数',u'应还年',u'应还月'],how='outer')
# df_a=df_a.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'未到期',u'逾期30天内',u'逾期30天上'].sum().reset_index()

df_a.fillna(0,inplace=True)
df_a=df_a.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'逾期30天上未收回金额',u'逾期30天上收回金额'].sum().reset_index()
df_a.to_excel(file_path+file_name % listen_time,encoding='UTF-8')