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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.mapper.auto.ProEconAlarmRuleMapper;
import com.gyee.alarm.model.auto.ProEconAlarmRule;
import com.gyee.alarm.model.vo.AlarmCustomType;
import com.gyee.alarm.model.vo.AlarmVo;
import com.gyee.alarm.service.AlarmCtService;
import com.gyee.alarm.service.auto.IProEconAlarmRuleService;
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-08-09
 */
@Service
public class ProEconAlarmRuleServiceImpl extends ServiceImpl<ProEconAlarmRuleMapper, ProEconAlarmRule> implements IProEconAlarmRuleService {


    @Resource
    private AlarmCtService alarmCtService;
    public void inital() throws Exception {


        List<ProEconAlarmRule> customls =list().stream().filter(i -> i.getEnable()).collect(Collectors.toList());

        List<ProEconAlarmRule> wtCustomls=new ArrayList<>();
        List<ProEconAlarmRule> inCustomls=new ArrayList<>();


        //对自定义报警进行分类
        if(!customls.isEmpty())
        {
            for(ProEconAlarmRule vo: customls)
            {
                if(vo.getCategory().equals(AlarmCustomType.WT.getCode()))
                {
                    wtCustomls.add(vo);
                }else  if(vo.getCategory().equals(AlarmCustomType.IN.getCode()))
                {
                    inCustomls.add(vo);
                }

            }
        }


        CacheContext.alarmRulesMap.put(AlarmCustomType.WT.getCode(),wtCustomls);
        CacheContext.alarmRulesMap.put(AlarmCustomType.IN.getCode(),inCustomls);
        //自定义报警初始化标签
        CacheContext.alarmCtList=alarmCtService.initalAlarmVo();
        if(!CacheContext.alarmCtList.isEmpty())
        {
            for(AlarmVo vo: CacheContext.alarmCtList)
            {
                CacheContext.alarmCtMap.put(vo.getTbName().toUpperCase(),vo);


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