package com.gyee.alarm.service;


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.mapper.auto.ProEconAlarmConfigurationMapper;
import com.gyee.alarm.mapper.auto.ProEconAlarmRuleMapper;
import com.gyee.alarm.model.auto.ProBasicPowerstation;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.model.auto.ProEconAlarmType;
import com.gyee.alarm.model.vo.*;
import com.gyee.alarm.service.auto.IProEconAlarmConfigurationService;
import com.gyee.alarm.util.SnowflakeGenerator;
import com.gyee.alarm.util.StringUtil;
import com.gyee.alarm.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class AlarmConfigurationService {

    @Resource
    private IProEconAlarmConfigurationService proEconAlarmConfigurationService;
    @Resource
    private RuleUpdateEventService ruleUpdateEventService;

    @Resource
    private ProEconAlarmConfigurationMapper proEconAlarmConfigurationMapper;
    private boolean saveAlertrule(ProEconAlarmConfiguration alarmRule) throws Exception {

        boolean result = false;

        if (StringUtils.notEmp(alarmRule)) {
            
            alarmRule.setId(String.valueOf(SnowflakeGenerator.generateId()));
  
            result= proEconAlarmConfigurationService.save(alarmRule);
        }
//        if (result > 0) {
//            int i = eventService.saveEventWarning(alarmRule, oldRule2, userData.getUserName());
        return result;
    }


    public boolean saveAndUpdateAlertrule(ProEconAlarmConfiguration alarmRule) throws Exception {

        boolean result =false;

        if (StringUtils.notEmp(alarmRule)) {
            List<ProEconAlarmConfiguration>  oldRulels =null;

            if (StringUtils.notEmp(alarmRule.getId())) {

                if(!alarmRule.getAlarmType().equals(AlarmTypeValue.BT.getCode()))
                {
                    oldRulels = proEconAlarmConfigurationMapper.queryObject(alarmRule.getDescription(),alarmRule.getStationId(),alarmRule.getModelId());

                }else
                {
                    oldRulels = proEconAlarmConfigurationMapper.queryObject(alarmRule.getDescription(),alarmRule.getStationId(),null);

                }
                if(!oldRulels.isEmpty())
                {
                    ProEconAlarmConfiguration oldRule2=oldRulels.get(0);
                    alarmRule.setId(oldRule2.getId());
                    result = proEconAlarmConfigurationService.updateById(alarmRule);
                    ruleUpdateEventService.saveEventWarning(alarmRule,oldRule2);
                }else
                {
                    result = proEconAlarmConfigurationService.updateById(alarmRule);
//                  ruleUpdateEventService.saveEventWarning(alarmRule,null);
                }

            }else
            {
                alarmRule.setId(String.valueOf(SnowflakeGenerator.generateId()));
//                ruleUpdateEventService.saveEventWarning(alarmRule,null);
                result = saveAlertrule(alarmRule);
            }

        }


   //     proEconAlarmConfigurationService.inital(alarmRule);

        return result;
    }

    public boolean deleteAlertrule(String id) {
        return proEconAlarmConfigurationService.removeById(id);
    }


    public IPage<ProEconAlarmConfiguration> pageQueryAll(Page page, String name, String station, String modelId,String alarmType) {


        if(StringUtils.notEmp(name))
        {
            name="%"+name+"%";
        }else
        {
            name=null;
        }
        if(StringUtils.empty(station))
        {
            station=null;
        }
        if(StringUtils.empty(modelId))
        {
            modelId=null;
        }
        if(StringUtils.empty(alarmType))
        {
            alarmType=null;
        }

        IPage<ProEconAlarmConfiguration> alertruleIPage = proEconAlarmConfigurationService.pageQueryAll(page, name, station, modelId,alarmType);

        return alertruleIPage;
    }

    /**
     * 查询报警信息接口
     * @param wpId 场站不编号
     * @param modelId 型号编号
     * @param components 部件编号
     * @return
     */
    public List<AlarmConfigurationVo> queryWtAlarmDescList(String wpId, String modelId, String components) {

        List<AlarmConfigurationVo> vos=new ArrayList<>();
        StringBuilder sb=new StringBuilder();
        sb.append(wpId).append(modelId);


        Map<String,String> map=new HashMap<>();

        if(com.gyee.common.model.StringUtils.notEmp(components))
        {
            String [] componentsStr=components.split(",");
            for(String c:componentsStr)
            {
                map.put(c,c);
            }

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

            if(!ls.isEmpty())
            {
                for(ProEconAlarmConfiguration ac:ls)
                {
                    if(map.containsKey(ac.getComponents()))
                    {

                        AlarmConfigurationVo vo=new AlarmConfigurationVo();
                        vo.setAlarmId(ac.getId());
                        vo.setDescription(ac.getDescription());
                        vos.add(vo);
                    }
                }
            }

        }

        return vos;
    }
}
