#=====================不可用统计==============================
# 不可用，截止到 8.31号的
SELECT DATE(t2.repaid_at),t3.fund_code,SUM(ref_amount),SUM(t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_service_fee),SUM(t1.service_fee-t1.mitigate_service_fee)
FROM `xjd_repay_plan_repay_record_ref` t1
JOIN `xjd_repay_plan_repay_record_ref_repay_date_flow` t2 ON t1.id = t2.xjd_ref_id
JOIN `user_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN audit.`funding_repayment_plan` t4 ON t4.ref_id = t3.ref_id AND t3.term_no = t4.term_no
WHERE DATE(t2.repaid_at) < DATE(t4.deadline) AND t4.deadline >= '2017-09-27' AND t2.repaid_at < '2017-09-01'
GROUP BY 1,2

# 不可用中属于提前结算的
SELECT SUM(ref_amount),sum(t1.principle-t1.mitigate_principle+t1.interest-t1.mitigate_interest),sum(t1.service_fee-t1.mitigate_service_fee)
FROM `xjd_repay_plan_repay_record_ref` t1
JOIN `xjd_repay_plan_repay_record_ref_repay_date_flow` t2 ON t1.id = t2.xjd_ref_id
JOIN `user_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN funding_repayment_record t4 ON t3.ref_id = t4.ref_id AND t3.term_no = t4.term_no
where  date(t4.deadline) > date(t2.repaid_at) AND t4.deadline >= '2017-09-01' and t2.repaid_at < '2017-09-01'
and t4.current_repayment > 0
#=====================不可用统计done==============================