# -*- coding: utf-8 -*-
import datetime
import sys
import multiprocessing as mtp
from itertools import product

import pandas as pd
from sqlalchemy import create_engine
from dateutil.relativedelta import relativedelta

reload(sys)
sys.setdefaultencoding('utf8')


# path = 'E:/hk/bt/'
# 注意修改数据源
engine_new_transaction = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/all_back_0630?charset=utf8',
    echo=True)

read_feiquna_sql='''select t0.order_no,
sum(case when t0.loan_age = '0未到期' then t0.amount else 0 end) as '0',
sum(case when t0.loan_age = '1-30' then t0.amount else 0 end) as '1-30',
sum(case when t0.loan_age = '31-60' then t0.amount else 0 end) as '31-60',
sum(case when t0.loan_age = '61-90' then t0.amount else 0 end) as '61-90',
sum(case when t0.loan_age = '91-120' then t0.amount else 0 end) as '91-120',
sum(case when t0.loan_age = '121-150' then t0.amount else 0 end) as '121-150',
sum(case when t0.loan_age = '151-180' then t0.amount else 0 end) as '151-180',
sum(case when t0.loan_age = '181-360' then t0.amount else 0 end) as '181-360',
sum(case when t0.loan_age = 'over 360' then t0.amount else 0 end) as '361+'
 from (
SELECT u.order_no,IF((DATEDIFF('2016-1-1',u.deadline))>360,'over 360',IF((DATEDIFF('2016-1-1',u.deadline))>180,'181-360',
IF((DATEDIFF('2016-1-1',u.deadline))>150,'151-180',IF((DATEDIFF('2016-1-1',u.deadline))>120,'121-150',
IF((DATEDIFF('2016-1-1',u.deadline))>90,'91-120',IF((DATEDIFF('2016-1-1',u.deadline))>60,'61-90',
IF((DATEDIFF('2016-1-1',u.deadline))>30,'31-60',IF((DATEDIFF('2016-1-1',u.deadline))>0,'1-30','0未到期')))))))) loan_age,
sum(u.principle) amount
FROM `baitiao_order` t4
JOIN user_bt_repayment_plan u
ON t4.ref_id=u.ref_id
WHERE DATE(t4.loan_paid_at)<'2016-1-1' and t4.is_active in(1)
GROUP BY 1,u.deadline
UNION ALL
SELECT u.order_no,IF((DATEDIFF('2016-1-1',u.deadline))>360,'over 360',IF((DATEDIFF('2016-1-1',u.deadline))>180,'181-360',
IF((DATEDIFF('2016-1-1',u.deadline))>150,'151-180',IF((DATEDIFF('2016-1-1',u.deadline))>120,'121-150',
IF((DATEDIFF('2016-1-1',u.deadline))>90,'91-120',IF((DATEDIFF('2016-1-1',u.deadline))>60,'61-90',
IF((DATEDIFF('2016-1-1',u.deadline))>30,'31-60',IF((DATEDIFF('2016-1-1',u.deadline))>0,'1-30','0未到期')))))))) loan_age,
-sum(t1.principle - t1.mitigate_principle) amount
FROM baitiao_repay_plan_repay_record_ref t1
join user_bt_repayment_plan u on u.id = t1.plan_id 
join test_mzh.baitiao_repay_plan_repay_record_ref_repay_date_flow t5
on t5.bt_ref_id = t1.id and t5.repaid_at<'2016-1-1'
join baitiao_order t4 on t4.ref_id = u.ref_id and t4.is_active in (1) and DATE(t4.loan_paid_at) < '2016-1-1'
GROUP BY 1,u.deadline
) as t0
GROUP BY t0.order_no
'''
df_feiquna=pd.read_sql(read_feiquna_sql,con=engine_new_transaction)
print df_feiquna.shape

ff_feiquna=pd.read_excel(u"C:\\Users\\dinghiguo\\Desktop\\未还本金_2015---非去哪儿最新.xlsx")
print ff_feiquna.shape
df_all=pd.merge(ff_feiquna,df_feiquna,how='left',left_on='cp',right_on='order_no')
# df_all=df_all.drop_duplicates([u'订单号'])
df_all.to_excel(u"C:\\Users\\dinghiguo\\Desktop\\最终版2015非去哪在贷明细-9.13.xlsx", index=None)

