Browse Source

新增根据id查询分组名称,所有的查询加上分页和模糊查询

易涛 3 years ago
parent
commit
cb8534b650

+ 2 - 1
src/main/java/com/persagy/apm/alarmservice/configuration/ElasticSearchConfig.java

@@ -4,13 +4,14 @@ import org.apache.http.HttpHost;
 import org.elasticsearch.client.RestClient;
 import org.elasticsearch.client.RestHighLevelClient;
 import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
 
 /**
  * @author 易涛
  * @version 1.0
  * @date 2021/9/7 18:05
  */
-//@Configuration
+@Configuration
 public class ElasticSearchConfig {
     @Bean
     public RestHighLevelClient restHighLevelClient() {

+ 18 - 31
src/main/java/com/persagy/apm/alarmservice/group/management/controller/GroupManagementController.java

@@ -1,17 +1,18 @@
 package com.persagy.apm.alarmservice.group.management.controller;
 
-import com.alibaba.fastjson.JSONArray;
 import com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeDetailVO;
+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.common.response.CommonResult;
 import com.persagy.apm.common.utils.ResultHelper;
+import com.persagy.apm.model.dto.EquipIdDTO;
+import com.persagy.apm.model.dto.EquipIdListDTO;
 import com.persagy.apm.model.dto.GroupManagementDTO;
 import com.persagy.apm.model.vo.GroupManagementVo;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,13 +21,12 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
+import javax.validation.Valid;
 import java.util.List;
 import java.util.Objects;
 
+import static com.persagy.apm.alarmservice.group.management.service.impl.GroupManagementServiceImpl.getGroupTree;
+
 /**
  * @author 易涛
  * @version 1.0
@@ -45,33 +45,14 @@ public class GroupManagementController {
     public CommonResult<List<GroupTreeVO>> queryGroupTree(){
         return ResultHelper.single(getGroupTree());
     }
-    
-    /**
-     * 获取分组树 
-     * @author 易涛
-     * @date 2021/9/8 14:44 
-     * @return java.util.List<com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeVO>
-     */
-    private List<GroupTreeVO> getGroupTree(){
-        InputStream inputStream = this.getClass().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);
-        }
-    }
+
     
     @ApiOperation(value = "获取分组名称")
     @PostMapping("/eq_groups/get")
