# -*- coding: utf-8 -*-
import datetime
import sys

import pandas as pd
from sqlalchemy import create_engine

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

"""
  1、2017-05 月份之后的华融赔付 使用这版出表
"""

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

# 还资金方时间-- 每次出赔付表 更新的变量
start_time_deadline = '2017-05-01 00:00:00'
end_time_deadline = '2017-06-01 00:00:00'

loan_class = 'B'
# loanPaidAt
start_time_loanPaidAt = '2016-01-01 00:00:00'
end_time_loanPaidAt = '2017-01-01 00:00:00'

path = u'E:/peifu_loanPaid/'

# 这个时间不用做更改
start_ancient_time = '2015-01-01 00:00:00'

"""
  1、应还资金方本息
"""
sql_fund = """
  SELECT
    id   fund_code,
    name fund_name
  FROM new_transaction.funding_corp
  WHERE id IN (150)
"""
sql_repay_channel = """
  SELECT
    id            repay_channel,
    approach_name channel_name
  FROM new_transaction.`repay_channel`
"""

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

# 5月份华融
sql_fund_hk_hr = """
  SELECT
    DATE(frr.repaid_at)                        day_hk,
    frr.funding_code                           fund_code,
    SUM(frr.`principle`) + SUM(frr.`interest`) plan_all
  FROM audit.`funding_repayment_record` frr
    JOIN audit.loan_manifest lm ON frr.ref_id = lm.ref_id
    JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
  WHERE
    frr.is_active = 1 AND lc.class = '%s' AND frr.funding_code = %s
    AND lm.is_active IN (1, -2) 
    AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
    AND frr.repaid_at >= '%s' AND frr.repaid_at < '%s'
  GROUP BY DATE(frr.repaid_at)
"""
# 5月份华融处理

