# -*- coding: utf-8 -*-
# author WuKong
import pandas as pd
from sqlalchemy import create_engine
import numpy as np
import openpyxl as px
from os import listdir, sys
from os.path import isfile, join, splitext, basename

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

engine_audit = create_engine('mysql+mysqldb://yulong:ohVU1nPITynSZwR2@172.16.3.201:3306/audit?charset=utf8', echo=True)
engine_xyqb = create_engine('mysql+mysqldb://bowang:in78u6ytgbhj56r@192.168.4.6:8066/xyqb?charset=utf8', echo=True)
engine_paycenter = create_engine('mysql+mysqldb://bowang:in78u6ytgbhj56r@192.168.4.6:8066/payment_center?charset=utf8',
                                 echo=True)
fund_loan_account_dict = {0: 1, 1: 3, 2: 1, 3: 1, 4: 1, 5: 1, 29: 29, 50: 1, 70: 1, 80: 1, 90: 90, 100: 100, 110: 1,
                          120: 1, 130: 1, 135: 2, 140: 140, 141: 1, 150: 150, 160: 160, 170: 2, 180: 180, 190: 2,
                          200: 200, 210: 210, 230: 230, 240: 240, 250: 2, 251: 2, 260: 2, 270: 6, 290: 290, 300: 300,
                          310: 310, 320: 311, 330: 330, 340: 340, 350: 350}

fund_source_fund_dict = {330: 210}
'''
17406567
SELECT MAX(id) FROM loan_manifest ;
5919163
SELECT MAX(ref_id) FROM loan_manifest;
'''
sql_xyqb_loan_manifest = """SELECT loan_application_history_id "loan_id",funding_corp_id "funding_code",contract_loan_amount "contract_loan_amount", real_loan_amount "real_loan_amount", contract_term "contract_term",
	monthly_interest_rate "monthly_interest_rate",
	service_fee_per_term "per_term_service_fee",
	one_time_service_fee "qg_one_time_service_fee",
	monthly_repayment "monthly_required_repayment_amount",
	0 "is_assignment_of_debt",
	1 "is_active",
	funding_corp_id "loan_account_id",
	funding_corp_id "source_corp",
	user_id "user_biz_id",
	company_income "funding_one_time_service_fee"
FROM
	loan_application_manifest_history
WHERE transaction_status in (2,5) AND
	loan_application_history_id IN (%s)"""

sql_xyqb_waiting_funding = """
SELECT request_id,loan_application_history_id loan_id FROM waiting_funding_corp_operate_people WHERE request_id in (%s)
"""

sql_payment_center = """
SELECT order_id, merchant_order_no loan_id FROM pay_order WHERE pay_status=3 AND  order_id IN (%s)
"""

sql_payment_center_temp = """
SELECT order_id, merchant_order_no loan_id FROM pay_order WHERE  order_id IN ("1504690217629")
"""
# 跨月退款 不考虑放款状态
sql_payment_center_ignore = """
SELECT order_id, merchant_order_no loan_id FROM pay_order WHERE  order_id IN (%s)
"""

sql_audit_query_max_ref_id = """select max(ref_id)+1 'max_ref_id' from loan_manifest;"""

sql_audit_query_loan_id = """SELECT id,loan_id FROM loan_manifest WHERE loan_id in (%s)"""

