|
@@ -0,0 +1,835 @@
|
|
|
+package com.persagy.apm.dmpalarm.service.impl;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.persagy.apm.dmpalarm.config.DmpParameterStorage;
|
|
|
+import com.persagy.apm.dmpalarm.criteria.CriteriaUtils;
|
|
|
+import com.persagy.apm.dmpalarm.criteria.JsonCriteria;
|
|
|
+import com.persagy.apm.dmpalarm.enumeration.EnumAlarmMessageType;
|
|
|
+import com.persagy.apm.dmpalarm.jms.MessageProcesser;
|
|
|
+import com.persagy.apm.dmpalarm.model.AlarmConfig;
|
|
|
+import com.persagy.apm.dmpalarm.model.AlarmItem;
|
|
|
+import com.persagy.apm.dmpalarm.model.AlarmTarget;
|
|
|
+import com.persagy.apm.dmpalarm.model.DmpMessage;
|
|
|
+import com.persagy.apm.dmpalarm.service.IAlarmConfigService;
|
|
|
+import com.persagy.apm.dmpalarm.service.IAlarmItemService;
|
|
|
+import com.persagy.apm.dmpalarm.service.IAlarmTargetService;
|
|
|
+import com.persagy.apm.dmpalarm.service.IPhysicalWorldService;
|
|
|
+import com.persagy.apm.dmpalarm.utils.CheckRequiredParam;
|
|
|
+import com.persagy.apm.dmpalarm.utils.DatePatternStyle;
|
|
|
+import com.persagy.apm.dmpalarm.web.BaseResponse;
|
|
|
+import com.persagy.apm.dmpalarm.web.MapResponse;
|
|
|
+import com.persagy.apm.dmpalarm.web.PagedResponse;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 报警定义(AlarmConfig) service层
|
|
|
+ *
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021-12-09 10:44:19
|
|
|
+ */
|
|
|
+@Service("dmpAlarmConfigService")
|
|
|
+@Slf4j
|
|
|
+public class AlarmConfigServiceImpl extends BaseServiceImpl<AlarmConfig> implements IAlarmConfigService {
|
|
|
+ @Resource
|
|
|
+ private MessageProcesser messageProcesser;
|
|
|
+ @Resource(name="dmpAlarmItemService")
|
|
|
+ private IAlarmItemService alarmItemService;
|
|
|
+ @Resource
|
|
|
+ private IAlarmTargetService alarmTargetService;
|
|
|
+ @Resource
|
|
|
+ private IPhysicalWorldService physicalWorldService;
|
|
|
+ @Resource
|
|
|
+ private CriteriaUtils criteriaUtils;
|
|
|
+
|
|
|
+ public PagedResponse<AlarmConfig> query(JsonCriteria criteria) {
|
|
|
+ QueryWrapper<AlarmConfig> queryWrapper = criteriaUtils.handleAlarmConditions(criteria, AlarmConfig.class);
|
|
|
+ return criteriaUtils.getPagedResponse(criteria, AlarmConfig.class, queryWrapper, getBaseMapper());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 当新增对象时,自动生成对应的报警定义
|
|
|
+ * @param: objId 对象id
|
|
|
+ * @param: classCode 对象分类
|
|
|
+ * @param: projectId 项目id
|
|
|
+ * @return: com.persagy.common.web.BaseResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/20 11:30 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ @Transactional
|
|
|
+ public BaseResponse createOnAddObj(String objId, String classCode, String projectId) {
|
|
|
+ BaseResponse response = new BaseResponse();
|
|
|
+ /* 查询根据对象类型查询对应的报警匹配条件 */
|
|
|
+ JsonCriteria criteria = new JsonCriteria();
|
|
|
+ criteria.getCriteria().put("classCode", classCode);
|
|
|
+ criteria.getCriteria().put("projectId", projectId);
|
|
|
+ PagedResponse<AlarmTarget> pagedTargets = alarmTargetService.query(criteria);
|
|
|
+ List<AlarmTarget> targets = pagedTargets.getData();
|
|
|
+ /* 拼装报警定义对象 */
|
|
|
+ if (!CollectionUtils.isEmpty(targets)) {
|
|
|
+ Map<String, AlarmItem> itemCodeModelMap = alarmTargetService.getItemCodeModelMapByModelList(targets);
|
|
|
+ List<AlarmConfig> alarmConfigs = new ArrayList<>(targets.size());
|
|
|
+ for (AlarmTarget targetModel : targets) {
|
|
|
+ /* 拼装报警定义实体 */
|
|
|
+ AlarmItem itemModel = itemCodeModelMap.get(targetModel.getItemCode());
|
|
|
+ // 报警触发条件
|
|
|
+ AlarmConfig entity = generateAlarmConfigEntity(objId, targetModel, itemModel);
|
|
|
+ alarmConfigs.add(entity);
|
|
|
+ }
|
|
|
+ DmpMessage createdMessage = saveAll(alarmConfigs);
|
|
|
+ if (createdMessage != null) {
|
|
|
+ messageProcesser.convertAndSend(createdMessage);
|
|
|
+ }
|
|
|
+ response.setMessage("成功创建对象的相关报警定义,生成条数:" + alarmConfigs.size());
|
|
|
+ } else {
|
|
|
+ response.setMessage("没有查找到对应的匹配条件,不生成报警定义");
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 生成报警定义对象
|
|
|
+ * @param: objId 对象id
|
|
|
+ * @param: targetModel 匹配条件
|
|
|
+ * @param: itemModel 报警条目
|
|
|
+ * @return: com.persagy.dmp.alarm.entity.AlarmConfig
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/21 11:09 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private AlarmConfig generateAlarmConfigEntity(String objId, AlarmTarget targetModel, AlarmItem itemModel) {
|
|
|
+ return generateAlarmConfigEntity(objId, targetModel, itemModel, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 生成报警定义对象
|
|
|
+ * @param: objId 对象id
|
|
|
+ * @param: targetModel 匹配条件
|
|
|
+ * @param: itemModel 报警条目
|
|
|
+ * @param: condition 触发条件
|
|
|
+ * @return: com.persagy.dmp.alarm.entity.AlarmConfig
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/21 11:09 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private AlarmConfig generateAlarmConfigEntity(String objId, AlarmTarget targetModel, AlarmItem itemModel, JSONObject condition) {
|
|
|
+ if (condition == null) {
|
|
|
+ condition = alarmTargetService.getCondition(targetModel, itemModel);
|
|
|
+ }
|
|
|
+ AlarmConfig entity = new AlarmConfig();
|
|
|
+ entity.setId(UUID.randomUUID().toString());
|
|
|
+ entity.setObjId(objId);
|
|
|
+ entity.setTargetId(targetModel.getId());
|
|
|
+ entity.setClassCode(targetModel.getClassCode());
|
|
|
+ entity.setProjectId(targetModel.getProjectId());
|
|
|
+ entity.setItemCode(targetModel.getItemCode());
|
|
|
+ entity.setCategory(itemModel.getCategory());
|
|
|
+ entity.setLevel(targetModel.getLevel());
|
|
|
+ entity.setRemark(itemModel.getRemark());
|
|
|
+ entity.setGroupCode(targetModel.getGroupCode());
|
|
|
+ entity.setCondition(condition);
|
|
|
+ entity.setOpen(targetModel.getOpen());
|
|
|
+ entity.setConcern(targetModel.getConcern());
|
|
|
+ entity.setUserDefined(0);
|
|
|
+ entity.setCreateUser("System");
|
|
|
+ entity.setCreateTime(new Date());
|
|
|
+ entity.setValid(1);
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 当报警条目修改时,批量更新对应的报警定义
|
|
|
+ * @param: itemCode 报警条目编码
|
|
|
+ * @param: projectId 项目id
|
|
|
+ * @param: groupCode 集团编码
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/20 11:30 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public MapResponse batchUpdateWhenItemUpdate(String itemCode, String projectId, String groupCode) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ // 查询报警条目
|
|
|
+ AlarmItem itemModel = alarmItemService.getAlarmItemByItemCode(itemCode, projectId, groupCode, null);
|
|
|
+ if (itemModel == null) {
|
|
|
+ response.setFail("自动生成报警定义失败,获取不到对应的报警条目!itemCode:" + itemCode);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ /* 获取报警条目对应的所有匹配条件 */
|
|
|
+ List<AlarmTarget> AlarmTargets = alarmTargetService.getAlarmTargetsByItemCode(itemCode, projectId);
|
|
|
+ if (CollectionUtils.isEmpty(AlarmTargets)) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> classCodes = AlarmTargets.stream().map(AlarmTarget::getClassCode).collect(Collectors.toList());
|
|
|
+ try {
|
|
|
+ /* 获取这些匹配条件对应的设备 */
|
|
|
+ Map<String, Set<String>> codeIdListMap = physicalWorldService.queryCodeIdListMapByClassCodes(projectId, groupCode, classCodes);
|
|
|
+ /* 获取要删除和重新生成的报警定义 */
|
|
|
+ List<AlarmConfig> alarmConfigs2Delete = Lists.newArrayList();
|
|
|
+ List<AlarmConfig> alarmConfigs2Create = Lists.newArrayList();
|
|
|
+ // 按照匹配条件统计要修改的报警定义列表
|
|
|
+ for (AlarmTarget AlarmTarget : AlarmTargets) {
|
|
|
+ String classCode = AlarmTarget.getClassCode();
|
|
|
+ Set<String> objIds = codeIdListMap.get(classCode);
|
|
|
+ Map<String, List<AlarmConfig>> statisticsMap = statisticConfigs2Update(objIds, projectId, AlarmTarget, itemModel);
|
|
|
+ if (statisticsMap == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(statisticsMap.get("deleted"))) {
|
|
|
+ alarmConfigs2Delete.addAll(statisticsMap.get("deleted"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(statisticsMap.get("created"))) {
|
|
|
+ alarmConfigs2Create.addAll(statisticsMap.get("created"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DmpMessage message = saveAndDeleteAll(alarmConfigs2Create, alarmConfigs2Delete);
|
|
|
+ if (message != null) {
|
|
|
+ messageProcesser.convertAndSend(message);
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ } catch (Exception e) {
|
|
|
+ response.setFail("调用物理世界接口获取设备类型下的设备失败!");
|
|
|
+ e.printStackTrace();
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 当报警匹配条件修改时,批量更新对应的报警定义
|
|
|
+ * @param: itemCode 报警条目编码
|
|
|
+ * @param: classCode 对象类型编码
|
|
|
+ * @param: projectId 项目id
|
|
|
+ * @param: groupCode 集团编码
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/20 11:30 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public MapResponse batchUpdateWhenTargetUpdate(String targetId, String projectId, String groupCode, String appId) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ AlarmTarget alarmTarget = alarmTargetService.get(targetId);
|
|
|
+ if (alarmTarget == null) {
|
|
|
+ response.setFail("自动生成报警定义失败,获取不到对应的报警匹配条件!targetId:" + targetId);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ String itemCode = alarmTarget.getItemCode();
|
|
|
+ String classCode = alarmTarget.getClassCode();
|
|
|
+
|
|
|
+ /* 获取到报警匹配条件和报警条目对象 */
|
|
|
+ AlarmItem itemModel = alarmItemService.getAlarmItemByItemCode(itemCode, projectId, groupCode, appId);
|
|
|
+ if (itemModel == null) {
|
|
|
+ response.setFail("自动生成报警定义失败,获取不到对应的报警条目!itemCode:" + itemCode);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> objIds = new HashSet<>();
|
|
|
+ try {
|
|
|
+ /* 查询设备类型下的所有设备 */
|
|
|
+ objIds = physicalWorldService.queryObjectIdListByClassCode(projectId, groupCode, classCode);
|
|
|
+ } catch (Exception e) {
|
|
|
+ response.setFail("调用物理世界接口获取设备类型下的设备失败!");
|
|
|
+ e.printStackTrace();
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ // 统计需要删除和需要重新生成的报警定义
|
|
|
+ Map<String, List<AlarmConfig>> statisticMap = statisticConfigs2Update(objIds, projectId, alarmTarget, itemModel);
|
|
|
+ if (statisticMap == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ /* 删掉需要删除的报警定义,生成需要重新生成的报警定义 */
|
|
|
+ List<AlarmConfig> deletedAlarmConfigs = statisticMap.get("deleted");
|
|
|
+ List<AlarmConfig> createdAlarmConfigs = statisticMap.get("created");
|
|
|
+ DmpMessage message = saveAndDeleteAll(createdAlarmConfigs, deletedAlarmConfigs);
|
|
|
+ if (message != null) {
|
|
|
+ messageProcesser.convertAndSend(message);
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return DmpMessage
|
|
|
+ * @description: 删除报警定义,并推送删除消息
|
|
|
+ * @param: deletedAlarmConfigs
|
|
|
+ * @return: void
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/26 11:14 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private DmpMessage deleteAll(List<AlarmConfig> deletedAlarmConfigs) {
|
|
|
+ if (!CollectionUtils.isEmpty(deletedAlarmConfigs)) {
|
|
|
+ DmpMessage message = generateMessage(null, deletedAlarmConfigs);
|
|
|
+ removeByIds(deletedAlarmConfigs.stream().map(AlarmConfig::getId).collect(Collectors.toList()));
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 创建报警定义,并推送创建消息
|
|
|
+ * @param: createdAlarmConfigs
|
|
|
+ * @return: com.persagy.dmp.rwd.model.DmpMessage
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/26 10:18 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private DmpMessage saveAll(List<AlarmConfig> createdAlarmConfigs) {
|
|
|
+ if (!CollectionUtils.isEmpty(createdAlarmConfigs)) {
|
|
|
+ DmpMessage message = generateMessage(createdAlarmConfigs, null);
|
|
|
+ saveBatch(createdAlarmConfigs);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 创建、删除报警定义,并推送相关消息
|
|
|
+ * @param: createdConfigs
|
|
|
+ * @param: deletedConfigs
|
|
|
+ * @return: com.persagy.dmp.rwd.model.DmpMessage
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/26 10:20 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private DmpMessage saveAndDeleteAll(List<AlarmConfig> createdConfigs, List<AlarmConfig> deletedConfigs) {
|
|
|
+ DmpMessage message = generateMessage(createdConfigs, deletedConfigs);
|
|
|
+ /* 先删后加 */
|
|
|
+ if (!CollectionUtils.isEmpty(deletedConfigs)) {
|
|
|
+ removeByIds(deletedConfigs.stream().map(AlarmConfig::getId).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(createdConfigs)) {
|
|
|
+ saveBatch(createdConfigs);
|
|
|
+ }
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 报警定义变动,生成需要推送的消息
|
|
|
+ * @param: createdConfigs 新增的报警定义
|
|
|
+ * @param: deletedConfigs 删除的报警定义
|
|
|
+ * @return: com.persagy.dmp.rwd.model.DmpMessage
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/26 11:34 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private DmpMessage generateMessage(List<AlarmConfig> createdConfigs, List<AlarmConfig> deletedConfigs) {
|
|
|
+ List<AlarmConfig.AlarmConfigUnique> createdConfigUniques = null;
|
|
|
+ List<AlarmConfig.AlarmConfigUnique> deletedConfigUniques = null;
|
|
|
+ if (!CollectionUtils.isEmpty(createdConfigs)) {
|
|
|
+ createdConfigUniques = createdConfigs.stream().map(
|
|
|
+ AlarmConfig::getAlarmConfigUnique).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(deletedConfigs)) {
|
|
|
+ deletedConfigUniques = deletedConfigs.stream().map(
|
|
|
+ AlarmConfig::getAlarmConfigUnique).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(createdConfigUniques) || !CollectionUtils.isEmpty(deletedConfigUniques)) {
|
|
|
+ DmpMessage msg = new DmpMessage();
|
|
|
+ msg.setMid(UUID.randomUUID().toString());
|
|
|
+ msg.setType(EnumAlarmMessageType.ALARM_CONFIGS_CHANGE.getValue());
|
|
|
+ msg.setGroupCode(DmpParameterStorage.getGroupCode());
|
|
|
+ msg.setProjectId(DmpParameterStorage.getProjectId());
|
|
|
+ msg.add("createdConfigUniques", createdConfigUniques);
|
|
|
+ msg.add("deletedConfigUniques", deletedConfigUniques);
|
|
|
+ msg.setAppId(DmpParameterStorage.getAppId());
|
|
|
+ msg.setSendTime(DateUtil.format(new Date(), DatePatternStyle.PATTERN_YYYYMMDDHHMMSS));
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 按照报警匹配条件统计需要删除和重新创建的报警定义
|
|
|
+ * @param: objIds 设备id列表
|
|
|
+ * @param: projectId 项目id
|
|
|
+ * @param: AlarmTarget 报警匹配条件
|
|
|
+ * @param: itemModel 报警条目
|
|
|
+ * @return: java.util.Map<java.lang.String, java.util.List < com.persagy.dmp.alarm.entity.AlarmConfig>>
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/21 2:50 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private Map<String, List<AlarmConfig>> statisticConfigs2Update(
|
|
|
+ Set<String> objIds, String projectId, AlarmTarget AlarmTarget, AlarmItem itemModel) {
|
|
|
+ log.info("设备id列表:" + objIds);
|
|
|
+ if (objIds == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ /* 查询这些设备的报警定义 */
|
|
|
+ JsonCriteria criteria = new JsonCriteria();
|
|
|
+ criteria.getCriteria().put("objId", Lists.newArrayList(objIds));
|
|
|
+ criteria.getCriteria().put("targetId", AlarmTarget.getId());
|
|
|
+ criteria.getCriteria().put("itemCode", itemModel.getCode());
|
|
|
+ criteria.getCriteria().put("projectId", projectId);
|
|
|
+ List<AlarmConfig> configModels = queryList(criteria);
|
|
|
+ // configModels为空,是因为查询发生了错误,正常情况下没有数据configModels也不是null
|
|
|
+ if (configModels == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ /* 确认要更新的范围,自定义的报警定义不处理 */
|
|
|
+ // 用户自定义的报警定义
|
|
|
+ List<AlarmConfig> userDefinedAlarmConfigs = configModels.stream().filter(
|
|
|
+ configModel -> configModel.getUserDefined() == 1
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+ // 自动生成的报警定义
|
|
|
+ List<AlarmConfig> autoAlarmConfigs = configModels.stream().filter(
|
|
|
+ configModel -> configModel.getUserDefined() != 1
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+ /* 获取本次需要自动生成报警定义的对象id列表 */
|
|
|
+ Set<String> userDefinedObjIds = userDefinedAlarmConfigs.stream().map(AlarmConfig::getObjId).collect(Collectors.toSet());
|
|
|
+ objIds.removeAll(userDefinedObjIds);
|
|
|
+ /* 删掉自动生成的报警定义,按照新的匹配条件生成新的报警定义 */
|
|
|
+ Map<String, List<AlarmConfig>> statisticMap = new HashMap<>();
|
|
|
+ statisticMap.put("deleted", autoAlarmConfigs);
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(objIds)) {
|
|
|
+ JSONObject condition = alarmTargetService.getCondition(AlarmTarget, itemModel);
|
|
|
+ List<AlarmConfig> alarmConfigs = new ArrayList<>();
|
|
|
+ for (String objId : objIds) {
|
|
|
+ AlarmConfig alarmConfig = generateAlarmConfigEntity(objId, AlarmTarget, itemModel, condition);
|
|
|
+ alarmConfigs.add(alarmConfig);
|
|
|
+ }
|
|
|
+ statisticMap.put("created", alarmConfigs);
|
|
|
+ }
|
|
|
+ return statisticMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public AlarmConfig get(String id) {
|
|
|
+ return getById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public MapResponse update(AlarmConfig param) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ AlarmConfig alarmConfig = get(param.getId());
|
|
|
+ if (alarmConfig == null) {
|
|
|
+ response.setFail("无法获取到要更新的数据!");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ AlarmParam alarmParam = prepareParam(response);
|
|
|
+ if (alarmParam == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ updateEntityByParam(param, alarmConfig);
|
|
|
+ // // 发生更新后,报警定义的自定义状态修改为1
|
|
|
+ // alarmConfig.setUserDefined(1);
|
|
|
+ alarmConfig.setUpdateUser(alarmParam.userId);
|
|
|
+ alarmConfig.setUpdateTime(new Date());
|
|
|
+ save(alarmConfig);
|
|
|
+ response.add("id", alarmConfig.getId());
|
|
|
+ // 报警定义发生变化,向mq推送消息
|
|
|
+ DmpMessage msg = getUpdateConfigsDmpMessage(Lists.newArrayList(alarmConfig));
|
|
|
+ if (msg != null) {
|
|
|
+ response.add(msg);
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取更新报警定义消息
|
|
|
+ *
|
|
|
+ * @param alarmConfigList 更新的报警定义id
|
|
|
+ * @return 更新消息
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/3/27 3:30 下午
|
|
|
+ */
|
|
|
+ private DmpMessage getUpdateConfigsDmpMessage(List<AlarmConfig> alarmConfigList) {
|
|
|
+ if (CollectionUtils.isEmpty(alarmConfigList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ DmpMessage msg = new DmpMessage();
|
|
|
+ msg.setMid(UUID.randomUUID().toString());
|
|
|
+ msg.setType(EnumAlarmMessageType.ALARM_CONFIGS_CHANGE.getValue());
|
|
|
+ msg.setGroupCode(DmpParameterStorage.getGroupCode());
|
|
|
+ msg.setProjectId(DmpParameterStorage.getProjectId());
|
|
|
+ List<AlarmConfig.AlarmConfigUnique> alarmConfigUniqueList = alarmConfigList.stream().
|
|
|
+ map(AlarmConfig::getAlarmConfigUnique).collect(Collectors.toList());
|
|
|
+ msg.add("updatedConfigUniques", alarmConfigUniqueList);
|
|
|
+ msg.setAppId(DmpParameterStorage.getAppId());
|
|
|
+ msg.setSendTime(DateUtil.format(new Date(), DatePatternStyle.PATTERN_YYYYMMDDHHMMSS));
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 根据更新条件更新实体
|
|
|
+ * @param: param
|
|
|
+ * @param: alarmConfig
|
|
|
+ * @return: void
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/22 2:52 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private void updateEntityByParam(AlarmConfig param, AlarmConfig alarmConfig) {
|
|
|
+ // 报警等级
|
|
|
+ if (param.getLevel() != null && !param.getLevel().isEmpty()) {
|
|
|
+ alarmConfig.setLevel(param.getLevel());
|
|
|
+ }
|
|
|
+ // 触发条件
|
|
|
+ if (param.getCondition() != null && !param.getCondition().isEmpty()) {
|
|
|
+ alarmConfig.setCondition(param.getCondition());
|
|
|
+ }
|
|
|
+ // 备注
|
|
|
+ if (param.getRemark() != null && !param.getRemark().isEmpty()) {
|
|
|
+ alarmConfig.setRemark(param.getRemark());
|
|
|
+ }
|
|
|
+ // 是否重点关注
|
|
|
+ if (param.getConcern() != null) {
|
|
|
+ alarmConfig.setConcern(param.getConcern());
|
|
|
+ }
|
|
|
+ // 是否屏蔽
|
|
|
+ if (param.getOpen() != null) {
|
|
|
+ alarmConfig.setOpen(param.getOpen());
|
|
|
+ }
|
|
|
+ // 报警定义的自定义状态
|
|
|
+ if (param.getUserDefined() != null) {
|
|
|
+ alarmConfig.setUserDefined(param.getUserDefined());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 删除设备时,同时删除设备对应的报警定义
|
|
|
+ * @param: objId 对象id
|
|
|
+ * @param: projectId 项目id
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/20 9:51 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public MapResponse deleteOnRemoveObj(String objId, String projectId) {
|
|
|
+ /* 查询设备相关的报警定义 */
|
|
|
+ JsonCriteria criteria = new JsonCriteria();
|
|
|
+ criteria.getCriteria().put("objId", objId);
|
|
|
+ criteria.getCriteria().put("projectId", projectId);
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ deleteByCriteria(criteria);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 条件查询
|
|
|
+ * @param: criteria
|
|
|
+ * @return: java.util.List<com.persagy.dmp.alarm.model.AlarmConfig>
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/21 2:22 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private List<AlarmConfig> queryList(JsonCriteria criteria) {
|
|
|
+ PagedResponse<AlarmConfig> pagedResponse = query(criteria);
|
|
|
+ if (pagedResponse == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return pagedResponse.getData();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 初始化所有报警定义
|
|
|
+ * @param: param
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/21 3:08 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public MapResponse init(AlarmConfig param) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ if (StringUtils.isEmpty(param.getProjectId())) {
|
|
|
+ response.setFail("项目id必填!");
|
|
|
+ }
|
|
|
+ JsonCriteria criteria = new JsonCriteria();
|
|
|
+ ;
|
|
|
+
|
|
|
+ List<AlarmItem> AlarmItems = alarmItemService.queryList(criteria);
|
|
|
+ for (AlarmItem AlarmItem : AlarmItems) {
|
|
|
+ batchUpdateWhenItemUpdate(AlarmItem.getCode(), param.getProjectId(), param.getGroupCode());
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 批量更新报警定义
|
|
|
+ * @param: param
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/22 2:38 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public MapResponse batchUpdate(List<AlarmConfig> params) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ if (params == null) {
|
|
|
+ response.setFail("请传入要修改的报警定义!");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 一次最多更新200条数据
|
|
|
+ if (params.size() > 200) {
|
|
|
+ response.setFail("一次最多更新200条数据");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ AlarmParam alarmParam = prepareParam(response);
|
|
|
+ if (alarmParam == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 更新报警定义对象 */
|
|
|
+ Date date = new Date();
|
|
|
+ List<AlarmConfig> configs = Lists.newArrayListWithCapacity(params.size());
|
|
|
+ for (AlarmConfig param : params) {
|
|
|
+ String checkResult = CheckRequiredParam.check(param, "id");
|
|
|
+ if (!StringUtils.isEmpty(checkResult)) {
|
|
|
+ response.setFail(checkResult);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ AlarmConfig alarmConfig = getById(param.getId());
|
|
|
+ updateEntityByParam(param, alarmConfig);
|
|
|
+ configs.add(alarmConfig);
|
|
|
+ }
|
|
|
+ saveBatch(configs);
|
|
|
+ DmpMessage updateConfigsDmpMessage = getUpdateConfigsDmpMessage(configs);
|
|
|
+ messageProcesser.convertAndSend(updateConfigsDmpMessage);
|
|
|
+ Date date1 = new Date();
|
|
|
+ System.out.println("执行时间:" + (date1.getTime() - date.getTime()));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 创建报警定义
|
|
|
+ * @param: param
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/11/25 9:38 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ public MapResponse create(AlarmConfig param) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ AlarmParam alarmParam = prepareParam(response);
|
|
|
+ if (alarmParam == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ AlarmConfig entity = initAlarmConfigEntity(param, alarmParam, response);
|
|
|
+ if (entity == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ save(entity);
|
|
|
+ // 发送报警定义创建消息
|
|
|
+ DmpMessage msg = generateMessage(Lists.newArrayList(entity), null);
|
|
|
+ messageProcesser.convertAndSend(msg);
|
|
|
+ response.add("id", entity.getId());
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ private AlarmConfig initAlarmConfigEntity(AlarmConfig entity, AlarmParam alarmParam, MapResponse response) {
|
|
|
+ // 必填项校验
|
|
|
+ String checkResult = CheckRequiredParam.check(entity,
|
|
|
+ "objId", "classCode", "itemCode", "condition", "level");
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(checkResult)) {
|
|
|
+ response.setFail(checkResult);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ entity.setId(UUID.randomUUID().toString());
|
|
|
+ entity.setCreateUser(alarmParam.userId);
|
|
|
+ entity.setCreateTime(new Date());
|
|
|
+ entity.setValid(1);
|
|
|
+ entity.setProjectId(alarmParam.projectId);
|
|
|
+ entity.setGroupCode(alarmParam.groupCode);
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量创建报警定义
|
|
|
+ *
|
|
|
+ * @param configModels 报警定义列表
|
|
|
+ * @return 报警定义id列表
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/10/11 8:08 下午
|
|
|
+ */
|
|
|
+ public MapResponse batchCreate(List<AlarmConfig> configModels) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ if (configModels == null) {
|
|
|
+ response.setFail("请传入要创建的报警定义!");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ AlarmParam alarmParam = prepareParam(response);
|
|
|
+ if (alarmParam == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ Date date = new Date();
|
|
|
+ List<AlarmConfig> configs = new ArrayList<>(configModels.size());
|
|
|
+ for (AlarmConfig configModel : configModels) {
|
|
|
+ AlarmConfig entity = initAlarmConfigEntity(configModel, alarmParam, response);
|
|
|
+ if (entity == null) {
|
|
|
+ response.setFail("批量创建报警条件失败,参数中存在null对象");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ configs.add(entity);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ saveBatch(configs);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Date date1 = new Date();
|
|
|
+ System.out.println("报警定义存储完成,耗时:" + (date1.getTime() - date.getTime()));
|
|
|
+ DmpMessage msg = generateMessage(configs, null);
|
|
|
+ messageProcesser.convertAndSend(msg);
|
|
|
+ List<String> ids = configs.stream().map(AlarmConfig::getId).collect(Collectors.toList());
|
|
|
+ date1 = new Date();
|
|
|
+ System.out.println("执行时间:" + (date1.getTime() - date.getTime()));
|
|
|
+ response.add("ids", ids);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 报警匹配条件删除时,删除对应的报警定义
|
|
|
+ * @param: targetId
|
|
|
+ * @param: projectId
|
|
|
+ * @param: groupCode
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/12/10 11:45 上午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public MapResponse batchDeleteWhenTargetDelete(String targetId, String projectId, String groupCode) {
|
|
|
+ MapResponse mapResponse = new MapResponse();
|
|
|
+ JsonCriteria criteria = new JsonCriteria();
|
|
|
+ criteria.getCriteria().put("targetId", targetId);
|
|
|
+ criteria.getCriteria().put("projectId", projectId);
|
|
|
+ criteria.getCriteria().put("groupCode", groupCode);
|
|
|
+ // 非用户自定义的
|
|
|
+ criteria.getCriteria().put("userDefined", 0);
|
|
|
+ deleteByCriteria(criteria);
|
|
|
+ return mapResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 根据条件删除
|
|
|
+ * @param: criteria
|
|
|
+ * @return: void
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/12/10 2:04 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private void deleteByCriteria(JsonCriteria criteria) {
|
|
|
+ List<AlarmConfig> configs = queryList(criteria);
|
|
|
+ if (configs == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ DmpMessage deletedMessage = deleteAll(configs);
|
|
|
+ if (deletedMessage != null) {
|
|
|
+ messageProcesser.convertAndSend(deletedMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除报警定义,真删
|
|
|
+ *
|
|
|
+ * @param param 报警定义对象AlarmConfig,只取id
|
|
|
+ * @return 是否删除成功
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/3/20 10:49 上午
|
|
|
+ */
|
|
|
+ public MapResponse delete(AlarmConfig param) {
|
|
|
+ JsonCriteria criteria = new JsonCriteria();
|
|
|
+ criteria.getCriteria().put("id", param.getId());
|
|
|
+ deleteByCriteria(criteria);
|
|
|
+ return new MapResponse();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除报警定义,真删
|
|
|
+ *
|
|
|
+ * @param param 批量删除对象AlarmConfigs
|
|
|
+ * @return 是否删除成功
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/3/20 10:59 上午
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public MapResponse batchDelete(AlarmConfig param) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ List<AlarmConfig> alarmConfigs = param.getAlarmConfigs();
|
|
|
+ if (CollectionUtils.isEmpty(alarmConfigs)) {
|
|
|
+ response.setFail("批量删除报警定义,没有传入需要删除的对象");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> configIds = alarmConfigs.stream().map(AlarmConfig::getId).collect(Collectors.toList());
|
|
|
+ JsonCriteria criteria = new JsonCriteria();
|
|
|
+ criteria.getCriteria().put("id", configIds);
|
|
|
+ deleteByCriteria(criteria);
|
|
|
+
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+}
|