# 华融逾期还款
sql_hk_fund_deadline_online_overdue_hr_5 = """
  (
   SELECT
     urp.id                                               plan_id,
     urp.ref_id,
     DATE(urp.xyqb_repaid_at)                             repaid_at,
     DATE(frr.deadline)                                   deadline,
     rp_rrr.repay_channel,
     urp.term_no                                          current_term,
     lm.contract_term,
     urp.fund_code,
     (rp_rrr.`principle` - rp_rrr.`mitigate_principle` +
      rp_rrr.`interest` - rp_rrr.`mitigate_interest`)     real_principle,
     rp_rrr.`service_fee` - rp_rrr.`mitigate_service_fee` real_service_fee,
     rp_rrr.punish - rp_rrr.`mitigate_collection_relief`  real_punish
   FROM
     new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
     JOIN new_transaction.`repay_record_online` rro ON rp_rrr.repay_channel < 13
                                                       AND rp_rrr.record_id = rro.id
     JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
     JOIN audit.funding_repayment_record frr ON frr.ref_id = urp.ref_id
                                                AND frr.term_no = urp.term_no
     JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
     JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
   WHERE
     lc.class = '%s' AND urp.fund_code = %s
     AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
     AND urp.xyqb_repaid_at >= '%s' AND urp.xyqb_repaid_at < '%s'
     AND urp.xyqb_repaid_at > frr.deadline
  )
  UNION ALL
  (
   SELECT
     urp.id                                               plan_id,
     urp.ref_id,
     DATE(oar.transfer_time)                              repaid_at,
     DATE(frr.deadline)                                   deadline,
     rp_rrr.repay_channel,
     urp.term_no                                          current_term,
     lm.contract_term,
     urp.fund_code,
     (rp_rrr.`principle` - rp_rrr.`mitigate_principle` +
      rp_rrr.`interest` - rp_rrr.`mitigate_interest`)     real_principle,
     rp_rrr.`service_fee` - rp_rrr.`mitigate_service_fee` real_service_fee,
     rp_rrr.punish - rp_rrr.`mitigate_collection_relief`  real_punish
   FROM
     new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
     JOIN new_transaction.`offline_alipay_record` oar ON rp_rrr.repay_channel = 13
                                                         AND rp_rrr.record_id = oar.id
     JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
     JOIN audit.funding_repayment_record frr ON frr.ref_id = urp.ref_id
                                                AND frr.term_no = urp.term_no
     JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
     JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
   WHERE
     lc.class = '%s' AND urp.fund_code = %s
     AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
     AND oar.transfer_time >= '%s' AND oar.transfer_time < '%s' 
     AND urp.xyqb_repaid_at > frr.deadline
  )
"""
# 正常还款
sql_hk_fund_deadline_online_normal_hr_5 = """
  (SELECT
     urp.id                                               plan_id,
     urp.ref_id,
     DATE(urp.xyqb_repaid_at)                             repaid_at,
     DATE(frr.deadline)                                   deadline,
     rp_rrr.repay_channel,
     urp.term_no                                          current_term,
     lm.contract_term,
     urp.fund_code,
     (rp_rrr.`principle` - rp_rrr.`mitigate_principle` +
      rp_rrr.`interest` - rp_rrr.`mitigate_interest`)     real_principle,
     rp_rrr.`service_fee` - rp_rrr.`mitigate_service_fee` real_service_fee,
     rp_rrr.punish - rp_rrr.`mitigate_collection_relief`  real_punish
   FROM
     new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
     JOIN new_transaction.`repay_record_online` rro ON rp_rrr.repay_channel < 13
                                                       AND rp_rrr.record_id = rro.id
     JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
     JOIN audit.funding_repayment_record frr ON frr.ref_id = urp.ref_id
                                                AND frr.term_no = urp.term_no
     JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
     JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
   WHERE
     lc.class = '%s' AND urp.fund_code = %s 
     AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
     AND urp.xyqb_repaid_at >= '%s' AND urp.xyqb_repaid_at < '%s' 
     AND frr.deadline >= '%s' AND frr.deadline < '%s'
  )
  UNION ALL
  (
   SELECT
     urp.id                                               plan_id,
     urp.ref_id,
     DATE(oar.transfer_time)                              repaid_at,
     DATE(frr.deadline)                                   deadline,
     rp_rrr.repay_channel,
     urp.term_no                                          current_term,
     lm.contract_term,
     urp.fund_code,
     (rp_rrr.`principle` - rp_rrr.`mitigate_principle` +
      rp_rrr.`interest` - rp_rrr.`mitigate_interest`)     real_principle,
     rp_rrr.`service_fee` - rp_rrr.`mitigate_service_fee` real_service_fee,
     rp_rrr.punish - rp_rrr.`mitigate_collection_relief`  real_punish
   FROM
     new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
     JOIN new_transaction.`offline_alipay_record` oar ON rp_rrr.repay_channel = 13
                                                         AND rp_rrr.record_id = oar.id
     JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
     JOIN audit.funding_repayment_record frr ON frr.ref_id = urp.ref_id
                                                AND frr.term_no = urp.term_no
     JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
     JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
   WHERE 
        lc.class = '%s' AND urp.fund_code = %s 
        AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
        AND oar.transfer_time >= '%s' AND oar.transfer_time < '%s' 
        AND frr.deadline >= '%s' AND frr.deadline < '%s'
  )
"""
# 提前还款
sql_hk_fund_deadline_online_advance_hr_5 = """
  (
   SELECT
     urp.id                                               plan_id,
     urp.ref_id,
     DATE(urp.xyqb_repaid_at)                             repaid_at,
     DATE(frr.deadline)                                   deadline,
     rp_rrr.repay_channel,
     urp.term_no                                          current_term,
     lm.contract_term,
     urp.fund_code,
     (rp_rrr.`principle` - rp_rrr.`mitigate_principle` +
      rp_rrr.`interest` - rp_rrr.`mitigate_interest`)     real_principle,
     rp_rrr.`service_fee` - rp_rrr.`mitigate_service_fee` real_service_fee,
     rp_rrr.punish - rp_rrr.`mitigate_collection_relief`  real_punish
   FROM
     new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
     JOIN new_transaction.`repay_record_online` rro ON rp_rrr.repay_channel < 13
                                                       AND rp_rrr.record_id = rro.id
     JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
     JOIN audit.funding_repayment_record frr ON frr.ref_id = urp.ref_id
                                                AND frr.term_no = urp.term_no
     JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
     JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
   WHERE
     lc.class = '%s' AND urp.fund_code = %s 
     AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
     AND frr.deadline >= '%s' AND frr.deadline < '%s'
     AND urp.xyqb_repaid_at < '%s' AND urp.xyqb_repaid_at < frr.deadline
  )
  UNION ALL
  (
   SELECT
     urp.id                                               plan_id,
     urp.ref_id,
     DATE(oar.transfer_time)                              repaid_at,
     DATE(frr.deadline)                                   deadline,
     rp_rrr.repay_channel,
     urp.term_no                                          current_term,
     lm.contract_term,
     urp.fund_code,
     (rp_rrr.`principle` - rp_rrr.`mitigate_principle` +
      rp_rrr.`interest` - rp_rrr.`mitigate_interest`)     real_principle,
     rp_rrr.`service_fee` - rp_rrr.`mitigate_service_fee` real_service_fee,
     rp_rrr.punish - rp_rrr.`mitigate_collection_relief`  real_punish
   FROM
     new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
     JOIN new_transaction.`offline_alipay_record` oar ON rp_rrr.repay_channel = 13
                                                         AND rp_rrr.record_id = oar.id
     JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
     JOIN audit.funding_repayment_record frr ON frr.ref_id = urp.ref_id
                                                AND frr.term_no = urp.term_no
     JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
     JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
   WHERE
     lc.class = '%s' AND urp.fund_code = %s
     AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
     AND frr.deadline >= '%s' AND frr.deadline < '%s'
     AND oar.transfer_time < '%s' AND oar.transfer_time < frr.deadline
  )
"""

