package com.gyee.alarm.service.auto.impl;

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.mapper.auto.ProEconAlarmConfigurationMapper;
import com.gyee.alarm.model.vo.AlarmSuperTalbeType;
import com.gyee.alarm.model.vo.AlarmTypeValue;
import com.gyee.alarm.model.vo.AlarmVo;

import com.gyee.alarm.service.AlarmBtService;
import com.gyee.alarm.service.AlarmInService;
import com.gyee.alarm.service.AlarmWtService;
import com.gyee.alarm.service.auto.IProEconAlarmConfigurationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 报警配置表 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2023-06-19
 */
@Service
public class ProEconAlarmConfigurationServiceImpl extends ServiceImpl<ProEconAlarmConfigurationMapper, ProEconAlarmConfiguration> implements IProEconAlarmConfigurationService {

    @Resource
    private  ProEconAlarmConfigurationMapper proEconAlarmConfigurationMapper;

    public IPage<ProEconAlarmConfiguration> pageQueryAll(Page page,
                                                         String description,
                                                         String wpId,
                                                         String modelId,String alarmType)
    {
        IPage<ProEconAlarmConfiguration> pages=  proEconAlarmConfigurationMapper.pageQueryAll(page,
                description,
                wpId,
                modelId, alarmType);

        return pages;
    }


