# -*- 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'E:/loan_15_16/'
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',lm.ref_id 'ref_id', lm.`user_biz_id` '用户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_required_repayment_amount '每期还款额',lm.monthly_interest_rate '月利率'
FROM loan_manifest lm 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'ref_id', u'用户id', u'放款日期', u'放款金额', u'放款账户', u'本金（债权）', u'一次性手续费（QG）', u'一次性手续费（点滴）',
           u'贷款总期数', u'每期手续费', u'每期还款额', u'月利率', u'第1期应还款额', u'客户应还款日-第1期', u'第2期应还款额', u'客户应还款日-第2期', u'第3期应还款额',
           u'客户应还款日-第3期',
           u'第4期应还款额', u'客户应还款日-第4期', u'第5期应还款额', u'客户应还款日-第5期', u'第6期应还款额', u'客户应还款日-第6期']


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

con_audit_audit = create_engine(
    'mysql+mysqldb://yulong_rw:TouBStYwN8wkdxVt@172.16.3.201:3306/all_back_0430?charset=utf8', echo=True)
for year in years:
    for mon in months:
        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)

            if mon == 12:
                print sql_loan % (year, mon, year + 1, 1, fund_code)
                df_loan_gen = pd.read_sql(sql=sql_loan % (year, mon, year + 1, 1, fund_code), con=con_audit,
                                          chunksize=chunksize)
            else:
                print sql_loan % (year, mon, year, mon + 1, fund_code)
                df_loan_gen = pd.read_sql(sql=sql_loan % (year, mon, year, mon + 1, fund_code), con=con_audit,
                                          chunksize=chunksize)
            df_loan = pd.DataFrame()
            for tmp in df_loan_gen:
                df_loan = df_loan.append(tmp, ignore_index=True)
            # 检查数据，可以不要
            # print df_loan.info()
            # 检查是否有重复的数据
            if len(df_loan) < 1:
                continue
            print '删除重复前', len(df_loan['loan_id'])
            print '删除重复后', len(df_loan['loan_id'].drop_duplicates())
            df_loan['loan_id'] = df_loan['loan_id'].astype(str)
            df_loan['ref_id'] = df_loan['ref_id'].astype(str)
            ref_ids = tuple(df_loan['ref_id'])

            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)

            # 验证数据是否有丢失
            print '放款记录汇总长度：', len(df_loan)
            df_loan.to_excel(file_path + u'现金贷放款表明细%s年-%s月-%s.xlsx' % (year, mon, fund_code), index=None,
                            columns=columns)
