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

reload(sys)
sys.setdefaultencoding('utf8')

engine_transaction = create_engine(
    'mysql+mysqldb://lkbpayment_w:ySsTfEzCPIh2de4wbe5BqdEGes8YGPh9@172.16.90.7:3306/transaction?charset=utf8',
    echo=True)

engine_lkbadmin = create_engine(
    'mysql+mysqldb://lkbadmin_r:921Spm4irOLxI4aMwyVY1CRKrRCoYpg8jewUP2g10v0=@172.16.90.129:3306/lkbadmin?charset=utf8',
    echo=True)

'''
select loanId, startTime 申请时间,submitTime 提交审核时间,auditPassTime 审核通过时间,allocationFundsTime 分配资金方时间 from transaction.t_loanbill where  loanId in ("-33","532949","162117","225523","o3292","o8161","o30253","o69986") ORDER BY loanId
绑卡确认时间，打款时间
  select loan_id, userId, bindcard_confirm_time 绑卡确认时间,loanPaidAt 打款时间 from t_xyqb_transaction_o where loan_id in ("-33","532949","162117","225523","o3292","o8161","o30253","o69986") order by loan_id

拿到userId后，去lkbadmin库， 查询注册时间
select createDate from t_user where id in ("0a09f734-3413-4e5f-81ce-0400bd2e9f9d","19889a7f-8e39-4d1a-bb2c-a00d25f14c1f","7dc93f14-2c1f-41ce-ab9a-a346652b2acd","55589172-8981-4959-9489-42f78eaf79b9","89338f30-8c82-4c60-bf8b-71d2dac4219c")

'''

old_loanids = "('o7204', 'o7864', 'o28279', 'o28240', 'o25288', 'o30754', 'o30556', 'o808', 'o856', 'o2650', 'o30814')"

sql_t_loanbill = u"""
select loanId, startTime 申请时间,submitTime 提交审核时间,auditPassTime 审核通过时间,allocationFundsTime 分配资金方时间 from transaction.t_loanbill where  loanId in %s
"""

sql_t_xyqb_transaction_o = u"""
select loan_id, userId, bindcard_confirm_time 绑卡确认时间,loanPaidAt 放款时间 from t_xyqb_transaction_o where loan_id in %s
"""

sql_t_user = u"""
select id userId, createDate 注册时间 from t_user where id in %s
"""

pd_df_loanbill = pd.read_sql(sql=sql_t_loanbill % (old_loanids,), con=engine_transaction)

pd_df_loanbill['loanId'] = pd_df_loanbill['loanId'].astype(str)

pd_df_banktime = pd.read_sql(sql=sql_t_xyqb_transaction_o % (old_loanids,), con=engine_transaction)
pd_df_banktime['userId'] = pd_df_banktime['userId'].astype(str)
pd_df_banktime['loan_id'] = pd_df_banktime['loan_id'].astype(str)

pd_df_user = pd.read_sql(sql=sql_t_user % str(tuple(pd_df_banktime['userId'].tolist())), con=engine_lkbadmin)
pd_df_user['userId'] = pd_df_user['userId'].astype(str)

pd_df_new = pd_df_loanbill.merge(pd_df_banktime, left_on='loanId',right_on='loan_id',how='outer').merge(pd_df_user, on='userId',how='outer')
pd_df_new.drop(['userId','loanId'], axis=1, inplace=True)
excel_file = pd.ExcelWriter('老系统七个时间.xlsx')
pd_df_new.to_excel(excel_file,columns=['loan_id','注册时间', '申请时间', '提交审核时间', '审核通过时间', '分配资金方时间', '绑卡确认时间','放款时间'], index=None)
excel_file.save()
