#===================2016年放款按季度拆Q的sql开始=========================================
#====导出文件命名为 loan.csv
SELECT ref_id,loan_id,DATE(loan_paid_at) loan_paid_at,contract_loan_amount loan_amount,funding_code FROM `all_back_0630`.loan_manifest
WHERE is_active IN (1,-2)

#====导出文件命名为 loan_class.csv
select ref_id,class FROM basic.loan_class

# 还资金方--- 查询的时候注意下 interest = NULL 的情况---导出文件命名为 fund.csv
SELECT ref_id,term_no,DATE(deadline) fund_deadline,DATE(repaid_at) fund_repaid_at,principle+interest fund_prin_inst
FROM audit.funding_repayment_record
WHERE `funding_code` NOT IN (1, 2, 3, 4, 5,210,270,310,320,50,80,130,70,135,141) AND is_active = 1 AND ref_id IS NOT NULL

#====== 导出文件命名为 ACD.csv
select ref_id,term_no,funding_code,date(deadline) fund_deadline,date(deadline) fund_repaid_at,principle+interest fund_prin_inst
from audit.funding_repayment_record_acdz where funding_code in (50,80,130);

#还款计划-----导出文件命名为 plan.csv
select ref_id,term_no,date(deadline) fund_deadline,date(deadline) fund_repaid_at,principle+interest fund_prin_inst
from all_back_0731.user_repayment_plan
 where fund_code in (1,2,3,4,5,210,70,135,141);


# 用户线上还款----repay_online.csv
-- select t2.ref_id,t2.term_no,t2.deadline,t2.xyqb_repaid_at repaid_at,t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest real_prin_inst,
-- t1.service_fee -  t1.mitigate_service_fee real_service_fee ,t1.punish - t1.mitigate_collection_relief real_punish
-- from xjd_repay_plan_repay_record_ref t1
-- join user_repayment_plan t2 on t1.plan_id = t2.id and t1.repay_channel < 13

select t2.ref_id,t2.term_no,t2.deadline,t2.xyqb_repaid_at repaid_at,t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest real_prin_inst,
t1.service_fee -  t1.mitigate_service_fee real_service_fee ,t1.punish - t1.mitigate_collection_relief real_punish
from all_back_0630.xjd_repay_plan_repay_record_ref t1
join day_compensate.user_repayment_plan t2 on t1.plan_id = t2.id and t1.repay_channel < 13 and t2.xyqb_repaid_at < '2017-07-01'


#用户线下还款-----repay_ali.csv
SELECT t2.ref_id,t2.term_no,t2.deadline,t3.transfer_time repaid_at,t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest real_prin_inst,
t1.service_fee -  t1.mitigate_service_fee real_service_fee ,t1.punish - t1.mitigate_collection_relief real_punish
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
JOIN all_back_0630.`offline_alipay_record` t3 ON t1.record_id = t3.id AND t1.repay_channel  = 13
#用户线下还款-----repay_bank.csv
SELECT t2.ref_id,t2.term_no,t2.deadline,t3.transfer_time repaid_at,t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest real_prin_inst,
t1.service_fee -  t1.mitigate_service_fee real_service_fee ,t1.punish - t1.mitigate_collection_relief real_punish
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
JOIN all_back_0630.`offline_bank_repay_record` t3 ON t1.record_id = t3.id AND t1.repay_channel  IN (14,15,16)

#===================2016年放款按季度拆Q的sql结束=========================================