    @Resource
    private AlarmWtService alarmWtService;
    @Resource
    private AlarmInService alarmInService;
    @Resource
    private AlarmBtService alarmBtService;
    public void inital(ProEconAlarmConfiguration alarm) throws Exception {


        if (alarm.getAlarmType().equals(AlarmTypeValue.WT.getCode())) {


            List<ProEconAlarmConfiguration> filteredList =  CacheContext.wtalarmls.stream()
                    .filter(myObject -> myObject.getId().equals(alarm.getId()))
                    .collect(Collectors.toList());

            if(filteredList.isEmpty()){
                CacheContext.wtalarmls.add(alarm);
            }else
            {
                updateAlarm(alarm, CacheContext.wtalarmls);
            }

            StringBuilder sb=new StringBuilder();
            sb.append(alarm.getStationId()).append(alarm.getModelId());

            if(CacheContext.wtAlarmMap.containsKey(String.valueOf(sb)))
            {
                List<ProEconAlarmConfiguration> ls=CacheContext.wtAlarmMap.get(String.valueOf(sb));

                List<ProEconAlarmConfiguration> list = ls.stream()
                        .filter(myObject -> myObject.getId().equals(alarm.getId()))
                        .collect(Collectors.toList());

                if(list.isEmpty()){
                    ls.add(alarm);
                }else
                {
                    updateAlarm(alarm, ls);
                }

            }else
            {
                List<ProEconAlarmConfiguration> ls=new ArrayList<>();
                ls.add(alarm);
                CacheContext.wtAlarmMap.put(String.valueOf(sb),ls);
            }
        } else if (alarm.getAlarmType().equals(AlarmTypeValue.BT.getCode())) {


            List<ProEconAlarmConfiguration> filteredList =   CacheContext.btalarmls.stream()
                    .filter(myObject -> myObject.getId().equals(alarm.getId()))
                    .collect(Collectors.toList());

            if(filteredList.isEmpty()){
                CacheContext.btalarmls.add(alarm);
            }else
            {
                updateAlarm(alarm, CacheContext.btalarmls);
            }
        }
        else if (alarm.getAlarmType().equals(AlarmTypeValue.IN.getCode())) {


            List<ProEconAlarmConfiguration> filteredList =   CacheContext.inalarmls.stream()
                    .filter(myObject -> myObject.getId().equals(alarm.getId()))
                    .collect(Collectors.toList());

            if(filteredList.isEmpty()){
                CacheContext.inalarmls.add(alarm);
            }else
            {
                updateAlarm(alarm, CacheContext.inalarmls);
            }
            StringBuilder sb=new StringBuilder();
            sb.append(alarm.getStationId()).append(alarm.getModelId());

            if(CacheContext.inAlarmMap.containsKey(String.valueOf(sb)))
            {

                List<ProEconAlarmConfiguration> ls=CacheContext.inAlarmMap.get(String.valueOf(sb));

                List<ProEconAlarmConfiguration> list = ls.stream()
                        .filter(myObject -> myObject.getId().equals(alarm.getId()))
                        .collect(Collectors.toList());

                if(list.isEmpty()){
                    ls.add(alarm);
                }else
                {
                    updateAlarm(alarm, ls);
                }

            }else
            {
                List<ProEconAlarmConfiguration> ls=new ArrayList<>();
                ls.add(alarm);
                CacheContext.inAlarmMap.put(String.valueOf(sb),ls);
            }
        }
        //风机报警初始化标签
        List<AlarmVo> list=alarmWtService.initalAlarmVo(alarm);
        if(!list.isEmpty())
        {
            AlarmVo alarmVo=list.get(0);

            List<AlarmVo> filteredList =   CacheContext.alarmWtList.stream()
                    .filter(myObject -> myObject.getTbName().equals(alarmVo.getTbName()))
                    .collect(Collectors.toList());

            if(filteredList.isEmpty()){
                CacheContext.alarmWtList.addAll(list);
            }else
            {
                updateAlarmVo(alarm, alarmVo);
            }

        }

        if(!CacheContext.alarmWtList.isEmpty())
        {
            for(AlarmVo vo: CacheContext.alarmWtList)
            {

                CacheContext.alarmWtMap.put(vo.getTbName().toUpperCase(),vo);

                if(CacheContext.alarmWtLsMap.containsKey(vo.getStationid()))
                {
                    List<AlarmVo> ls=CacheContext.alarmWtLsMap.get(vo.getStationid());
                    ls.add(vo);
                }else {
                    List<AlarmVo> ls=new ArrayList<>();
                    ls.add(vo);
                    CacheContext.alarmWtLsMap.put(vo.getStationid(),ls);
                }
            }
        }
        //逆变器报警初始化标签

        List<AlarmVo> inList=alarmInService.initalAlarmVo(alarm);
        if(!list.isEmpty())
        {
            AlarmVo alarmVo=list.get(0);

            List<AlarmVo> filteredList =   CacheContext.alarmInList.stream()
                    .filter(myObject -> myObject.getTbName().equals(alarmVo.getTbName()))
                    .collect(Collectors.toList());

            if(filteredList.isEmpty()){
                CacheContext.alarmInList.addAll(inList);
            }else
            {
                updateAlarmVo(alarm, alarmVo);
            }

        }

        if(!CacheContext.alarmInList.isEmpty())
        {
            for(AlarmVo vo: CacheContext.alarmInList)
            {
                CacheContext.alarmInMap.put(vo.getTbName().toUpperCase(),vo);

                if(CacheContext.alarmInLsMap.containsKey(vo.getStationid()))
                {
                    List<AlarmVo> ls=CacheContext.alarmInLsMap.get(vo.getStationid());
                    ls.add(vo);
                }else {
                    List<AlarmVo> ls=new ArrayList<>();
                    ls.add(vo);
                    CacheContext.alarmInLsMap.put(vo.getStationid(),ls);
                }
            }
        }


        //升压站报警初始化标签
//        CacheContext.alarmBtList=alarmBtService.initalAlarmVo();

        List<AlarmVo> btList=alarmBtService.initalAlarmVo(alarm);
        if(!list.isEmpty())
        {
            AlarmVo alarmVo=list.get(0);

            List<AlarmVo> filteredList =   CacheContext.alarmBtList.stream()
                    .filter(myObject -> myObject.getTbName().equals(alarmVo.getTbName()))
                    .collect(Collectors.toList());

            if(filteredList.isEmpty()){
                CacheContext.alarmBtList.addAll(btList);
            }else
            {
                updateAlarmVo(alarm, alarmVo);
            }

        }

        if(!CacheContext.alarmBtList.isEmpty())
        {
            for(AlarmVo vo: CacheContext.alarmBtList)
            {
                CacheContext.alarmBtMap.put(vo.getTbName().toUpperCase(),vo);

                if(CacheContext.alarmBtLsMap.containsKey(vo.getStationid()))
                {
                    List<AlarmVo> ls=CacheContext.alarmBtLsMap.get(vo.getStationid());
                    ls.add(vo);
                }else {
                    List<AlarmVo> ls=new ArrayList<>();
                    ls.add(vo);
                    CacheContext.alarmBtLsMap.put(vo.getStationid(),ls);
                }
            }
        }
    }

