# -*- coding: utf-8 -*-
# # 开始匹配各个放款表
from threading import Thread

import pandas as pd
from sqlalchemy import create_engine
import numpy as np

pd.options.mode.chained_assignment = None
import sys

reload(sys)
sys.setdefaultencoding('utf8')

# --------------------配置变量-------------------------------
years = (2015, 2016)
months = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
fundcodes = (
    1, 0, 3, 2, 5, 29, 4, 141, 50, 70, 80, 90, 100, 120, 110, 130, 150, 170, 180, 140, 190, 200, 160, 210, 135, 230,
    240,
    250, 251, 260, 300, 290, 270, 330, 310, 320, 350, 340)


file_path = u'/vpants/svn_loan_xjd_txt/'
sql_funding_corp = """select funding_code 'funding_code', name '资金方' from audit.funding_corp;"""

sql_loan_account = """select id loan_account_id, accountName '放款渠道' from audit.loan_account;"""

sql_loan = """SELECT lm.funding_code 'funding_code',lm.loan_id 'loan_id',loan_paid_at '放款日期',lm.real_loan_amount '放款金额',lm.loan_account_id 'loan_account_id',lm.contract_loan_amount '本金（债权）',lm.qg_one_time_service_fee '一次性手续费（QG）',lm.funding_one_time_service_fee '一次性手续费（点滴）',lm.contract_term '贷款总期数',lm.per_term_service_fee '每期手续费',lm.monthly_interest_rate '月利率',luc.uuid '借款人id'
FROM loan_manifest lm JOIN temp.loan_uuid_class luc ON lm.ref_id = luc.ref_id WHERE lm.loan_paid_at>="%d-%d-01" AND lm.loan_paid_at<"%d-%d-01" AND lm.funding_code=%d AND lm.is_active in (1,-2)  ;"""

sql_plan = """SELECT ref_id ,term_no ,deadline ,required_repayment  FROM user_repayment_plan WHERE ref_id in %s """
chunksize = 10000
step = 3000
columns = [u'资金方', u'loan_id', u'放款日期', u'放款金额', u'放款渠道', u'本金（债权）', u'一次性手续费（QG）', u'一次性手续费（点滴）',
           u'贷款总期数', u'每期手续费', u'月利率',u'借款人id']


con_audit_audit = create_engine(
    'mysql+mysqldb://yulong_rw:TouBStYwN8wkdxVt@172.16.3.201:3306/all_back_0430?charset=utf8', echo=True)

for fund_code in fundcodes:
    df_fund_corp_gen = pd.read_sql(sql=sql_funding_corp, con=con_audit_audit, chunksize=chunksize)
    df_fund_corp = pd.DataFrame()
    for tmp in df_fund_corp_gen:
        df_fund_corp = df_fund_corp.append(tmp, ignore_index=True)

    df_loan_account_gen = pd.read_sql(sql=sql_loan_account, con=con_audit_audit, chunksize=chunksize)
    df_loan_account = pd.DataFrame()
    for tmp in df_loan_account_gen:
        df_loan_account = df_loan_account.append(tmp, ignore_index=True)

    print sql_loan % (2015, 1, 2017, 1, fund_code)
    df_loan_gen = pd.read_sql(sql=sql_loan % (2015, 1, 2017, 1, fund_code), con=con_audit_audit,
                              chunksize=chunksize)
    df_loan = pd.DataFrame()
    for tmp in df_loan_gen:
        df_loan = df_loan.append(tmp, ignore_index=True)
    if len(df_loan) < 1:
        continue
    df_loan['loan_id'] = df_loan['loan_id'].astype(str)
    df_loan['funding_code'] = df_loan['funding_code'].astype(int)
    df_loan['loan_account_id'] = df_loan['loan_account_id'].astype(int)
    df_loan = df_loan.merge(df_fund_corp, on='funding_code', how='left').merge(df_loan_account,
                                                                               on='loan_account_id', how='left')
    df_loan.drop(['funding_code', 'loan_account_id'], axis=1, inplace=True)
    df_loan[u'放款日期'] = df_loan[u'放款日期'].apply(lambda x: pd.datetime.strftime(x, '%Y-%m-%d %H:%M'))
    header_line = ','.join(df_loan.columns)
    # loan_id,放款日期,放款金额,本金（债权）,一次性手续费（QG）,一次性手续费（点滴）,贷款总期数,每期手续费,月利率,资金方,放款渠道
    # np.savetxt(file_path + 'xjd_loan_2015-2016_' +str(fund_code)+ '.txt', X=df_loan.values,
    #            fmt='%s|%s|%f|%f|%.2f|%.2f|%d|%.2f|%f|%s|%s|%s|', header=header_line, delimiter='|')
    df_loan.to_csv(file_path + 'xjd_loan_2015-2016_' +str(fund_code)+ '.csv')
