Commit fd0b67d5 authored by tangwei's avatar tangwei

封装接口

parent 815306ed
...@@ -11,8 +11,10 @@ import com.yeejoin.amos.boot.module.jxiop.api.mapper.MonitorFanIndicatorMapper; ...@@ -11,8 +11,10 @@ import com.yeejoin.amos.boot.module.jxiop.api.mapper.MonitorFanIndicatorMapper;
import com.yeejoin.amos.boot.module.jxiop.api.mapper.StationBasicMapper; import com.yeejoin.amos.boot.module.jxiop.api.mapper.StationBasicMapper;
import com.yeejoin.amos.boot.module.jxiop.biz.ESDto.ESEquipments; import com.yeejoin.amos.boot.module.jxiop.biz.ESDto.ESEquipments;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.IndicatorsDto; import com.yeejoin.amos.boot.module.jxiop.biz.dto.IndicatorsDto;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.QueryDto;
import com.yeejoin.amos.boot.module.jxiop.biz.entity.Test; import com.yeejoin.amos.boot.module.jxiop.biz.entity.Test;
import com.yeejoin.amos.boot.module.jxiop.biz.repository.ESEquipmentsRepository; import com.yeejoin.amos.boot.module.jxiop.biz.repository.ESEquipmentsRepository;
import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.CommonServiceImpl;
import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.StationBasicServiceImpl; import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.StationBasicServiceImpl;
import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.TestServiceImpl; import com.yeejoin.amos.boot.module.jxiop.biz.service.impl.TestServiceImpl;
import com.yeejoin.amos.boot.module.jxiop.biz.utils.InfluxDButils; import com.yeejoin.amos.boot.module.jxiop.biz.utils.InfluxDButils;
...@@ -20,10 +22,29 @@ import com.yeejoin.amos.component.influxdb.InfluxDbConnection; ...@@ -20,10 +22,29 @@ import com.yeejoin.amos.component.influxdb.InfluxDbConnection;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.component.emq.EmqKeeper; import org.typroject.tyboot.component.emq.EmqKeeper;
import org.typroject.tyboot.core.foundation.enumeration.UserType; import org.typroject.tyboot.core.foundation.enumeration.UserType;
...@@ -61,6 +82,8 @@ public class DemoController extends BaseController { ...@@ -61,6 +82,8 @@ public class DemoController extends BaseController {
private ElasticsearchRestTemplate elasticsearchTemplate; private ElasticsearchRestTemplate elasticsearchTemplate;
@Autowired @Autowired
ESEquipmentsRepository equipmentsRepository; ESEquipmentsRepository equipmentsRepository;
@Autowired
CommonServiceImpl commonServiceImpl;
@TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY) @TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
@ApiOperation(value = "demo测试prodcutcion 库") @ApiOperation(value = "demo测试prodcutcion 库")
...@@ -109,12 +132,11 @@ public class DemoController extends BaseController { ...@@ -109,12 +132,11 @@ public class DemoController extends BaseController {
System.out.println(DATE); System.out.println(DATE);
List<MonitorFanIndicator> list = monitorFanIndicatorregionMapper.selectList(QueryWrapper); List<MonitorFanIndicator> list = monitorFanIndicatorregionMapper.selectList(QueryWrapper);
num=num+list.size(); num=num+list.size();
List<ESEquipments> listit =new ArrayList<>(); List<ESEquipments> listit =new ArrayList<>();
for (MonitorFanIndicator monitorFanIndicator : list) { for (MonitorFanIndicator monitorFanIndicator : list) {
ESEquipments eSEquipments=new ESEquipments( ESEquipments eSEquipments=new ESEquipments(
null, 3.55,
monitorFanIndicator.getAddressGateway(), monitorFanIndicator.getAddressGateway(),
monitorFanIndicator.getIndexAddress(), monitorFanIndicator.getIndexAddress(),
monitorFanIndicator.getDataType(), monitorFanIndicator.getDataType(),
...@@ -208,7 +230,7 @@ public class DemoController extends BaseController { ...@@ -208,7 +230,7 @@ public class DemoController extends BaseController {
ESEquipments eSEquipments=new ESEquipments( ESEquipments eSEquipments=new ESEquipments(
null, 3.55,
monitorFanIndicator.getAddressGateway(), monitorFanIndicator.getAddressGateway(),
monitorFanIndicator.getIndexAddress(), monitorFanIndicator.getIndexAddress(),
monitorFanIndicator.getDataType(), monitorFanIndicator.getDataType(),
...@@ -270,7 +292,95 @@ public class DemoController extends BaseController { ...@@ -270,7 +292,95 @@ public class DemoController extends BaseController {
public ResponseModel<HashMap<String, String>> demoTest6() { public ResponseModel<HashMap<String, String>> demoTest6() {
HashMap<String, String> hashMap = new HashMap<>(); HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("url", "upload/jxiop/amos_studio/758F6F4BC695B777899485DB0C7E5D3.gif"); hashMap.put("url", "upload/jxiop/amos_studio/758F6F4BC695B777899485DB0C7E5D3.gif");
return ResponseHelper.buildResponse(hashMap); return ResponseHelper.buildResponse(hashMap);
} }
@TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
@ApiOperation(value = "测试求和")
@GetMapping("/test8")
public ResponseModel<Object> demoTest8() {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("equipmentIndexName", "有功功率");
SumAggregationBuilder buyCountSum = AggregationBuilders.sum("buyCountSum").field("valueDouble");
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountSum)
.withPageable(PageRequest.of(0, 1))
.build();
query.setTrackTotalHits(true); // 允许返回查询结果的total - search.getTotalHits()
SearchHits<ESEquipments> search = elasticsearchTemplate.search(query, ESEquipments.class);
return ResponseHelper.buildResponse(search);
}
@TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
@ApiOperation(value = "测试平均数")
@GetMapping("/test9")
public ResponseModel<Object> demoTest9() {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("equipmentIndexName", "有功功率");
AvgAggregationBuilder buyCountAvg = AggregationBuilders.avg("buyCountAvg").field("valueDouble");
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountAvg)
.withPageable(PageRequest.of(0, 1))
.build();
query.setTrackTotalHits(true);
SearchHits<ESEquipments> search = elasticsearchTemplate.search(query, ESEquipments.class);
return ResponseHelper.buildResponse(search);
}
@TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
@ApiOperation(value = "分组求和数")
@GetMapping("/test10")
public SearchHits demoTest10() {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("equipmentIndexName", "有功功率");
// 如果只对一个字段进行分组写一个就好
TermsAggregationBuilder tb1 = AggregationBuilders.terms("group_gatewayId").field("gatewayId.keyword");//gatewayId 是分组字段名,group_gatewayId是查询结果的别名
// TermsAggregationBuilder tb2 = AggregationBuilders.terms("group_equipmentNumber").field("equipmentNumber.keyword");//equipmentNumber 是分组字段名,group_equipmentNumber是查询结果的别名
SumAggregationBuilder sb = AggregationBuilders.sum("sum_valueDouble").field("valueDouble");//valueDouble是求和字段名称,sun_valueDouble是结果别名
// 注意顺序,决定先通过谁分组
tb1.subAggregation(sb); // 通过typeId字段分组统计总数
// tb2.subAggregation(sb); // 通过houseId字段分组统计总数
// tb1.subAggregation(tb2); // 合并
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(tb1)
.withPageable(PageRequest.of(0, 1))
.build();
query.setTrackTotalHits(true);
SearchHits search = elasticsearchTemplate.search(query, ESEquipments.class);
return search;
}
@TycloudOperation(needAuth = false, ApiLevel = UserType.AGENCY)
@ApiOperation(value = "分组求和数")
@GetMapping("/test11")
public Map demoTest11() {
List<QueryDto> queryDto=new ArrayList<>();
QueryDto ff1=new QueryDto("gatewayId","1668801435891929089");
QueryDto ff2=new QueryDto("equipmentIndexName","有功功率");
queryDto.add(ff1);
queryDto.add(ff2);
Double avg= commonServiceImpl.getavg(queryDto,"valueDouble",ESEquipments.class);
Double sum= commonServiceImpl.getsum(queryDto,"valueDouble",ESEquipments.class);
Map<String,Double> map=new HashMap();
map.put("1668801435891929089_avg",avg);
map.put("1668801435891929089_sum",sum);
return map;
}
} }
package com.yeejoin.amos.boot.module.jxiop.biz.dto;
import lombok.Data;
/**
* @description:
* @author: tw
* @createDate: 2023/8/9
*/
@Data
public class QueryDto {
private String key;
private String value;
public QueryDto(String key, String value) {
this.key = key;
this.value = value;
}
public QueryDto() {
}
}
...@@ -3,18 +3,37 @@ package com.yeejoin.amos.boot.module.jxiop.biz.service.impl; ...@@ -3,18 +3,37 @@ package com.yeejoin.amos.boot.module.jxiop.biz.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.module.jxiop.api.entity.StationBasic; import com.yeejoin.amos.boot.module.jxiop.api.entity.StationBasic;
import com.yeejoin.amos.boot.module.jxiop.biz.ESDto.ESEquipments;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.IndicatorsDto; import com.yeejoin.amos.boot.module.jxiop.biz.dto.IndicatorsDto;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.QueryDto;
import com.yeejoin.amos.boot.module.jxiop.biz.dto.SocialContributionDto; import com.yeejoin.amos.boot.module.jxiop.biz.dto.SocialContributionDto;
import com.yeejoin.amos.boot.module.jxiop.biz.mapper2.SjglZsjZsbtzMapper; import com.yeejoin.amos.boot.module.jxiop.biz.mapper2.SjglZsjZsbtzMapper;
import com.yeejoin.amos.boot.module.jxiop.biz.repository.ESEquipmentsRepository;
import com.yeejoin.amos.boot.module.jxiop.biz.utils.InfluxDButils; import com.yeejoin.amos.boot.module.jxiop.biz.utils.InfluxDButils;
import com.yeejoin.amos.component.influxdb.InfluxdbUtil; import com.yeejoin.amos.component.influxdb.InfluxdbUtil;
import org.apache.poi.ss.formula.functions.T;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.metrics.Avg;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.Sum;
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -28,6 +47,81 @@ public class CommonServiceImpl { ...@@ -28,6 +47,81 @@ public class CommonServiceImpl {
@Autowired @Autowired
SjglZsjZsbtzMapper sjglZsjZsbtzMapper; SjglZsjZsbtzMapper sjglZsjZsbtzMapper;
@Autowired
private ElasticsearchRestTemplate elasticsearchTemplate;
@Autowired
ESEquipmentsRepository equipmentsRepository;
/**
* 带参数 求平均值
*
* */
public Double getavg(List<QueryDto> queryDto,String keyavg,Class clas){
Double value=null;
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (QueryDto dto : queryDto) {
queryBuilder.must( QueryBuilders.matchQuery(dto.getKey(), dto.getValue()));
}
AvgAggregationBuilder buyCountAvg = AggregationBuilders.avg("buyCountAvg").field(keyavg);
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountAvg)
.withPageable(PageRequest.of(0, 1))
.build();
query.setTrackTotalHits(true);
SearchHits search = elasticsearchTemplate.search(query,clas);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Avg avg = aggregations.get("buyCountAvg");
value= avg.getValue();
}
}
return value;
}
/**
* 带参数 求和
*
* */
public Double getsum(List<QueryDto> queryDto,String keysum,Class clas){
Double value=null;
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (QueryDto dto : queryDto) {
queryBuilder.must( QueryBuilders.matchQuery(dto.getKey(), dto.getValue()));
}
SumAggregationBuilder buyCountSum = AggregationBuilders.sum("buyCountSum").field(keysum);
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountSum)
.withPageable(PageRequest.of(0, 1))
.build();
query.setTrackTotalHits(true);
SearchHits search = elasticsearchTemplate.search(query,clas);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Sum sum = aggregations.get("buyCountSum");
value= sum.getValue();
}
}
return value;
}
/** /**
* @deprecated 获取指标值总和 * @deprecated 获取指标值总和
* @param gatewayId 网关id 用于拼接sql语句 * @param gatewayId 网关id 用于拼接sql语句
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment