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 org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

@Service

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

    public void dropSuperTable() {
        alarmTsService.dropSuperTable(AlarmSuperTalbeType.BT.getCode());
    }

    public void createSuperTable() {

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

    public void initalBtAlarm() throws SQLException {


        List<AlarmTag> alarmTags = new ArrayList<>();

        List<AlarmVo> alarmVoList = CacheContext.alarmBtList;

        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());


                AlarmTag po = new AlarmTag();
                po.setId(vo.getTbName());
                po.setVal(Double.valueOf(String.valueOf(vo.getTriggertype())));
                po.setTriggerType(vo.getTriggertype());
                po.setTagId(vo.getTagid());
                po.setOval(po.getVal());
                po.setRank(vo.getRank());
                po.setTs(new Date().getTime());
                po.setTimeLong(0.0);
                alarmTags.add(po);
            }
        }


        alarmTsService.insertList(alarmTags);
    }


    public List<AlarmVo> initalAlarmVo() {

        List<ProEconAlarmConfiguration> btalarmls = CacheContext.btalarmls;


        List<AlarmVo> alarmVoList = new ArrayList<>();
        if (!btalarmls.isEmpty()) {
            for (ProEconAlarmConfiguration alarm : btalarmls) {
                if (StringUtils.notEmp(initialcode) &&
                        StringUtils.notEmp(alarm.getTagId()) &&
                        !alarm.getTagId().equals(initialcode)) {
                    AlarmVo vo = new AlarmVo();
                    StringBuilder tbName = new StringBuilder();
                    tbName.append(alarm.getStationId()).append("_").append(alarm.getId());
                    vo.setTbName(String.valueOf(tbName));
                    vo.setAlarmid(alarm.getId());
                    vo.setTagid(alarm.getTagId());
                    vo.setAlarmtype(alarm.getAlarmType());
                    vo.setCharacteristic(alarm.getCharacteristic());
                    vo.setComponents(alarm.getComponents());
                    vo.setDescription(alarm.getDescription());
                    vo.setDeviceid("");
                    vo.setDevicename("");
                    vo.setDevicetype(DeviceTypeValue.BT.getCode());
                    vo.setEnabled(alarm.getEnable());
                    vo.setLineid("");
                    vo.setLinename("");
                    vo.setModelId(alarm.getModelId());
                    vo.setProjectid("");
                    vo.setProjectname("");
                    vo.setRank(alarm.getRank());
                    vo.setResettable(alarm.getResetTable());
                    vo.setStationid(alarm.getStationId());
                    if (CacheContext.subwpmap.containsKey(alarm.getStationId())) {
                        ProBasicSubStation wp = CacheContext.subwpmap.get(alarm.getStationId());
                        vo.setStationname(wp.getName());
                    }
                    vo.setSubcomponents(alarm.getSubcomponents());
                    vo.setSuffix(alarm.getSuffix());
                    vo.setTriggertype(alarm.getTriggerType());
                    vo.setUniformcode(alarm.getUniformCode());
                    vo.setSuperTableName(AlarmSuperTalbeType.BT.getCode());
                    alarmVoList.add(vo);
                }
            }
        }

        return alarmVoList;
    }


    public List<AlarmTag> findTags(String stbaleName) {
        List<AlarmTag> ls = new ArrayList<>();
        if (StringUtils.notEmp(stbaleName)) {
            // List<AlarmVo> vos= alarmTsService.findTags(stbaleName);

            List<AlarmVo> vos = CacheContext.alarmBtList;
            if (!vos.isEmpty()) {
                for (AlarmVo vo : vos) {
                    AlarmTag po=new AlarmTag();
                    po.setId(vo.getTbName());

                    po.setTriggerType(vo.getTriggertype());
                    po.setTagId(vo.getTagid());
                    po.setRank(vo.getRank());

                    ls.add(po);
                }
            }
        }
        return ls;
    }


    public synchronized void saveAlarms(List<AlarmTag> ls) {

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

            alarmTsService.insertList(ls);


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

                List<ProEconAlarmInfo> valuels = new ArrayList<>();
                for (AlarmTag alarm : ls) {
                    if (CacheContext.alarmBtMap.containsKey(alarm.getId())) {
                        AlarmVo vo = CacheContext.alarmBtMap.get(alarm.getId());
                        ProEconAlarmInfo po = new ProEconAlarmInfo();
                        po.setTbname(vo.getTbName());

                        po.setLastUpdateTime(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.setConfirmed(vo.getConfirmed());
                        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.setVal(alarm.getVal().intValue());
                        po.setIsOpen(1);
                        valuels.add(po);
                    }
                }



                List<String> idls = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {
                    idls.add(vo.getTbname());
                }
                if (!idls.isEmpty()) {

                    proEconAlarmInfoService.removeByIds(idls);
                }


                List<ProEconAlarmInfo> templs = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {
                    templs.add(vo);
                    if (templs.size() == 100) {
                        proEconAlarmInfoService.saveBatch(templs);
                        templs = new ArrayList<>();
                    }
                }

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


            }
        }

    }


    public synchronized void updateAlarms(List<AlarmTag> ls) {

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

            StringBuilder sb=new StringBuilder();
            Map<String,AlarmSimpleVo> map=new HashMap<>();
            for (AlarmTag alarm : ls) {
                sb.append("'").append(alarm.getId()).append("',");

            }

            String ids=sb.substring(0,sb.length()-1);

            List<AlarmSimpleVo> alarmls=alarmTsService.selectLastRowByTbname(AlarmSuperTalbeType.BT.getCode(),ids);
            if(!alarmls.isEmpty())
            {
                for(AlarmSimpleVo vo:alarmls)
                {

                    map.put(vo.getTbName(),vo);

                }
            }

            for (AlarmTag alarm : ls) {
                if(map.containsKey(alarm.getId()))
                {
                    AlarmSimpleVo tag=map.get(alarm.getId());
                    tag.setTs(new Timestamp(alarm.getTs()));

                    Date begin = new Date(alarm.getEndts());
                    Date end = new Date(alarm.getTs());

                    double value= DateUtils.hoursDiff(begin,end);
                    tag.setTimelong(value);
                }

            }

            alarmTsService.insertList(ls);


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

                List<ProEconAlarmInfo> valuels = new ArrayList<>();
                for (AlarmTag alarm : ls) {
                    if (CacheContext.alarmBtMap.containsKey(alarm.getId())) {
                        AlarmVo vo = CacheContext.alarmBtMap.get(alarm.getId());
                        ProEconAlarmInfo po = new ProEconAlarmInfo();
                        po.setTbname(vo.getTbName());
                        if(StringUtils.notEmp(alarm.getEndts()))
                        {
                            po.setLastUpdateTime(new Date(alarm.getEndts()));
                        }else
                        {
                            po.setLastUpdateTime(new Date());
                        }

                        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.setConfirmed(vo.getConfirmed());
                        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.setVal(alarm.getVal().intValue());
                        if(StringUtils.notEmp(alarm.getEndts()))
                        {
                            po.setCloseTime(new Date(alarm.getEndts()));
                        }else
                        {
                            po.setCloseTime(new Date());
                        }

                        po.setIsOpen(0);
                        valuels.add(po);
                    }
                }

                List<String> idls = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {
                    idls.add(vo.getTbname());
                }
                if (!idls.isEmpty()) {

                    proEconAlarmInfoService.removeByIds(idls);
                }

                List<ProEconAlarmInfo> templs = new ArrayList<>();
                for (ProEconAlarmInfo vo : valuels) {
                    templs.add(vo);
                    if (templs.size() == 100) {
                        proEconAlarmInfoService.saveBatch(templs);
                        templs = new ArrayList<>();
                    }
                }

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

            }
        }

    }

}
