# -*- 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,
	t2.loan_id,
	t2.loan_paid_at,
	t1.repay_date,
	t2.real_loan_amount,
	t1.actual_principle,
	t1.actual_interest,
	t1.actual_service_fee,
	t1.actual_repay_amount,
	t1.actual_overdue_interest
FROM
	yg_huishang_repay_term t1
JOIN loan_manifest t2 ON t1.ref_id = t2.ref_id WHERE repay_date>="%s" AND repay_date<"%s"
'''
engine_new_transaction = engine_new_transaction = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/all_back_0630?charset=utf8',
    echo=True)


if __name__ == '__main__':
    start_time = datetime.datetime(2017, 1, 1, 0, 0, 0)
    end_time = datetime.datetime(2017, 7, 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':'实还款日','real_loan_amount':'放款金额',
                       'actual_principle':'实还本金','actual_interest':'实还利息','actual_service_fee':'实还服务费'
        ,'actual_overdue_interest':'逾期费','fund_name':'资金方','actual_repay_amount':'实还款'},inplace=True)
    df.to_excel(path + str(1) + '_' + str(7) + '.xlsx', columns=df.columns, index=None, encoding='utf8')




