# -*- 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_xjd = """SELECT
	oar.id "支付宝id",
	oar.business_flow "支付宝流水号",
	oar.income "支付宝转账金额",
	date(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 "实还罚息",
	xrprrr.remain_income "remain_income",
	date(urp.xyqb_repaid_at) "还款确认时间",
	lm.loan_id "Loan_id",
	fc.`name` "资金方"
FROM
	all_back_0831.xjd_repay_plan_repay_record_ref xrprrr
LEFT JOIN all_back_0731.xjd_repay_plan_repay_record_ref t2 ON xrprrr.id = t2.id
JOIN all_back_0831.offline_alipay_record oar ON xrprrr.record_id = oar.id
AND oar.transfer_time < '2017-08-01'
JOIN all_back_0831.user_repayment_plan urp ON urp.id = xrprrr.plan_id
JOIN basic.funding_corp fc ON urp.fund_code = fc.funding_code
join all_back_0831.loan_manifest lm on lm.ref_id = urp.ref_id
WHERE
	xrprrr.repay_channel = 13
AND t2.id IS NULL ORDER  BY  urp.id
"""

df_xjd_diff = pd.read_sql(sql=sql_query_xjd, con=con_all_back)
df_xjd_diff.to_excel(u"E:/审计出表/现金贷-还款表（1月份-6月份）/现金贷_支付宝差异_8月份.xlsx",index=None)

