# -*- coding: utf-8 -*-
import datetime
import sys
import multiprocessing as mtp
from itertools import product
import numpy as np
import pandas as pd
from sqlalchemy import create_engine
from dateutil.relativedelta import relativedelta
reload(sys)
sys.setdefaultencoding('utf8')


'''
目标：根据时间点，以 订单为维度，计算未还本金  即 放款本金 - 已还本金
@author linfang
脚本失败原因：如果你修改了数据源，可能是 sql 中涉及到的表不存在该数据源指定的库下，加上指定的库即可
脚本中放款+还款数据都是成功订单 即（is_active=1）,如果金额有差异，有可能是 失败订单有还款。。。。
注意：订单统一为成功订单（is_active = 1）
'''

#----------------配置变量项开始--------------------------

#TODO 文件输出路劲
path=u'E:/数据汇总/白条/汇总/'
#输出文件名
file_name=u'未还本金_2015'
#TODO 还款时间开始 --- 还款时间结束
start_time = '2015-01-01 00:00:00'
end_time = '2017-01-01 00:00:00'
#TODO  放款时间开始 --- 放款时间结束
fk_start_time='2015-01-01 00:00:00'
fk_end_time='2017-01-01 00:00:00'

#TODO 数据源
engine_new_transaction = create_engine(
    'mysql+mysqldb://yulong_rw:TouBStYwN8wkdxVt@172.16.3.201:3306/new_transaction?charset=utf8',
    echo=True)
#----------------配置变量项结束--------------------------




read_merchant_sql='''SELECT merchant_id,merchant_name FROM baitiao_audit.merchant'''
df_merchant=pd.read_sql(read_merchant_sql ,con=engine_new_transaction)
df_merchant['merchant_id']=df_merchant['merchant_id'].astype(int)
read_repay_sql='''
SELECT id,`approach_name` FROM new_transaction.`repay_channel`
'''
df_approach=pd.read_sql(read_repay_sql,engine_new_transaction)
df_approach['id']=df_approach['id'].astype(int)

read_fund_sql='''
SELECT id,fund_name FROM baitiao_audit.`fund_corp`
'''
df_fund = pd.read_sql(read_fund_sql,engine_new_transaction)
df_fund['id']=df_fund['id'].astype(int)


def fk(start_time,end_time):
    sql_1 = '''
        SELECT t4.ref_id,t4.funding_corp_id,t4.`merchantId`,t4.`order_no`,t4.`real_loan_amount`,t4.`contract_term`,t4.`loan_paid_at`
    FROM baitiao_audit.`baitiao_order` t4
    WHERE t4.real_loan_amount > 0  AND t4.`is_active` = 1 AND t4.loan_paid_at >= '%s' and t4.loan_paid_at < '%s'
        ''' % (start_time,end_time)
    return pd.read_sql(sql_1,engine_new_transaction)


def hk(start_time,end_time):
    sql_2_1='''
    SELECT t2.ref_id,SUM(t1.principle-t1.mitigate_principle) real_principle
FROM `baitiao_repay_plan_repay_record_ref` t1
JOIN `offline_alipay_record` t3 ON t1.`record_id` = t3.id AND t1.`repay_channel` = 13
JOIN `user_bt_repayment_plan` t2 ON t1.plan_id = t2.id
JOIN baitiao_audit.`baitiao_order` t4 ON t2.ref_id = t4.ref_id AND t4.`is_active` = 1
WHERE t3.transfer_time >= '%s' and t3.transfer_time < '%s'
GROUP BY t2.ref_id
    ''' % (start_time,end_time)
    sql_2_2 = '''
        SELECT t2.ref_id,SUM(t1.principle-t1.mitigate_principle) real_principle
    FROM `baitiao_repay_plan_repay_record_ref` t1
    JOIN `repay_record_online` t3 ON t1.`record_id` = t3.id AND t1.`repay_channel` < 13
    JOIN `user_bt_repayment_plan` t2 ON t1.plan_id = t2.id
    JOIN baitiao_audit.`baitiao_order` t4 ON t2.ref_id = t4.ref_id AND t4.`is_active` = 1
    WHERE t3.bill_time >= '%s' and t3.bill_time < '%s'
    GROUP BY t2.ref_id
        ''' % (start_time,end_time)
    sql_2_3='''
        SELECT t2.ref_id,SUM(t1.principle-t1.mitigate_principle) real_principle
FROM `baitiao_repay_plan_repay_record_ref` t1
JOIN `offline_bank_repay_record` t3 ON t1.`record_id` = t3.id AND t1.`repay_channel` in (14,15,16)
JOIN `user_bt_repayment_plan` t2 ON t1.plan_id = t2.id
JOIN baitiao_audit.`baitiao_order` t4 ON t2.ref_id = t4.ref_id AND t4.`is_active` = 1
WHERE t3.transfer_time >= '%s' and t3.transfer_time < '%s'
GROUP BY t2.ref_id
    ''' % (start_time,end_time)
    df_2_1=pd.read_sql(sql_2_1 ,engine_new_transaction)
    df_2_2=pd.read_sql(sql_2_2 ,engine_new_transaction)
    df_2_3 = pd.read_sql(sql_2_3 , engine_new_transaction)
    return pd.concat([df_2_1,df_2_2,df_2_3],axis=0,ignore_index=True)


def tj(df_fk,df_hk,filename):
    df_2 = df_hk.groupby(by='ref_id')['real_principle'].agg({'sum'}).reset_index().rename(
        columns={'sum': 'principle_sum'})
    print '----before---',len(df_2)
    df_2['ref_id'] = df_2['ref_id'].astype(int)
    df_fk['ref_id'] = df_fk['ref_id'].astype(int)
    df_2=df_2.ix[df_2.ref_id.isin(df_fk.ref_id)]
    print '----after---', len(df_2)
    df = pd.merge(df_fk, df_2, on='ref_id', how='left')
    df.fillna(0, inplace=True)
    df['diff'] = np.round(df['real_loan_amount'] - df['principle_sum'], 2)
    df = df.ix[df['diff'] != 0]
    df['merchantId'] = df['merchantId'].astype(int)
    df['funding_corp_id'] = df['funding_corp_id'].astype(int)
    df = pd.merge(df, df_merchant, left_on=['merchantId'], right_on='merchant_id', how='left')
    df = pd.merge(df, df_fund, left_on='funding_corp_id', right_on='id', how='left')
    df.rename(columns={'merchant_name': '商户', 'fund_name': '资金方', 'order_no': '订单号', 'real_loan_amount': '订单金额',
                       'contract_term': '合同期数',
                       'loan_paid_at': '放款时间', 'diff': '未还本金'}, inplace=True)
    df = df[['资金方', '商户', '订单号', '订单金额', '合同期数', '放款时间', '未还本金']]
    df.to_excel(path + filename+'.xlsx', columns=df.columns, index=None, encoding='utf8')


if __name__ == '__main__':
    pool = mtp.Pool(processes=2)
    df_fk=pool.apply_async(fk,(fk_start_time,fk_end_time))
    df_hk=pool.apply_async(hk,(start_time,end_time))
    pool.close()
    pool.join()
    tj(df_fk._value,df_hk._value,file_name)

print '======================main done==================================='
