# -*- encoding: utf8 -*-
import sys
import datetime
import pandas as pd
from sqlalchemy import create_engine

reload(sys)
sys.setdefaultencoding("utf-8")
pd.options.mode.chained_assignment = None

'''
@author linfang
目标：本月确认支付宝还款在本月之前的数据。即支付宝还款 有些不会在当月确认。
现金贷表头：支付宝流水号，支付宝转账金额，支付宝转账时间，支付宝关联还款计划金额，实还本金，实还利息，实还服务费，实还罚息，其他收益，还款确认时间，还款计划ID，资金方
白条表头：支付宝流水号，支付宝转账金额，支付宝转账时间，支付宝关联还款计划金额，实还本金，实还利息，实还服务费，实还罚息，其他收益，还款确认时间，还款计划ID
注意：订单均为成功订单
'''
#流水转账时间
trans_start_time=datetime.date(2015,1,1)
trans_end_time = datetime.date(2017,1,1)
#信用钱包业务确认时间
xyqb_start_time=datetime.date(2017,5,1)
xyqb_end_time = datetime.date(2017,7,1)

path='E:/'

engine_new_transaction = create_engine('mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/new_transaction?charset=utf8', echo=False).connect()

sql_xjd='''
select t2.business_flow '支付宝流水号',t2.income '支付宝转账金额',t2.transfer_time '支付宝转账时间',
t1.ref_amount '支付宝关联还款计划金额',t1.principle-t1.mitigate_principle '实还本金',t1.interest-t1.mitigate_interest '实还利息',
t1.service_fee - t1.mitigate_service_fee '实还服务费',t1.punish - t1.mitigate_collection_relief '实还罚息',t1.remain_income '其他收益',
t3.xyqb_repaid_at '还款确认时间',t1.plan_id '还款计划ID',t5.name '资金方'
from xjd_repay_plan_repay_record_ref t1
join offline_alipay_record t2 on t1.record_id = t2.id and t1.repay_channel = 13 and t2.transfer_time >= '%s' and t2.transfer_time < '%s'
join user_repayment_plan t3 on t1.plan_id = t3.id and t3.xyqb_repaid_at >= '%s' and t3.xyqb_repaid_at < '%s'
join audit.loan_manifest t4 on t3.ref_id = t4.ref_id and t4.is_active = 1
join funding_corp t5 on t3.fund_code = t5.id
''' % (trans_start_time,trans_end_time,xyqb_start_time,xyqb_end_time)

df_xjd=pd.read_sql(sql_xjd,engine_new_transaction)
df_xjd.to_excel(path+u'现金贷支付宝差异.xlsx',columns=df_xjd.columns,index=None)


sql_bt='''
select t2.business_flow '支付宝流水号',t2.income '支付宝转账金额',t2.transfer_time '支付宝转账时间',
t1.ref_amount '支付宝关联还款计划金额',t1.principle-t1.mitigate_principle '实还本金',t1.interest-t1.mitigate_interest '实还利息',
t1.service_fee - t1.mitigate_service_fee '实还服务费',t1.punish - t1.mitigate_collection_relief '实还罚息',t1.remain_income '其他收益',
t3.biz_repaid_at '还款确认时间',t1.plan_id '还款计划ID'
from baitiao_repay_plan_repay_record_ref t1
join offline_alipay_record t2 on t1.record_id = t2.id and t1.repay_channel = 13 and t2.transfer_time >= '%s' and t2.transfer_time < '%s'
join user_bt_repayment_plan t3 on t1.plan_id = t3.id and t3.biz_repaid_at >= '%s' and t3.biz_repaid_at < '%s'
join baitiao_audit.baitiao_order t4 on t3.ref_id = t4.ref_id and t4.is_active = 1
''' % (trans_start_time,trans_end_time,xyqb_start_time,xyqb_end_time)

df_bt = pd.read_sql(sql_bt,engine_new_transaction)
df_bt.to_excel(path+u'白条支付宝差异.xlsx',columns=df_bt.columns,index=None)



