|
@@ -1,11 +1,18 @@
|
|
|
package com.persagy.apm.alarmservice.group.management.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.persagy.apm.configuration.ElasticSearchConfig;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.persagy.apm.alarmservice.group.management.model.GroupManagement;
|
|
|
+import com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeInfoVO;
|
|
|
+import com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeVO;
|
|
|
+import com.persagy.apm.alarmservice.group.management.service.GroupManagementService;
|
|
|
import com.persagy.apm.model.dto.GroupManagementDTO;
|
|
|
import com.persagy.apm.model.vo.GroupManagementVo;
|
|
|
-import com.persagy.apm.service.GroupManagementService;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.elasticsearch.action.get.GetRequest;
|
|
|
import org.elasticsearch.action.get.GetResponse;
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
@@ -18,15 +25,24 @@ import org.elasticsearch.rest.RestStatus;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
+import org.elasticsearch.search.sort.SortOrder;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import com.persagy.apm.alarmservice.group.management.service.GroupManagementService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
+import static cn.hutool.core.util.StrUtil.COMMA;
|
|
|
import static com.persagy.apm.constants.ElasticStrConstant.INDEX_NAME;
|
|
|
+import static org.apache.commons.lang3.math.NumberUtils.INTEGER_ONE;
|
|
|
|
|
|
/**
|
|
|
* @author 易涛
|
|
@@ -45,9 +61,16 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
|
|
|
queryBuilder.must(QueryBuilders.matchQuery("class_code",managementDTO.getClassCode()));
|
|
|
queryBuilder.must(QueryBuilders.matchPhraseQuery("infos",managementDTO.getCondition()));
|
|
|
+ //模糊查询使用
|
|
|
+ if(StringUtils.isNotEmpty(managementDTO.getLocalName())) {
|
|
|
+ queryBuilder.must(QueryBuilders.fuzzyQuery("local_name", managementDTO.getLocalName()));
|
|
|
+ }
|
|
|
queryBuilder.must(QueryBuilders.termQuery("valid",1));
|
|
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
sourceBuilder.query(queryBuilder);
|
|
|
+ sourceBuilder.from(Math.max(managementDTO.getCurrent(), INTEGER_ONE));
|
|
|
+ sourceBuilder.size(managementDTO.getSize());
|
|
|
+ sourceBuilder.sort("create_time", SortOrder.DESC);
|
|
|
SearchRequest rq = new SearchRequest(INDEX_NAME);
|
|
|
rq.source(sourceBuilder);
|
|
|
try {
|
|
@@ -69,15 +92,126 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
|
|
|
@Override
|
|
|
public GroupManagementVo equipsGet(String id) {
|
|
|
+ String equipStr = findEquipById(id);
|
|
|
+ if(StringUtils.isEmpty(equipStr)){
|
|
|
+ throw new RuntimeException("id为"+id+"的设备不存在");
|
|
|
+ }
|
|
|
+ return JSONObject.parseObject(equipStr,GroupManagementVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据id查询设备-常用方法
|
|
|
+ * @param id
|
|
|
+ * @author 易涛
|
|
|
+ * @date 2021/9/10 10:05
|
|
|
+ * @return java.lang.String
|
|
|
+ */
|
|
|
+ private String findEquipById(String id){
|
|
|
GetRequest getRequest = new GetRequest(INDEX_NAME,id);
|
|
|
try {
|
|
|
GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
|
|
|
if(getResponse.isExists()){
|
|
|
- return JSONObject.parseObject(getResponse.getSourceAsString(),GroupManagementVo.class);
|
|
|
+ return getResponse.getSourceAsString();
|
|
|
}
|
|
|
}catch (IOException e){
|
|
|
throw new RuntimeException("es查询出错",e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ private List<GroupManagement> findEquipsByIds(List<String> ids){
|
|
|
+ if(CollectionUtils.isEmpty(ids)||ids.size()>100){
|
|
|
+ throw new RuntimeException("批量查询的数据过短或过长");
|
|
|
+ }
|
|
|
+ List<GroupManagement> managementList = new ArrayList<>();
|
|
|
+ BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
|
|
|
+ queryBuilder.must(QueryBuilders.termsQuery("id",ids));
|
|
|
+ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
+ sourceBuilder.query(queryBuilder);
|
|
|
+ sourceBuilder.from(INTEGER_ONE);
|
|
|
+ sourceBuilder.size(ids.size());
|
|
|
+ sourceBuilder.sort("create_time",SortOrder.DESC);
|
|
|
+ SearchRequest rq = new SearchRequest(INDEX_NAME);
|
|
|
+ rq.source(sourceBuilder);
|
|
|
+ try {
|
|
|
+ SearchResponse searchResponse = client.search(rq, RequestOptions.DEFAULT);
|
|
|
+ // 根据状态和数据条数验证是否返回了数据
|
|
|
+ if (RestStatus.OK.equals(searchResponse.status())&&searchResponse.getHits().getTotalHits().value>0) {
|
|
|
+ SearchHits hits = searchResponse.getHits();
|
|
|
+ for (SearchHit hit : hits) {
|
|
|
+ // 将 JSON 转换成对象
|
|
|
+ GroupManagement management = JSON.parseObject(hit.getSourceAsString(), GroupManagement.class);
|
|
|
+ managementList.add(management);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new RuntimeException("es查询出错",e);
|
|
|
+ }
|
|
|
+ return managementList;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public List<GroupTreeInfoVO> equipsGroupQuery(List<String> ids) {
|
|
|
+ List<GroupTreeInfoVO> groupTreeInfoVOList = Lists.newArrayList();
|
|
|
+ List<GroupManagement> groupManagementList = findEquipsByIds(ids);
|
|
|
+ for(GroupManagement management:groupManagementList) {
|
|
|
+ String infos = management.getInfos();
|
|
|
+ if (StringUtils.isEmpty(infos)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Map<String, String> infoMap = Arrays.stream(infos.split(COMMA))
|
|
|
+ .collect(Collectors.toMap(key-> key, value->value, (e1, e2) -> e1));
|
|
|
+ List<GroupTreeVO> groupTreeVOList = getGroupTree();
|
|
|
+ GroupTreeInfoVO infoVO = new GroupTreeInfoVO();
|
|
|
+ groupTreeInfoVOList.add(queryGroupName(groupTreeVOList,infoVO,infoMap,management.getId()));
|
|
|
+ }
|
|
|
+ return groupTreeInfoVOList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归匹配condition
|
|
|
+ * @param groupTreeVOList
|
|
|
+ * @param infoVO
|
|
|
+ * @param infoMap
|
|
|
+ * @param equipId
|
|
|
+ * @author 易涛
|
|
|
+ * @date 2021/9/10 11:24
|
|
|
+ * @return com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeInfoVO
|
|
|
+ */
|
|
|
+ private GroupTreeInfoVO queryGroupName(List<GroupTreeVO> groupTreeVOList,GroupTreeInfoVO infoVO,
|
|
|
+ Map<String, String> infoMap,String equipId){
|
|
|
+ for(GroupTreeVO groupTreeVO:groupTreeVOList){
|
|
|
+ if(StringUtils.isNotEmpty(infoVO.getId())){
|
|
|
+ return infoVO;
|
|
|
+ }
|
|
|
+ if(infoMap.containsKey(groupTreeVO.getCondition())){
|
|
|
+ BeanUtils.copyProperties(groupTreeVO,infoVO);
|
|
|
+ infoVO.setEquipId(equipId);
|
|
|
+ return infoVO;
|
|
|
+ }
|
|
|
+ if(CollectionUtils.isNotEmpty(groupTreeVO.getChildren())){
|
|
|
+ queryGroupName(groupTreeVO.getChildren(),infoVO,infoMap,equipId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return infoVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取分组树
|
|
|
+ * @author 易涛
|
|
|
+ * @date 2021/9/8 14:44
|
|
|
+ * @return java.util.List<com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeVO>
|
|
|
+ */
|
|
|
+ public static List<GroupTreeVO> getGroupTree(){
|
|
|
+ InputStream inputStream = GroupManagementServiceImpl.class.getClassLoader()
|
|
|
+ .getResourceAsStream(File.separator+"json"+File.separator+"GroupTree.json");
|
|
|
+ if(inputStream==null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ String str = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
|
|
|
+ return JSONArray.parseArray(str,GroupTreeVO.class);
|
|
|
+ }catch (IOException ioe){
|
|
|
+ throw new RuntimeException("json文件有误",ioe);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|