package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.pagehelper.PageInfo; import com.whyc.dto.Response; import com.whyc.mapper.AlarmRuleMapper; import com.whyc.pojo.AlarmRule; import io.swagger.models.auth.In; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.stream.Stream; @Service public class AlarmRuleService { @Resource private AlarmRuleMapper mapper; public Response> getRuleByDeviceFlag(int deviceFlag) { QueryWrapper wrapper = Wrappers.query(); if(deviceFlag==201){ wrapper.in("device_flag",201,202,203,204); }else { wrapper.eq("device_flag", deviceFlag); } List alarmRules = mapper.selectList(wrapper); return new Response>().set(1,alarmRules); } /** * 必须考虑阈值和级别的关系 * TODO 必须考虑阈值和级别的关系 * @param alarmRule * @return */ public Response add(AlarmRule alarmRule) { mapper.insert(alarmRule); return new Response().setMsg(1,"新增成功"); } public Response update(AlarmRule alarmRule) { mapper.updateById(alarmRule); return new Response().setMsg(1,"更新成功"); } public Response delete(int id) { mapper.deleteById(id); return new Response().set(1,"删除成功"); } /** * 提前校验,如果库中存在 同 设备&字段&阈值标识 * 的数据,则需要根据当前选择的级别,给出阈值 * * 如果数据库中已经存在当前传入的级别,说明当前是编辑时修改阈值,故先排除当前级别的值 * @param alarmRule * @return */ public Response getThresholdValueByLevel(AlarmRule alarmRule) { List listWithSameThreshold = mapper.getListWithSameThreshold(alarmRule); //查询到level和thresholdValue,对比当前的level,得到允许的阈值范围 //获取到的就近的level //如果thresholdFlag为1,过低,level越大,thresholdValue应该越小 //如果thresholdFlag为2,过高,level越大,thresholdValue应该越大 final Object[][] aboveCloserLevel = new Object[1][2]; final Object[][] belowCloserLevel = new Object[1][2]; LinkedList thresholdValueRange = new LinkedList<>(); if(alarmRule.getThresholdFlag()==2) { listWithSameThreshold.stream().forEach(alarmRuleLevel -> { if(alarmRule.getLevel()==alarmRuleLevel.getLevel()){ //如果告警级别相等,不携带数据作参考,这里必定是编辑时的阈值范围获取 } else if (alarmRule.getLevel() > alarmRuleLevel.getLevel()) { if (belowCloserLevel[0][0] != null) { if ((Integer) (belowCloserLevel[0][0]) < alarmRuleLevel.getLevel()) { belowCloserLevel[0][0] = alarmRuleLevel.getLevel(); belowCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } else { belowCloserLevel[0][0] = alarmRuleLevel.getLevel(); belowCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } else { if (aboveCloserLevel[0][0] != null) { if ((Integer) (aboveCloserLevel[0][0]) > alarmRuleLevel.getLevel()) { aboveCloserLevel[0][0] = alarmRuleLevel.getLevel(); aboveCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } else { aboveCloserLevel[0][0] = alarmRuleLevel.getLevel(); aboveCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } }); thresholdValueRange.add(belowCloserLevel[0][1]); thresholdValueRange.add(aboveCloserLevel[0][1]); }else{ listWithSameThreshold.stream().forEach(alarmRuleLevel -> { if(alarmRule.getLevel()==alarmRuleLevel.getLevel()){ //如果告警级别相等,不携带数据作参考,这里必定是编辑时的阈值范围获取 } else if (alarmRule.getLevel() > alarmRuleLevel.getLevel()) { if (belowCloserLevel[0][0] != null) { if ((Integer) (belowCloserLevel[0][0]) < alarmRuleLevel.getLevel()) { belowCloserLevel[0][0] = alarmRuleLevel.getLevel(); belowCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } else { belowCloserLevel[0][0] = alarmRuleLevel.getLevel(); belowCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } else { if (aboveCloserLevel[0][0] != null) { if ((Integer) (aboveCloserLevel[0][0]) > alarmRuleLevel.getLevel()) { aboveCloserLevel[0][0] = alarmRuleLevel.getLevel(); aboveCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } else { aboveCloserLevel[0][0] = alarmRuleLevel.getLevel(); aboveCloserLevel[0][1] = alarmRuleLevel.getThresholdValue(); } } }); thresholdValueRange.add(aboveCloserLevel[0][1]); thresholdValueRange.add(belowCloserLevel[0][1]); } return new Response().set(1,thresholdValueRange); } public Response getLevelListWithSameAlarm(Integer deviceFlag, String field, Integer thresholdFlag) { List levelList = mapper.getLevelListWithSameAlarm(deviceFlag,field,thresholdFlag); return new Response().set(1,levelList); } }