# -*- encoding: utf8 -*-
import sys
import datetime
import pandas as pd
from sqlalchemy import create_engine
from dateutil.relativedelta import relativedelta
import multiprocessing as mtp
from itertools import product

reload(sys)
sys.setdefaultencoding("utf-8")
pd.options.mode.chained_assignment = None


file_path = u'E:/量化派/去哪儿/常规出账/'
file_name = u'去哪儿净放款_%s_%s.xlsx'
engine_qunaer = create_engine('mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/qunaer_new?charset=utf8',
							  echo=False).connect()

year=2017
month=10
start_time=datetime.date(year,month,1)
end_time=start_time + relativedelta(months=+1)

sql_pay='''
select fund.fund_name 资金方,product_no 产品编号,pay.stages 期数,date(trans_time) 放款时间,loan_amount 放款金额 
from qunaer_new.pay_detail pay
JOIN qunaer_new.fund_corp fund on pay.fund_code = fund.fund_code
where status=1 and pay.fund_code != 1 AND trans_time >= '%s' AND trans_time <'%s'
''' % (start_time,end_time)

sql_refund='''
select fund.fund_name 资金方,t1.product_no 产品编号,t2.stages 期数,DATE(refund_time) 放款时间,refund_principle 放款金额
from qunaer_new.refund_detail t1 
JOIN qunaer_new.pay_detail t2 on t1.product_no = t2.product_no 
JOIN qunaer_new.fund_corp fund on t2.fund_code = fund.fund_code
WHERE t1.fund_code != 1 AND t2.status=1 AND t1.refund_status=1 AND refund_time >= '%s' AND refund_time < '%s'
''' % (start_time,end_time)

df_pay=pd.read_sql(sql_pay,engine_qunaer)
df_refund=pd.read_sql(sql_refund,engine_qunaer)
df_pay=pd.concat([df_pay,df_refund],ignore_index=True)
df_pay.to_excel(file_path+file_name % (year,month))

