|  | @@ -1,6 +1,8 @@
 | 
	
		
			
				|  |  |  package com.zcxk.rmcp.web.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.github.pagehelper.page.PageMethod;
 | 
	
		
			
				|  |  | +import com.zcxk.core.common.exception.BusinessException;
 | 
	
		
			
				|  |  |  import com.zcxk.core.common.util.BeanCopyUtils;
 | 
	
		
			
				|  |  |  import com.zcxk.core.mysql.pageing.Pagination;
 | 
	
		
			
				|  |  |  import com.zcxk.core.oauth2.pojo.LoginUser;
 | 
	
	
		
			
				|  | @@ -11,7 +13,10 @@ import com.zcxk.rmcp.api.dto.alarm.ConfigDataDto;
 | 
	
		
			
				|  |  |  import com.zcxk.rmcp.api.dto.alarm.RuleMeasuringDto;
 | 
	
		
			
				|  |  |  import com.zcxk.rmcp.core.dao.DeviceAlarmRuleMapper;
 | 
	
		
			
				|  |  |  import com.zcxk.rmcp.core.entity.DeviceAlarmRule;
 | 
	
		
			
				|  |  | +import com.zcxk.rmcp.core.entity.TplMeasuringDataDef;
 | 
	
		
			
				|  |  |  import com.zcxk.rmcp.web.service.AlarmRuleService;
 | 
	
		
			
				|  |  | +import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  | +import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.springframework.util.CollectionUtils;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -24,6 +29,7 @@ import java.util.List;
 | 
	
		
			
				|  |  |   * @date 2021-07-30 9:44
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  | +@Slf4j
 | 
	
		
			
				|  |  |  public class AlarmRuleServiceImpl implements AlarmRuleService {
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      private DeviceAlarmRuleMapper deviceAlarmRuleMapper;
 | 
	
	
		
			
				|  | @@ -46,15 +52,55 @@ public class AlarmRuleServiceImpl implements AlarmRuleService {
 | 
	
		
			
				|  |  |          deviceAlarmRule.setCreateDate(new Date());
 | 
	
		
			
				|  |  |          deviceAlarmRule.setCreateBy(currentUser.getName());
 | 
	
		
			
				|  |  |          deviceAlarmRule.setTenantId(currentUser.getTenantId());
 | 
	
		
			
				|  |  | +        log.info("begin addAlarmType alarmType:" + JSON.toJSONString(deviceAlarmRule));
 | 
	
		
			
				|  |  | +        generationRules(deviceAlarmRule);
 | 
	
		
			
				|  |  |          deviceAlarmRuleMapper.insert(deviceAlarmRule);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    private void generationRules( DeviceAlarmRule deviceAlarmRule){
 | 
	
		
			
				|  |  | +        int resultName = deviceAlarmRuleMapper.findByNameUnique(deviceAlarmRule.getId(), deviceAlarmRule.getRuleName(), deviceAlarmRule.getAlarmTypeId());
 | 
	
		
			
				|  |  | +        if (resultName > 0) {
 | 
	
		
			
				|  |  | +            throw BusinessException.builder(-900, "规则名称已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //查询测点的规则是否添加过
 | 
	
		
			
				|  |  | +        int ruleCount = deviceAlarmRuleMapper.countMeasuringCode(deviceAlarmRule.getId(), deviceAlarmRule.getAlarmTypeId(), deviceAlarmRule.getMeasuringCode());
 | 
	
		
			
				|  |  | +        if (ruleCount > 0) {
 | 
	
		
			
				|  |  | +            throw BusinessException.builder(-900, "该测点的规则已添加过");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //生成表达式和规格
 | 
	
		
			
				|  |  | +        String expression = "";
 | 
	
		
			
				|  |  | +        //规格
 | 
	
		
			
				|  |  | +        String specification = "";
 | 
	
		
			
				|  |  | +        if (deviceAlarmRule.getMeasuringDataType() == 1) {
 | 
	
		
			
				|  |  | +            expression = "#value == \"" + deviceAlarmRule.getValue1() + "\"";
 | 
	
		
			
				|  |  | +            DeviceAlarmRule deviceAlarmRuleTemp = deviceAlarmRuleMapper.findById(deviceAlarmRule.getId());
 | 
	
		
			
				|  |  | +            TplMeasuringDataDef tplMeasuringDataDef = deviceAlarmRuleMapper.findTplMeasuringDataDef(deviceAlarmRuleTemp.getDeviceType(),deviceAlarmRule.getMeasuringCode(),deviceAlarmRule.getValue1());
 | 
	
		
			
				|  |  | +            if (tplMeasuringDataDef != null) {
 | 
	
		
			
				|  |  | +                specification = tplMeasuringDataDef.getDataName();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            String value1 = "#value >= " + deviceAlarmRule.getValue1();
 | 
	
		
			
				|  |  | +            String value2 = "#value <= " + deviceAlarmRule.getValue2();
 | 
	
		
			
				|  |  | +            if (!StringUtils.equals("", deviceAlarmRule.getValue1()) && !StringUtils.equals("", deviceAlarmRule.getValue2())) {
 | 
	
		
			
				|  |  | +                expression = value1 + " and " + value2;
 | 
	
		
			
				|  |  | +                specification = deviceAlarmRule.getValue1() + "<=" + deviceAlarmRule.getMeasuringCode() + "<=" + deviceAlarmRule.getValue2();
 | 
	
		
			
				|  |  | +            } else if (!StringUtils.equals("", deviceAlarmRule.getValue1()) && StringUtils.equals("", deviceAlarmRule.getValue2())) {
 | 
	
		
			
				|  |  | +                expression = value1;
 | 
	
		
			
				|  |  | +                specification = deviceAlarmRule.getMeasuringCode() + ">=" + deviceAlarmRule.getValue1();
 | 
	
		
			
				|  |  | +            } else if (StringUtils.equals("", deviceAlarmRule.getValue1()) && !StringUtils.equals("", deviceAlarmRule.getValue2())) {
 | 
	
		
			
				|  |  | +                expression = value2;
 | 
	
		
			
				|  |  | +                specification = deviceAlarmRule.getMeasuringCode() + "<=" + deviceAlarmRule.getValue2();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        deviceAlarmRule.setExpression(expression);
 | 
	
		
			
				|  |  | +        deviceAlarmRule.setSpecification(specification);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public void edit(AlarmRuleDto alarmRuleDto) {
 | 
	
		
			
				|  |  |          DeviceAlarmRule deviceAlarmRule=new DeviceAlarmRule();
 | 
	
		
			
				|  |  |          BeanCopyUtils.copyProperties(alarmRuleDto, deviceAlarmRule, DeviceAlarmRule.class);
 | 
	
		
			
				|  |  |          deviceAlarmRule.setUpdateBy(UserUtil.getCurrentUser().getName());
 | 
	
		
			
				|  |  |          deviceAlarmRule.setUpdateDate(new Date());
 | 
	
		
			
				|  |  | +        generationRules(deviceAlarmRule);
 | 
	
		
			
				|  |  |          deviceAlarmRuleMapper.update(deviceAlarmRule);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 |