# 查询当月提前结清的还款计划id
sql_query_advance_settle_plan_id = """
  SELECT urp.id plan_id
  FROM
    new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
    JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
    JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
    JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
  WHERE
    lc.class = '%s' AND urp.fund_code = %s
    AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s'
    AND urp.xyqb_repaid_at >= '%s' AND urp.xyqb_repaid_at < '%s'
    AND urp.xyqb_repaid_at <= urp.deadline AND urp.deadline >= '%s'
    AND urp.ref_id IN (
      SELECT urp.ref_id
      FROM
        new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
        JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
        JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
                                       AND urp.term_no = lm.contract_term
      WHERE
        urp.fund_code = %s
        AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s' 
        AND urp.xyqb_repaid_at < '%s'
        AND urp.xyqb_repaid_at <= urp.deadline
    );
"""

sql_query_advance_settle = """
  SELECT
    urp.id                                               plan_id,
    urp.ref_id,
    DATE(urp.xyqb_repaid_at)                             repaid_at,
    DATE(frr.deadline)                                   deadline,
    rp_rrr.repay_channel,
    urp.term_no                                          current_term,
    lm.contract_term,
    urp.fund_code,
    (rp_rrr.`principle` - rp_rrr.`mitigate_principle` +
     rp_rrr.`interest` - rp_rrr.`mitigate_interest`
    )                                                    real_principle,
    rp_rrr.`service_fee` - rp_rrr.`mitigate_service_fee` real_service_fee,
    rp_rrr.punish - rp_rrr.`mitigate_collection_relief`  real_punish
  FROM
    new_transaction.`xjd_repay_plan_repay_record_ref` rp_rrr
    JOIN new_transaction.`repay_record_online` rro ON rp_rrr.repay_channel < 13
                                                      AND rp_rrr.record_id = rro.id
    JOIN new_transaction.`user_repayment_plan` urp ON rp_rrr.plan_id = urp.id
    JOIN audit.funding_repayment_record frr ON frr.ref_id = urp.ref_id
                                               AND frr.term_no = urp.term_no
    JOIN audit.loan_manifest lm ON urp.ref_id = lm.ref_id
    JOIN temp.loan_class lc ON lm.ref_id = lc.ref_id
  WHERE
    lc.class = '%s' AND urp.fund_code = %s 
    AND lm.loan_paid_at >= '%s' AND lm.loan_paid_at < '%s' 
    AND urp.id IN %s
"""


