import pandas as pd
import numpy as np
import datetime
from mvp import xgbreport
from data.analyis import datacal
# from mvp import dhb
from data.samples import dhb,sample

if __name__ == '__main__':
    # features=[
    #     'third_data_source#xy_pan_newapplyAcredibility',
    #     'third_data_source#xy_pan_newapplyAscore',
    #     'third_data_source#xy_pan_newconsfinAavgAlimit',
    #     'third_data_source#xy_pan_newconsfinAcredibility',
    #     'third_data_source#xy_pan_newconsfinAcreditAlimit',
    #     'third_data_source#xy_pan_newconsfinAmaxAlimit',
    #     'third_data_source#xy_pan_newconsfinAorgAcountq',
    #     'third_data_source#xy_pan_newconsfinAorgAcountx',
    #     'third_data_source#xy_pan_newconsfinAproductAcount',
    #     'third_data_source#xy_pan_newhistoryAfailAfee',
    #     'third_data_source#xy_pan_newhistoryAsucAfee',
    #     'third_data_source#xy_pan_newlatestAoneAmonthAfail',
    #     'third_data_source#xy_pan_newlatestAoneAmonthAsuc',
    #     'third_data_source#xy_pan_newlatestAoneAmonthd',
    #     'third_data_source#xy_pan_newlatestAoneAmonthj',
    #     'third_data_source#xy_pan_newlatestAqueryAtime',
    #     'third_data_source#xy_pan_newlatestAsixAmontha',
    #     'third_data_source#xy_pan_newlatestAsixAmonthv',
    #     'third_data_source#xy_pan_newlatestAthreeAmonthb',
    #     'third_data_source#xy_pan_newlatestAthreeAmonthf',
    #     'third_data_source#xy_pan_newloansAavgAlimit',
    #     'third_data_source#xy_pan_newloansAcashAcount',
    #     'third_data_source#xy_pan_newloansAcount',
    #     'third_data_source#xy_pan_newloansAcredibilityh',
    #     'third_data_source#xy_pan_newloansAcredibilitys',
    #     'third_data_source#xy_pan_newloansAcreditAlimit',
    #     'third_data_source#xy_pan_newloansAlatestAtime',
    #     'third_data_source#xy_pan_newloansAlongAtime',
    #     'third_data_source#xy_pan_newloansAmaxAlimit',
    #     'third_data_source#xy_pan_newloansAorgAcounta',
    #     'third_data_source#xy_pan_newloansAorgAcountg',
    #     'third_data_source#xy_pan_newloansAoverdueAcount',
    #     'third_data_source#xy_pan_newloansAproductAcount',
    #     'third_data_source#xy_pan_newloansAscore',
    #     'third_data_source#xy_pan_newloansAsettleAcount',
    #     'third_data_source#xy_pan_newqueryAcashAcount',
    #     'third_data_source#xy_pan_newqueryAfinanceAcount',
    #     'third_data_source#xy_pan_newqueryAorgAcount',
    #     'third_data_source#xy_pan_newqueryAsumAcount'
    # ]
    # dhb = dhb.dhb(start_time_period='2019-01-19 11:00:00',end_time_period='2019-01-20 12:00:00')
    # df=dhb.dhb_features_extract()
    # print(df.columns.tolist())
    # print(df.target.unique())
    # label='target'
    # features=dhb.get_feature()
    # df[features]=df[features].astype(float)
    # df['target']=df['target'].astype(int)
    # print('----feature---',len(features))
    # df=pd.read_csv('test.csv')
    #== 模型名称
    model_name='dhb'
    #== 目标是15天
    passdue_day=15
    df_log=sample.get_last_record(model_name)
    if df_log.shape[0]==1:
        start_date,end_date=sample.cal_sample_date(df_log.max_date[0],passdue_day)
    else:
        start_date, end_date = sample.cal_sample_date(passdue_day=passdue_day)
    start_date='2019-01-01'
    end_date='2019-01-10'
    print(start_date,end_date)
    df_sample=dhb.query_sample(start_date,end_date)
    df_sample['applied_at'] = pd.to_datetime(df_sample['applied_at'])
    df_sample['label']=1
    df_sample.loc[df_sample.passdue_day >= passdue_day,'label']=0
    dftrain,dftest=datacal.split_train_val(df_sample,trainsplit='timeSeries',trainsplitRatio=0.8,sort_col='applied_at')
    # 记录样本信息
    # sample.save_model_record(model_name,min_date=df_sample.applied_at.min(),max_date=df_sample.applied_at.max(),sample_cnt=df_sample.shape[0],
    #                          train_min_date=dftrain.applied_at.min(),train_max_date=dftrain.applied_at.max(),train_cnt=dftrain.shape[0],
    #                          test_min_date=dftest.applied_at.min(),test_max_date=dftest.applied_at.max(),test_cnt=dftest.shape[0])
    #== xgboost gbtree
    xgbreport.report(dftrain,dftest,dhb.get_feature(),'label','','xgboost_%s.doc' % datetime.datetime.now().date().strftime('%y%m%d'),kfold=2)
