# -*- coding: utf-8 -*-
# author WuKong
import pandas as pd
from sqlalchemy import create_engine
import numpy as np
import openpyxl as px
from os import listdir, sys
from os.path import isfile, join, splitext, basename

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

engine_audit = create_engine('mysql+mysqldb://bo.wang:#mn89453YU9io80@172.16.3.201:3306/audit?charset=utf8', echo=True)
engine_xyqb = create_engine('mysql+mysqldb://bowang:in78u6ytgbhj56r@192.168.4.6:8066/xyqb?charset=utf8', echo=True)
engine_paycenter = create_engine('mysql+mysqldb://bowang:in78u6ytgbhj56r@192.168.4.6:8066/payment_center?charset=utf8',
                                 echo=True)
fund_loan_account_dict = {0: 1, 1: 3, 2: 1, 3: 1, 4: 1, 5: 1, 29: 29, 50: 1, 70: 1, 80: 1, 90: 90, 100: 100, 110: 1,
                          120: 1, 130: 1, 135: 2, 140: 140, 141: 1, 150: 150, 160: 160, 170: 2, 180: 180, 190: 2,
                          200: 200, 210: 210, 230: 230, 240: 240, 250: 2, 251: 2, 260: 2, 270: 6, 290: 290, 300: 300,
                          310: 310, 320: 311, 330: 330, 340: 340, 350: 350}

fund_source_fund_dict = {330: 210}

sql_query_loan_manifest = """
SELECT * FROM loan_manifest WHERE ref_id in (%s);
"""

sql_payment_center = """
SELECT order_id, merchant_order_no loan_id FROM pay_order WHERE  order_id IN %s
"""


sql_audit_query_max_ref_id = """select max(ref_id)+1 'max_ref_id' from loan_manifest;"""


ref_id_list = ["6165354","6165355","6165356","6165357","6165358","6165359","6165360","6165361","6165362","6165363","6165364","6165365","6165366","6165367","6165368","6165369","6165370","6165371","6165372","6165373","6165374","6165375","6165376","6165377","6165378","6165379","6165380","6165381","6165382","6165383","6165384","6165385","6165386","6165387","6165388","6165389","6165390","6165391","6165392","6165393","6165394","6165395","6165396","6165397","6165398","6165399","6165400","6165401","6165402","6165403","6165404","6165405","6165406","6165407","6165408","6165409","6165410","6165411","6165412","6165413","6165414","6165415","6165416","6165417","6165418","6165419","6165420","6165421","6165422","6165423","6165424","6165425"]


path = u"""C:\\Users\\wangbo\\Desktop\\11月份对账\\放款数据\\九月份跨月退款.xlsx"""
df_refund_data = pd.read_excel(path)
order_id_list = df_refund_data.order_id.astype(str).tolist()
df_pay_center = pd.read_sql(sql=sql_payment_center % str(tuple(order_id_list)),con=engine_paycenter)
df_pay_center.order_id = df_pay_center.order_id.astype(str)
df_refund_data.order_id = df_refund_data.order_id.astype(str)
df_merge = pd.merge(df_pay_center,df_refund_data,on=['order_id'],how='inner')


df_manifest = pd.read_sql(sql=sql_query_loan_manifest % ("'" + "','".join(ref_id_list) + "'"), con=engine_audit)
df_manifest.drop(['ref_id','loan_paid_at','id'], inplace=True, axis=1)
df_manifest['contract_loan_amount']= -df_manifest['contract_loan_amount']
df_manifest['real_loan_amount']= -df_manifest['real_loan_amount']
df_manifest['per_term_service_fee']= -df_manifest['per_term_service_fee']
df_manifest['qg_one_time_service_fee']= -df_manifest['qg_one_time_service_fee']
df_manifest['funding_one_time_service_fee']= -df_manifest['funding_one_time_service_fee']
df_manifest['monthly_required_repayment_amount']= -df_manifest['monthly_required_repayment_amount']

df_manifest = pd.merge(df_manifest,df_merge[['loan_id','loan_paid_at']],on=['loan_id'],how='inner')
df_audit_max_ref_id = pd.read_sql(sql=sql_audit_query_max_ref_id, con=engine_audit)
max_ref_id = df_audit_max_ref_id['max_ref_id'][0]
if max_ref_id == None:
    max_ref_id = 1
df_manifest.insert(1, 'ref_id', range(max_ref_id, max_ref_id + len(df_manifest)))
df_manifest.to_sql(name='loan_manifest', con=engine_audit, if_exists='append', index=None,chunksize=10000)
print "finish insert loan_manifest"
