Browse Source

规则添加

hym 3 năm trước cách đây
mục cha
commit
cd1a0ecb36

+ 12 - 0
zoniot-rmcp/zoniot-rmcp-core/src/main/java/com/zcxk/rmcp/core/dao/DeviceAlarmRuleMapper.java

@@ -5,6 +5,7 @@ import com.zcxk.rmcp.api.dto.alarm.AlarmRulePageDto;
 import com.zcxk.rmcp.api.dto.alarm.ConfigDataDto;
 import com.zcxk.rmcp.api.dto.alarm.RuleMeasuringDto;
 import com.zcxk.rmcp.core.entity.DeviceAlarmRule;
+import com.zcxk.rmcp.core.entity.TplMeasuringDataDef;
 import org.apache.ibatis.annotations.Param;
 
 
@@ -93,4 +94,15 @@ public interface DeviceAlarmRuleMapper {
 
     List<ConfigDataDto> getMeasuringOptionList(@Param("deviceTypeId")Integer deviceTypeId,
                                                @Param("measuringId") Integer measuringId);
+
+    int findByNameUnique(@Param("id")Integer id,@Param("ruleName") String ruleName,
+                         @Param("alarmTypeId") Integer alarmTypeId);
+
+    int countMeasuringCode(@Param("id")Integer id,@Param("alarmTypeId") Integer alarmTypeId,
+                           @Param("measuringCode") String measuringCode);
+
+    DeviceAlarmRule findById(Integer id);
+
+    TplMeasuringDataDef findTplMeasuringDataDef(@Param("deviceType")Integer deviceType, @Param("measuringCode")String measuringCode, @Param("value")String value);
+
 }

+ 48 - 0
zoniot-rmcp/zoniot-rmcp-core/src/main/java/com/zcxk/rmcp/core/entity/TplMeasuringDataDef.java

@@ -0,0 +1,48 @@
+package com.zcxk.rmcp.core.entity;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+
+/**
+* Created by Mybatis Generator 2019/01/17
+*/
+@ApiModel(value="com.bz.smart_city.entity.TplMeasuringDataDef")
+@Data
+public class TplMeasuringDataDef {
+
+    @ApiModelProperty(value="测点描述")
+    private Integer measuringDesc;
+
+    @ApiModelProperty(value="数据值")
+    private String dataValue;
+
+    @ApiModelProperty(value="数据名称")
+    private String dataName;
+
+    @ApiModelProperty(value="数据描述")
+    private String dataDesc;
+
+    @JsonIgnore
+    @ApiModelProperty(value="状态")
+    private Integer status;
+
+    @JsonIgnore
+    @ApiModelProperty(value="创建人")
+    private String createBy;
+
+    @JsonIgnore
+    @ApiModelProperty(value="更新人")
+    private String updateBy;
+
+    @JsonIgnore
+    @ApiModelProperty(value="创建时间")
+    private LocalDateTime dateCreate;
+
+    @JsonIgnore
+    @ApiModelProperty(value="更新时间")
+    private LocalDateTime dateUpdate;
+}

+ 20 - 0
zoniot-rmcp/zoniot-rmcp-core/src/main/java/com/zcxk/rmcp/core/mapper/DeviceAlarmRuleMapper.xml

@@ -191,5 +191,25 @@ a.update_date,a. update_by,a. rule_desc, a.value1,a. value2,a. measuring_data_ty
             WHERE stmd.device_type = #{deviceTypeId} and measuring_id=#{measuringId}
         )
     </select>
+    <select id="findByNameUnique" resultType="java.lang.Integer">
+        select count(1) from rmcp_device_alarm_rule
+        where status = 1 and rule_name = #{ruleName} and alarm_type_id = #{alarmTypeId}
+        <if test="id != null"> and id != #{id} </if>
+    </select>
+    <select id="countMeasuringCode" resultType="java.lang.Integer">
+        select count(1) from rmcp_device_alarm_rule
+        where status = 1 and alarm_type_id = #{alarmTypeId} and measuring_code = #{measuringCode}
+        <if test="id != null"> and id != #{id} </if>
+    </select>
+    <select id="findById" resultType="com.zcxk.rmcp.core.entity.DeviceAlarmRule">
+        select <include refid="Base_Column_List"/> from rmcp_device_alarm_rule where status = 1 and id = #{id}
+
+    </select>
+    <select id="findTplMeasuringDataDef" resultType="com.zcxk.rmcp.core.entity.TplMeasuringDataDef">
+        select stmdd.* from rmcp_tpl_measuring_desc stmd
+                                left join rmcp_tpl_measuring_data_def stmdd on (stmdd.measuring_desc = stmd.id and stmdd.data_value = #{value} and stmdd.status = 1)
+        where stmd.status = 1 and stmd.device_type = #{deviceType} and stmd.measuring_code = #{measuringCode}
+
+    </select>
 
 </mapper>

+ 47 - 1
zoniot-rmcp/zoniot-rmcp-web/src/main/java/com/zcxk/rmcp/web/service/impl/AlarmRuleServiceImpl.java

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