import numpy as np
import pandas as pd
from pandas.core.frame import DataFrame
from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
from LIB.BACKEND import DBManager
import datetime,time

class Low_soc_alarm():
    def low_soc_alarm(param,df_bms,low_soc_bat_list,sn,df_OprtnSta):
        #if len(df_bms) and df_OprtnSta.loc[0,'status'] !=3:#0禁用 1正常 2故障 3返修 4 损毁 5丢失已赔偿，6丢失未赔偿:
        VoltageNum=['单体电压'+str(i) for i in range(1,param.CellVoltNums+1)]
        df_bms[VoltageNum]=df_bms[VoltageNum]/1000
        CellVol=df_bms[VoltageNum]
        PackCrnt=df_bms['总电流[A]']
        ZeroCrntCount=len(df_bms[abs(df_bms['总电流[A]'])<=1])
        CrntCount=len(PackCrnt)-ZeroCrntCount
        if CrntCount>0:
            ZeroCrntRate=(ZeroCrntCount/len(PackCrnt))*100
        else:
            ZeroCrntRate=100
        if ZeroCrntRate>99.85:#静置比大于99.85%
            ZeroSoc_Volt=np.interp(0,param.LookTab_SOC,param.LookTab_OCV)
            FiveSoc_Volt=np.interp(10,param.LookTab_SOC,param.LookTab_OCV)
            CellVolMean=DataFrame()
            CellVolMean=CellVol.mean()
            CellVolMin_Index=CellVolMean.idxmin()
            print(ZeroSoc_Volt)
            CellLowVolZERO=df_bms[df_bms[CellVolMin_Index] < ZeroSoc_Volt]
            CellLowVolFive=df_bms[df_bms[CellVolMin_Index] > FiveSoc_Volt]
            CellLowVolZeroCrnt=CellLowVolZERO[abs(CellLowVolZERO['总电流[A]'])<1]
            CellLowVolFiveCrnt=CellLowVolFive[abs(CellLowVolFive['总电流[A]'])<1]
            if len(CellLowVolZeroCrnt)>3 and len(CellLowVolFiveCrnt)<3:
                CellLowVolZeroCrnt=CellLowVolZeroCrnt.reset_index(drop=True)
                low_soc_bat_list.loc[len(low_soc_bat_list)]=[sn,CellLowVolZeroCrnt.loc[len(CellLowVolZeroCrnt)-1,'时间戳'],3]
        return low_soc_bat_list
