package com.gyee.alarm.service;

import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.model.vo.*;
import com.gyee.alarm.service.auto.IAlarmTsService;
import com.gyee.alarm.service.auto.IProEconAlarmInfoService;
import com.gyee.alarm.util.DateUtils;
import com.gyee.alarm.util.StringUtils;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import com.gyee.alarm.websocket.PushParams;
import com.gyee.alarm.websocket.WebSocket;
import com.gyee.common.model.PointData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service

public class AlarmInService {
    @Resource
    private IAlarmTsService alarmTsService;
    @Resource
    private IProEconAlarmInfoService proEconAlarmInfoService;
    @Value("${initialcode}")
    private String initialcode;

    @Value("${pushinterval}")
    private Double pushinterval;
    @Resource
    private WebSocket webSocket;
    @Resource
    private PushDataSevice pushDataSevice;

    @Value("${read-rows}")
    private Integer readRows;

    @Resource
    private IEdosUtil edosUtil;

    private static Logger logger = LoggerFactory.getLogger(AlarmWtService.class);
    public void dropSuperTable()
    {
        alarmTsService.dropSuperTable(AlarmSuperTalbeType.WT.getCode());
    }
    public void createSuperTable()
    {

        alarmTsService.createSuperTable(AlarmSuperTalbeType.WT.getCode());
    }

    public void initalInAlarm() throws Exception {



        List<AlarmVo> alarmVoList=CacheContext.alarmInList;
        List<AlarmTag>  alarmTags=new ArrayList<>();


        if(!alarmVoList.isEmpty())
        {
            for(AlarmVo vo: alarmVoList)
            {


                alarmTsService.createTable(vo.getTbName(), vo.getAlarmid(), vo.getAlarmtype(),
                        vo.getCharacteristic(), vo.getComponents(),
                        vo.getDescription(),
                        vo.getDeviceid(), vo.getDevicename(),
                        vo.getDevicetype(), vo.getEnabled(),
                        vo.getLineid(), vo.getLinename(),
                        vo.getModelId(), vo.getProjectid(),
                        vo.getProjectname(), vo.getRank(),
                        vo.getResettable(), vo.getStationid(),
                        vo.getStationname(), vo.getSubcomponents(),
                        vo.getSuffix(), vo.getTagid(),
                        vo.getTriggertype(), vo.getUniformcode(),vo.getSuperTableName(),
                        vo.getName(),
                        vo.getNemCode(),vo.getFaultCause(),vo.getResolvent(),vo.getFaultType(),vo.getAname());
                alarmTsService.updateTable(vo.getTbName(), vo.getAlarmid(), vo.getAlarmtype(),
                        vo.getCharacteristic(), vo.getComponents(),
                        vo.getDescription(),
                        vo.getDeviceid(), vo.getDevicename(),
                        vo.getDevicetype(), vo.getEnabled(),
                        vo.getLineid(), vo.getLinename(),
                        vo.getModelId(), vo.getProjectid(),
                        vo.getProjectname(), vo.getRank(),
                        vo.getResettable(), vo.getStationid(),
                        vo.getStationname(), vo.getSubcomponents(),
                        vo.getSuffix(), vo.getTagid(),
                        vo.getTriggertype(), vo.getUniformcode(), vo.getSuperTableName(),
                        vo.getName(),
                        vo.getNemCode(),vo.getFaultCause(),vo.getResolvent(),vo.getFaultType(),vo.getAname());

            }
        }


//        alarmTsService.insertList(alarmTags);
    }


