# -*- 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

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

engine_new_transaction = create_engine(
    'mysql+mysqldb://yulong_rw:TouBStYwN8wkdxVt@172.16.3.201:3306/new_transaction?charset=utf8',
    echo=True)
# 思路 按资金方出赔付表，如果有问题 按资金方修改
path = u"""C:/Users/wangbo/Desktop/八月份对账/数据库数据/"""
# 风险等级 Loan id 资金方 放款时间 应还款日 放款金额 应还资金方本息 客户实还可用本息
data_df_fund = pd.read_csv(path + "fund.csv")
data_df_fund.funding_code = data_df_fund.funding_code.astype(int)

data_df_user_fund = pd.read_csv(path + "user_fund.csv")
data_df_user_fund.funding_code = data_df_user_fund.funding_code.astype(int)

data_df_fund_150 = pd.read_csv(path + "150_fund.csv")
data_df_fund_150.funding_code = data_df_fund_150.funding_code.astype(int)

data_df_loan_class = pd.read_csv(path + "loan_class.csv")
data_df_loan_manifest = pd.read_csv(path + "loan_manifest.csv")
data_df_repay_bank = pd.read_csv(path + "repay_bank.csv")
data_df_repay_offline = pd.read_csv(path + "repay_offline.csv")
data_df_repay_online = pd.read_csv(path + "repay_online.csv")
data_df_repay_yghuishang = pd.read_csv(path + "repay_yghuishang.csv")
data_df_repay = pd.concat([data_df_repay_online, data_df_repay_offline, data_df_repay_bank])
data_df_repay = data_df_repay.ix[~data_df_repay.funding_code.isin([340, 160])]
# ===========================原始数据读取===================================

# 风险等级 loan_id 资金方 放款时间 应还日 放款金额 应还资金方本息 客户实还可用本息
# 270, 310, 320, 210,365,350 资金方应还取客户的还款计划
fund_code_list = [270, 310, 320, 210, 365, 350, 290, 330, 340]
df_fund_plan = data_df_user_fund.ix[data_df_user_fund.funding_code in fund_code_list]
df_fund_plan = pd.concat([df_fund_plan, data_df_fund.ix[data_df_fund.funding_code not in fund_code_list]])
df_repay = data_df_repay.ix[data_df_repay.funding_code in fund_code_list]
df_fund_plan.ref_id = df_fund_plan.ref_id.astype(int)
df_fund_plan['fund_principle_interest'] = df_fund_plan['principle']+df_fund_plan['interest']
df_repay['repay_principle_interest'] = df_repay['principle'] + df_repay['interest']
