# -*- 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_new_test_wangbo = create_engine(
    'mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/audit?charset=utf8',
    echo=True)


dir_path = u"""C:\\Users\\wangbo\\Desktop\\十月份对账\\settle_data\\"""

sql_update_settle = """
UPDATE  audit.funding_repayment_record SET settle_status=1 , settle_date=%s WHERE loan_id=%s AND term_no=%s limit 1;
"""
allfiles = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]
df_all = pd.DataFrame()
for file_name in allfiles:
    print "start handle file_name=%s" % file_name
    df_settle_data = pd.read_excel(dir_path+file_name)
    df_settle_data.loan_id = df_settle_data.loan_id.astype(str)
    df_all = pd.concat([df_settle_data,df_all])
    update_values = df_settle_data[['settle_date', 'loan_id','term_no']].values.tolist()
    engine_new_test_wangbo.execute(sql_update_settle,update_values)

# df_all['settle_date_month'] = pd.to_datetime(df_all['settle_date']).apply(lambda x: x.date().month)
# df_all = df_all.groupby(['settle_date_month']).count()
# df_all.head()