#--放款导出 ========导出文件名 loan_2015.csv
select ref_id,loan_id,date(loan_paid_at) loan_paid_at,funding_code from all_back_0630.loan_manifest
WHERE is_active in (1,-2) AND  loan_paid_at < '2016-01-01';

#还资金方导出- 其他资金方========导出文件名 fund_2015.csv
select ref_id,term_no,funding_code,date(deadline) fund_deadline,date(repaid_at) fund_repaid_at,principle+interest fund_prin_inst
from test_linfang.funding_repayment_record
where is_active = 1 AND funding_code not in (1,2,3,4,5) AND
 deadline <  '2016-01-01';
#还资金方导出- 使用还款计划========导出文件名 plan_2015.csv
select ref_id,term_no ,fund_code funding_code,date(deadline) fund_deadline,date(deadline) fund_deadline,principle+interest fund_prin_inst
from all_back_0630.user_repayment_plan
where fund_code in (1,2,3,4,5) AND  deadline <  '2016-01-01';

#客户还款--线上还款========导出文件名 repay_online_2015.csv
SELECT ref_id,term_no,t2.fund_code,date(t2.deadline) deadline,date(t2.xyqb_repaid_at) repaid_at,t1.principle+t1.interest-t1.mitigate_principle-t1.mitigate_interest real_prin_inst
,t1.punish-t1.mitigate_collection_relief real_punish,t1.service_fee - t1.mitigate_service_fee real_service_fee
FROM all_back_0630.xjd_repay_plan_repay_record_ref t1
JOIN all_back_0630.user_repayment_plan t2 on t1.plan_id = t2.id AND t1.repay_channel < 13 AND
      t2.xyqb_repaid_at < '2016-01-01';
#客户还款--线下还款========导出文件名 repay_offline_2015.csv
SELECT ref_id,term_no,t2.fund_code,date(t2.deadline) deadline,date(t3.repaid_at) repaid_at,t1.principle+t1.interest-t1.mitigate_principle-t1.mitigate_interest real_prin_inst
,t1.punish-t1.mitigate_collection_relief real_punish,t1.service_fee - t1.mitigate_service_fee real_service_fee
FROM all_back_0630.xjd_repay_plan_repay_record_ref t1
JOIN all_back_0630.user_repayment_plan t2 on t1.plan_id = t2.id AND t1.repay_channel IN (13,14,15,16)
JOIN new_transaction.xjd_repay_plan_repay_record_ref_repay_date t3 on t1.id = t3.xjd_ref_id AND
    t3.repaid_at < '2016-01-01';
