|
@@ -0,0 +1,775 @@
|
|
|
+package com.persagy.dmp.alarm.service;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.persagy.common.criteria.CriteriaUtils;
|
|
|
+import com.persagy.common.criteria.JacksonCriteria;
|
|
|
+import com.persagy.common.date.DateUtils;
|
|
|
+import com.persagy.common.web.BaseResponse;
|
|
|
+import com.persagy.common.web.MapResponse;
|
|
|
+import com.persagy.common.web.PagedResponse;
|
|
|
+import com.persagy.dmp.alarm.entity.AlarmConfig;
|
|
|
+import com.persagy.dmp.alarm.entity.AlarmTarget;
|
|
|
+import com.persagy.dmp.alarm.entity.QAlarmConfig;
|
|
|
+import com.persagy.dmp.alarm.enumeration.EnumAlarmMessageType;
|
|
|
+import com.persagy.dmp.alarm.jms.MessageProcesser;
|
|
|
+import com.persagy.dmp.alarm.model.AlarmConfigModel;
|
|
|
+import com.persagy.dmp.alarm.model.AlarmItemModel;
|
|
|
+import com.persagy.dmp.alarm.model.AlarmTargetModel;
|
|
|
+import com.persagy.dmp.alarm.repository.AlarmConfigRepository;
|
|
|
+import com.persagy.dmp.alarm.util.CheckRequiredParam;
|
|
|
+import com.persagy.dmp.config.DmpParameterStorage;
|
|
|
+import com.persagy.dmp.rwd.model.DmpMessage;
|
|
|
+import com.querydsl.core.types.dsl.BooleanExpression;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import javax.transaction.Transactional;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class AlarmConfigService extends AlarmBaseService {
|
|
|
+ @Autowired
|
|
|
+ private MessageProcesser messageProcesser;
|
|
|
+ @Autowired
|
|
|
+ private AlarmConfigRepository alarmConfigRepository;
|
|
|
+ @Autowired
|
|
|
+ private AlarmItemService alarmItemService;
|
|
|
+ @Autowired
|
|
|
+ private AlarmTargetService alarmTargetService;
|
|
|
+ @Autowired
|
|
|
+ private PhysicalWorldService physicalWorldService;
|
|
|
+ @Autowired
|
|
|
+ private CriteriaUtils criteriaUtils;
|
|
|
+
|
|
|
+ private List<BooleanExpression> parse(ObjectNode criteria) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public PagedResponse<AlarmConfigModel> query(JacksonCriteria criteria) {
|
|
|
+ PagedResponse<AlarmConfig> resp = criteriaUtils.query(QAlarmConfig.alarmConfig, this::parse, criteria);
|
|
|
+ PagedResponse<AlarmConfigModel> result = new PagedResponse<>();
|
|
|
+ result.setCount(resp.getCount());
|
|
|
+ List<AlarmConfig> dataList = resp.getData();
|
|
|
+ if (dataList != null && dataList.size() > 0) {
|
|
|
+ List<AlarmConfigModel> collect = dataList.stream().map(entity -> {
|
|
|
+ AlarmConfigModel model = entity.toModel();
|
|
|
+ return model;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ result.setData(collect);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @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
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public BaseResponse createOnAddObj(String objId, String classCode, String projectId) {
|
|
|
+ BaseResponse response = new BaseResponse();
|
|
|
+
|
|
|
+ JacksonCriteria criteria = JacksonCriteria.newInstance();
|
|
|
+ criteria.add("classCode", classCode);
|
|
|
+ criteria.add("projectId", projectId);
|
|
|
+ PagedResponse<AlarmTargetModel> pagedTargets = alarmTargetService.query(criteria);
|
|
|
+ List<AlarmTargetModel> targets = pagedTargets.getData();
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(targets)) {
|
|
|
+ Map<String, AlarmItemModel> itemCodeModelMap = alarmTargetService.getItemCodeModelMapByModelList(targets);
|
|
|
+ List<AlarmConfig> alarmConfigs = new ArrayList<>(targets.size());
|
|
|
+ for (AlarmTargetModel targetModel : targets) {
|
|
|
+
|
|
|
+ AlarmItemModel 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, AlarmTargetModel targetModel, AlarmItemModel 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, AlarmTargetModel targetModel, AlarmItemModel itemModel, ObjectNode 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
|
|
|
+ public MapResponse batchUpdateWhenItemUpdate(String itemCode, String projectId, String groupCode) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+
|
|
|
+ AlarmItemModel itemModel = alarmItemService.getAlarmItemByItemCode(itemCode);
|
|
|
+ if (itemModel == null) {
|
|
|
+ response.setFail("自动生成报警定义失败,获取不到对应的报警条目!itemCode:" + itemCode);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<AlarmTargetModel> alarmTargetModels = alarmTargetService.getAlarmTargetModelsByItemCode(itemCode, projectId);
|
|
|
+ if (CollectionUtils.isEmpty(alarmTargetModels)) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> classCodes = alarmTargetModels.stream().map(AlarmTargetModel::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 (AlarmTargetModel alarmTargetModel : alarmTargetModels) {
|
|
|
+ String classCode = alarmTargetModel.getClassCode();
|
|
|
+ Set<String> objIds = codeIdListMap.get(classCode);
|
|
|
+ Map<String, List<AlarmConfig>> statisticsMap = statisticConfigs2Update(objIds, projectId, alarmTargetModel, 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
|
|
|
+ public MapResponse batchUpdateWhenTargetUpdate(String targetId, String projectId, String groupCode) {
|
|
|
+ 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();
|
|
|
+
|
|
|
+
|
|
|
+ AlarmItemModel itemModel = alarmItemService.getAlarmItemByItemCode(itemCode);
|
|
|
+ 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.toModel(), 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(deletedAlarmConfigs, null);
|
|
|
+ alarmConfigRepository.deleteAll(deletedAlarmConfigs);
|
|
|
+ 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);
|
|
|
+ alarmConfigRepository.saveAll(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)) {
|
|
|
+ alarmConfigRepository.deleteAll(deletedConfigs);
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(createdConfigs)) {
|
|
|
+ alarmConfigRepository.saveAll(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(DateUtils.format(new Date()));
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @description: 按照报警匹配条件统计需要删除和重新创建的报警定义
|
|
|
+ * @param: objIds 设备id列表
|
|
|
+ * @param: projectId 项目id
|
|
|
+ * @param: alarmTargetModel 报警匹配条件
|
|
|
+ * @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, AlarmTargetModel alarmTargetModel, AlarmItemModel itemModel) {
|
|
|
+ log.info("设备id列表:" + objIds);
|
|
|
+ if (objIds == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ JacksonCriteria criteria = JacksonCriteria.newInstance();
|
|
|
+ criteria.add("objId", Lists.newArrayList(objIds));
|
|
|
+ criteria.add("targetId", alarmTargetModel.getId());
|
|
|
+ criteria.add("itemCode", itemModel.getCode());
|
|
|
+ criteria.add("projectId", projectId);
|
|
|
+ List<AlarmConfigModel> configModels = queryList(criteria);
|
|
|
+
|
|
|
+ if (configModels == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ List<AlarmConfigModel> userDefinedAlarmConfigModels = configModels.stream().filter(
|
|
|
+ configModel -> configModel.getUserDefined() == 1
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<AlarmConfigModel> autoAlarmConfigModels = configModels.stream().filter(
|
|
|
+ configModel -> configModel.getUserDefined() != 1
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+ List<AlarmConfig> autoAlarmConfigs = autoAlarmConfigModels.stream().map(
|
|
|
+ AlarmConfig::fromModel
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+
|
|
|
+ Set<String> userDefinedObjIds = userDefinedAlarmConfigModels.stream().map(AlarmConfigModel::getObjId).collect(Collectors.toSet());
|
|
|
+ objIds.removeAll(userDefinedObjIds);
|
|
|
+
|
|
|
+ Map<String, List<AlarmConfig>> statisticMap = new HashMap<>();
|
|
|
+ statisticMap.put("deleted", autoAlarmConfigs);
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(objIds)) {
|
|
|
+ ObjectNode condition = alarmTargetService.getCondition(alarmTargetModel, itemModel);
|
|
|
+ List<AlarmConfig> alarmConfigs = new ArrayList<>();
|
|
|
+ for (String objId : objIds) {
|
|
|
+ AlarmConfig alarmConfig = generateAlarmConfigEntity(objId, alarmTargetModel, itemModel, condition);
|
|
|
+ alarmConfigs.add(alarmConfig);
|
|
|
+ }
|
|
|
+ statisticMap.put("created", alarmConfigs);
|
|
|
+ }
|
|
|
+ return statisticMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public AlarmConfig get(String id) {
|
|
|
+ Optional<AlarmConfig> result = alarmConfigRepository.findById(id);
|
|
|
+ return result.orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public MapResponse update(AlarmConfigModel 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);
|
|
|
+
|
|
|
+
|
|
|
+ alarmConfig.setUpdateUser(alarmParam.userId);
|
|
|
+ alarmConfig.setUpdateTime(new Date());
|
|
|
+ alarmConfigRepository.save(alarmConfig);
|
|
|
+ response.add("id", alarmConfig.getId());
|
|
|
+
|
|
|
+ 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("updatedConfigUniques", Lists.newArrayList(alarmConfig.getAlarmConfigUnique()));
|
|
|
+ msg.setAppId(DmpParameterStorage.getAppId());
|
|
|
+ msg.setSendTime(DateUtils.format(new Date()));
|
|
|
+ response.add(msg);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @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(AlarmConfigModel 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
|
|
|
+ public MapResponse deleteOnRemoveObj(String objId, String projectId) {
|
|
|
+
|
|
|
+ JacksonCriteria criteria = JacksonCriteria.newInstance();
|
|
|
+ criteria.add("objId", objId);
|
|
|
+ criteria.add("projectId", projectId);
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ deleteByCriteria(criteria);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @description: 条件查询
|
|
|
+ * @param: criteria
|
|
|
+ * @return: java.util.List<com.persagy.dmp.alarm.model.AlarmConfigModel>
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/10/21 2:22 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ private List<AlarmConfigModel> queryList(JacksonCriteria criteria) {
|
|
|
+ PagedResponse<AlarmConfigModel> 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(AlarmConfigModel param) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ if (StringUtils.isEmpty(param.getProjectId())) {
|
|
|
+ response.setFail("项目id必填!");
|
|
|
+ }
|
|
|
+ JacksonCriteria criteria = JacksonCriteria.newInstance();
|
|
|
+
|
|
|
+ List<AlarmItemModel> alarmItemModels = alarmItemService.queryList(criteria);
|
|
|
+ for (AlarmItemModel alarmItemModel : alarmItemModels) {
|
|
|
+ batchUpdateWhenItemUpdate(alarmItemModel.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<AlarmConfigModel> params) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ if (params == null) {
|
|
|
+ response.setFail("请传入要修改的报警定义!");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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 (AlarmConfigModel param : params) {
|
|
|
+ String checkResult = CheckRequiredParam.check(param, "id");
|
|
|
+ if (!StringUtils.isEmpty(checkResult)) {
|
|
|
+ response.setFail(checkResult);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ String configId = param.getId();
|
|
|
+ AlarmConfig alarmConfig = alarmConfigRepository.getOne(configId);
|
|
|
+ updateEntityByParam(param, alarmConfig);
|
|
|
+ configs.add(alarmConfig);
|
|
|
+ }
|
|
|
+ alarmConfigRepository.saveAll(configs);
|
|
|
+ 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(AlarmConfigModel 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;
|
|
|
+ }
|
|
|
+ alarmConfigRepository.save(entity);
|
|
|
+ response.add("id", entity.getId());
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ private AlarmConfig initAlarmConfigEntity(AlarmConfigModel param, AlarmParam alarmParam, MapResponse response) {
|
|
|
+
|
|
|
+ String checkResult = CheckRequiredParam.check(param,
|
|
|
+ "objId", "classCode", "itemCode", "condition", "level");
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(checkResult)) {
|
|
|
+ response.setFail(checkResult);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ AlarmConfig entity = AlarmConfig.fromModel(param);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @description: 批量创建报警定义
|
|
|
+ * @param: configModels
|
|
|
+ * @return: com.persagy.common.web.MapResponse
|
|
|
+ * @exception:
|
|
|
+ * @author: lixing
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 2020/12/4 6:45 下午
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public MapResponse batchCreate(List<AlarmConfigModel> configModels) {
|
|
|
+ MapResponse response = new MapResponse();
|
|
|
+ if (configModels == null) {
|
|
|
+ response.setFail("请传入要创建的报警定义!");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (configModels.size() > 200) {
|
|
|
+ response.setFail("一次最多创建200条数据");
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ AlarmParam alarmParam = prepareParam(response);
|
|
|
+ if (alarmParam == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ Date date = new Date();
|
|
|
+ List<String> ids = new ArrayList<>();
|
|
|
+ for (AlarmConfigModel configModel : configModels) {
|
|
|
+ AlarmConfig entity = initAlarmConfigEntity(configModel, alarmParam, response);
|
|
|
+ if (entity == null) {
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ alarmConfigRepository.save(entity);
|
|
|
+ ids.add(entity.getId());
|
|
|
+ }
|
|
|
+ Date 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
|
|
|
+ public MapResponse batchDeleteWhenTargetDelete(String targetId, String projectId, String groupCode) {
|
|
|
+ MapResponse mapResponse = new MapResponse();
|
|
|
+ JacksonCriteria criteria = JacksonCriteria.newInstance();
|
|
|
+ criteria.add("targetId", targetId);
|
|
|
+ criteria.add("projectId", projectId);
|
|
|
+ criteria.add("groupCode", groupCode);
|
|
|
+
|
|
|
+ criteria.add("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(JacksonCriteria criteria) {
|
|
|
+ List<AlarmConfigModel> configModels = queryList(criteria);
|
|
|
+ if (configModels == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<AlarmConfig> configs = configModels.stream().map(AlarmConfig::fromModel).collect(Collectors.toList());
|
|
|
+ DmpMessage deletedMessage = deleteAll(configs);
|
|
|
+ if (deletedMessage != null) {
|
|
|
+ messageProcesser.convertAndSend(deletedMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|