# -*- coding: utf-8 -*-
import os
import sys
import datetime
import threading
from itertools import product
from threading import Thread

import pandas as pd
from dateutil.relativedelta import relativedelta

from sqlalchemy import create_engine
import multiprocessing as mtp
reload(sys)
sys.setdefaultencoding('utf8')

path='./'

sql_1='''
SELECT '新马上' fund_name,t1.loan_id,t1.loan_paid_at,t2.amount,t1.repay_amt,t1.repay_principle,t1.repay_intrest,t1.fund_service_fee,t1.late_fee,t1.service_fee,t1.penalty_fee
,t1.repay_date
FROM `new_ms_transaction` t1
JOIN `new_ms_pay` t2 ON t1.loan_id = t2.loan_id
where t1.repay_date >= '%s' and t1.repay_date < '%s'
'''
engine_new_transaction = engine_new_transaction = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/new_transaction?charset=utf8',
    echo=True)


if __name__ == '__main__':
    start_time = datetime.datetime(2017, 1, 1, 0, 0, 0)
    end_time = datetime.datetime(2017, 4, 1, 0, 0, 0)
    df=pd.read_sql(sql_1 % (start_time,end_time),engine_new_transaction)
    df.rename(columns={'loan_id':'放款id','loan_paid_at':'放款时间','repay_date':'实还款日','amount':'放款金额',
                       'repay_principle':'实还本金','repay_intrest':'实还利息','fund_service_fee':'实还资金方服务费',
                       'service_fee':'实还量化派服务费','penalty_fee':'逾期费','late_fee':'滞纳金','fund_name':'资金方',
                       'repay_amt':'实还款'},inplace=True)
    df.to_excel(path + str(2015) + '_' + str(2017) + '.xlsx', columns=df.columns, index=None, encoding='utf8')




