# -*- coding: utf8 -*-
import sys
import datetime
import itertools
import pandas as pd
from sqlalchemy import create_engine
import multiprocessing as mtp
reload(sys)
sys.setdefaultencoding('utf8')

engine_new_transaction = create_engine(
    'mysql+mysqldb://yulong_rw:TouBStYwN8wkdxVt@172.16.3.201:3306/new_transaction?charset=utf8',
    echo=True)

'''
1、应还资金方本息
'''
sql_fund='''
select id fund_code,name fund_name from new_transaction.funding_corp
'''

df_fund=pd.read_sql(sql_fund,con=engine_new_transaction)


#应还资金方本息
sql_fund_hk='''
SELECT DATE(deadline) day_hk,t1.funding_code fund_code,SUM(`principle`+interest) plan_all
FROM audit.`funding_repayment_record` t1 JOIN audit.loan_manifest t2  ON t1.loan_id=t2.loan_id  WHERE  t1.is_active = 1 AND
 t2.loan_paid_at >='2016-01-01' and t2.loan_paid_at<'2017-01-01' and t1.funding_code not in (160,210,50,80,130)
GROUP BY 1,2
'''

select_ACD ='''
SELECT DATE(deadline) day_hk,t1.funding_code fund_code,SUM(principle+interest) plan_all
FROM audit.`funding_repayment_record_acdz` t1 JOIN audit.loan_manifest t2  ON t1.loan_id=t2.loan_id WHERE
 t2.loan_paid_at >='2016-01-01' and t2.loan_paid_at<'2017-01-01'  AND t1.funding_code IN (50,80,130)
GROUP BY 1,2
'''

#客户还款计划
sql_plan_hk='''
SELECT DATE(deadline) day_hk,t1.fund_code,SUM(principle+interest) plan_all
FROM  `user_repayment_plan` t1 JOIN audit.loan_manifest t2  ON t1.ref_id=t2.ref_id WHERE t2.is_active in (1,-2) AND
 t2.loan_paid_at >='2016-01-01' and t2.loan_paid_at<'2017-01-01' AND
 t1.fund_code in (210)
GROUP BY 1,2
'''


#用户实际还款--加入渠道---repaid_at 使用账单时间
sql_hk_yq='''
SELECT DATE(t2.repaid_at) day_hk,t3.fund_code,
  sum(t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest) yq_real_principle_interest
FROM new_transaction.xjd_repay_plan_repay_record_ref t1
JOIN temp.xjd_repay_plan_repay_record_ref_repay_date t2 ON t1.id = t2.xjd_ref_id
JOIN new_transaction.user_repayment_plan t3 ON t1.plan_id = t3.id and t3.fund_code != 160
JOIN audit.loan_manifest t4  ON t4.ref_id=t3.ref_id
WHERE  t2.repaid_at < '2017-06-01' and date(t2.repaid_at) > DATE(t3.deadline) AND
 t4.loan_paid_at >='2016-01-01' and t4.loan_paid_at<'2017-01-01'
GROUP by 1,2
'''
sql_hk_zc='''
SELECT DATE(t2.repaid_at) day_hk,t3.fund_code,
  sum(t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest)  zc_real_principle_interest
FROM xjd_repay_plan_repay_record_ref t1
JOIN temp.xjd_repay_plan_repay_record_ref_repay_date t2 ON t1.id = t2.xjd_ref_id
JOIN user_repayment_plan t3 ON t1.plan_id = t3.id and t3.fund_code != 160
JOIN audit.loan_manifest t4  ON t4.ref_id=t3.ref_id
WHERE  t2.repaid_at < '2017-06-01' and date(t2.repaid_at) = DATE(t3.deadline) AND
 t4.loan_paid_at >='2016-01-01' and t4.loan_paid_at<'2017-01-01'
GROUP by 1,2
'''

sql_hk_tq='''
SELECT DATE(t3.deadline) day_hk,t3.fund_code,
  sum(t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest) tq_real_principle_interest
FROM xjd_repay_plan_repay_record_ref t1
JOIN temp.xjd_repay_plan_repay_record_ref_repay_date t2 ON t1.id = t2.xjd_ref_id
JOIN user_repayment_plan t3 ON t1.plan_id = t3.id and t3.fund_code != 160
JOIN audit.loan_manifest t4  ON t4.ref_id=t3.ref_id
WHERE t2.repaid_at < '2017-06-01' and date(t2.repaid_at) < DATE(t3.deadline) AND
 t4.loan_paid_at >='2016-01-01' and t4.loan_paid_at<'2017-01-01'
GROUP by 1,2
'''
sql_fee='''
SELECT DATE(t2.repaid_at) day_hk,t3.fund_code,
  SUM(t1.service_fee-t1.mitigate_service_fee) real_service_fee,sum(t1.punish-t1.mitigate_collection_relief) real_punish
FROM xjd_repay_plan_repay_record_ref t1
JOIN temp.xjd_repay_plan_repay_record_ref_repay_date t2 ON t1.id = t2.xjd_ref_id
JOIN user_repayment_plan t3 ON t1.plan_id = t3.id and t3.fund_code != 160
JOIN audit.loan_manifest t4  ON t4.ref_id=t3.ref_id
WHERE t2.repaid_at < '2017-06-01' and date(t2.repaid_at) = DATE(t3.deadline) AND
 t4.loan_paid_at >='2016-01-01' and t4.loan_paid_at<'2017-01-01'
GROUP by 1,2
'''








#
if __name__ == '__main__':
    df_acd = pd.read_sql(select_ACD, engine_new_transaction)
    df_fund = pd.read_sql(sql_fund_hk, engine_new_transaction)
    df_plan = pd.read_sql(sql_plan_hk, engine_new_transaction)
    df_fund = pd.concat([df_acd, df_fund, df_plan], ignore_index=True, axis=0)
    df_zc = pd.read_sql(sql_hk_zc, engine_new_transaction)
    df_tq = pd.read_sql(sql_hk_tq, engine_new_transaction)
    df_yq = pd.read_sql(sql_hk_yq, engine_new_transaction)
    df_fund.fillna(0,inplace=True)
    df_zc.fillna(0, inplace=True)
    df_tq.fillna(0, inplace=True)
    df_yq.fillna(0, inplace=True)
    df_fund.day_hk=pd.to_datetime(df_fund.day_hk).dt.date
    df_fund.fund_code=df_fund.fund_code.astype(int)
    df_zc.day_hk = pd.to_datetime(df_zc.day_hk).dt.date
    df_zc.fund_code = df_zc.fund_code.astype(int)
    df_tq.day_hk = pd.to_datetime(df_tq.day_hk).dt.date
    df_tq.fund_code = df_tq.fund_code.astype(int)
    df_yq.day_hk = pd.to_datetime(df_yq.day_hk).dt.date
    df_yq.fund_code = df_yq.fund_code.astype(int)

    df = pd.merge(df_fund,df_zc,on=['day_hk','fund_code'],how='outer')
    df = pd.merge(df, df_tq, on=['day_hk', 'fund_code'], how='outer')
    df = pd.merge(df, df_yq, on=['day_hk', 'fund_code'], how='outer')
    df.to_excel('./peifu_new_transaction_2.xlsx', index=None, columns=df.columns, encoding='utf8')
print '=====================main done======================'