    public List<AlarmVo> initalAlarmVo() throws Exception {

        List<AlarmVo> alarmVoList=new ArrayList<>();
        Map<String, List<ProEconAlarmConfiguration>> wtAlarmMap = CacheContext.inAlarmMap;
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        for (ProBasicEquipment wt : CacheContext.wtls) {

            if(wt.getId().contains("_F_"))
            {
                continue;
            }


            if (wtpAimap.containsKey(wt.getId())) {
                Map<String, ProBasicEquipmentPoint> pointMap = wtpAimap.get(wt.getId());

                StringBuilder sb = new StringBuilder();
                sb.append(wt.getWindpowerstationId() + wt.getModelId());


                if (StringUtils.notEmp(pointMap) && wtAlarmMap.containsKey(String.valueOf(sb))) {
                    List<ProEconAlarmConfiguration> alarmls = wtAlarmMap.get(String.valueOf(sb));

                    if (!alarmls.isEmpty()) {
//                        System.out.println(wt.getId());
                        for (ProEconAlarmConfiguration alarm : alarmls) {
                            if (pointMap.containsKey(alarm.getUniformCode()) && alarm.getDeviceId().equals(wt.getId())) {
                                ProBasicEquipmentPoint equipmentPoint = pointMap.get(alarm.getUniformCode());

                                if(StringUtils.notEmp(initialcode) &&
                                        StringUtils.notEmp(equipmentPoint.getNemCode()) &&
                                        !equipmentPoint.getNemCode().equals(initialcode) &&
                                        alarm.getAlarmType().equals(AlarmTypeValue.IN.getCode())
                                )


                                {

//                                    System.out.println("ok");
                                    AlarmVo vo=new AlarmVo();
                                    StringBuilder tbName = new StringBuilder();
                                    tbName.append(wt.getId()).append("_").append(alarm.getId());
                                    vo.setTbName(String.valueOf(tbName));
                                    vo.setAlarmid(alarm.getId());
                                    vo.setTagid(equipmentPoint.getNemCode());
                                    vo.setAlarmtype(alarm.getAlarmType());
                                    vo.setCharacteristic(alarm.getCharacteristic());
                                    vo.setComponents(alarm.getComponents());
                                    vo.setDescription(alarm.getDescription());
                                    vo.setDeviceid(wt.getId());
                                    vo.setDevicename(wt.getName());
                                    vo.setDevicetype(alarm.getDeviceType());
                                    vo.setEnabled(alarm.getEnable());
                                    vo.setLineid(wt.getLineId());
                                    if(CacheContext.lnmap.containsKey(wt.getLineId()))
                                    {
                                        ProBasicLine ln=CacheContext.lnmap.get(wt.getLineId());
                                        vo.setLinename(ln.getName());
                                    }
                                    vo.setModelId(wt.getModelId());
                                    vo.setProjectid(wt.getProjectId());
                                    if(CacheContext.pjmap.containsKey(wt.getProjectId()))
                                    {
                                        ProBasicProject pj=CacheContext.pjmap.get(wt.getProjectId());
                                        vo.setProjectname(pj.getName());
                                    }
                                    vo.setRank(alarm.getRank());
                                    vo.setResettable(alarm.getResetTable());
                                    vo.setStationid(wt.getWindpowerstationId());
                                    if(CacheContext.wpmap.containsKey(wt.getWindpowerstationId()))
                                    {
                                        ProBasicPowerstation wp=CacheContext.wpmap.get(wt.getWindpowerstationId());
                                        vo.setStationname(wp.getName());
                                    }
                                    vo.setSubcomponents(alarm.getSubcomponents());
                                    vo.setSuffix(alarm.getSuffix());
                                    vo.setTriggertype(alarm.getTriggerType());
                                    vo.setUniformcode(alarm.getUniformCode());
                                    vo.setSuperTableName(AlarmSuperTalbeType.WT.getCode());
                                    vo.setFaultType(alarm.getDeviceId());
                                    vo.setAname(alarm.getSuffix());
//                                    PointData temp=edosUtil.getRealData(equipmentPoint.getNemCode());
//                                    vo.setVal((int)temp.getPointValueInDouble());
//                                    vo.setOval((int)temp.getPointValueInDouble());

                                    vo.setVal(null);
                                    vo.setOval(null);
                                    vo.setName(alarm.getName());
                                    vo.setNemCode(alarm.getNemCode());
                                    vo.setFaultCause(alarm.getFaultCause());
                                    vo.setResolvent(alarm.getResolvent());
                                    alarmVoList.add(vo);
                                }

                            }else if (pointMap.containsKey(alarm.getUniformCode()) && StringUtils.empty(alarm.getDeviceId())) {
                                ProBasicEquipmentPoint equipmentPoint = pointMap.get(alarm.getUniformCode());

                                if(StringUtils.notEmp(initialcode) &&
                                        StringUtils.notEmp(equipmentPoint.getNemCode()) &&
                                        !equipmentPoint.getNemCode().equals(initialcode) &&
                                        alarm.getAlarmType().equals(AlarmTypeValue.IN.getCode())
                                )


                                {

//                                    System.out.println("ok");
                                    AlarmVo vo=new AlarmVo();
                                    StringBuilder tbName = new StringBuilder();
                                    tbName.append(wt.getId()).append("_").append(alarm.getId());
                                    vo.setTbName(String.valueOf(tbName));
                                    vo.setAlarmid(alarm.getId());
                                    vo.setTagid(equipmentPoint.getNemCode());
                                    vo.setAlarmtype(alarm.getAlarmType());
                                    vo.setCharacteristic(alarm.getCharacteristic());
                                    vo.setComponents(alarm.getComponents());
                                    vo.setDescription(alarm.getDescription());
                                    vo.setDeviceid(wt.getId());
                                    vo.setDevicename(wt.getName());
                                    vo.setDevicetype(alarm.getDeviceType());
                                    vo.setEnabled(alarm.getEnable());
                                    vo.setLineid(wt.getLineId());
                                    if(CacheContext.lnmap.containsKey(wt.getLineId()))
                                    {
                                        ProBasicLine ln=CacheContext.lnmap.get(wt.getLineId());
                                        vo.setLinename(ln.getName());
                                    }
                                    vo.setModelId(wt.getModelId());
                                    vo.setProjectid(wt.getProjectId());
                                    if(CacheContext.pjmap.containsKey(wt.getProjectId()))
                                    {
                                        ProBasicProject pj=CacheContext.pjmap.get(wt.getProjectId());
                                        vo.setProjectname(pj.getName());
                                    }
                                    vo.setRank(alarm.getRank());
                                    vo.setResettable(alarm.getResetTable());
                                    vo.setStationid(wt.getWindpowerstationId());
                                    if(CacheContext.wpmap.containsKey(wt.getWindpowerstationId()))
                                    {
                                        ProBasicPowerstation wp=CacheContext.wpmap.get(wt.getWindpowerstationId());
                                        vo.setStationname(wp.getName());
                                    }
                                    vo.setSubcomponents(alarm.getSubcomponents());
                                    vo.setSuffix(alarm.getSuffix());
                                    vo.setTriggertype(alarm.getTriggerType());
                                    vo.setUniformcode(alarm.getUniformCode());
                                    vo.setSuperTableName(AlarmSuperTalbeType.WT.getCode());
                                    vo.setFaultType(alarm.getDeviceId());
                                    vo.setAname(alarm.getSuffix());
//                                    PointData temp=edosUtil.getRealData(equipmentPoint.getNemCode());
//                                    vo.setVal((int)temp.getPointValueInDouble());
//                                    vo.setOval((int)temp.getPointValueInDouble());

                                    vo.setVal(null);
                                    vo.setOval(null);
                                    vo.setName(alarm.getName());
                                    vo.setNemCode(alarm.getNemCode());
                                    vo.setFaultCause(alarm.getFaultCause());
                                    vo.setResolvent(alarm.getResolvent());
                                    alarmVoList.add(vo);
                                }

                            }

                        }
                    }
                }
            }

        }
        return  alarmVoList;
    }




