from LIB.BACKEND import DBManager
from LIB.MIDDLE.CellStateEstimation.Common import log
import datetime
import pandas as pd
import numpy as np
import os   
from DataPreProcessMGMC import DataPreProcess
from scipy import stats

#log信息配置
mylog=log.Mylog('log.txt','error')
mylog.logcfg()

dbManager = DBManager.DBManager()
dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='MGMC')
fileNames = dataSOH['sn']
fileNames = list(fileNames)

data_GMGC=[]
for k in range(len(fileNames)):

    try:    
        sn = fileNames[k]
        df_data = dbManager.get_data(sn=sn, start_time='2021-10-15 00:00:00', end_time='2021-11-06 00:00:00', data_groups=['bms','gps'])
        data_bms = df_data['bms']
        data_gps = df_data['gps']
        df_merge = pd.merge(data_bms, data_gps,how='outer',sort=True)
        df_merge['sn'] = sn
        df_sheetCat = DataPreProcess.data_split_by_status(DataPreProcess,df_merge, drive_interval_threshold=120, charge_interval_threshold=300, drive_stand_threshold=120, charge_stand_threshold=300)
        unique_status_idx = np.unique(df_sheetCat.data_split_by_status.values)
        data_result = pd.DataFrame()
        list_result = []
        for n in unique_status_idx:
            list_result.append(df_sheetCat[df_sheetCat.data_split_by_status==n]) 
        data_GMGC.append(list_result)

    except Exception as e:
        print(repr(e))
        mylog.logopt(sn,e)
        pass  

df_result=pd.DataFrame()
for k in range(len(data_GMGC)):
     
    maxI=[]
    modI=[]
    lenI=[]
    temp1_start=[]
    temp2_start=[]
    temp1_fin=[]
    temp2_fin=[]
    time_start=[]
    time_fin=[]
    time_drivefin=[]
    temp1_drivefin=[]
    temp2_drivefin=[]
    sn=[]
    
    for i in range(1,len(data_GMGC[k])):
       if data_GMGC[k][i]['data_status'].values[0]=='charge':
           
            maxI.append(max(data_GMGC[k][i]['总电流[A]'].values[:10]))
            modI.append(stats.mode(data_GMGC[k][i]['总电流[A]'].values[:10])[0][0])
            lenI.append(len(data_GMGC[k][i]['总电流[A]'].values))
            temp1_start.append(data_GMGC[k][i]['单体温度1'].values[0])
            temp2_start.append(data_GMGC[k][i]['单体温度2'].values[0])
            temp1_fin.append(data_GMGC[k][i]['单体温度1'].values[-1])
            temp2_fin.append(data_GMGC[k][i]['单体温度2'].values[-1])
            time_start.append(data_GMGC[k][i]['时间戳'].values[0])
            time_fin.append(data_GMGC[k][i]['时间戳'].values[-1])
            time_drivefin.append(data_GMGC[k][i-1]['时间戳'].values[0])
            temp1_drivefin.append(data_GMGC[k][i-1]['单体温度1'].values[0])
            temp2_drivefin.append(data_GMGC[k][i-1]['单体温度2'].values[0])           
            sn.append(data_GMGC[k][i]['sn'].values[0])
    dic={"初始充电电流max":maxI,"初始充电电流mod":modI,"电流长度":lenI,"初始充电温度1":temp1_start,"初始充电温度2":temp2_start,
        "结束充电温度1":temp1_fin,"结束充电温度2":temp2_fin,"初始充电时间":time_start,
        "结束充电时间":time_fin,"行驶结束时间":time_drivefin,"行驶结束温度1":temp1_drivefin,
        "行驶结束温度2":temp2_drivefin,"sn":sn}
    df_analyse=pd.DataFrame(dic)
    df_result=df_result.append(df_analyse)

data_analyse=df_result[df_result['电流长度'].values>5]

mean_temp1=[]
mean_temp2=[]
mean_temp3=[]
time_diff_stand=[]
time_diff_charge=[]

for i in range(len(data_analyse)):
    meantemp1=(data_analyse['初始充电温度1'].values[i]+data_analyse['初始充电温度2'].values[i])/2
    mean_temp1.append(meantemp1)
    meantemp2=(data_analyse['结束充电温度1'].values[i]+data_analyse['结束充电温度2'].values[i])/2
    mean_temp2.append(meantemp2)
    meantemp3=(data_analyse['行驶结束温度1'].values[i]+data_analyse['行驶结束温度2'].values[i])/2
    mean_temp3.append(meantemp3)
    td_charge=float(np.diff([data_analyse['初始充电时间'].values[i],data_analyse['结束充电时间'].values[i]])/np.timedelta64(1, 'm'))
    time_diff_charge.append(td_charge)
    td_stand=float(np.diff([data_analyse['行驶结束时间'].values[i],data_analyse['初始充电时间'].values[i]])/np.timedelta64(1, 'm'))
    time_diff_stand.append(td_stand) 
   
data_analyse['初始充电温度']=mean_temp1
data_analyse['结束充电温度']=mean_temp2
data_analyse['行驶结束温度']=mean_temp3
data_analyse['闲置时长']=time_diff_stand
data_analyse['充电时长']=time_diff_charge

diff_temp=[]
for i in range(len(data_analyse)):
    difftemp=data_analyse['结束充电温度'].values[i]-data_analyse['初始充电温度'].values[i]
    diff_temp.append(difftemp)
data_analyse['充电温差']=diff_temp
data_select=data_analyse[data_analyse['充电时长'].values>5]

