# -*- encoding: utf8 -*-

import pandas as pd
from sqlalchemy import create_engine
import numpy as np


con_all_back = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/?charset=utf8', echo=False)

sql_query_baitiao = """SELECT
	oar.id "支付宝id",
	oar.business_flow "支付宝流水号",
	oar.income "支付宝转账金额",
	oar.transfer_time "支付宝转账时间",
	xrprrr.ref_amount "支付宝关联还款计划金额",
	xrprrr.principle - xrprrr.mitigate_principle "实还本金",
	xrprrr.interest - xrprrr.mitigate_interest "实还利息",
	xrprrr.service_fee - xrprrr.mitigate_service_fee "实还服务费",
	xrprrr.punish - xrprrr.mitigate_collection_relief "实还罚息",
	urp.biz_repaid_at "还款确认时间",
	urp.id "被确认的plan_id"
FROM
	all_back_0930.baitiao_repay_plan_repay_record_ref xrprrr
LEFT JOIN all_back_0831.baitiao_repay_plan_repay_record_ref t2 ON xrprrr.id = t2.id
JOIN all_back_0930.offline_alipay_record oar ON xrprrr.record_id = oar.id
AND oar.transfer_time < 2017-09-01
JOIN all_back_0930.user_bt_repayment_plan urp ON urp.id = xrprrr.plan_id
WHERE
	xrprrr.repay_channel = 13
AND t2.id IS NULL
ORDER BY
	urp.id
"""

df_xjd_diff = pd.read_sql(sql=sql_query_baitiao, con=con_all_back)
df_xjd_diff.to_excel(u"E:/审计出表/非去哪儿白条-客户实还表（1月份-6月份）/白条_支付宝差异_9月份.xlsx",index=None)