    private static void updateAlarmVo(ProEconAlarmConfiguration alarm, AlarmVo alarmVo) {
        for(AlarmVo vo:CacheContext.alarmWtList)
        {
            if(vo.getTbName().equals(alarmVo.getTbName()))
            {
                vo.setTbName(alarmVo.getTbName());
                vo.setAlarmId(alarmVo.getId());
                vo.setTagid(alarmVo.getTagid());
                vo.setAlarmType(alarmVo.getAlarmType());
                vo.setCharacteristic(alarmVo.getCharacteristic());
                vo.setComponents(alarmVo.getComponents());
                vo.setDescription(alarmVo.getDescription());
                vo.setDeviceId(alarmVo.getDeviceId());
                vo.setDevicename(alarmVo.getDevicename());
                vo.setDeviceType(alarmVo.getDeviceType());
                vo.setEnabled(alarmVo.getEnabled());
                vo.setLineid(alarmVo.getLineid());
                vo.setLinename(alarmVo.getLinename());
                vo.setModelId(alarmVo.getModelId());
                vo.setProjectid(alarmVo.getProjectid());
                vo.setProjectname(alarmVo.getProjectname());
                vo.setRank(alarm.getRank());
                vo.setResettable(alarm.getResetTable());
                vo.setStationid(alarmVo.getStationid());
                vo.setStationname(alarmVo.getStationname());
                vo.setSubcomponents(alarmVo.getSubcomponents());
                vo.setSuffix(alarmVo.getSuffix());
                vo.setTriggertype(alarmVo.getTriggertype());
                vo.setUniformcode(alarmVo.getUniformcode());
                vo.setSuperTableName(alarmVo.getSuperTableName());
                vo.setVal(alarmVo.getVal());
                vo.setName(alarmVo.getName());
                vo.setNemCode(alarmVo.getNemCode());
                vo.setFaultCause(alarmVo.getFaultCause());
                vo.setResolvent(alarmVo.getResolvent());
                break;
            }
        }
    }

    private static void updateAlarm(ProEconAlarmConfiguration alarm, List<ProEconAlarmConfiguration> ls) {
        for(ProEconAlarmConfiguration ac: ls)
        {
            if(ac.getId().equals(alarm.getId()))
            {
                ac.setId(alarm.getId());
                ac.setTagId(alarm.getTagId());
                ac.setTriggerType(alarm.getTriggerType());
                ac.setDeviceId(alarm.getDeviceId());
                ac.setSuffix(alarm.getSuffix());
                ac.setRank(alarm.getRank());
                ac.setUniformCode(alarm.getUniformCode());
                ac.setStationId(alarm.getStationId());
                ac.setModelId(alarm.getModelId());
                ac.setAlarmType(alarm.getAlarmType());
                ac.setDeviceType(alarm.getDeviceType());
                ac.setComponents(alarm.getComponents());
                ac.setSubcomponents(alarm.getSubcomponents());
                ac.setDescription(alarm.getDescription());
                ac.setCharacteristic(alarm.getCharacteristic());
                ac.setResetTable(alarm.getResetTable());
                ac.setEnable(alarm.getEnable());
                ac.setName(alarm.getName());
                ac.setNemCode(alarm.getNemCode());
                ac.setFaultCause(alarm.getFaultCause());
                ac.setResolvent(alarm.getResolvent());
                break;
            }

        }
    }

}

