# -*- encoding: utf8 -*-
import sys
import pandas as pd
from sqlalchemy import create_engine


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-06-30'
user_plan_2016='repayment_plan_2016_201706'
user_plan_2017='repayment_plan_201706'
# baoli_plan = 'baoli_repayment_plan_201709'
# # 去哪儿
sql_1='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 未到期
FROM %s t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (1) AND date(t1.deadline) > '%s' AND t2.fund_code=1
GROUP BY 1,2,3,4,5
''' % (user_plan_2016,listen_time)

sql_2='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 未到期
FROM %s t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (1) AND date(t1.deadline) > '%s'  AND t2.fund_code=1
GROUP BY 1,2,3,4,5
''' % (user_plan_2017,listen_time)

df_q_1=pd.read_sql(sql_1,engine_qunaer)
df_q_2=pd.read_sql(sql_2,engine_qunaer)

df_q_1=pd.concat([df_q_1,df_q_2],ignore_index=True)
sql_3='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天内
 FROM %s t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (1) AND date(t1.`deadline`) <= '%s' AND t2.fund_code=1
AND DATEDIFF('%s',date(t1.`deadline`)) <= 30
GROUP BY 1,2,3,4,5
''' % (user_plan_2016,listen_time,listen_time)
sql_4='''
SELECT YEAR(t2.`loan_time`) 放款年,MONTH(t2.`loan_time`) 放款月,t1.`current_stage_no` 期数,YEAR(t1.`deadline`) 应还年,MONTH(t1.`deadline`) 应还月,
SUM(t1.`principle`) 逾期30天内
FROM %s t1
JOIN `pay_detail` t2 ON t1.`product_no` = t2.`product_no`
WHERE t1.`status` IN (1) AND date(t1.`deadline`) <= '%s' AND t2.fund_code=1
AND DATEDIFF('%s',date(t1.`deadline`)) <= 30
GROUP BY 1,2,3,4,5
''' % (user_plan_2017,listen_time,listen_time)

df_3=pd.read_sql(sql_3,engine_qunaer)
df_4=pd.read_sql(sql_4,engine_qunaer)

df_q_2=pd.concat([df_3,df_4],ignore_index=True)

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 `%s` 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
''' % (user_plan_2016,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 `%s` 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
''' % (user_plan_2017,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_3=pd.concat([df_5,df_6],ignore_index=True)

df_q_2 = df_q_2.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'逾期30天内'].sum().reset_index()
df_q_3 = df_q_3.groupby([u'放款年',u'放款月',u'期数',u'应还年',u'应还月'])[u'逾期30天上'].sum().reset_index()

df_a=pd.merge(df_q_1,df_q_2,on=[u'放款年',u'放款月',u'期数',u'应还年',u'应还月'],how='outer')
df_a=pd.merge(df_a,df_q_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.to_excel(file_path+file_name % listen_time,encoding='UTF-8',index=False)