# dir_path = "E:/loan_test/"
dir_path = u"C:\\Users\\wangbo\\Desktop\\十月份对账\\放款数据\\处理数据"
# 晋商2 300 company_income
# 债转
allfiles = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]
for file_name in allfiles:
    print "start handle file_name=%s" % file_name
    # 债转之后的资金方id
    funding_code = int(file_name[:file_name.index('.')])
    df = pd.read_excel(join(dir_path, file_name))
    if 'request_id' in df.columns:
        df.rename(columns={u'放款金额': 'contract_loan_amount_detail', u'放款日期': 'loan_paid_at'},
                  inplace=True)
        df['request_id'] = df['request_id'].astype(str)
        request_id_query = "'" + "','".join(df['request_id'].astype(str).tolist()) + "'"
        df_xyqb_loan_id = pd.read_sql(sql=sql_xyqb_waiting_funding % request_id_query, con=engine_xyqb)
        df_xyqb_loan_id['request_id'] = df_xyqb_loan_id['request_id'].astype(str)
        df = pd.merge(df, df_xyqb_loan_id, how='inner', on=['request_id'])
        del df['request_id']
    elif 'order_id' in df.columns:
        df.rename(columns={u'放款金额': 'contract_loan_amount_detail', u'放款日期': 'loan_paid_at'},
                  inplace=True)
        order_id_query = "'" + "','".join(df['order_id'].astype('str').tolist()) + "'"
        if funding_code > 0:
            df_pay_center = pd.read_sql(sql_payment_center % order_id_query, con=engine_paycenter)
            # df_pay_center = pd.read_sql(sql_payment_center_temp, con=engine_paycenter)
        else:
            df_pay_center = pd.read_sql(sql_payment_center_ignore % order_id_query, con=engine_paycenter)
        df.order_id = df.order_id.astype(str)
        df_pay_center.order_id = df_pay_center.order_id.astype(str)
        df = pd.merge(df, df_pay_center, how='left', on='order_id')
        # df = pd.merge(df, df_pay_center, how='inner', on='order_id')
        df.fillna(0, inplace=True)
        df.loan_id = df.loan_id.astype(int)
        print "放款未成功的订单： " + '","'.join(df.ix[df.loan_id == 0, 'order_id'].astype(str).tolist())
        df = df.ix[df.loan_id > 0]
        df.drop(['order_id'], inplace=True, axis=1)

    else:
        df.rename(columns={u'借款订单号': 'loan_id', u'放款金额': 'contract_loan_amount_detail', u'放款日期': 'loan_paid_at'},
                  inplace=True)

    loan_id_query = "'" + "','".join(df['loan_id'].astype('str').tolist()) + "'"
    sql_xyqb_query = sql_xyqb_loan_manifest % loan_id_query
    # 检查loan_id是否已经录入进去过
    df_audit_exist_loan = pd.read_sql(sql=sql_audit_query_loan_id % loan_id_query, con=engine_audit)
    if len(df_audit_exist_loan) > 0:
        print "exist loan_id: " + ",".join(df_audit_exist_loan['loan_id'].astype('str').tolist())
        continue
    df_xyqb_loan_manifest = pd.read_sql(sql=sql_xyqb_query, con=engine_xyqb)
    df_audit_loan_manifest = df_xyqb_loan_manifest.merge(df, on='loan_id', how='left')
    df_audit_loan_manifest.fillna(0, inplace=True)
    # 如果是多个资金方的合并文件 则不做处理赋值
    if funding_code > 0:
        df_audit_loan_manifest['funding_code'] = funding_code
    df_audit_loan_manifest['contract_loan_amount_detail'] = df_audit_loan_manifest[
        'contract_loan_amount_detail'].astype(int)
    df_audit_loan_manifest['contract_loan_amount'] = df_audit_loan_manifest['contract_loan_amount'].astype(int)
    df_diff_audit_loan_amount = df_audit_loan_manifest.ix[
        df_audit_loan_manifest['contract_loan_amount_detail'] != df_audit_loan_manifest['contract_loan_amount']]

    if len(df_diff_audit_loan_amount) > 0:
        print "exist contract_loan_amount different data"
        print ",".join(df_diff_audit_loan_amount['loan_id'].astype('str').tolist())
    else:
        # 1 is the axis number (0 for rows and 1 for columns.)
        df_audit_loan_manifest = df_audit_loan_manifest.drop('contract_loan_amount_detail', 1)
        if funding_code == 300:
            # 晋商2 有砍头息
            df_audit_loan_manifest['real_loan_amount'] = df_audit_loan_manifest['contract_loan_amount'] - \
                                                         df_audit_loan_manifest['funding_one_time_service_fee']
        else:
            df_audit_loan_manifest['funding_one_time_service_fee'] = int(0)

        # 债转的标识符
        df_audit_loan_manifest['is_assignment_of_debt'] = df_audit_loan_manifest['is_assignment_of_debt'].astype(int)
        if funding_code > 0:
            df_audit_loan_manifest.ix[df_audit_loan_manifest.source_corp != funding_code, 'is_assignment_of_debt'] = 1
        elif funding_code == -1:
            # 跨月退款
            df_audit_loan_manifest['is_active'] = -2

        df_audit_max_ref_id = pd.read_sql(sql=sql_audit_query_max_ref_id, con=engine_audit)
        max_ref_id = df_audit_max_ref_id['max_ref_id'][0]
        if max_ref_id == None:
            max_ref_id = 1
        df_audit_loan_manifest.insert(1, 'ref_id', range(max_ref_id, max_ref_id + len(df_audit_loan_manifest)))
        df_audit_loan_manifest.to_sql(name='loan_manifest', con=engine_audit, if_exists='append', index=None,
                                      chunksize=10000)

    print "finish handle file_name=%s" % file_name
