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

import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.model.auto.ProEconAlarmRule;
import com.gyee.alarm.mapper.auto.ProEconAlarmRuleMapper;
import com.gyee.alarm.model.vo.AlarmCustomType;
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.AlarmCtService;
import com.gyee.alarm.service.auto.IAlarmTsService;
import com.gyee.alarm.service.auto.IProEconAlarmRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.alarm.util.ChineseDes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
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 {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private AlarmCtService alarmCtService;
    @Resource
    private IAlarmTsService alarmTsService;
    public void inital(ProEconAlarmRule rule) throws Exception {



        List<ProEconAlarmRule> wtCustomls= CacheContext.alarmRulesMap.get(AlarmCustomType.WT.getCode());
        List<ProEconAlarmRule> inCustomls=CacheContext.alarmRulesMap.get(AlarmCustomType.IN.getCode());



        //对自定义报警进行分类
        if(rule.getCategory().equals(AlarmCustomType.WT.getCode()))
        {
            //筛选是否存在预警配置
            List<ProEconAlarmRule> filteredList = wtCustomls.stream()
                    .filter(myObject -> myObject.getId().equals(rule.getId()))
                    .collect(Collectors.toList());
            //为空的新建测点
            if(filteredList.isEmpty())
            {
                wtCustomls.add(rule);

                List<AlarmVo> ctls=alarmCtService.initalAlarmVo(rule);

                if (!ctls.isEmpty()) {

                    for (AlarmVo vo : ctls) {
                        try
                        {
                            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());

                        }catch(Exception e)
                        {
                            logger.error("创建实时数据库测点异常"+vo.getTbName());
                        }
                    }

                }
                CacheContext.alarmCtList.addAll(ctls);
            }else {
                for(ProEconAlarmRule ct:wtCustomls)
                {
                    if(ct.getId().equals(rule.getId())) {
                        ct.setId(rule.getId());
                        ct.setName(rule.getName());
                        ct.setDescription(rule.getDescription());
                        ct.setExpression(rule.getExpression());
                        ct.setTag(rule.getTag());
                        ct.setRank(rule.getRank());
                        ct.setModelId(rule.getModelId());
                        ct.setCategory(rule.getCategory());
                        ct.setRange(rule.getRange());
                        ct.setStationId(rule.getStationId());
                        ct.setStationName(rule.getStationName());
                        ct.setProjectId(rule.getProjectId());
                        ct.setLineId(rule.getLineId());
                        ct.setDeviceId(rule.getDeviceId());
                        ct.setElectricalId(rule.getElectricalId());
                        ct.setRelatedParts(rule.getRelatedParts());
                        ct.setCreateTime(rule.getCreateTime());
                        ct.setEnable(rule.getEnable());
                        ct.setUniformCode(rule.getUniformCode());
                        ct.setRelatedPartsName(rule.getRelatedPartsName());
                        ct.setAlarmPlan(rule.getAlarmPlan());
                        break;
                    }
                }
            }

        }else  if(rule.getCategory().equals(AlarmCustomType.IN.getCode()))
        {
            List<ProEconAlarmRule> filteredList = inCustomls.stream()
                    .filter(myObject -> myObject.getId().equals(rule.getId()))
                    .collect(Collectors.toList());

            if(filteredList.isEmpty())
            {
                inCustomls.add(rule);
                List<AlarmVo> ctls=alarmCtService.initalAlarmVo(rule);
                if (!ctls.isEmpty()) {
                    for (AlarmVo vo : ctls) {
                        try
                        {
                            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());

                        }catch(Exception e)
                        {
                            logger.error("创建实时数据库测点异常"+vo.getTbName());
                        }
                    }

                }
                CacheContext.alarmCtList.addAll(ctls);
            }else {
                for(ProEconAlarmRule ct:inCustomls)
                {

                    if(ct.getId().equals(rule.getId())) {
                        ct.setId(rule.getId());
                        ct.setName(rule.getName());
                        ct.setDescription(rule.getDescription());
                        ct.setExpression(rule.getExpression());
                        ct.setTag(rule.getTag());
                        ct.setRank(rule.getRank());
                        ct.setModelId(rule.getModelId());
                        ct.setCategory(rule.getCategory());
                        ct.setRange(rule.getRange());
                        ct.setStationId(rule.getStationId());
                        ct.setStationName(rule.getStationName());
                        ct.setProjectId(rule.getProjectId());
                        ct.setLineId(rule.getLineId());
                        ct.setDeviceId(rule.getDeviceId());
                        ct.setElectricalId(rule.getElectricalId());
                        ct.setRelatedParts(rule.getRelatedParts());
                        ct.setCreateTime(rule.getCreateTime());
                        ct.setEnable(rule.getEnable());
                        ct.setUniformCode(rule.getUniformCode());
                        ct.setRelatedPartsName(rule.getRelatedPartsName());
                        ct.setAlarmPlan(rule.getAlarmPlan());
                        break;
                    }
                }
            }

        }


        CacheContext.alarmRulesMap.put(AlarmCustomType.WT.getCode(),wtCustomls);
        CacheContext.alarmRulesMap.put(AlarmCustomType.IN.getCode(),inCustomls);

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