    public List<AlarmTag> findTags()
    {
 
        List<AlarmTag> ls=new ArrayList<>();
        if(!CacheContext.alarmInList.isEmpty())
        {
            for(AlarmVo vo:CacheContext.alarmInList)
            {
                AlarmTag po=new AlarmTag();
                po.setId(vo.getTbName());
                po.setOval(null);
                po.setTriggerType(vo.getTriggertype());
                po.setTagId(vo.getTagid());
                po.setVal(null);
                po.setRank(vo.getRank());
                po.setAlarmType(vo.getAlarmtype());
                po.setUniformCode(vo.getUniformcode());
                po.setWpId(vo.getStationid());
                ls.add(po);
            }
        }
        return ls;
    }


    public synchronized void saveAlarms(List<AlarmTag> ls) throws EncodeException, IOException, InterruptedException {

        if(StringUtils.notEmp(ls) && !ls.isEmpty()) {

            for (AlarmTag alarm : ls) {
                alarm.setConfirmed(false);

            }
            alarmTsService.insertList(ls);
            Map<String,ProEconAlarmInfo> tempmap=new HashMap<>();

            if (StringUtils.notEmp(ls) && !ls.isEmpty()) {

                List<ProEconAlarmInfo> valuels = new ArrayList<>();
                for (AlarmTag alarm : ls) {
                    if (CacheContext.alarmInMap.containsKey(alarm.getId())) {
                        AlarmVo vo = CacheContext.alarmInMap.get(alarm.getId());
                        ProEconAlarmInfo po = new ProEconAlarmInfo();
                        po.setTbname(vo.getTbName());
                        po.setLastUpdateTime(new Date(alarm.getTs()));
                        po.setUpdateTime(new Date(alarm.getTs()));
                        po.setTagId(alarm.getTagId());
                        po.setTriggerType(alarm.getTriggerType());
                        po.setAlarmId(vo.getAlarmid());
                        po.setDeviceId(vo.getDeviceid());
                        po.setSubcomponents(vo.getSuffix());
                        po.setRank(vo.getRank());
                        po.setConfirmed(false);
                        po.setUniformCode(vo.getUniformcode());
                        po.setAlarmType(vo.getAlarmtype());
                        po.setDeviceType(vo.getDevicetype());
                        po.setComponents(vo.getComponents());
                        po.setSubcomponents(vo.getSubcomponents());
                        po.setDescription(vo.getDescription());
                        po.setModelId(vo.getModelId());
                        po.setResetTable(vo.getResettable());
                        po.setEnable(vo.getEnabled());
                        po.setCharacteristic(vo.getCharacteristic());
                        po.setStationId(vo.getStationid());
                        po.setProjectId(vo.getProjectid());
                        po.setLineId(vo.getLineid());
                        po.setStationName(vo.getStationname());
                        po.setProjectiName(vo.getProjectname());
                        po.setLineidName(vo.getLinename());
                        po.setDeviceName(vo.getDevicename());
                        po.setOval(alarm.getOval().intValue());
                        po.setIsOpen(1);
                        po.setVal(alarm.getVal().intValue());

                        alarm.setRank(vo.getRank());
                        alarm.setDescription(vo.getDescription());
                        alarm.setAlarmType(vo.getAlarmtype());
                        alarm.setDeviceType(vo.getDevicetype());

                        alarm.setCharacteristic(vo.getCharacteristic());
                        alarm.setFaultCause(vo.getFaultCause());
                        alarm.setResolvent(vo.getResolvent());
                        alarm.setIsClose(false);
                        alarm.setDeviceId(vo.getDeviceid());
                        alarm.setAlarmId(vo.getAlarmid());

                        if(StringUtils.notEmp(vo.getDeviceid()) && CacheContext.wtmap.containsKey(vo.getDeviceid()))
                        {
                            alarm.setCode(CacheContext.wtmap.get(vo.getDeviceid()).getNemCode());
                        }

                        if(CacheContext.wpmap.containsKey(vo.getStationid()))
                        {
                            alarm.setWpName(CacheContext.wpmap.get(vo.getStationid()).getName());
                        }

                        alarm.setWpId(vo.getStationid());
                        try {
                            pushDataSevice.pushBtData(alarm);
                        }catch (Exception e)
                        {
                            System.out.println(e.getMessage());
                        }


                        tempmap.put(po.getTbname(),po);
                    }
                }


                for (Map.Entry<String, ProEconAlarmInfo> entry : tempmap.entrySet()) {
                    valuels.add(entry.getValue());
                }
                List<String> idls = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {
                    idls.add(vo.getTbname());
                }
//                Map<String,ProEconAlarmInfo> infoMap=new HashMap<>();
                if (!idls.isEmpty()) {

//                    List<ProEconAlarmInfo> templs= proEconAlarmInfoService.listByIds(idls);
//                    if(!templs.isEmpty())
//                    {
//                        for(ProEconAlarmInfo info:templs)
//                        {
//                            infoMap.put(info.getTbname().toLowerCase(),info);
//                        }
//                    }
                    proEconAlarmInfoService.removeByIds(idls);
                }


                List<ProEconAlarmInfo> templs = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {

//                    if(infoMap.containsKey(vo.getTbname().toLowerCase()))
//                    {
//                        ProEconAlarmInfo in=infoMap.get(vo.getTbname().toLowerCase());
//                        vo.setUpdateTime(in.getLastUpdateTime());
//                    }
                    templs.add(vo);
                    if (templs.size() == 900) {
                        proEconAlarmInfoService.saveBatch(templs);
                        templs = new ArrayList<>();
                    }
                }

                if (!templs.isEmpty()) {
                    proEconAlarmInfoService.saveBatch(templs);
                }

            }
            if (StringUtils.notEmp(ls) && !ls.isEmpty()) {

                for (AlarmTag alarm : ls) {
                    if (CacheContext.alarmInMap.containsKey(alarm.getId())) {
                        AlarmVo vo = CacheContext.alarmInMap.get(alarm.getId());


                        alarm.setRank(vo.getRank());
                        alarm.setDescription(vo.getDescription());
                        alarm.setAlarmType(vo.getAlarmtype());
                        alarm.setDeviceType(vo.getDevicetype());

                        alarm.setCharacteristic(vo.getCharacteristic());
                        alarm.setFaultCause(vo.getFaultCause());
                        alarm.setResolvent(vo.getResolvent());
                        alarm.setIsClose(false);
                        alarm.setDeviceId(vo.getDeviceid());
                        alarm.setAlarmId(vo.getAlarmid());

                        if(StringUtils.notEmp(vo.getDeviceid()) && CacheContext.wtmap.containsKey(vo.getDeviceid()))
                        {
                            alarm.setCode(CacheContext.wtmap.get(vo.getDeviceid()).getNemCode());
                        }

                        if(CacheContext.wpmap.containsKey(vo.getStationid()))
                        {
                            alarm.setWpName(CacheContext.wpmap.get(vo.getStationid()).getName());
                        }

                        alarm.setWpId(vo.getStationid());
                        pushDataSevice.pushWtData(alarm);

                    }
                }

                //            Thread.sleep(300);
                Thread.sleep(pushinterval.intValue());

            }

        }
    }


