# -*- coding:utf-8 -*-
# Author: WuKong

import sys
import datetime
import itertools
import pandas as pd
from sqlalchemy import create_engine
import multiprocessing as mtp
import os

path = u"./"

engine_con = create_engine(
    'mysql+mysqldb://internal_r:ArbNgtvlJzZHXsEu@172.16.3.201:3306/all_back_0930?charset=utf8',
    echo=True)

sql_query_bohai = """
SELECT
	t1.loan_id loan_id,
	t2.term_no '期数',
  t2.repayment_plan_id 'repayment_plan_id',
	t3.ref_amount '还款金额',
  (t3.principle-t3.mitigate_principle) '实还本金',
  (t3.interest-t3.mitigate_interest) '实还利息',
  (t3.punish-t3.mitigate_collection_relief) '实还罚息',
  (t3.service_fee-t3.mitigate_service_fee) '实还服务费',
  t4.repaid_at '还款时间',
  t5.approach_name '还款渠道',
  t6.`name` '资金方'

FROM
	loan_manifest t1
JOIN user_repayment_plan t2 ON t1.ref_id = t2.ref_id AND t1.funding_code in (%s)
JOIN xjd_repay_plan_repay_record_ref t3 ON t2.id = t3.plan_id
JOIN test_mzh.repay_record_amount t4 ON t3.record_id = t4.record_id AND t3.repay_channel = t4.repay_channel
JOIN repay_channel t5 ON t3.repay_channel = t5.id
JOIN basic.funding_corp t6 ON t1.funding_code = t6.funding_code
"""

bohai_list = [320,410,270,310]
for funding_code in bohai_list:
    df_bohai = pd.read_sql(sql=sql_query_bohai % (funding_code,),con=engine_con)
    df_bohai.to_excel(path + u'渤海_%s' % funding_code + '.xlsx', columns=df_bohai.columns, index=None, encoding='utf8')