-    public CommonResult<GroupTreeDetailVO> queryTreeById(@RequestBody String id){
+    public CommonResult<GroupTreeDetailVO> queryTreeById(@Valid @RequestBody EquipIdDTO equipIdDTO){
         List<GroupTreeVO> groupTreeVOList = getGroupTree();
         GroupTreeDetailVO groupTreeDetailVO = new GroupTreeDetailVO();
-        queryById(groupTreeVOList,groupTreeDetailVO,id);
+        queryById(groupTreeVOList,groupTreeDetailVO,equipIdDTO.getId());
         return ResultHelper.single(groupTreeDetailVO);
     }
     
@@ -99,7 +80,7 @@ public class GroupManagementController {
         return groupTreeDetailVO;
     }
 
-    @ApiOperation(value = "分组条件(设备类+查询条件)+项目 获取设备")
+    @ApiOperation(value = "分组条件(设备类+查询条件)+项目 获取设备  加上分页和根据设备名称查询")
     @PostMapping("/equips/query")
     public CommonResult<List<GroupManagementVo>> equipsQuery(@RequestBody GroupManagementDTO managementDTO){
         return ResultHelper.single(managementService.equipsQuery(managementDTO));
@@ -107,7 +88,13 @@ public class GroupManagementController {
 
     @ApiOperation(value = "获取设备名称")
     @PostMapping("/equips/get")
-    public CommonResult<GroupManagementVo> equipsQuery(@RequestBody String id) {
-        return ResultHelper.single(managementService.equipsGet(id));
+    public CommonResult<GroupManagementVo> equipsQuery(@Valid @RequestBody EquipIdDTO equipIdDTO) {
+        return ResultHelper.single(managementService.equipsGet(equipIdDTO.getId()));
+    }
+
+    @ApiOperation(value = "获取设备名称")
+    @PostMapping("/equipsGroup/query")
+    public CommonResult<List<GroupTreeInfoVO>> equipsGroupQuery(@Valid @RequestBody EquipIdListDTO equipIdListDTO){
+        return ResultHelper.single(managementService.equipsGroupQuery(equipIdListDTO.getEquipIdList())) ;
     }
 }

+ 1 - 13
src/main/java/com/persagy/apm/alarmservice/group/management/model/GroupManagement.java

@@ -14,39 +14,27 @@ import java.time.LocalDateTime;
  * @version 1.0
  * @date 2021/9/7 18:11
  */
-/*@Document(indexName = "rwd_object")
-@Setting(settingPath = "commaAnalyzer.json")*/
 @Data
 public class GroupManagement {
-    //@Id
-    //@Field(type = FieldType.Text)
+
     private String id;
 
-    //@Field(name = "class_code",type = FieldType.Text)
     private String classCode;
 
-    //@Field(type = FieldType.Text,analyzer = "comma")
     private String infos;
 
-    //@Field(name = "local_id",type = FieldType.Text)
     private String localId;
 
-    //@Field(name = "local_name",type = FieldType.Text)
     private String localName;
 
-    //@Field(type = FieldType.Text)
     private String name;
 
-    //@Field(name = "obj_type",type = FieldType.Text)
     private String objType;
 
-    //@Field(name = "project_id",type = FieldType.Text)
     private String projectId;
 
-    //@Field(name = "group_code",type = FieldType.Text)
     private String groupCode;
 
-    //@Field(name = "update_time")
     private LocalDateTime updateTime;
 
     private Long valid;

+ 20 - 0
src/main/java/com/persagy/apm/alarmservice/group/management/model/vo/GroupTreeInfoVO.java

@@ -0,0 +1,20 @@
+package com.persagy.apm.alarmservice.group.management.model.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * @author 易涛
+ * @version 1.0
+ * @date 2021/9/8 11:32
+ */
+@ApiModel(value = "分组树详情")
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class GroupTreeInfoVO extends GroupTreeDetailVO{
+
+    @ApiModelProperty("分组id")
+    private String equipId;
+}

+ 11 - 0
src/main/java/com/persagy/apm/alarmservice/group/management/service/GroupManagementService.java

@@ -1,5 +1,7 @@
 package com.persagy.apm.alarmservice.group.management.service;
 
+import com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeDetailVO;
+import com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeInfoVO;
 import com.persagy.apm.model.dto.GroupManagementDTO;
 import com.persagy.apm.model.vo.GroupManagementVo;
 
@@ -29,4 +31,13 @@ public interface GroupManagementService{
      * @return com.persagy.apm.model.vo.GroupManagementVo
      */
     GroupManagementVo equipsGet(String id);
+
+    /**
+     * 根据设备id查询分组名称
+     * @param ids
+     * @author 易涛
+     * @date 2021/9/10 10:02
+     * @return com.persagy.apm.alarmservice.group.management.model.vo.GroupTreeDetailVO
+     */
+    List<GroupTreeInfoVO> equipsGroupQuery(List<String> ids);
 }

+ 138 - 4
src/main/java/com/persagy/apm/alarmservice/group/management/service/impl/GroupManagementServiceImpl.java

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

+ 24 - 0
src/main/java/com/persagy/apm/model/dto/EquipIdDTO.java

@@ -0,0 +1,24 @@
+package com.persagy.apm.model.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.Size;
+import java.util.List;
+
+/**
+ * @author 易涛
+ * @version 1.0
+ * @date 2021/9/8 16:46
+ */
+@ApiModel("设备id传输实体")
+@Data
+public class EquipIdDTO {
+
+    @ApiModelProperty("设备或者分组id")
+    @NotEmpty(message = "主键必填")
+    private String id;
+}

+ 22 - 0
src/main/java/com/persagy/apm/model/dto/EquipIdListDTO.java

@@ -0,0 +1,22 @@
+package com.persagy.apm.model.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.Size;
+import java.util.List;
+
+/**
+ * @author 易涛
+ * @version 1.0
+ * @date 2021/9/8 16:46
+ */
+@ApiModel("设备id传输实体")
+@Data
+public class EquipIdListDTO {
+
+    @ApiModelProperty("设备id")
+    @Size(message = "设备id传输信息过长或过短",min = 1,max = 100)
+    private List<String> equipIdList;
+}

+ 18 - 3
src/main/java/com/persagy/apm/model/dto/GroupManagementDTO.java

@@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
+import javax.validation.constraints.NotBlank;
+
 /**
  * @author 易涛
  * @version 1.0
@@ -11,11 +13,24 @@ import lombok.Data;
  */
 @ApiModel("设备信息")
 @Data
-public class GroupManagementDTO {
+public class GroupManagementDTO{
+
+    @ApiModelProperty("设备名称")
+    private String localName;
 
-    @ApiModelProperty("对象类编码")
+    @ApiModelProperty(value = "对象类编码",required = true)
+    @NotBlank(message = "对象类编码必传")
     private String classCode;
 
-    @ApiModelProperty("查询条件")
+    @ApiModelProperty(value = "查询条件",required = true)
+    @NotBlank(message = "查询条件必传")
     private String condition;
+
+    @ApiModelProperty(value = "当前页",required = true)
+    @NotBlank(message = "当前页必传")
+    private Integer current;
+
+    @ApiModelProperty(value = "每页条数",required = true)
+    @NotBlank(message = "每页条数必传")
+    private Integer size;
 }