# coding: utf-8
# Author: Perry_Zhu
"""
说明：改版本在贷明细是按照ref_id为维度进行统计的数据，与财务的在贷最为接近，但是由于在贷会出现负数，导致无法使用！！！

"""
import sys
import pandas as pd
import datetime
from sqlalchemy import create_engine

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

engine_new_transaction = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/all_back_0930?charset=utf8',
    echo=True)

end_point = '2017-10-01'
watch_point = datetime.date(2017, 9, 30)
out_file_path = u'E:/数据汇总/白条/在贷/'

query_loan = """
SELECT tt.ref_id,tt.term_no,tt.deadline,SUM(tt.principle)principle,tt.loan_y,tt.loan_m,tt.plan_year,tt.plan_month 
FROM(
    SELECT
        plan.ref_id,
        plan.term_no,
        date(plan.deadline)deadline,
        plan.principle,
        year(bo.loan_paid_at) loan_y,
        month(bo.loan_paid_at) loan_m,
        year(plan.deadline) plan_year,
        month(plan.deadline) plan_month
    FROM
        baitiao_order bo
    JOIN user_bt_repayment_plan plan ON bo.ref_id=plan.ref_id
    WHERE
        bo.loan_paid_at >= '2015-01-01'
    AND bo.loan_paid_at < '%s'
    AND bo.is_active =1
    AND bo.funding_corp_id = 1
    GROUP BY 1,2
    
    UNION ALL
        SELECT
            plan.ref_id,
            plan.term_no,
            date(plan.deadline)deadline,
            plan.principle,
            year(bo.train_cancel_at) loan_y,
            month(bo.train_cancel_at) loan_m,
            year(plan.deadline) plan_year,
            month(plan.deadline) plan_month
        FROM
            baitiao_order bo
        JOIN user_bt_repayment_plan plan ON bo.ref_id=plan.ref_id
        WHERE
            bo.train_cancel_at >= '2015-01-01'
        AND bo.train_cancel_at < '%s'
        AND bo.is_active =- 2
        AND bo.funding_corp_id = 1
        GROUP BY 1,2
    
    
    UNION ALL
    
        SELECT
            bo.ref_id,
            1 term_no ,
            date(bo.loan_paid_at) deadline,
            bcr.real_order_amount principle,
            year(bo.loan_paid_at) loan_y,
            month(bo.loan_paid_at) loan_m,
            year(bo.loan_paid_at) plan_year,
            month(bo.loan_paid_at) plan_month
        FROM
            baitiao_cash_repay bcr
        JOIN baitiao_order bo ON bo.ref_id = bcr.ref_id AND bo.is_active=1 AND bo.funding_corp_id= 1 
            AND bo.loan_paid_at >= '2017-07-01' AND bo.loan_paid_at < '%s'
         GROUP BY 1,2
    UNION ALL
    SELECT
      bo.ref_id,
        1 term_no ,
        date(bo.train_cancel_at) deadline,
        bcr.real_order_amount principle,
        year(bo.train_cancel_at) loan_y,
        month(bo.train_cancel_at) loan_m,
        year(bo.train_cancel_at) plan_year,
        month(bo.train_cancel_at) plan_month

    FROM
        baitiao_cash_repay bcr
    JOIN baitiao_order bo ON bo.ref_id = bcr.ref_id AND bo.is_active=-2 AND bo.funding_corp_id =1 
        AND bo.train_cancel_at >= '2017-07-01' AND bo.train_cancel_at < '%s'
     GROUP BY 1,2
     
    )tt
    GROUP BY 1,2

"""

