import CBMSBatDiag
import QX_BatteryParam
from SC_SamplingSafty import SamplingSafty
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 import DBDownload
from LIB.MIDDLE.CellStateEstimation.Common import log
from pandas.core.frame import DataFrame
import datacompy



#...................................电池包电芯安全诊断函数......................................................................................................................
def diag_cal():
    global SNnums
    global df_Diag_Ram

    start=time.time()
    end_time=datetime.datetime.now()
    start_time=end_time-datetime.timedelta(seconds=120)
    start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
    end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')

    for sn in SNnums:
        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)     
        # sn='PK50201A000002039'
        # celltype=2
        # start_time='2021-05-02 09:12:26'
        # end_time='2021-06-03 19:12:26'
        # # df_bms= pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\98Download\\'+'BMS_'+sn+'.csv',encoding='GB18030')

        #读取原始数据库数据........................................................................................................................................................
        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']
        # df_bms.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\99Result\\''BMS_'+sn+'.csv',encoding='GB18030')

        #读取结果数据库数据........................................................................................................................................................
        host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
        port=3306
        db='qx_cas'
        user='qx_read'
        password='Qx@123456'
        mode=1
        tablename1='cellstateestimation_soh'
        tablename2='cellstateestimation_uniform_socvoltdiff'
        tablename3='cellstateestimation_soc'
        DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
        with DBRead as DBRead:
            df_soh=DBRead.getdata('time_st','sn','soh', 'cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
            df_uniform=DBRead.getdata('time','sn','cellsoc_diff','cellmin_num','cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
            # df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn)

        #电池诊断................................................................................................................................................................
        #BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform)
        #df_res=BatDiag.diag()
        #df_Diag_Ram_old=df_Diag_Ram
        df_Diag_Ram_Update=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
        CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
        if not df_bms.empty:
            CellFltInfo=df_Diag_Ram[df_Diag_Ram['product_id']==sn]
            df_Diag_Ram_Update=SamplingSafty.main(sn,param,df_bms,CellFltInfo)       
        if not df_Diag_Ram_Update.empty:
            sn_index=df_Diag_Ram[df_Diag_Ram['product_id']==sn].index
            df_Diag_Ram=df_Diag_Ram.drop(index=sn_index)
            df_Diag_Ram=df_Diag_Ram.append(df_Diag_Ram_Update)
            df_Diag_Ram.reset_index(inplace=True,drop=True)

        Diag_Ram_Dif=datacompy.Compare(df_Diag_Ram_Update,CellFltInfo,join_columns=['product_id','end_time','code'])
        Diag_Ram_Dif=Diag_Ram_Dif.df1_unq_rows    
        if len(Diag_Ram_Dif)>0:
            Diag_Ram_Dif_New=Diag_Ram_Dif[Diag_Ram_Dif['end_time']=='0000-00-00 00:00:00']
            Diag_Ram_Dif_Finish=df_Diag_Ram[df_Diag_Ram['end_time']!='0000-00-00 00:00:00']
            if len(Diag_Ram_Dif_New)>0:
                result=pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',encoding='gbk')
                result=result.append(Diag_Ram_Dif_New)
                result.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',index=False,encoding='GB18030')
            if len(Diag_Ram_Dif_Finish)>0:
                result=pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',encoding='gbk')
                Diag_Ram_Dif_Finish=Diag_Ram_Dif_Finish.reset_index(drop=True)
                for i in range(0,len(Diag_Ram_Dif_Finish)):
                    result.loc[result[result[result['product_id']==Diag_Ram_Dif_Finish.loc[i,'product_id']]['code']==Diag_Ram_Dif_Finish.loc[i,'code']].index,'end_time']=Diag_Ram_Dif_Finish.loc[i,'end_time']
                result.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',index=False,encoding='GB18030')
        end=time.time()
        print(end-start)
        # print(df_soh)
            
#...................................电池包电芯安全诊断函数......................................................................................................................
def shortdiag_cal():
    global SNnums
    global df_Diag_Ram
    start=time.time()
    end_time=datetime.datetime.now()
    start_time=end_time-datetime.timedelta(days=30)
    start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
    end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')

    for sn in SNnums:
        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()

        #读取结果数据库数据........................................................................................................................................................
        host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
        port=3306
        db='qx_cas'
        user='qx_read'
        password='Qx@123456'
        mode=2
        tablename1='cellstateestimation_intershort'
        DBRead=DBDownload.DBDownload(host, port, db, user, password, mode)  #mode==1取数据库最后一行数据
        with DBRead as DBRead:
            df_short=DBRead.getdata('time_sp','sn','short_current', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)  
        
        #电池诊断................................................................................................................................................................
        ShortDiag=CBMSBatDiag.ShortDiag(sn,celltype, df_short)
        df_res=ShortDiag.shortdiag()
        # df_res.to_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\99Result\\'+'CBMS_diag_'+sn+'.csv',encoding='GB18030')
        
        print(df_res)

        end=time.time()
        print(end-start)
        # print(df_soh)
        

#...............................................主函数.......................................................................................................................
if __name__ == "__main__":

    excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\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=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014']
    #SNnums=['MGMLXN750N2189014']
    
    mylog=log.Mylog('log_diag.txt','error')
    mylog.logcfg()
    #............................模块运行前，先读取数据库中所有结束时间为0的数据，需要从数据库中读取................
    result=pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\result.csv',encoding='gbk')
    
    df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00']
    print('----------------输入--------')
    print(df_Diag_Ram)
    #定时任务.......................................................................................................................................................................
    scheduler = BlockingScheduler()
    scheduler.add_job(diag_cal, 'interval', seconds=120, id='diag_job')
    scheduler.add_job(shortdiag_cal, 'interval', days=7, id='shortdiag_job')

    try:  
        scheduler.start()
    except Exception as e:
        scheduler.shutdown()
        print(repr(e))
        mylog.logopt(e)