# 白条 实还表--- 按照实还时间出表
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount < 0 ,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t3.principle 应还本金 ,0 AS '应还利息-直投模式下',t3.principle-t1.`principle`+t1.`mitigate_principle` '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t2.repaid_at >= '2017-06-01' AND t2.repaid_at < '2017-07-01'

# 客户实还表---按照放款时间出表
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount < 0 ,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t3.principle 应还本金 ,0 AS '应还利息-直投模式下',t3.principle-t1.`principle`+t1.`mitigate_principle` '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t4.loan_paid_at >= '2017-06-01' AND t4.loan_paid_at < '2017-07-01'


# 白条实还表----7月份，包括现金支付
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount <0,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t3.principle 应还本金 ,0 AS '应还利息-直投模式下',t3.principle-t1.`principle`+t1.`mitigate_principle` '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t2.repaid_at >= '2017-07-01' AND t2.repaid_at < '2017-08-01'
UNION ALL
SELECT t3.order_no  商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,'' 当前期数,t1.ref_amount 还款总额,
t1.ref_amount 实还本金,0 AS '实还利息-直投模式下',0 实还服务费,0 实还罚息,
0 其他收益,t2.order_id 流水号,t2.amount 账单金额,
'' 当前期数,'' 应还款日,t3.should_order_amount 应还本金 ,0 AS '应还利息-直投模式下',t3.should_order_amount-t1.ref_amount '应还本息-实还本息',
t1.ref_amount - t2.amount '还款总额-账单金额', '' 当前期数
FROM new_transaction.`baitiao_cash_record_ref` t1
JOIN new_transaction.`baitiao_cash_repay` t3 ON t1.repay_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
JOIN new_transaction.`repay_record_online` t2 ON t1.record_id = t2.id AND t1.repay_channel != 13
WHERE t2.bill_time >= '2017-07-01'  AND t2.bill_time < '2017-08-01'

# 白条实还表----7月份，包括现金支付----按照放款时间出
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount <0,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t3.principle 应还本金 ,0 AS '应还利息-直投模式下',t3.principle-t1.`principle`+t1.`mitigate_principle` '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t4.loan_paid_at >= '2017-07-01' AND t4.loan_paid_at < '2017-08-01'
UNION ALL
SELECT t3.order_no  商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,'' 当前期数,t1.ref_amount 还款总额,
t1.ref_amount 实还本金,0 AS '实还利息-直投模式下',0 实还服务费,0 实还罚息,
0 其他收益,t2.order_id 流水号,t2.amount 账单金额,
'' 当前期数,'' 应还款日,t3.should_order_amount 应还本金 ,0 AS '应还利息-直投模式下',t3.should_order_amount-t1.ref_amount '应还本息-实还本息',
t1.ref_amount - t2.amount '还款总额-账单金额', '' 当前期数
FROM new_transaction.`baitiao_cash_record_ref` t1
JOIN new_transaction.`baitiao_cash_repay` t3 ON t1.repay_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
JOIN new_transaction.`repay_record_online` t2 ON t1.record_id = t2.id AND t1.repay_channel != 13
WHERE t4.loan_paid_at >= '2017-07-01'  AND t4.loan_paid_at < '2017-08-01'


# 白条实还表----8月份，包括直投
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount < 0 ,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t3.principle 应还本金 ,0 AS '应还利息-直投模式下',t3.principle-t1.`principle`+t1.`mitigate_principle` '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id AND t4.funding_corp_id != 5
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t2.repaid_at >= '2017-08-01' AND t2.repaid_at < '2017-09-01'
UNION ALL
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'笑脸直投' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount <0,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t8.principal 应还本金 ,t8.interest AS '应还利息-直投模式下',t8.principal+t8.interest-t1.`principle`+t1.`mitigate_principle`-t1.interest+t1.mitigate_interest '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN new_transaction.`bt_funding_repayment_plan` t8 ON t3.order_no= t8.order_no AND t3.term_no = t8.term_no
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id AND t4.funding_corp_id = 5 AND t4.is_active = 1
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t2.repaid_at >= '2017-08-01' AND t2.repaid_at < '2017-09-01'
UNION ALL
SELECT t3.order_no  商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,'' 当前期数,t1.ref_amount 还款总额,
t1.ref_amount 实还本金,0 AS '实还利息-直投模式下',0 实还服务费,0 实还罚息,
0 其他收益,t2.order_id 流水号,t2.amount 账单金额,
'' 当前期数,'' 应还款日,t3.should_order_amount 应还本金 ,0 AS '应还利息-直投模式下',t3.should_order_amount-t1.ref_amount '应还本息-实还本息',
t1.ref_amount - t2.amount '还款总额-账单金额', '' 当前期数
FROM new_transaction.`baitiao_cash_record_ref` t1
JOIN new_transaction.`baitiao_cash_repay` t3 ON t1.repay_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
JOIN new_transaction.`repay_record_online` t2 ON t1.record_id = t2.id AND t1.repay_channel != 13
WHERE t2.bill_time >= '2017-08-01'  AND t2.bill_time < '2017-09-01'