repay_online = """
    SELECT
        t2.ref_id,
        t2.term_no,
        SUM(ifnull(t1.principle, 0) - IFNULL(t1.mitigate_principle, 0)) paid_principle
    FROM
        test_shiyao.baitiao_repay_plan_repay_record_ref t1
    JOIN repay_record_online t3 ON t1.repay_channel in(1,2,3,4,5,6,7,8,9,10,11,12,22,23,24) AND t3.id = t1.record_id 
      AND t3.bill_time >= '2015-01-01' AND t3.bill_time < '%s'
    JOIN user_bt_repayment_plan t2 ON t2.id = t1.plan_id
    JOIN baitiao_audit.baitiao_order t5 ON t2.ref_id = t5.ref_id AND t5.funding_corp_id = 1
    GROUP BY 1,2
"""
repay_ali = """
    SELECT
        t2.ref_id,
        t2.term_no,
        SUM(ifnull(t1.principle,0)-ifnull(t1.mitigate_principle,0)) paid_principle
    FROM
        test_shiyao.baitiao_repay_plan_repay_record_ref t1
    JOIN user_bt_repayment_plan t2 ON t2.id = t1.plan_id
    JOIN offline_alipay_record t3 ON t1.repay_channel = 13 AND t3.id = t1.record_id  AND  t3.is_ref=1 
      AND t3.transfer_time>='2015-01-01' AND t3.transfer_time<'%s'
    JOIN baitiao_audit.baitiao_order t5 ON t2.ref_id = t5.ref_id  AND t5.funding_corp_id = 1
    GROUP BY 1,2
"""
repay_bank = """
    SELECT
        t2.ref_id,
        t2.term_no,
        SUM(ifnull(t1.principle,0)-ifnull(t1.mitigate_principle,0)) paid_principle
    FROM
            test_shiyao.baitiao_repay_plan_repay_record_ref t1
    JOIN user_bt_repayment_plan t2 ON t2.id = t1.plan_id
    JOIN offline_bank_repay_record t3 ON t1.repay_channel IN (14, 15, 16) AND t3.id = t1.record_id 
      AND t3.transfer_time>='2015-01-01' AND t3.transfer_time<'%s'
    JOIN baitiao_audit.baitiao_order t5 ON t2.ref_id = t5.ref_id 
    GROUP BY 1,2
"""
repay_cash = """
select tt.ref_id,tt.term_no,tt.paid_principle 
from(
    SELECT
        bo.ref_id,
        1 term_no,
        sum(bcrr.ref_amount) paid_principle
    FROM
        new_transaction.baitiao_cash_repay bcr
    JOIN (
        SELECT
            repay_id,
            sum(ref_amount) ref_amount
        FROM
            new_transaction.baitiao_cash_record_ref
        WHERE
            repay_channel IN (18, 24)
        GROUP BY 1
    ) bcrr ON bcrr.repay_id = bcr.id
    JOIN baitiao_order bo ON bo.ref_id = bcr.ref_id
    AND bo.baitiao_type = 1
    AND bo.loan_paid_at >= '2017-07-01'
    AND bo.loan_paid_at < '%s'
    GROUP BY 1,2
    UNION ALL
        SELECT
            bo.ref_id,
            1 term_no,
            sum(bcrr.ref_amount) paid_principle
        FROM
            baitiao_cash_repay bcr
        JOIN (
            SELECT
                repay_id,
                sum(ref_amount) ref_amount
            FROM
                baitiao_cash_record_ref
            WHERE
                repay_channel = 13
            GROUP BY 1
        ) bcrr ON bcrr.repay_id = bcr.id
        JOIN baitiao_order bo ON bo.ref_id = bcr.ref_id
        AND bo.is_active = -2
        AND bo.train_cancel_at >= '2017-07-01'
        AND bo.train_cancel_at < '%s'
      GROUP BY 1,2
)tt
GROUP BY 1,2
"""
df_loan = pd.read_sql(query_loan % (end_point, end_point,end_point, end_point), engine_new_transaction)
df_online = pd.read_sql(repay_online % end_point, engine_new_transaction)
df_ali = pd.read_sql(repay_ali % end_point, engine_new_transaction)
df_bank = pd.read_sql(repay_bank % end_point, engine_new_transaction)
df_cash = pd.read_sql(repay_cash % (end_point,end_point), engine_new_transaction)
df_repay = pd.concat([df_online, df_ali, df_bank,df_cash], axis=0, ignore_index=True)
df_repay.dropna(axis=0,inplace=True)
df_loan.ref_id = df_loan.ref_id.astype(int)
df_loan.term_no = df_loan.term_no.astype(int)
df_repay.ref_id = df_repay.ref_id.astype(int)
df_repay.term_no = df_repay.term_no.astype(int)
df_repay = df_repay.groupby(['ref_id', 'term_no'])['paid_principle'].agg({'sum'}).reset_index().rename(columns={'sum': 'paid_principle'})
df_out = pd.merge(df_loan, df_repay, on=['ref_id', 'term_no'], how='left')
df_out['deadline'] = pd.to_datetime(df_out.deadline)
df_out.fillna(0, inplace=True)
df_out['zaidai'] = df_out['principle'] - df_out['paid_principle']
df_1 = df_out.loc[df_out['deadline'] > watch_point]
df_2 = df_out.loc[((df_out['deadline'] - watch_point) < datetime.timedelta(30)) & (df_out['deadline'] <= watch_point)]
df_3 = df_out.loc[((df_out['deadline'] - watch_point) >= datetime.timedelta(30)) & (df_out['deadline'] < watch_point)]
df_1t = df_1.groupby(['loan_y', 'loan_m', 'term_no', 'plan_year', 'plan_month'])['zaidai'].agg(
    {'sum'}).reset_index().rename(columns={'sum': u'未到期在贷'})
df_2t = df_2.groupby(['loan_y', 'loan_m', 'term_no', 'plan_year', 'plan_month'])['zaidai'].agg(
    {'sum'}).reset_index().rename(columns={'sum': u'逾期30天内在贷'})
df_3t = df_3.groupby(['loan_y', 'loan_m', 'term_no', 'plan_year', 'plan_month'])['zaidai'].agg(
    {'sum'}).reset_index().rename(columns={'sum': u'逾期30天以上在贷'})
df = pd.merge(df_1t, df_2t, on=['loan_y', 'loan_m', 'term_no', 'plan_year', 'plan_month'], how='outer')
df = pd.merge(df, df_3t, on=['loan_y', 'loan_m', 'term_no', 'plan_year', 'plan_month'], how='outer')
df.fillna(0, inplace=True)
df = df[['loan_y', 'loan_m', 'term_no', 'plan_year', 'plan_month', u'未到期在贷', u'逾期30天内在贷', u'逾期30天以上在贷']]
df.rename(columns={'loan_y': u'放款年',
                   'loan_m': u'放款月',
                   'term_no': u'期数',
                   'plan_year': u'应还年',
                   'plan_month': u'应还月',
                   }, inplace=True)

df.to_excel(out_file_path + u'白条在贷_%s.xlsx' % end_point, index=None)