import pandas as pd
import numpy as np
from datetime import datetime
from datetime import timedelta

def cal_gps_score(df):
    '''在获取信号，优、良、合格、掉线的比例之后，计算gps的总评分'''
    score=0
    for index in range(len(df)):
        time_percent=df.loc[index,'累计时间占比']
        if df.loc[index,'GPS质量']=='优':
            score+=time_percent*0
        elif df.loc[index,'GPS质量']=='良':
            score+=time_percent*0.3
        elif df.loc[index,'GPS质量']=='合格':
            score+=time_percent*0.5
        elif df.loc[index,'GPS质量']=='掉线':
            score+=time_percent*1
    return (1-score)*100

def gps_rank(df_gps_signal_table,df_gps,signal_rank,dist_factor):
    '''gps信号质量分析函数,需要输入表格，df_gps,信号等级，权重'''
    gps_signal_condition=(df_gps['gps_signal']==signal_rank)
    dist=df_gps.loc[gps_signal_condition,'distance'].values.sum()
    deltatime=df_gps.loc[gps_signal_condition,'deltatime'].values.sum()
    df_gps_signal_table_condition=(df_gps_signal_table['gps_signal']==signal_rank)
    df_gps_signal_table.loc[df_gps_signal_table_condition,'accum_distance']=dist/1000
    df_gps_signal_table.loc[df_gps_signal_table_condition,'accum_deltatime']=deltatime
    df_gps_signal_table.loc[df_gps_signal_table_condition,'accum_distance_factor']=dist/1000*dist_factor
    return df_gps_signal_table

def get_df_gps_score(starttime,endtime,df_gps):
    '''对df_gps中的gps质量进行评分，返回一个数值'''
    test_start_time=starttime#'2021-05-29 17:16:39'
    test_end_time=endtime#'2021-05-29 20:08:08'

    test_time_condition=(df_gps['time']>test_start_time)&(df_gps['time']<test_end_time)
    df_gps_test=df_gps.loc[test_time_condition,:].copy()
    df_gps_test=df_gps_test.reset_index(drop=True)#重置index
    #按照deltatime打标签
    gps_deltatime_bins=[0,30,60,120,10000]#优-良-合格-掉线
    name=['优','良','合格','掉线']
    df_gps_test['gps_signal']=pd.cut(df_gps_test['deltatime'], gps_deltatime_bins,labels=name)
    df_gps_test['gps_signal'].value_counts()
    #声明一个gps信号按类别统计table
    df_gps_signal_table=pd.DataFrame()
    df_gps_signal_table['gps_signal']=df_gps_test['gps_signal'].value_counts().index.tolist()
    df_gps_signal_table['num']=df_gps_test['gps_signal'].value_counts().values.tolist()

    #分类进行统计
    df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'优',1.00)
    df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'良',1.05)
    df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'合格',1.2)
    df_gps_signal_table=gps_rank(df_gps_signal_table,df_gps_test,'掉线',1)

    #次数占比，时间占比
    all_num=df_gps_signal_table['num'].sum()
    df_gps_signal_table['num_percent']=df_gps_signal_table['num']/all_num
    all_accum_deltatime=df_gps_signal_table['accum_deltatime'].sum()
    df_gps_signal_table['accum_deltatime_percent']=df_gps_signal_table['accum_deltatime']/all_accum_deltatime

    #选择参数
    df_gps_signal_table=df_gps_signal_table[['gps_signal','num','num_percent','accum_distance',
                                            'accum_distance_factor','accum_deltatime','accum_deltatime_percent']]
    df_gps_signal_table=df_gps_signal_table.rename(columns={'gps_signal':'GPS质量','num':'数量','num_percent':'数量占比',
                                                        'accum_distance':'累计里程','accum_distance_factor':'累计里程修正值',
                                                        'accum_deltatime':'累计时间','accum_deltatime_percent':'累计时间占比'})

    df_gps_signal_table.loc[:,['GPS质量','累计时间','累计时间占比']]
    gps_score=cal_gps_score(df_gps_signal_table)#调用函数计算gps评分
    
    #输出结果，评分
    #print('From '+test_start_time+'  to '+test_end_time)
    #print('GPS信号质量评分:'+str(gps_score))

    return gps_score