    public  synchronized void updateAlarms(List<AlarmTag> ls) throws EncodeException, IOException, InterruptedException {

        if (StringUtils.notEmp(ls) && !ls.isEmpty()) {

            StringBuilder sb=new StringBuilder();
            Map<String, AlarmSimpleVo> map=new HashMap<>();
            for (AlarmTag alarm : ls) {
                if (CacheContext.alarmInMap.containsKey(alarm.getId())) {
                    AlarmVo vo = CacheContext.alarmInMap.get(alarm.getId());
                    if(vo.getAlarmtype().equals(AlarmTypeValue.IN.getCode())  )
                    {
                        sb.append("'").append(alarm.getId().toLowerCase()).append("',");
                    }
                }

            }
            String ids=null;
            if(sb.length()>0)
            {
                ids=sb.substring(0,sb.length()-1);
            }
            List<AlarmSimpleVo> alarmls=new ArrayList<>();
            if( null !=ids)
            {
                alarmls=alarmTsService.selectLastRowByTbname(AlarmSuperTalbeType.WT.getCode(),ids);
            }

            if(!alarmls.isEmpty())
            {
                for(AlarmSimpleVo vo:alarmls)
                {
                    map.put(vo.getTbName().toLowerCase(),vo);
                }
            }
            List<AlarmTag> alarmTagls=new ArrayList<>();
            for (AlarmTag alarm : ls) {


                if(map.containsKey(alarm.getId().toLowerCase()))
                {
                    AlarmSimpleVo tag=map.get(alarm.getId().toLowerCase());

                    if(null !=tag.getTs())
                    {
                        alarm.setTs(tag.getTs());
                        Date begin = new Date(alarm.getEndts());
                        Date end = new Date(alarm.getTs());

                        double value= DateUtils.secondsDiff(begin,end);
                        alarm.setTimeLong(value);
                        alarm.setConfirmed(tag.getConfirmed());
                        alarm.setSuperTalbe(AlarmSuperTalbeType.WT.getCode());

                        alarmTagls.add(alarm);


                    }
                }
//                else
//                {
//                    alarm.setSuperTalbe(AlarmSuperTalbeType.BT.getCode());
//                    alarmTagls.add(alarm);
//                }
            }

            if (StringUtils.notEmp(alarmTagls) && !alarmTagls.isEmpty()) {
                alarmTsService.insertList(alarmTagls);
            }

            Map<String,ProEconAlarmInfo> tempmap=new HashMap<>();
            if (StringUtils.notEmp(alarmTagls) && !alarmTagls.isEmpty()) {

                List<ProEconAlarmInfo> valuels = new ArrayList<>();
                for (AlarmTag alarm : alarmTagls) {
                    if (CacheContext.alarmInMap.containsKey(alarm.getId())) {
                        AlarmVo vo = CacheContext.alarmInMap.get(alarm.getId());

                        ProEconAlarmInfo po = new ProEconAlarmInfo();
                        po.setTbname(vo.getTbName());
                        po.setLastUpdateTime(new Date(alarm.getEndts()));
                        po.setUpdateTime(new Date(alarm.getTs()));
                        po.setTagId(alarm.getTagId());
                        po.setTriggerType(alarm.getTriggerType());
                        po.setAlarmId(vo.getAlarmid());
                        po.setDeviceId(vo.getDeviceid());
                        po.setSubcomponents(vo.getSuffix());
                        po.setRank(vo.getRank());
                        po.setConfirmed(alarm.getConfirmed());
                        po.setUniformCode(vo.getUniformcode());
                        po.setAlarmType(vo.getAlarmtype());
                        po.setDeviceType(vo.getDevicetype());
                        po.setComponents(vo.getComponents());
                        po.setSubcomponents(vo.getSubcomponents());
                        po.setDescription(vo.getDescription());
                        po.setModelId(vo.getModelId());
                        po.setResetTable(vo.getResettable());
                        po.setEnable(vo.getEnabled());
                        po.setCharacteristic(vo.getCharacteristic());
                        po.setStationId(vo.getStationid());
                        po.setProjectId(vo.getProjectid());
                        po.setLineId(vo.getLineid());
                        po.setStationName(vo.getStationname());
                        po.setProjectiName(vo.getProjectname());
                        po.setLineidName(vo.getLinename());
                        po.setDeviceName(vo.getDevicename());
                        po.setOval(alarm.getOval().intValue());
                        po.setCloseTime(new Date(alarm.getEndts()));
                        po.setIsOpen(0);
                        po.setVal(alarm.getVal().intValue());

                        alarm.setRank(vo.getRank());
                        alarm.setDescription(vo.getDescription());
                        alarm.setAlarmType(vo.getAlarmtype());
                        alarm.setDeviceType(vo.getDevicetype());

                        alarm.setCharacteristic(vo.getCharacteristic());
                        alarm.setFaultCause(vo.getFaultCause());
                        alarm.setResolvent(vo.getResolvent());

                        alarm.setDeviceId(vo.getDeviceid());
                        alarm.setAlarmId(vo.getAlarmid());
                        alarm.setIsClose(true);
                        if(CacheContext.wtmap.containsKey(vo.getDeviceid()))
                        {
                            alarm.setCode(CacheContext.wtmap.get(vo.getDeviceid()).getNemCode());
                        }

                        if(CacheContext.wpmap.containsKey(vo.getStationid()))
                        {
                            alarm.setWpName(CacheContext.wpmap.get(vo.getStationid()).getName());
                        }

                        pushDataSevice.pushBtData(alarm);


                        tempmap.put(po.getTbname(),po);
                    }
                }
                for (Map.Entry<String, ProEconAlarmInfo> entry : tempmap.entrySet()) {
                    valuels.add(entry.getValue());
                }
                List<String> idls = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {
                    idls.add(vo.getTbname());
                }
//                Map<String,ProEconAlarmInfo> infoMap=new HashMap<>();
                if (!idls.isEmpty()) {

//                    List<ProEconAlarmInfo> templs= proEconAlarmInfoService.listByIds(idls);
//                    if(!templs.isEmpty())
//                    {
//                        for(ProEconAlarmInfo info:templs)
//                        {
//                            infoMap.put(info.getTbname().toLowerCase(),info);
//                        }
//                    }
                    proEconAlarmInfoService.removeByIds(idls);
                }


                List<ProEconAlarmInfo> templs = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {

//                    if(infoMap.containsKey(vo.getTbname().toLowerCase()))
//                    {
//                        ProEconAlarmInfo in=infoMap.get(vo.getTbname().toLowerCase());
//                        vo.setUpdateTime(in.getLastUpdateTime());
//                    }
                    templs.add(vo);
                    if (templs.size() == 900) {
                        proEconAlarmInfoService.saveBatch(templs);
                        templs = new ArrayList<>();
                    }
                }

                if (!templs.isEmpty()) {
                    proEconAlarmInfoService.saveBatch(templs);
                }

            }


            if (StringUtils.notEmp(alarmTagls) && !alarmTagls.isEmpty()) {


                for (AlarmTag alarm : alarmTagls) {
                    if (CacheContext.alarmInMap.containsKey(alarm.getId())) {
                        AlarmVo vo = CacheContext.alarmInMap.get(alarm.getId());


                        alarm.setRank(vo.getRank());
                        alarm.setDescription(vo.getDescription());
                        alarm.setAlarmType(vo.getAlarmtype());
                        alarm.setDeviceType(vo.getDevicetype());

                        alarm.setCharacteristic(vo.getCharacteristic());
                        alarm.setFaultCause(vo.getFaultCause());
                        alarm.setResolvent(vo.getResolvent());

                        alarm.setDeviceId(vo.getDeviceid());
                        alarm.setAlarmId(vo.getAlarmid());
                        alarm.setIsClose(true);
                        if(CacheContext.wtmap.containsKey(vo.getDeviceid()))
                        {
                            alarm.setCode(CacheContext.wtmap.get(vo.getDeviceid()).getNemCode());
                        }

                        if(CacheContext.wpmap.containsKey(vo.getStationid()))
                        {
                            alarm.setWpName(CacheContext.wpmap.get(vo.getStationid()).getName());
                        }

                        pushDataSevice.pushWtData(alarm);

                    }

                    //            Thread.sleep(300);
                    Thread.sleep(pushinterval.intValue());
                }


            }
        }

    }


}

