import datetime
import pandas as pd
from LIB.BACKEND import DBManager, Log
from sqlalchemy import create_engine
import time, datetime
from apscheduler.schedulers.blocking import BlockingScheduler
from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload as DBDownload
from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
from pandas.core.frame import DataFrame
import datacompy
from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
from low_soc_alarm import Low_soc_alarm
def Low_Soc_Alarm():
    end_time=datetime.datetime.now()
    start_time=end_time-datetime.timedelta(days=1)
    start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
    end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
    #-------------------------读取数据-------------------------------------------------------------------
    excelpath=r'D:\ZLWORK\code\data_analyze_platform\USER\eric\SaftyCenter_CODE_V1\sn-20210903.xlsx'
    SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
    SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
    SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
    SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
    SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
    SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
    SNnums_6060=SNdata_6060['SN号'].tolist()
    SNnums_6040=SNdata_6040['SN号'].tolist()
    SNnums_4840=SNdata_4840['SN号'].tolist()
    SNnums_L7255=SNdata_L7255['SN号'].tolist()
    SNnums_C7255=SNdata_C7255['SN号'].tolist()
    SNnums_U7255=SNdata_U7255['SN号'].tolist()
    SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
    #SNnums=['MGMCLN750N215I009']
    #-------------------------执行循环----------------------------------------------------------------------
    low_soc_bat_list=DataFrame(columns=['sn','time','level'])
    for sn in SNnums:#SN遍历
        print(sn)
        if 'PK500' in sn:
            celltype=1 #6040三元电芯
        elif 'PK502' in sn:
            celltype=2 #4840三元电芯
        elif 'K504B' in sn:
            celltype=99    #60ah林磷酸铁锂电芯
        elif 'MGMLXN750' in sn:
            celltype=3 #力信50ah三元电芯
        elif 'MGMCLN750' or 'UD' in sn: 
            celltype=4 #CATL 50ah三元电芯
        else:
            print('SN：{}，未找到对应电池类型！！！'.format(sn))
            continue
            # sys.exit()
        param=QX_BatteryParam.BatteryInfo(celltype)   
        #读取原始数据库数据.....................................................................................
        dbManager = DBManager.DBManager()
        df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
        df_bms = df_data['bms']
        #读取电池当前运营状态.....................................................................................................................................................
        host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
        port=3306
        db='qixiang_manage'
        user='qx_query'
        password='@Qx_query'
        mode=4
        tablename1='py_battery'     
        DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
        with DBRead as DBRead:
            df_OprtnSta=DBRead.getdata('qrcode','status', tablename=tablename1, sn=sn, timename='', st='', sp='',factory='')#取最后一条运营状态
        
        
        
        #执行主程序-------------------------------------------------------------------------------------------
        low_soc_bat_list=Low_soc_alarm.low_soc_alarm(param,df_bms,low_soc_bat_list,sn,df_OprtnSta)


    return low_soc_bat_list
mylog=log.Mylog('log_diag.txt','error')
mylog.logcfg()


try:  
    low_soc_bat_list=Low_Soc_Alarm()
    low_soc_bat_list.to_excel('low_soc_bat_list.xlsx')
except Exception as e:
    print(repr(e))
    mylog.logopt(e)