# 华融，需要剔除 应还在 16年但还款时间在17年1-10号的数据
def hr_table(fund_code, fund_name):
    # =========取实还资金方做应还==========
    df_plan = pd.read_sql(sql_fund_hk_hr % (loan_class,
                                            fund_code, start_time_loanPaidAt, end_time_loanPaidAt, start_time_deadline,
                                            end_time_deadline),
                          con=engine_new_transaction)
    df_plan.fillna(0, inplace=True)
    df_plan['day_hk'] = pd.to_datetime(df_plan['day_hk']).apply(lambda x: x.date())
    df_plan['repay_channel'] = 2

    # =============正常还款========= 时间范围就是查询范围
    print "获取正常还款 sql=" + sql_hk_fund_deadline_online_normal_hr_5 % (loan_class,
                                                                     fund_code, start_time_loanPaidAt,
                                                                     end_time_loanPaidAt, start_time_deadline,
                                                                     end_time_deadline,
                                                                     start_time_deadline, end_time_deadline, loan_class,
                                                                     fund_code,
                                                                     start_time_loanPaidAt, end_time_loanPaidAt,
                                                                     start_time_deadline, end_time_deadline,
                                                                     start_time_deadline,
                                                                     end_time_deadline)
    df_zc = pd.read_sql(sql_hk_fund_deadline_online_normal_hr_5 % (
        loan_class, fund_code, start_time_loanPaidAt, end_time_loanPaidAt, start_time_deadline, end_time_deadline,
        start_time_deadline, end_time_deadline,
        loan_class, fund_code, start_time_loanPaidAt, end_time_loanPaidAt, start_time_deadline, end_time_deadline,
        start_time_deadline, end_time_deadline),
                        con=engine_new_transaction)
    df_zc['repaid_at'] = pd.to_datetime(df_zc['repaid_at']).apply(lambda x: x.date())
    df_zc['deadline'] = pd.to_datetime(df_zc['deadline']).apply(lambda x: x.date())
    df_zc['fund_code'] = df_zc['fund_code'].astype(int)
    df_zc['fund_name'] = fund_name
    df_zc.fillna(0, inplace=True)
    df_zc = df_zc.ix[df_zc['repaid_at'] == df_zc['deadline']]
    df_zc = df_zc.groupby(by=['repaid_at', 'repay_channel'])['real_principle'].sum()
    df_zc = df_zc.to_frame()
    df_zc.reset_index(inplace=True)
    df_zc.rename(columns={'repaid_at': 'day_hk', 'real_principle': 'zc_prin_inst_total'}, inplace=True)
    df_zc['day_hk'] = pd.to_datetime(df_zc['day_hk']).apply(lambda x: x.date())

    # ===========逾期还款==============
    print "获取逾期还款数据 sql=" + sql_hk_fund_deadline_online_overdue_hr_5 % (loan_class, fund_code,
                                                                        start_time_loanPaidAt, end_time_loanPaidAt,
                                                                        start_time_deadline, end_time_deadline,
                                                                        loan_class, fund_code,
                                                                        start_time_loanPaidAt, end_time_loanPaidAt,
                                                                        start_time_deadline, end_time_deadline
                                                                    )
    df_yq = pd.read_sql(sql_hk_fund_deadline_online_overdue_hr_5 % (loan_class,
                                                                    fund_code, start_time_loanPaidAt,
                                                                    end_time_loanPaidAt, start_time_deadline,
                                                                    end_time_deadline, loan_class, fund_code,
                                                                    start_time_loanPaidAt, end_time_loanPaidAt,
                                                                    start_time_deadline, end_time_deadline),
                        con=engine_new_transaction)
    df_yq['repaid_at'] = pd.to_datetime(df_yq['repaid_at']).apply(lambda x: x.date())
    df_yq['deadline'] = pd.to_datetime(df_yq['deadline']).apply(lambda x: x.date())
    df_yq['fund_code'] = df_yq['fund_code'].astype(int)
    df_yq['fund_name'] = fund_name
    df_yq.fillna(0, inplace=True)

    # TODO ====逾期还款提前 2017.1-10 的数据 按照 deadline 进行统计即可
    df_yq.ix[(df_yq.deadline < datetime.date(2017, 1, 1)) & (df_yq.repaid_at >= datetime.date(2017, 1, 1)) & (
        df_yq.repaid_at < datetime.date(2017, 1, 11)), 'repaid_at'] = \
        df_yq.ix[(df_yq.deadline < datetime.date(2017, 1, 1)) & (df_yq.repaid_at >= datetime.date(2017, 1, 1)) & (
            df_yq.repaid_at < datetime.date(2017, 1, 11))].apply(lambda x: x['deadline'], axis=1)

    df_yq = df_yq.ix[df_yq['deadline'] < df_yq['repaid_at']]
    df_yq = df_yq.groupby(by=['repaid_at', 'repay_channel'])['real_principle'].sum()
    df_yq = df_yq.to_frame()
    df_yq.reset_index(inplace=True)
    df_yq.rename(columns={'repaid_at': 'day_hk', 'real_principle': 'yq_prin_inst_total'}, inplace=True)
    df_yq['day_hk'] = pd.to_datetime(df_yq['day_hk']).apply(lambda x: x.date())

    # ============提前还款==============
    print "获取提前还款数据 sql=" + sql_hk_fund_deadline_online_advance_hr_5 % (loan_class,
                                                                        fund_code, start_time_loanPaidAt,
                                                                        end_time_loanPaidAt, start_time_deadline,
                                                                        end_time_deadline,
                                                                        end_time_deadline,
                                                                        loan_class, fund_code, start_time_loanPaidAt,
                                                                        end_time_loanPaidAt, start_time_deadline,
                                                                        end_time_deadline,
                                                                        end_time_deadline)
    df_tq = pd.read_sql(sql_hk_fund_deadline_online_advance_hr_5 % (loan_class,
                                                                    fund_code, start_time_loanPaidAt,
                                                                    end_time_loanPaidAt, start_time_deadline,
                                                                    end_time_deadline,
                                                                    end_time_deadline,
                                                                    loan_class, fund_code, start_time_loanPaidAt,
                                                                    end_time_loanPaidAt, start_time_deadline,
                                                                    end_time_deadline,
                                                                    end_time_deadline),
                        con=engine_new_transaction)
    df_tq['repaid_at'] = pd.to_datetime(df_tq['repaid_at']).apply(lambda x: x.date())
    df_tq['deadline'] = pd.to_datetime(df_tq['deadline']).apply(lambda x: x.date())
    df_tq['fund_code'] = df_tq['fund_code'].astype(int)
    df_tq['fund_name'] = fund_name
    df_tq.fillna(0, inplace=True)
    # df_tq = df_tq.ix[df_tq['deadline'] > df_tq['repaid_at']]

    # ============= 构成还款总集合 逾期还款 正常还款 提前还款
    df_hk = pd.concat([df_yq, df_zc, df_tq], ignore_index=True)
    df_advance_plan_id = pd.read_sql(
        sql_query_advance_settle_plan_id % (loan_class,
                                            fund_code, start_time_loanPaidAt, end_time_loanPaidAt, start_time_deadline,
                                            end_time_deadline,
                                            start_time_deadline, fund_code, start_time_loanPaidAt, end_time_loanPaidAt,
                                            end_time_deadline),
        con=engine_new_transaction)
    advance_planid_list = df_advance_plan_id['plan_id'].tolist()
    advance_planid_set = set(advance_planid_list)

    #  ==============符合提前结清的提前还款
    # df_tq_settle = df_tq.ix[df_tq.plan_id.isin(advance_planid_set)]
    advance_planid_list = map(int, advance_planid_list)
    df_tq_settle = pd.read_sql(sql_query_advance_settle % (loan_class,
                                                           fund_code, start_time_loanPaidAt, end_time_loanPaidAt,
                                                           str(tuple(advance_planid_list))),
                               con=engine_new_transaction)
    df_tq_settle_max_deadline = df_tq_settle.groupby(by=['ref_id'])['repaid_at'].agg(['max']).reset_index().rename(
        columns={'max': 'max_repaid_at'})
    df_tq_settle_max_deadline.reset_index(inplace=True)
    df_tq_settle_max_deadline['max_repaid_at'] = pd.to_datetime(df_tq_settle_max_deadline['max_repaid_at']).apply(
        lambda x: x.date())

    df_tq_settle = pd.merge(df_tq_settle, df_tq_settle_max_deadline, how='left', on=['ref_id'])
    # 这部分没有包含到当月的提前结清中
    df_tq_settle_leak = df_tq_settle.ix[df_tq_settle.max_repaid_at < datetime.date(2017, 5, 1)]
    df_tq_settle_leak.drop(['max_repaid_at'], inplace=True, axis=1)

    df_tq_settle = df_tq_settle.ix[df_tq_settle.max_repaid_at >= datetime.date(2017, 5, 1)]
    df_tq_settle.drop(['repaid_at'], inplace=True, axis=1)
    df_tq_settle.rename(columns={'deadline': 'repaid_at'}, inplace=True)
    df_tq_settle.rename(columns={'max_repaid_at': 'deadline'}, inplace=True)

    # ================ 符合未结清的提前还款
    df_tq_no_settle = df_tq[~df_tq.plan_id.isin(advance_planid_set)]
    df_tq = pd.concat([df_tq_no_settle, df_tq_settle, df_tq_settle_leak], ignore_index=True)
    df_tq = df_tq.groupby(by=['deadline', 'repay_channel'])['real_principle'].sum()
    df_tq = df_tq.to_frame()
    df_tq.reset_index(inplace=True)
    df_tq['deadline'] = pd.to_datetime(df_tq['deadline']).apply(lambda x: x.date())
    df_tq.rename(columns={'deadline': 'day_hk', 'real_principle': 'tq_prin_inst_total'}, inplace=True)
    df_tq['repay_channel'] = df_tq['repay_channel'].astype(int)

    # ==============服务费===============
    df_service_fee = df_hk.groupby(by=['repaid_at', 'repay_channel'])['real_service_fee'].sum()
    df_service_fee = df_service_fee.to_frame()
    df_service_fee.reset_index(inplace=True)
    df_service_fee.rename(columns={'repaid_at': 'day_hk'}, inplace=True)
    df_service_fee['day_hk'] = pd.to_datetime(df_service_fee['day_hk']).apply(lambda x: x.date())
    df_service_fee['repay_channel'] = df_service_fee['repay_channel'].astype(int)

    # ==============逾期费=============
    df_deplay_fee = df_hk.groupby(by=['repaid_at', 'repay_channel'])['real_punish'].sum()
    df_deplay_fee = df_deplay_fee.to_frame()
    df_deplay_fee.reset_index(inplace=True)
    df_deplay_fee.rename(columns={'repaid_at': 'day_hk'}, inplace=True)
    df_deplay_fee['day_hk'] = pd.to_datetime(df_deplay_fee['day_hk']).apply(lambda x: x.date())
    df_deplay_fee['repay_channel'] = df_deplay_fee['repay_channel'].astype(int)

    # =========================合并
    df_hk_new = pd.merge(df_zc, df_yq, how='outer', on=['day_hk', 'repay_channel'])
    df_hk_new = pd.merge(df_hk_new, df_tq, how='outer', on=['day_hk', 'repay_channel'])
    df_hk_new = pd.merge(df_plan, df_hk_new, how='outer', on=['day_hk', 'repay_channel'])
    df_hk_new = pd.merge(df_hk_new, df_service_fee, how='outer', on=['day_hk', 'repay_channel'])
    df_hk_new = pd.merge(df_hk_new, df_deplay_fee, how='outer', on=['day_hk', 'repay_channel'])
    df_hk_new.fillna(0, inplace=True)
    df_hk_new = pd.merge(df_hk_new, df_repay_channel, on=['repay_channel'], how='left')
    df_hk_new['real_all'] = df_hk_new['zc_prin_inst_total'] + df_hk_new['tq_prin_inst_total'] + df_hk_new[
        'yq_prin_inst_total']
    df_hk_new['fund_name'] = fund_name
    df_hk_new = df_hk_new[
        ['fund_name', 'day_hk', 'repay_channel', 'channel_name', 'plan_all', 'real_all', 'zc_prin_inst_total',
         'tq_prin_inst_total', 'yq_prin_inst_total', 'real_service_fee', 'real_punish']]
    # df_hk_new.sort(columns=['day_hk', 'repay_channel'], axis=0, inplace=True)
    df_hk_new.rename(
        columns={'day_hk': u'还款日期',
                 'repay_channel': u'还款账户',
                 'plan_all': u'应还资金方本息',
                 'real_all': u'客户实还本息',
                 'zc_prin_inst_total': u'客户正常还款本息',
                 'yq_prin_inst_total': u'客户逾期还款本息',
                 'tq_prin_inst_total': u'客户提前还款本息',
                 'real_service_fee': u'客户实还服务费',
                 'real_punish': u'客户实还罚息'},
        inplace=True)
    df_hk_new.to_excel(path + str(fund_code) + '.xlsx', index=None, columns=df_hk_new.columns, encoding='utf8')


# =======================================================================================================================================

if __name__ == '__main__':
    for index, row in df_fund.iterrows():
        fund_code = int(row['fund_code'])
        fund_name = row['fund_name']
        print '------begin---', fund_name
        hr_table(fund_code, fund_name)
        # ============= 合并文件 begin=============
        # excel_out = pd.ExcelWriter(path + 'all.xlsx')
        # df_out = pd.DataFrame()
        # for file in os.listdir(path):
        #     tmp_file = file.replace('.xlsx', '')
        #     if tmp_file in ['160', '200', '290'] > 0:
        #         df_tmp = pd.read_excel(path + file)
        #         df_tmp.to_excel(excel_out, sheet_name=tmp_file, columns=df_tmp.columns, index=None, encoding='utf8')
        #     elif tmp_file == 'all':
        #         continue
        #     else:
        #         df_out = df_out.append(pd.read_excel(path + file))
        # df_out.to_excel(excel_out, sheet_name='sheet1', columns=df_out.columns, index=None, encoding='utf8')
        # excel_out.save()
        # ============= 合并文件 end=============

print '=====================main done======================'
