# -*- 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/new_transaction?charset=utf8', echo=False)


sql_query_xjd = """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 "实还罚息",
	xrprrr.remain_income "remain_income",
	urp.xyqb_repaid_at "还款确认时间",
	urp.id "被确认的plan_id",
	fc.`name` "资金方",
	xrprrr.id "xjd_ref_id"
FROM
	xjd_repay_plan_repay_record_ref xrprrr
LEFT JOIN all_back_0930.xjd_repay_plan_repay_record_ref t2 ON xrprrr.id = t2.id
JOIN offline_alipay_record oar ON xrprrr.record_id = oar.id
AND oar.transfer_time < "2017-10-01"
JOIN user_repayment_plan urp ON urp.id = xrprrr.plan_id
JOIN basic.funding_corp fc ON urp.fund_code = fc.funding_code
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("./xjd_alipay_diff.xlsx",index=None)



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 "实还罚息",
	xrprrr.remain_income "remain_income",
	urp.biz_repaid_at "还款确认时间",
	urp.id "被确认的plan_id",
  fcp.fund_name "资金方"
FROM
	baitiao_repay_plan_repay_record_ref xrprrr
LEFT JOIN all_back_0930.baitiao_repay_plan_repay_record_ref t2 ON xrprrr.id = t2.id
JOIN offline_alipay_record oar ON xrprrr.record_id = oar.id
AND oar.transfer_time < "2017-07-01"
JOIN user_bt_repayment_plan urp ON urp.id = xrprrr.plan_id
JOIN baitiao_audit.baitiao_order bor ON urp.ref_id = bor.ref_id
JOIN baitiao_audit.fund_corp fcp ON bor.funding_corp_id = fcp.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("./baitiao_alipay_diff.xlsx",index=None)