# 白条实还表----8月份，包括直投-----按照放款时间出表
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount < 0 ,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t3.principle 应还本金 ,0 AS '应还利息-直投模式下',t3.principle-t1.`principle`+t1.`mitigate_principle` '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id AND t4.funding_corp_id != 5
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t4.loan_paid_at >= '2017-08-01' AND t4.loan_paid_at < '2017-09-01'
UNION ALL
SELECT  t3.`order_no` 商品编号,t5.`merchant_name` 商户名称,'笑脸直投' AS 资金方,t4.`contract_term` 放款期数,t3.`term_no` 当前期数,t1.`ref_amount` 还款总额,
t1.`principle`-t1.`mitigate_principle` 实还本金,0 AS '实还利息-直投模式下',t1.interest-t1.mitigate_interest 实还服务费,t1.punish - t1.mitigate_collection_relief 实还罚息,
t1.remain_income 其他收益,IF(t1.repay_channel = 13,t6.business_flow,t7.order_id) 流水号,IF(t1.repay_channel=13,IF(t1.ref_amount <0,t6.outcome,t6.income),t7.amount) 账单金额,
t3.term_no 当前期数,t3.deadline 应还款日,t8.principal 应还本金 ,t8.interest AS '应还利息-直投模式下',t8.principal+t8.interest-t1.`principle`+t1.`mitigate_principle`-t1.interest+t1.mitigate_interest '应还本息-实还本息',t1.`ref_amount`-IF(t1.repay_channel=13,t6.income,t7.amount) '还款总额-账单金额',
t3.term_no 当前期数
FROM new_transaction.`baitiao_repay_plan_repay_record_ref` t1
JOIN new_transaction.`baitiao_ref_repay_date` t2 ON t1.id = t2.`bt_ref_id`
JOIN new_transaction.`user_bt_repayment_plan` t3 ON t1.plan_id = t3.id
JOIN new_transaction.`bt_funding_repayment_plan` t8 ON t3.order_no= t8.order_no AND t3.term_no = t8.term_no
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id AND t4.funding_corp_id = 5 AND t4.is_active = 1
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
LEFT JOIN `offline_alipay_record` t6 ON t1.record_id = t6.id AND t1.repay_channel = 13
LEFT JOIN `repay_record_online` t7 ON t1.record_id = t7.id AND t1.repay_channel != 13
WHERE t4.loan_paid_at >= '2017-08-01' AND t4.loan_paid_at < '2017-09-01'
UNION ALL
SELECT t3.order_no  商品编号,t5.`merchant_name` 商户名称,'内保' AS 资金方,t4.`contract_term` 放款期数,'' 当前期数,t1.ref_amount 还款总额,
t1.ref_amount 实还本金,0 AS '实还利息-直投模式下',0 实还服务费,0 实还罚息,
0 其他收益,t2.order_id 流水号,t2.amount 账单金额,
'' 当前期数,'' 应还款日,t3.should_order_amount 应还本金 ,0 AS '应还利息-直投模式下',t3.should_order_amount-t1.ref_amount '应还本息-实还本息',
t1.ref_amount - t2.amount '还款总额-账单金额', '' 当前期数
FROM new_transaction.`baitiao_cash_record_ref` t1
JOIN new_transaction.`baitiao_cash_repay` t3 ON t1.repay_id = t3.id
JOIN baitiao_audit.`baitiao_order` t4 ON t4.ref_id = t3.ref_id
JOIN baitiao_audit.`merchant` t5 ON t4.`merchantId` = t5.`merchant_id`
JOIN new_transaction.`repay_record_online` t2 ON t1.record_id = t2.id AND t1.repay_channel != 13
WHERE t4.loan_paid_at >= '2017-08-01'  AND t4.loan_paid_at < '2017-09-01'