diff --git a/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java new file mode 100644 index 0000000..c280e19 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java @@ -0,0 +1,79 @@ +package com.casic.missiles.dto; + +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.casic.missiles.core.es.HitResponse; + +import java.util.ArrayList; +import java.util.List; + +public class EsResponse { + private HitResponse hits; + + public List getDatas() { + List datas = new ArrayList(); + if (ObjectUtils.isEmpty(this.hits)) { + return datas; + } else { + for (int i = 0; i < this.hits.getHits().size(); ++i) { + datas.add((this.hits.getHits().get(i)).getData()); + } + + return datas; + } + } + + public EsResponse() { + } + + public HitResponse getHits() { + return this.hits; + } + + public void setHits(HitResponse hits) { + this.hits = hits; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof com.casic.missiles.core.es.EsResponse)) { + return false; + } else { + EsResponse other = (EsResponse) o; + if (!other.canEqual(this)) { + return false; + } else { + Object this$hits = this.getHits(); + Object other$hits = other.getHits(); + if (this$hits == null) { + if (other$hits != null) { + return false; + } + } else if (!this$hits.equals(other$hits)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(Object other) { + return other instanceof com.casic.missiles.core.es.EsResponse; + } + + @Override + public int hashCode() { + Boolean PRIME = true; + int result = 1; + Object $hits = this.getHits(); + result = result * 59 + ($hits == null ? 43 : $hits.hashCode()); + return result; + } + + @Override + public String toString() { + return "EsResponse(hits=" + this.getHits() + ")"; + } +} diff --git a/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java new file mode 100644 index 0000000..c280e19 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java @@ -0,0 +1,79 @@ +package com.casic.missiles.dto; + +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.casic.missiles.core.es.HitResponse; + +import java.util.ArrayList; +import java.util.List; + +public class EsResponse { + private HitResponse hits; + + public List getDatas() { + List datas = new ArrayList(); + if (ObjectUtils.isEmpty(this.hits)) { + return datas; + } else { + for (int i = 0; i < this.hits.getHits().size(); ++i) { + datas.add((this.hits.getHits().get(i)).getData()); + } + + return datas; + } + } + + public EsResponse() { + } + + public HitResponse getHits() { + return this.hits; + } + + public void setHits(HitResponse hits) { + this.hits = hits; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof com.casic.missiles.core.es.EsResponse)) { + return false; + } else { + EsResponse other = (EsResponse) o; + if (!other.canEqual(this)) { + return false; + } else { + Object this$hits = this.getHits(); + Object other$hits = other.getHits(); + if (this$hits == null) { + if (other$hits != null) { + return false; + } + } else if (!this$hits.equals(other$hits)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(Object other) { + return other instanceof com.casic.missiles.core.es.EsResponse; + } + + @Override + public int hashCode() { + Boolean PRIME = true; + int result = 1; + Object $hits = this.getHits(); + result = result * 59 + ($hits == null ? 43 : $hits.hashCode()); + return result; + } + + @Override + public String toString() { + return "EsResponse(hits=" + this.getHits() + ")"; + } +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java index aeef63d..12d18a6 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -39,17 +39,17 @@ /** * 生成查询请求体基础结构: * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } * } */ public ElasticSearchQuery() { @@ -63,7 +63,7 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - public ElasticSearchQuery(JSONObject body){ + public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -71,9 +71,9 @@ return body; } - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); + private JSONObject query() { + if (ObjectUtil.isEmpty(this.body.get(QUERY))) { + this.body.put("query", new JSONObject()); } return this.body.getJSONObject(QUERY); @@ -81,175 +81,177 @@ private JSONObject bool() { JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(query.get(BOOL))) { + query.put(BOOL, new JSONObject()); } return query.getJSONObject(BOOL); } - private JSONArray boolMust(){ + private JSONArray boolMust() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONObject filter(){ + private JSONObject filter() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); + if (ObjectUtil.isEmpty(bool.get(FILTER))) { + bool.put(FILTER, new JSONObject()); } return bool.getJSONObject(FILTER); } - private JSONObject filterBool(){ + private JSONObject filterBool() { JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(filter.get(BOOL))) { + filter.put(BOOL, new JSONObject()); } return filter.getJSONObject(BOOL); } - private JSONArray filterBoolMust(){ + private JSONArray filterBoolMust() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONArray filterBoolMustnot(){ + private JSONArray filterBoolMustnot() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST_NOT))) { + bool.put(MUST_NOT, new JSONArray()); } return bool.getJSONArray(MUST_NOT); } - public void match(String filed, Serializable value){ + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); + filedJson.put(filed, value); + match.put("match", filedJson); boolMust.add(match); } - public void matchPhrase(String filed, Serializable value){ + public void matchPhrase(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject matchPhrase = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); + filedJson.put(filed, value); + matchPhrase.put("match_phrase", filedJson); boolMust.add(matchPhrase); } - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); + public void term(String filed, Serializable value) { + this.term(filed, value, MUST_FLAG); } - public void term(String filed, Serializable value,String mustFlag){ + public void term(String filed, Serializable value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + term.put("term", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(term); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(term); } } - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); + public void terms(String filed, List value) { + this.terms(filed, value, MUST_FLAG); } - public void terms(String filed, List value, String mustFlag){ + public void terms(String filed, List value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + terms.put("terms", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(terms); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(terms); } } - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); + public void range(String filed, Serializable from, Serializable to) { + this.range(filed, from, to, MUST_FLAG); } - public void range(String filed, Serializable from, Serializable to, String mustFlag){ + /** + * 范围条件是否必须 + */ + public void range(String filed, Serializable from, Serializable to, String mustFlag) { JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); + if (ObjectUtil.isNotEmpty(from)) { + rangeJson.put("from", from); } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); + if (ObjectUtil.isNotEmpty(to)) { + rangeJson.put("to", to); } JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); + filedJson.put(filed, rangeJson); JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + range.put("range", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(range); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(range); } } - public void size(int size){ - this.body.put("size",size); + public void size(int size) { + this.body.put("size", size); } public void from(int from) { - this.body.put("from",from); + this.body.put("from", from); } - public void sort(String sort, String order){ + public void sort(String sort, String order) { JSONObject sortJson = new JSONObject(); JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); + orderJson.put("order", order); + sortJson.put(sort, orderJson); + this.body.put("sort", sortJson); } - public void aggs(String aggsName, String filed){ + public void aggs(String aggsName, String filed) { JSONObject terms = new JSONObject(); - terms.put("field",filed); + terms.put("field", filed); JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); + aggsTerms.put("terms", terms); JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); + aggsBody.put(aggsName, aggsTerms); + this.body.put("aggs", aggsBody); } - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); + public void aggs(JSONObject aggBody) { + this.body.put("aggs", aggBody); } - public static void main(String[] args) { ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); + query.matchPhrase("name", "张三"); + query.term("sex", "2"); List deptIds = new ArrayList<>(); deptIds.add(0L); deptIds.add(24L); query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); + Date date1 = DateUtil.parse("2020-01-01", "yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01", "yyyy-MM-dd"); + query.range("collTime", date1.getTime(), date2.getTime()); + query.term("collState", "3", MUST_NOT_FLAG); query.size(10); query.from(0); - query.sort("irId","asc"); + query.sort("irId", "asc"); System.out.println(query.getBody().toJSONString()); } diff --git a/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java new file mode 100644 index 0000000..c280e19 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java @@ -0,0 +1,79 @@ +package com.casic.missiles.dto; + +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.casic.missiles.core.es.HitResponse; + +import java.util.ArrayList; +import java.util.List; + +public class EsResponse { + private HitResponse hits; + + public List getDatas() { + List datas = new ArrayList(); + if (ObjectUtils.isEmpty(this.hits)) { + return datas; + } else { + for (int i = 0; i < this.hits.getHits().size(); ++i) { + datas.add((this.hits.getHits().get(i)).getData()); + } + + return datas; + } + } + + public EsResponse() { + } + + public HitResponse getHits() { + return this.hits; + } + + public void setHits(HitResponse hits) { + this.hits = hits; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof com.casic.missiles.core.es.EsResponse)) { + return false; + } else { + EsResponse other = (EsResponse) o; + if (!other.canEqual(this)) { + return false; + } else { + Object this$hits = this.getHits(); + Object other$hits = other.getHits(); + if (this$hits == null) { + if (other$hits != null) { + return false; + } + } else if (!this$hits.equals(other$hits)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(Object other) { + return other instanceof com.casic.missiles.core.es.EsResponse; + } + + @Override + public int hashCode() { + Boolean PRIME = true; + int result = 1; + Object $hits = this.getHits(); + result = result * 59 + ($hits == null ? 43 : $hits.hashCode()); + return result; + } + + @Override + public String toString() { + return "EsResponse(hits=" + this.getHits() + ")"; + } +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java index aeef63d..12d18a6 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -39,17 +39,17 @@ /** * 生成查询请求体基础结构: * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } * } */ public ElasticSearchQuery() { @@ -63,7 +63,7 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - public ElasticSearchQuery(JSONObject body){ + public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -71,9 +71,9 @@ return body; } - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); + private JSONObject query() { + if (ObjectUtil.isEmpty(this.body.get(QUERY))) { + this.body.put("query", new JSONObject()); } return this.body.getJSONObject(QUERY); @@ -81,175 +81,177 @@ private JSONObject bool() { JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(query.get(BOOL))) { + query.put(BOOL, new JSONObject()); } return query.getJSONObject(BOOL); } - private JSONArray boolMust(){ + private JSONArray boolMust() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONObject filter(){ + private JSONObject filter() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); + if (ObjectUtil.isEmpty(bool.get(FILTER))) { + bool.put(FILTER, new JSONObject()); } return bool.getJSONObject(FILTER); } - private JSONObject filterBool(){ + private JSONObject filterBool() { JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(filter.get(BOOL))) { + filter.put(BOOL, new JSONObject()); } return filter.getJSONObject(BOOL); } - private JSONArray filterBoolMust(){ + private JSONArray filterBoolMust() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONArray filterBoolMustnot(){ + private JSONArray filterBoolMustnot() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST_NOT))) { + bool.put(MUST_NOT, new JSONArray()); } return bool.getJSONArray(MUST_NOT); } - public void match(String filed, Serializable value){ + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); + filedJson.put(filed, value); + match.put("match", filedJson); boolMust.add(match); } - public void matchPhrase(String filed, Serializable value){ + public void matchPhrase(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject matchPhrase = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); + filedJson.put(filed, value); + matchPhrase.put("match_phrase", filedJson); boolMust.add(matchPhrase); } - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); + public void term(String filed, Serializable value) { + this.term(filed, value, MUST_FLAG); } - public void term(String filed, Serializable value,String mustFlag){ + public void term(String filed, Serializable value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + term.put("term", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(term); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(term); } } - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); + public void terms(String filed, List value) { + this.terms(filed, value, MUST_FLAG); } - public void terms(String filed, List value, String mustFlag){ + public void terms(String filed, List value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + terms.put("terms", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(terms); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(terms); } } - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); + public void range(String filed, Serializable from, Serializable to) { + this.range(filed, from, to, MUST_FLAG); } - public void range(String filed, Serializable from, Serializable to, String mustFlag){ + /** + * 范围条件是否必须 + */ + public void range(String filed, Serializable from, Serializable to, String mustFlag) { JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); + if (ObjectUtil.isNotEmpty(from)) { + rangeJson.put("from", from); } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); + if (ObjectUtil.isNotEmpty(to)) { + rangeJson.put("to", to); } JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); + filedJson.put(filed, rangeJson); JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + range.put("range", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(range); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(range); } } - public void size(int size){ - this.body.put("size",size); + public void size(int size) { + this.body.put("size", size); } public void from(int from) { - this.body.put("from",from); + this.body.put("from", from); } - public void sort(String sort, String order){ + public void sort(String sort, String order) { JSONObject sortJson = new JSONObject(); JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); + orderJson.put("order", order); + sortJson.put(sort, orderJson); + this.body.put("sort", sortJson); } - public void aggs(String aggsName, String filed){ + public void aggs(String aggsName, String filed) { JSONObject terms = new JSONObject(); - terms.put("field",filed); + terms.put("field", filed); JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); + aggsTerms.put("terms", terms); JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); + aggsBody.put(aggsName, aggsTerms); + this.body.put("aggs", aggsBody); } - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); + public void aggs(JSONObject aggBody) { + this.body.put("aggs", aggBody); } - public static void main(String[] args) { ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); + query.matchPhrase("name", "张三"); + query.term("sex", "2"); List deptIds = new ArrayList<>(); deptIds.add(0L); deptIds.add(24L); query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); + Date date1 = DateUtil.parse("2020-01-01", "yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01", "yyyy-MM-dd"); + query.range("collTime", date1.getTime(), date2.getTime()); + query.term("collState", "3", MUST_NOT_FLAG); query.size(10); query.from(0); - query.sort("irId","asc"); + query.sort("irId", "asc"); System.out.println(query.getBody().toJSONString()); } diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java index dc9a968..4c7cea5 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,9 +3,10 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; +import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; +import org.apache.poi.ss.formula.functions.T; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; @@ -43,65 +44,82 @@ private static RestClient client; @PostConstruct - public void init(){ + public void init() { client = clientMapping; username = env.getProperty("casic.data.es.username"); password = env.getProperty("casic.data.es.password"); } - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + /** + * + * @param index + * @param type + * @param id + * @return + * @throws IOException + */ + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException { String method = "GET"; String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); + Response response = performRequest(method, endPoint, null); JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); return result.getJSONObject("_source"); } - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id新增 + */ + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ + if (ObjectUtil.isNotEmpty(id)) { endPoint.concat(id); } endPoint.concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + /** + * 根据id删除 + */ + public static Response deleteDocumentById(String index, String type, String id) throws IOException { String method = "DELETE"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); + return performRequest(method, endPoint, null); } - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id更新 + */ + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件查询 + */ + public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); logger.debug(query.getBody().toString()); Response response = performRequest(method, entPoint, query.getBody().toString()); // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results=esResponse.getDatas(); + return results; } - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件进行滚动查询 + */ + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); + List results = new ArrayList<>(); String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); logger.debug(query.getBody().toString()); @@ -109,20 +127,20 @@ JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } // 循环发送scroll请求,直到返回结果为空 entPoint = "_search/scroll"; JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ + scrollBody.put("scroll", "1m"); + scrollBody.put("scroll_id", scrollId); + while (esResponse.getDatas().size() > 0) { method = "GET"; response = performRequest(method, entPoint, scrollBody.toString()); result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } } @@ -133,37 +151,43 @@ return results; } - public static Response bulk(String index,String type,String entity) throws IOException{ + + /** + * 批处理 + * + */ + public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ + if (ObjectUtil.isNotEmpty(index)) { endPoint.append(index).append("/"); } - if(ObjectUtil.isNotEmpty(type)){ + if (ObjectUtil.isNotEmpty(type)) { endPoint.append(type).append("/"); } endPoint.append("_bulk"); endPoint.append("?refresh=true"); logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); + return performRequest(method, endPoint.toString(), entity); } + /** + * 请求操作通用类 + */ private static Response performRequest(String method, String endpoint, String entity) throws IOException { //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); + Request request = new Request(method, endpoint); + if (ObjectUtil.isNotEmpty(entity)) { + request.setEntity(new NStringEntity(entity, "utf-8")); } - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + if (ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)) { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); String token = username.concat(":").concat(password); String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); + builder.addHeader("Authorization", "Basic " + base64encode); request.setOptions(builder.build()); } - - Response response = client.performRequest(request); logger.debug(response.toString()); return response; diff --git a/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java new file mode 100644 index 0000000..c280e19 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java @@ -0,0 +1,79 @@ +package com.casic.missiles.dto; + +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.casic.missiles.core.es.HitResponse; + +import java.util.ArrayList; +import java.util.List; + +public class EsResponse { + private HitResponse hits; + + public List getDatas() { + List datas = new ArrayList(); + if (ObjectUtils.isEmpty(this.hits)) { + return datas; + } else { + for (int i = 0; i < this.hits.getHits().size(); ++i) { + datas.add((this.hits.getHits().get(i)).getData()); + } + + return datas; + } + } + + public EsResponse() { + } + + public HitResponse getHits() { + return this.hits; + } + + public void setHits(HitResponse hits) { + this.hits = hits; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof com.casic.missiles.core.es.EsResponse)) { + return false; + } else { + EsResponse other = (EsResponse) o; + if (!other.canEqual(this)) { + return false; + } else { + Object this$hits = this.getHits(); + Object other$hits = other.getHits(); + if (this$hits == null) { + if (other$hits != null) { + return false; + } + } else if (!this$hits.equals(other$hits)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(Object other) { + return other instanceof com.casic.missiles.core.es.EsResponse; + } + + @Override + public int hashCode() { + Boolean PRIME = true; + int result = 1; + Object $hits = this.getHits(); + result = result * 59 + ($hits == null ? 43 : $hits.hashCode()); + return result; + } + + @Override + public String toString() { + return "EsResponse(hits=" + this.getHits() + ")"; + } +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java index aeef63d..12d18a6 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -39,17 +39,17 @@ /** * 生成查询请求体基础结构: * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } * } */ public ElasticSearchQuery() { @@ -63,7 +63,7 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - public ElasticSearchQuery(JSONObject body){ + public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -71,9 +71,9 @@ return body; } - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); + private JSONObject query() { + if (ObjectUtil.isEmpty(this.body.get(QUERY))) { + this.body.put("query", new JSONObject()); } return this.body.getJSONObject(QUERY); @@ -81,175 +81,177 @@ private JSONObject bool() { JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(query.get(BOOL))) { + query.put(BOOL, new JSONObject()); } return query.getJSONObject(BOOL); } - private JSONArray boolMust(){ + private JSONArray boolMust() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONObject filter(){ + private JSONObject filter() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); + if (ObjectUtil.isEmpty(bool.get(FILTER))) { + bool.put(FILTER, new JSONObject()); } return bool.getJSONObject(FILTER); } - private JSONObject filterBool(){ + private JSONObject filterBool() { JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(filter.get(BOOL))) { + filter.put(BOOL, new JSONObject()); } return filter.getJSONObject(BOOL); } - private JSONArray filterBoolMust(){ + private JSONArray filterBoolMust() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONArray filterBoolMustnot(){ + private JSONArray filterBoolMustnot() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST_NOT))) { + bool.put(MUST_NOT, new JSONArray()); } return bool.getJSONArray(MUST_NOT); } - public void match(String filed, Serializable value){ + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); + filedJson.put(filed, value); + match.put("match", filedJson); boolMust.add(match); } - public void matchPhrase(String filed, Serializable value){ + public void matchPhrase(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject matchPhrase = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); + filedJson.put(filed, value); + matchPhrase.put("match_phrase", filedJson); boolMust.add(matchPhrase); } - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); + public void term(String filed, Serializable value) { + this.term(filed, value, MUST_FLAG); } - public void term(String filed, Serializable value,String mustFlag){ + public void term(String filed, Serializable value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + term.put("term", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(term); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(term); } } - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); + public void terms(String filed, List value) { + this.terms(filed, value, MUST_FLAG); } - public void terms(String filed, List value, String mustFlag){ + public void terms(String filed, List value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + terms.put("terms", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(terms); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(terms); } } - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); + public void range(String filed, Serializable from, Serializable to) { + this.range(filed, from, to, MUST_FLAG); } - public void range(String filed, Serializable from, Serializable to, String mustFlag){ + /** + * 范围条件是否必须 + */ + public void range(String filed, Serializable from, Serializable to, String mustFlag) { JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); + if (ObjectUtil.isNotEmpty(from)) { + rangeJson.put("from", from); } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); + if (ObjectUtil.isNotEmpty(to)) { + rangeJson.put("to", to); } JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); + filedJson.put(filed, rangeJson); JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + range.put("range", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(range); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(range); } } - public void size(int size){ - this.body.put("size",size); + public void size(int size) { + this.body.put("size", size); } public void from(int from) { - this.body.put("from",from); + this.body.put("from", from); } - public void sort(String sort, String order){ + public void sort(String sort, String order) { JSONObject sortJson = new JSONObject(); JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); + orderJson.put("order", order); + sortJson.put(sort, orderJson); + this.body.put("sort", sortJson); } - public void aggs(String aggsName, String filed){ + public void aggs(String aggsName, String filed) { JSONObject terms = new JSONObject(); - terms.put("field",filed); + terms.put("field", filed); JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); + aggsTerms.put("terms", terms); JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); + aggsBody.put(aggsName, aggsTerms); + this.body.put("aggs", aggsBody); } - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); + public void aggs(JSONObject aggBody) { + this.body.put("aggs", aggBody); } - public static void main(String[] args) { ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); + query.matchPhrase("name", "张三"); + query.term("sex", "2"); List deptIds = new ArrayList<>(); deptIds.add(0L); deptIds.add(24L); query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); + Date date1 = DateUtil.parse("2020-01-01", "yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01", "yyyy-MM-dd"); + query.range("collTime", date1.getTime(), date2.getTime()); + query.term("collState", "3", MUST_NOT_FLAG); query.size(10); query.from(0); - query.sort("irId","asc"); + query.sort("irId", "asc"); System.out.println(query.getBody().toJSONString()); } diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java index dc9a968..4c7cea5 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,9 +3,10 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; +import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; +import org.apache.poi.ss.formula.functions.T; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; @@ -43,65 +44,82 @@ private static RestClient client; @PostConstruct - public void init(){ + public void init() { client = clientMapping; username = env.getProperty("casic.data.es.username"); password = env.getProperty("casic.data.es.password"); } - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + /** + * + * @param index + * @param type + * @param id + * @return + * @throws IOException + */ + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException { String method = "GET"; String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); + Response response = performRequest(method, endPoint, null); JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); return result.getJSONObject("_source"); } - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id新增 + */ + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ + if (ObjectUtil.isNotEmpty(id)) { endPoint.concat(id); } endPoint.concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + /** + * 根据id删除 + */ + public static Response deleteDocumentById(String index, String type, String id) throws IOException { String method = "DELETE"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); + return performRequest(method, endPoint, null); } - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id更新 + */ + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件查询 + */ + public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); logger.debug(query.getBody().toString()); Response response = performRequest(method, entPoint, query.getBody().toString()); // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results=esResponse.getDatas(); + return results; } - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件进行滚动查询 + */ + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); + List results = new ArrayList<>(); String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); logger.debug(query.getBody().toString()); @@ -109,20 +127,20 @@ JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } // 循环发送scroll请求,直到返回结果为空 entPoint = "_search/scroll"; JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ + scrollBody.put("scroll", "1m"); + scrollBody.put("scroll_id", scrollId); + while (esResponse.getDatas().size() > 0) { method = "GET"; response = performRequest(method, entPoint, scrollBody.toString()); result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } } @@ -133,37 +151,43 @@ return results; } - public static Response bulk(String index,String type,String entity) throws IOException{ + + /** + * 批处理 + * + */ + public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ + if (ObjectUtil.isNotEmpty(index)) { endPoint.append(index).append("/"); } - if(ObjectUtil.isNotEmpty(type)){ + if (ObjectUtil.isNotEmpty(type)) { endPoint.append(type).append("/"); } endPoint.append("_bulk"); endPoint.append("?refresh=true"); logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); + return performRequest(method, endPoint.toString(), entity); } + /** + * 请求操作通用类 + */ private static Response performRequest(String method, String endpoint, String entity) throws IOException { //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); + Request request = new Request(method, endpoint); + if (ObjectUtil.isNotEmpty(entity)) { + request.setEntity(new NStringEntity(entity, "utf-8")); } - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + if (ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)) { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); String token = username.concat(":").concat(password); String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); + builder.addHeader("Authorization", "Basic " + base64encode); request.setOptions(builder.build()); } - - Response response = client.performRequest(request); logger.debug(response.toString()); return response; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java index 923cdd7..30eccdb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java @@ -1,105 +1,35 @@ -//package com.casic.missiles.modular.system.controller; -// -//import com.casic.missiles.core.base.controller.ExportController; -//import com.stylefeng.guns.core.result.ResultData; -//import org.springframework.stereotype.Controller; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.ResponseBody; -//import org.springframework.web.bind.annotation.PathVariable; -//import org.springframework.beans.factory.annotation.Autowired; -//import com.casic.missiles.core.page.PageFactory; -//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -//import java.util.List; -//import com.stylefeng.guns.core.common.constant.factory.PageFactory; -//import org.springframework.web.bind.annotation.RequestParam; -//import com.stylefeng.guns.modular.system.model.DataGas; -//import com.stylefeng.guns.modular.system.service.IDataGasService; -// -///** -// * 甲烷数据控制器 -// * -// * @author dev -// * @Date 2023-04-18 15:59:47 -// */ -//@Controller -//@RequestMapping("/dataGas") -//public class DataGasController extends ExportController { -// -// -// @Autowired -// private IDataGasService dataGasService; -// -// -// /** -// * 获取甲烷数据列表 -// */ -// @RequestMapping(value = "/list") -// @ResponseBody -// public Object list(String condition) { -// return dataGasService.selectList(null); -// } -// -// /** -// * 获取甲烷数据分页列表 -// */ -// @RequestMapping(value = "/listPage") -// @ResponseBody -// public Object listPage(String condition) { -// Page page = new PageFactory().defaultPage(); -// EntityWrapper query = new EntityWrapper<>(); -// page = dataGasService.selectPage(page,query); -// return super.packForBT(page); -// } -// /** -// * 新增甲烷数据 -// */ -// @RequestMapping(value = "/add") -// @ResponseBody -// public Object add(DataGas dataGas) { -// dataGasService.insert(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 删除甲烷数据 -// */ -// @RequestMapping(value = "/delete") -// @ResponseBody -// public Object delete(@RequestParam String dataGasId) { -// dataGasService.deleteById(dataGasId); -// return SUCCESS_TIP; -// } -// -// /** -// * 批量删除 -// */ -// @RequestMapping(value = "/batchDelete") -// @ResponseBody -// public Object delete(@RequestParam("dataGasIds") List dataGasIds) { -// dataGasService.deleteBatchIds(dataGasIds); -// return SUCCESS_TIP; -// } -// -// /** -// * 修改甲烷数据 -// */ -// @RequestMapping(value = "/update") -// @ResponseBody -// public Object update(DataGas dataGas) { -// dataGasService.updateById(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 甲烷数据详情 -// */ -// @RequestMapping(value = "/detail/{dataGasId}") -// @ResponseBody -// public Object detail(@PathVariable("dataGasId") String dataGasId) { -// ResultData resultData = new ResultData(); -// resultData.setCode(200); -// resultData.setMessage("查询详情成功"); -// resultData.setData(dataGasService.selectById(dataGasId)); -// return resultData; -// } -//} +package com.casic.missiles.modular.system.controller; + +import com.casic.missiles.core.base.controller.ExportController; +import com.casic.missiles.modular.system.service.IDataGasService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.IOException; +import java.util.List; + +/** + * 甲烷数据控制器 + * + * @author dev + * @Date 2023-04-18 15:59:47 + */ +@RestController +@RequestMapping("/data") +public class DataGasController extends ExportController { + + + @Autowired + private IDataGasService dataGasService; + + + /** + * 获取甲烷数据列表 + */ + @RequestMapping(value = "/hisList") + public Object historicalData(Long monitorId,String startTime,String endTime) throws IOException { + return dataGasService.historicalData(monitorId,startTime,endTime); + } + +} diff --git a/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java new file mode 100644 index 0000000..c280e19 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java @@ -0,0 +1,79 @@ +package com.casic.missiles.dto; + +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.casic.missiles.core.es.HitResponse; + +import java.util.ArrayList; +import java.util.List; + +public class EsResponse { + private HitResponse hits; + + public List getDatas() { + List datas = new ArrayList(); + if (ObjectUtils.isEmpty(this.hits)) { + return datas; + } else { + for (int i = 0; i < this.hits.getHits().size(); ++i) { + datas.add((this.hits.getHits().get(i)).getData()); + } + + return datas; + } + } + + public EsResponse() { + } + + public HitResponse getHits() { + return this.hits; + } + + public void setHits(HitResponse hits) { + this.hits = hits; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof com.casic.missiles.core.es.EsResponse)) { + return false; + } else { + EsResponse other = (EsResponse) o; + if (!other.canEqual(this)) { + return false; + } else { + Object this$hits = this.getHits(); + Object other$hits = other.getHits(); + if (this$hits == null) { + if (other$hits != null) { + return false; + } + } else if (!this$hits.equals(other$hits)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(Object other) { + return other instanceof com.casic.missiles.core.es.EsResponse; + } + + @Override + public int hashCode() { + Boolean PRIME = true; + int result = 1; + Object $hits = this.getHits(); + result = result * 59 + ($hits == null ? 43 : $hits.hashCode()); + return result; + } + + @Override + public String toString() { + return "EsResponse(hits=" + this.getHits() + ")"; + } +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java index aeef63d..12d18a6 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -39,17 +39,17 @@ /** * 生成查询请求体基础结构: * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } * } */ public ElasticSearchQuery() { @@ -63,7 +63,7 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - public ElasticSearchQuery(JSONObject body){ + public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -71,9 +71,9 @@ return body; } - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); + private JSONObject query() { + if (ObjectUtil.isEmpty(this.body.get(QUERY))) { + this.body.put("query", new JSONObject()); } return this.body.getJSONObject(QUERY); @@ -81,175 +81,177 @@ private JSONObject bool() { JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(query.get(BOOL))) { + query.put(BOOL, new JSONObject()); } return query.getJSONObject(BOOL); } - private JSONArray boolMust(){ + private JSONArray boolMust() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONObject filter(){ + private JSONObject filter() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); + if (ObjectUtil.isEmpty(bool.get(FILTER))) { + bool.put(FILTER, new JSONObject()); } return bool.getJSONObject(FILTER); } - private JSONObject filterBool(){ + private JSONObject filterBool() { JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(filter.get(BOOL))) { + filter.put(BOOL, new JSONObject()); } return filter.getJSONObject(BOOL); } - private JSONArray filterBoolMust(){ + private JSONArray filterBoolMust() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONArray filterBoolMustnot(){ + private JSONArray filterBoolMustnot() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST_NOT))) { + bool.put(MUST_NOT, new JSONArray()); } return bool.getJSONArray(MUST_NOT); } - public void match(String filed, Serializable value){ + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); + filedJson.put(filed, value); + match.put("match", filedJson); boolMust.add(match); } - public void matchPhrase(String filed, Serializable value){ + public void matchPhrase(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject matchPhrase = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); + filedJson.put(filed, value); + matchPhrase.put("match_phrase", filedJson); boolMust.add(matchPhrase); } - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); + public void term(String filed, Serializable value) { + this.term(filed, value, MUST_FLAG); } - public void term(String filed, Serializable value,String mustFlag){ + public void term(String filed, Serializable value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + term.put("term", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(term); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(term); } } - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); + public void terms(String filed, List value) { + this.terms(filed, value, MUST_FLAG); } - public void terms(String filed, List value, String mustFlag){ + public void terms(String filed, List value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + terms.put("terms", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(terms); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(terms); } } - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); + public void range(String filed, Serializable from, Serializable to) { + this.range(filed, from, to, MUST_FLAG); } - public void range(String filed, Serializable from, Serializable to, String mustFlag){ + /** + * 范围条件是否必须 + */ + public void range(String filed, Serializable from, Serializable to, String mustFlag) { JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); + if (ObjectUtil.isNotEmpty(from)) { + rangeJson.put("from", from); } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); + if (ObjectUtil.isNotEmpty(to)) { + rangeJson.put("to", to); } JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); + filedJson.put(filed, rangeJson); JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + range.put("range", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(range); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(range); } } - public void size(int size){ - this.body.put("size",size); + public void size(int size) { + this.body.put("size", size); } public void from(int from) { - this.body.put("from",from); + this.body.put("from", from); } - public void sort(String sort, String order){ + public void sort(String sort, String order) { JSONObject sortJson = new JSONObject(); JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); + orderJson.put("order", order); + sortJson.put(sort, orderJson); + this.body.put("sort", sortJson); } - public void aggs(String aggsName, String filed){ + public void aggs(String aggsName, String filed) { JSONObject terms = new JSONObject(); - terms.put("field",filed); + terms.put("field", filed); JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); + aggsTerms.put("terms", terms); JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); + aggsBody.put(aggsName, aggsTerms); + this.body.put("aggs", aggsBody); } - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); + public void aggs(JSONObject aggBody) { + this.body.put("aggs", aggBody); } - public static void main(String[] args) { ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); + query.matchPhrase("name", "张三"); + query.term("sex", "2"); List deptIds = new ArrayList<>(); deptIds.add(0L); deptIds.add(24L); query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); + Date date1 = DateUtil.parse("2020-01-01", "yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01", "yyyy-MM-dd"); + query.range("collTime", date1.getTime(), date2.getTime()); + query.term("collState", "3", MUST_NOT_FLAG); query.size(10); query.from(0); - query.sort("irId","asc"); + query.sort("irId", "asc"); System.out.println(query.getBody().toJSONString()); } diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java index dc9a968..4c7cea5 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,9 +3,10 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; +import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; +import org.apache.poi.ss.formula.functions.T; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; @@ -43,65 +44,82 @@ private static RestClient client; @PostConstruct - public void init(){ + public void init() { client = clientMapping; username = env.getProperty("casic.data.es.username"); password = env.getProperty("casic.data.es.password"); } - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + /** + * + * @param index + * @param type + * @param id + * @return + * @throws IOException + */ + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException { String method = "GET"; String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); + Response response = performRequest(method, endPoint, null); JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); return result.getJSONObject("_source"); } - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id新增 + */ + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ + if (ObjectUtil.isNotEmpty(id)) { endPoint.concat(id); } endPoint.concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + /** + * 根据id删除 + */ + public static Response deleteDocumentById(String index, String type, String id) throws IOException { String method = "DELETE"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); + return performRequest(method, endPoint, null); } - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id更新 + */ + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件查询 + */ + public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); logger.debug(query.getBody().toString()); Response response = performRequest(method, entPoint, query.getBody().toString()); // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results=esResponse.getDatas(); + return results; } - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件进行滚动查询 + */ + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); + List results = new ArrayList<>(); String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); logger.debug(query.getBody().toString()); @@ -109,20 +127,20 @@ JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } // 循环发送scroll请求,直到返回结果为空 entPoint = "_search/scroll"; JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ + scrollBody.put("scroll", "1m"); + scrollBody.put("scroll_id", scrollId); + while (esResponse.getDatas().size() > 0) { method = "GET"; response = performRequest(method, entPoint, scrollBody.toString()); result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } } @@ -133,37 +151,43 @@ return results; } - public static Response bulk(String index,String type,String entity) throws IOException{ + + /** + * 批处理 + * + */ + public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ + if (ObjectUtil.isNotEmpty(index)) { endPoint.append(index).append("/"); } - if(ObjectUtil.isNotEmpty(type)){ + if (ObjectUtil.isNotEmpty(type)) { endPoint.append(type).append("/"); } endPoint.append("_bulk"); endPoint.append("?refresh=true"); logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); + return performRequest(method, endPoint.toString(), entity); } + /** + * 请求操作通用类 + */ private static Response performRequest(String method, String endpoint, String entity) throws IOException { //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); + Request request = new Request(method, endpoint); + if (ObjectUtil.isNotEmpty(entity)) { + request.setEntity(new NStringEntity(entity, "utf-8")); } - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + if (ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)) { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); String token = username.concat(":").concat(password); String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); + builder.addHeader("Authorization", "Basic " + base64encode); request.setOptions(builder.build()); } - - Response response = client.performRequest(request); logger.debug(response.toString()); return response; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java index 923cdd7..30eccdb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java @@ -1,105 +1,35 @@ -//package com.casic.missiles.modular.system.controller; -// -//import com.casic.missiles.core.base.controller.ExportController; -//import com.stylefeng.guns.core.result.ResultData; -//import org.springframework.stereotype.Controller; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.ResponseBody; -//import org.springframework.web.bind.annotation.PathVariable; -//import org.springframework.beans.factory.annotation.Autowired; -//import com.casic.missiles.core.page.PageFactory; -//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -//import java.util.List; -//import com.stylefeng.guns.core.common.constant.factory.PageFactory; -//import org.springframework.web.bind.annotation.RequestParam; -//import com.stylefeng.guns.modular.system.model.DataGas; -//import com.stylefeng.guns.modular.system.service.IDataGasService; -// -///** -// * 甲烷数据控制器 -// * -// * @author dev -// * @Date 2023-04-18 15:59:47 -// */ -//@Controller -//@RequestMapping("/dataGas") -//public class DataGasController extends ExportController { -// -// -// @Autowired -// private IDataGasService dataGasService; -// -// -// /** -// * 获取甲烷数据列表 -// */ -// @RequestMapping(value = "/list") -// @ResponseBody -// public Object list(String condition) { -// return dataGasService.selectList(null); -// } -// -// /** -// * 获取甲烷数据分页列表 -// */ -// @RequestMapping(value = "/listPage") -// @ResponseBody -// public Object listPage(String condition) { -// Page page = new PageFactory().defaultPage(); -// EntityWrapper query = new EntityWrapper<>(); -// page = dataGasService.selectPage(page,query); -// return super.packForBT(page); -// } -// /** -// * 新增甲烷数据 -// */ -// @RequestMapping(value = "/add") -// @ResponseBody -// public Object add(DataGas dataGas) { -// dataGasService.insert(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 删除甲烷数据 -// */ -// @RequestMapping(value = "/delete") -// @ResponseBody -// public Object delete(@RequestParam String dataGasId) { -// dataGasService.deleteById(dataGasId); -// return SUCCESS_TIP; -// } -// -// /** -// * 批量删除 -// */ -// @RequestMapping(value = "/batchDelete") -// @ResponseBody -// public Object delete(@RequestParam("dataGasIds") List dataGasIds) { -// dataGasService.deleteBatchIds(dataGasIds); -// return SUCCESS_TIP; -// } -// -// /** -// * 修改甲烷数据 -// */ -// @RequestMapping(value = "/update") -// @ResponseBody -// public Object update(DataGas dataGas) { -// dataGasService.updateById(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 甲烷数据详情 -// */ -// @RequestMapping(value = "/detail/{dataGasId}") -// @ResponseBody -// public Object detail(@PathVariable("dataGasId") String dataGasId) { -// ResultData resultData = new ResultData(); -// resultData.setCode(200); -// resultData.setMessage("查询详情成功"); -// resultData.setData(dataGasService.selectById(dataGasId)); -// return resultData; -// } -//} +package com.casic.missiles.modular.system.controller; + +import com.casic.missiles.core.base.controller.ExportController; +import com.casic.missiles.modular.system.service.IDataGasService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.IOException; +import java.util.List; + +/** + * 甲烷数据控制器 + * + * @author dev + * @Date 2023-04-18 15:59:47 + */ +@RestController +@RequestMapping("/data") +public class DataGasController extends ExportController { + + + @Autowired + private IDataGasService dataGasService; + + + /** + * 获取甲烷数据列表 + */ + @RequestMapping(value = "/hisList") + public Object historicalData(Long monitorId,String startTime,String endTime) throws IOException { + return dataGasService.historicalData(monitorId,startTime,endTime); + } + +} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java new file mode 100644 index 0000000..c354a27 --- /dev/null +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java @@ -0,0 +1,17 @@ +package com.casic.missiles.modular.system.dto; + +import lombok.Data; + +@Data +public class GasHistoricalDataVO { + + private Long stationId; + private String stationName; + private String monitorId; + private String monitorName; + private String devcode; + private String logtime; + private String direction; + private String pitch; + private String concentration; +} diff --git a/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java new file mode 100644 index 0000000..c280e19 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java @@ -0,0 +1,79 @@ +package com.casic.missiles.dto; + +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.casic.missiles.core.es.HitResponse; + +import java.util.ArrayList; +import java.util.List; + +public class EsResponse { + private HitResponse hits; + + public List getDatas() { + List datas = new ArrayList(); + if (ObjectUtils.isEmpty(this.hits)) { + return datas; + } else { + for (int i = 0; i < this.hits.getHits().size(); ++i) { + datas.add((this.hits.getHits().get(i)).getData()); + } + + return datas; + } + } + + public EsResponse() { + } + + public HitResponse getHits() { + return this.hits; + } + + public void setHits(HitResponse hits) { + this.hits = hits; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof com.casic.missiles.core.es.EsResponse)) { + return false; + } else { + EsResponse other = (EsResponse) o; + if (!other.canEqual(this)) { + return false; + } else { + Object this$hits = this.getHits(); + Object other$hits = other.getHits(); + if (this$hits == null) { + if (other$hits != null) { + return false; + } + } else if (!this$hits.equals(other$hits)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(Object other) { + return other instanceof com.casic.missiles.core.es.EsResponse; + } + + @Override + public int hashCode() { + Boolean PRIME = true; + int result = 1; + Object $hits = this.getHits(); + result = result * 59 + ($hits == null ? 43 : $hits.hashCode()); + return result; + } + + @Override + public String toString() { + return "EsResponse(hits=" + this.getHits() + ")"; + } +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java index aeef63d..12d18a6 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -39,17 +39,17 @@ /** * 生成查询请求体基础结构: * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } * } */ public ElasticSearchQuery() { @@ -63,7 +63,7 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - public ElasticSearchQuery(JSONObject body){ + public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -71,9 +71,9 @@ return body; } - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); + private JSONObject query() { + if (ObjectUtil.isEmpty(this.body.get(QUERY))) { + this.body.put("query", new JSONObject()); } return this.body.getJSONObject(QUERY); @@ -81,175 +81,177 @@ private JSONObject bool() { JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(query.get(BOOL))) { + query.put(BOOL, new JSONObject()); } return query.getJSONObject(BOOL); } - private JSONArray boolMust(){ + private JSONArray boolMust() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONObject filter(){ + private JSONObject filter() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); + if (ObjectUtil.isEmpty(bool.get(FILTER))) { + bool.put(FILTER, new JSONObject()); } return bool.getJSONObject(FILTER); } - private JSONObject filterBool(){ + private JSONObject filterBool() { JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(filter.get(BOOL))) { + filter.put(BOOL, new JSONObject()); } return filter.getJSONObject(BOOL); } - private JSONArray filterBoolMust(){ + private JSONArray filterBoolMust() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONArray filterBoolMustnot(){ + private JSONArray filterBoolMustnot() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST_NOT))) { + bool.put(MUST_NOT, new JSONArray()); } return bool.getJSONArray(MUST_NOT); } - public void match(String filed, Serializable value){ + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); + filedJson.put(filed, value); + match.put("match", filedJson); boolMust.add(match); } - public void matchPhrase(String filed, Serializable value){ + public void matchPhrase(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject matchPhrase = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); + filedJson.put(filed, value); + matchPhrase.put("match_phrase", filedJson); boolMust.add(matchPhrase); } - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); + public void term(String filed, Serializable value) { + this.term(filed, value, MUST_FLAG); } - public void term(String filed, Serializable value,String mustFlag){ + public void term(String filed, Serializable value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + term.put("term", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(term); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(term); } } - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); + public void terms(String filed, List value) { + this.terms(filed, value, MUST_FLAG); } - public void terms(String filed, List value, String mustFlag){ + public void terms(String filed, List value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + terms.put("terms", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(terms); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(terms); } } - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); + public void range(String filed, Serializable from, Serializable to) { + this.range(filed, from, to, MUST_FLAG); } - public void range(String filed, Serializable from, Serializable to, String mustFlag){ + /** + * 范围条件是否必须 + */ + public void range(String filed, Serializable from, Serializable to, String mustFlag) { JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); + if (ObjectUtil.isNotEmpty(from)) { + rangeJson.put("from", from); } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); + if (ObjectUtil.isNotEmpty(to)) { + rangeJson.put("to", to); } JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); + filedJson.put(filed, rangeJson); JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + range.put("range", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(range); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(range); } } - public void size(int size){ - this.body.put("size",size); + public void size(int size) { + this.body.put("size", size); } public void from(int from) { - this.body.put("from",from); + this.body.put("from", from); } - public void sort(String sort, String order){ + public void sort(String sort, String order) { JSONObject sortJson = new JSONObject(); JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); + orderJson.put("order", order); + sortJson.put(sort, orderJson); + this.body.put("sort", sortJson); } - public void aggs(String aggsName, String filed){ + public void aggs(String aggsName, String filed) { JSONObject terms = new JSONObject(); - terms.put("field",filed); + terms.put("field", filed); JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); + aggsTerms.put("terms", terms); JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); + aggsBody.put(aggsName, aggsTerms); + this.body.put("aggs", aggsBody); } - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); + public void aggs(JSONObject aggBody) { + this.body.put("aggs", aggBody); } - public static void main(String[] args) { ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); + query.matchPhrase("name", "张三"); + query.term("sex", "2"); List deptIds = new ArrayList<>(); deptIds.add(0L); deptIds.add(24L); query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); + Date date1 = DateUtil.parse("2020-01-01", "yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01", "yyyy-MM-dd"); + query.range("collTime", date1.getTime(), date2.getTime()); + query.term("collState", "3", MUST_NOT_FLAG); query.size(10); query.from(0); - query.sort("irId","asc"); + query.sort("irId", "asc"); System.out.println(query.getBody().toJSONString()); } diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java index dc9a968..4c7cea5 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,9 +3,10 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; +import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; +import org.apache.poi.ss.formula.functions.T; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; @@ -43,65 +44,82 @@ private static RestClient client; @PostConstruct - public void init(){ + public void init() { client = clientMapping; username = env.getProperty("casic.data.es.username"); password = env.getProperty("casic.data.es.password"); } - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + /** + * + * @param index + * @param type + * @param id + * @return + * @throws IOException + */ + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException { String method = "GET"; String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); + Response response = performRequest(method, endPoint, null); JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); return result.getJSONObject("_source"); } - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id新增 + */ + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ + if (ObjectUtil.isNotEmpty(id)) { endPoint.concat(id); } endPoint.concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + /** + * 根据id删除 + */ + public static Response deleteDocumentById(String index, String type, String id) throws IOException { String method = "DELETE"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); + return performRequest(method, endPoint, null); } - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id更新 + */ + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件查询 + */ + public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); logger.debug(query.getBody().toString()); Response response = performRequest(method, entPoint, query.getBody().toString()); // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results=esResponse.getDatas(); + return results; } - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件进行滚动查询 + */ + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); + List results = new ArrayList<>(); String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); logger.debug(query.getBody().toString()); @@ -109,20 +127,20 @@ JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } // 循环发送scroll请求,直到返回结果为空 entPoint = "_search/scroll"; JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ + scrollBody.put("scroll", "1m"); + scrollBody.put("scroll_id", scrollId); + while (esResponse.getDatas().size() > 0) { method = "GET"; response = performRequest(method, entPoint, scrollBody.toString()); result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } } @@ -133,37 +151,43 @@ return results; } - public static Response bulk(String index,String type,String entity) throws IOException{ + + /** + * 批处理 + * + */ + public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ + if (ObjectUtil.isNotEmpty(index)) { endPoint.append(index).append("/"); } - if(ObjectUtil.isNotEmpty(type)){ + if (ObjectUtil.isNotEmpty(type)) { endPoint.append(type).append("/"); } endPoint.append("_bulk"); endPoint.append("?refresh=true"); logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); + return performRequest(method, endPoint.toString(), entity); } + /** + * 请求操作通用类 + */ private static Response performRequest(String method, String endpoint, String entity) throws IOException { //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); + Request request = new Request(method, endpoint); + if (ObjectUtil.isNotEmpty(entity)) { + request.setEntity(new NStringEntity(entity, "utf-8")); } - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + if (ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)) { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); String token = username.concat(":").concat(password); String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); + builder.addHeader("Authorization", "Basic " + base64encode); request.setOptions(builder.build()); } - - Response response = client.performRequest(request); logger.debug(response.toString()); return response; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java index 923cdd7..30eccdb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java @@ -1,105 +1,35 @@ -//package com.casic.missiles.modular.system.controller; -// -//import com.casic.missiles.core.base.controller.ExportController; -//import com.stylefeng.guns.core.result.ResultData; -//import org.springframework.stereotype.Controller; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.ResponseBody; -//import org.springframework.web.bind.annotation.PathVariable; -//import org.springframework.beans.factory.annotation.Autowired; -//import com.casic.missiles.core.page.PageFactory; -//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -//import java.util.List; -//import com.stylefeng.guns.core.common.constant.factory.PageFactory; -//import org.springframework.web.bind.annotation.RequestParam; -//import com.stylefeng.guns.modular.system.model.DataGas; -//import com.stylefeng.guns.modular.system.service.IDataGasService; -// -///** -// * 甲烷数据控制器 -// * -// * @author dev -// * @Date 2023-04-18 15:59:47 -// */ -//@Controller -//@RequestMapping("/dataGas") -//public class DataGasController extends ExportController { -// -// -// @Autowired -// private IDataGasService dataGasService; -// -// -// /** -// * 获取甲烷数据列表 -// */ -// @RequestMapping(value = "/list") -// @ResponseBody -// public Object list(String condition) { -// return dataGasService.selectList(null); -// } -// -// /** -// * 获取甲烷数据分页列表 -// */ -// @RequestMapping(value = "/listPage") -// @ResponseBody -// public Object listPage(String condition) { -// Page page = new PageFactory().defaultPage(); -// EntityWrapper query = new EntityWrapper<>(); -// page = dataGasService.selectPage(page,query); -// return super.packForBT(page); -// } -// /** -// * 新增甲烷数据 -// */ -// @RequestMapping(value = "/add") -// @ResponseBody -// public Object add(DataGas dataGas) { -// dataGasService.insert(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 删除甲烷数据 -// */ -// @RequestMapping(value = "/delete") -// @ResponseBody -// public Object delete(@RequestParam String dataGasId) { -// dataGasService.deleteById(dataGasId); -// return SUCCESS_TIP; -// } -// -// /** -// * 批量删除 -// */ -// @RequestMapping(value = "/batchDelete") -// @ResponseBody -// public Object delete(@RequestParam("dataGasIds") List dataGasIds) { -// dataGasService.deleteBatchIds(dataGasIds); -// return SUCCESS_TIP; -// } -// -// /** -// * 修改甲烷数据 -// */ -// @RequestMapping(value = "/update") -// @ResponseBody -// public Object update(DataGas dataGas) { -// dataGasService.updateById(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 甲烷数据详情 -// */ -// @RequestMapping(value = "/detail/{dataGasId}") -// @ResponseBody -// public Object detail(@PathVariable("dataGasId") String dataGasId) { -// ResultData resultData = new ResultData(); -// resultData.setCode(200); -// resultData.setMessage("查询详情成功"); -// resultData.setData(dataGasService.selectById(dataGasId)); -// return resultData; -// } -//} +package com.casic.missiles.modular.system.controller; + +import com.casic.missiles.core.base.controller.ExportController; +import com.casic.missiles.modular.system.service.IDataGasService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.IOException; +import java.util.List; + +/** + * 甲烷数据控制器 + * + * @author dev + * @Date 2023-04-18 15:59:47 + */ +@RestController +@RequestMapping("/data") +public class DataGasController extends ExportController { + + + @Autowired + private IDataGasService dataGasService; + + + /** + * 获取甲烷数据列表 + */ + @RequestMapping(value = "/hisList") + public Object historicalData(Long monitorId,String startTime,String endTime) throws IOException { + return dataGasService.historicalData(monitorId,startTime,endTime); + } + +} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java new file mode 100644 index 0000000..c354a27 --- /dev/null +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java @@ -0,0 +1,17 @@ +package com.casic.missiles.modular.system.dto; + +import lombok.Data; + +@Data +public class GasHistoricalDataVO { + + private Long stationId; + private String stationName; + private String monitorId; + private String monitorName; + private String devcode; + private String logtime; + private String direction; + private String pitch; + private String concentration; +} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java index b2aa6cb..f766aa4 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java @@ -1,10 +1,14 @@ package com.casic.missiles.modular.system.service; +import com.casic.missiles.core.es.HitResponse; import com.casic.missiles.es.DataGasEs; +import com.casic.missiles.modular.system.dto.GasHistoricalDataVO; import com.casic.missiles.modular.system.model.DataGas; import com.baomidou.mybatisplus.extension.service.IService; +import org.apache.poi.ss.formula.functions.T; import java.io.IOException; +import java.util.List; /** *

@@ -15,5 +19,9 @@ * @since 2023-04-18 */ public interface IDataGasService extends IService { + void insertDataGasEs(DataGasEs dataGasEs) throws IOException; + + List historicalData(Long monitorId, String startTime, String endTime) throws IOException; + } diff --git a/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java new file mode 100644 index 0000000..c280e19 --- /dev/null +++ b/casic-common/src/main/java/com/casic/missiles/dto/EsResponse.java @@ -0,0 +1,79 @@ +package com.casic.missiles.dto; + +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; +import com.casic.missiles.core.es.HitResponse; + +import java.util.ArrayList; +import java.util.List; + +public class EsResponse { + private HitResponse hits; + + public List getDatas() { + List datas = new ArrayList(); + if (ObjectUtils.isEmpty(this.hits)) { + return datas; + } else { + for (int i = 0; i < this.hits.getHits().size(); ++i) { + datas.add((this.hits.getHits().get(i)).getData()); + } + + return datas; + } + } + + public EsResponse() { + } + + public HitResponse getHits() { + return this.hits; + } + + public void setHits(HitResponse hits) { + this.hits = hits; + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof com.casic.missiles.core.es.EsResponse)) { + return false; + } else { + EsResponse other = (EsResponse) o; + if (!other.canEqual(this)) { + return false; + } else { + Object this$hits = this.getHits(); + Object other$hits = other.getHits(); + if (this$hits == null) { + if (other$hits != null) { + return false; + } + } else if (!this$hits.equals(other$hits)) { + return false; + } + + return true; + } + } + } + + protected boolean canEqual(Object other) { + return other instanceof com.casic.missiles.core.es.EsResponse; + } + + @Override + public int hashCode() { + Boolean PRIME = true; + int result = 1; + Object $hits = this.getHits(); + result = result * 59 + ($hits == null ? 43 : $hits.hashCode()); + return result; + } + + @Override + public String toString() { + return "EsResponse(hits=" + this.getHits() + ")"; + } +} diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java index aeef63d..12d18a6 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchQuery.java @@ -39,17 +39,17 @@ /** * 生成查询请求体基础结构: * { - * "query": { - * "bool": { - * "must" [], - * "filter": { - * "bool": { - * "must": [], - * "must_not": [] - * } - * } - * } - * } + * "query": { + * "bool": { + * "must" [], + * "filter": { + * "bool": { + * "must": [], + * "must_not": [] + * } + * } + * } + * } * } */ public ElasticSearchQuery() { @@ -63,7 +63,7 @@ this.filterBoolMustnot = this.filterBoolMustnot(); } - public ElasticSearchQuery(JSONObject body){ + public ElasticSearchQuery(JSONObject body) { this.body = body; } @@ -71,9 +71,9 @@ return body; } - private JSONObject query(){ - if(ObjectUtil.isEmpty(this.body.get(QUERY))){ - this.body.put("query",new JSONObject()); + private JSONObject query() { + if (ObjectUtil.isEmpty(this.body.get(QUERY))) { + this.body.put("query", new JSONObject()); } return this.body.getJSONObject(QUERY); @@ -81,175 +81,177 @@ private JSONObject bool() { JSONObject query = this.query; - if(ObjectUtil.isEmpty(query.get(BOOL))){ - query.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(query.get(BOOL))) { + query.put(BOOL, new JSONObject()); } return query.getJSONObject(BOOL); } - private JSONArray boolMust(){ + private JSONArray boolMust() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONObject filter(){ + private JSONObject filter() { JSONObject bool = this.bool; - if(ObjectUtil.isEmpty(bool.get(FILTER))){ - bool.put(FILTER,new JSONObject()); + if (ObjectUtil.isEmpty(bool.get(FILTER))) { + bool.put(FILTER, new JSONObject()); } return bool.getJSONObject(FILTER); } - private JSONObject filterBool(){ + private JSONObject filterBool() { JSONObject filter = this.filter; - if(ObjectUtil.isEmpty(filter.get(BOOL))){ - filter.put(BOOL,new JSONObject()); + if (ObjectUtil.isEmpty(filter.get(BOOL))) { + filter.put(BOOL, new JSONObject()); } return filter.getJSONObject(BOOL); } - private JSONArray filterBoolMust(){ + private JSONArray filterBoolMust() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST))){ - bool.put(MUST,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST))) { + bool.put(MUST, new JSONArray()); } return bool.getJSONArray(MUST); } - private JSONArray filterBoolMustnot(){ + private JSONArray filterBoolMustnot() { JSONObject bool = this.filterBool; - if(ObjectUtil.isEmpty(bool.get(MUST_NOT))){ - bool.put(MUST_NOT,new JSONArray()); + if (ObjectUtil.isEmpty(bool.get(MUST_NOT))) { + bool.put(MUST_NOT, new JSONArray()); } return bool.getJSONArray(MUST_NOT); } - public void match(String filed, Serializable value){ + public void match(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject match = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - match.put("match",filedJson); + filedJson.put(filed, value); + match.put("match", filedJson); boolMust.add(match); } - public void matchPhrase(String filed, Serializable value){ + public void matchPhrase(String filed, Serializable value) { JSONArray boolMust = this.boolMust; JSONObject matchPhrase = new JSONObject(); JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); - matchPhrase.put("match_phrase",filedJson); + filedJson.put(filed, value); + matchPhrase.put("match_phrase", filedJson); boolMust.add(matchPhrase); } - public void term(String filed, Serializable value){ - this.term(filed,value,MUST_FLAG); + public void term(String filed, Serializable value) { + this.term(filed, value, MUST_FLAG); } - public void term(String filed, Serializable value,String mustFlag){ + public void term(String filed, Serializable value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject term = new JSONObject(); - term.put("term",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + term.put("term", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(term); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(term); } } - public void terms(String filed,List value){ - this.terms(filed,value,MUST_FLAG); + public void terms(String filed, List value) { + this.terms(filed, value, MUST_FLAG); } - public void terms(String filed, List value, String mustFlag){ + public void terms(String filed, List value, String mustFlag) { JSONObject filedJson = new JSONObject(); - filedJson.put(filed,value); + filedJson.put(filed, value); JSONObject terms = new JSONObject(); - terms.put("terms",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + terms.put("terms", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(terms); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(terms); } } - public void range(String filed, Serializable from, Serializable to){ - this.range(filed,from,to,MUST_FLAG); + public void range(String filed, Serializable from, Serializable to) { + this.range(filed, from, to, MUST_FLAG); } - public void range(String filed, Serializable from, Serializable to, String mustFlag){ + /** + * 范围条件是否必须 + */ + public void range(String filed, Serializable from, Serializable to, String mustFlag) { JSONObject rangeJson = new JSONObject(); - if(ObjectUtil.isNotEmpty(from)){ - rangeJson.put("from",from); + if (ObjectUtil.isNotEmpty(from)) { + rangeJson.put("from", from); } - if(ObjectUtil.isNotEmpty(to)){ - rangeJson.put("to",to); + if (ObjectUtil.isNotEmpty(to)) { + rangeJson.put("to", to); } JSONObject filedJson = new JSONObject(); - filedJson.put(filed,rangeJson); + filedJson.put(filed, rangeJson); JSONObject range = new JSONObject(); - range.put("range",filedJson); - if(MUST_FLAG.equals(mustFlag)){ + range.put("range", filedJson); + if (MUST_FLAG.equals(mustFlag)) { this.filterBoolMust.add(range); } - if(MUST_NOT_FLAG.equals(mustFlag)){ + if (MUST_NOT_FLAG.equals(mustFlag)) { this.filterBoolMustnot.add(range); } } - public void size(int size){ - this.body.put("size",size); + public void size(int size) { + this.body.put("size", size); } public void from(int from) { - this.body.put("from",from); + this.body.put("from", from); } - public void sort(String sort, String order){ + public void sort(String sort, String order) { JSONObject sortJson = new JSONObject(); JSONObject orderJson = new JSONObject(); - orderJson.put("order",order); - sortJson.put(sort,orderJson); - this.body.put("sort",sortJson); + orderJson.put("order", order); + sortJson.put(sort, orderJson); + this.body.put("sort", sortJson); } - public void aggs(String aggsName, String filed){ + public void aggs(String aggsName, String filed) { JSONObject terms = new JSONObject(); - terms.put("field",filed); + terms.put("field", filed); JSONObject aggsTerms = new JSONObject(); - aggsTerms.put("terms",terms); + aggsTerms.put("terms", terms); JSONObject aggsBody = new JSONObject(); - aggsBody.put(aggsName,aggsTerms); - this.body.put("aggs",aggsBody); + aggsBody.put(aggsName, aggsTerms); + this.body.put("aggs", aggsBody); } - public void aggs(JSONObject aggBody){ - this.body.put("aggs",aggBody); + public void aggs(JSONObject aggBody) { + this.body.put("aggs", aggBody); } - public static void main(String[] args) { ElasticSearchQuery query = new ElasticSearchQuery(); - query.matchPhrase("name","张三"); - query.term("sex","2"); + query.matchPhrase("name", "张三"); + query.term("sex", "2"); List deptIds = new ArrayList<>(); deptIds.add(0L); deptIds.add(24L); query.terms("deptId", deptIds); - Date date1 = DateUtil.parse("2020-01-01","yyyy-MM-dd"); - Date date2 = DateUtil.parse("2020-04-01","yyyy-MM-dd"); - query.range("collTime",date1.getTime(),date2.getTime()); - query.term("collState","3",MUST_NOT_FLAG); + Date date1 = DateUtil.parse("2020-01-01", "yyyy-MM-dd"); + Date date2 = DateUtil.parse("2020-04-01", "yyyy-MM-dd"); + query.range("collTime", date1.getTime(), date2.getTime()); + query.term("collState", "3", MUST_NOT_FLAG); query.size(10); query.from(0); - query.sort("irId","asc"); + query.sort("irId", "asc"); System.out.println(query.getBody().toJSONString()); } diff --git a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java index dc9a968..4c7cea5 100644 --- a/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java +++ b/casic-common/src/main/java/com/casic/missiles/es/ElasticSearchUtil.java @@ -3,9 +3,10 @@ import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.casic.missiles.core.es.EsResponse; +import com.casic.missiles.dto.EsResponse; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; +import org.apache.poi.ss.formula.functions.T; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; @@ -43,65 +44,82 @@ private static RestClient client; @PostConstruct - public void init(){ + public void init() { client = clientMapping; username = env.getProperty("casic.data.es.username"); password = env.getProperty("casic.data.es.password"); } - public static JSONObject selectDocumentById(String index, String type, String id) throws IOException{ + /** + * + * @param index + * @param type + * @param id + * @return + * @throws IOException + */ + public static JSONObject selectDocumentById(String index, String type, String id) throws IOException { String method = "GET"; String endPoint = index.concat("/").concat(type).concat("/").concat(id); - Response response = performRequest(method,endPoint,null); + Response response = performRequest(method, endPoint, null); JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); return result.getJSONObject("_source"); } - public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id新增 + */ + public static Response addDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/"); - if(ObjectUtil.isNotEmpty(id)){ + if (ObjectUtil.isNotEmpty(id)) { endPoint.concat(id); } endPoint.concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static Response deleteDocumentById(String index, String type, String id) throws IOException{ + /** + * 根据id删除 + */ + public static Response deleteDocumentById(String index, String type, String id) throws IOException { String method = "DELETE"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); - return performRequest(method,endPoint,null); + return performRequest(method, endPoint, null); } - public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException{ + /** + * 根据id更新 + */ + public static Response updateDocumentById(String index, String type, String id, Object entity) throws IOException { String method = "POST"; String endPoint = index.concat("/").concat(type).concat("/").concat(id).concat("?refresh=true"); logger.debug(entity.toString()); - return performRequest(method,endPoint,JSON.toJSONString(entity)); + return performRequest(method, endPoint, JSON.toJSONString(entity)); } - public static EsResponse searchQuery(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件查询 + */ + public static List searchQuery(String index, String type, ElasticSearchQuery query) throws IOException { String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); logger.debug(query.getBody().toString()); Response response = performRequest(method, entPoint, query.getBody().toString()); // 获取response body,转换为json对象 JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); - return result.toJavaObject(EsResponse.class); + EsResponse esResponse = result.toJavaObject(EsResponse.class); + List results=esResponse.getDatas(); + return results; } - public static Response aggsQuery(String index, String type, ElasticSearchQuery query) throws IOException{ - String method = "POST"; - String entPoint = index.concat("/").concat(type).concat("/").concat("_search"); - logger.debug(query.getBody().toString()); - Response response = performRequest(method, entPoint, query.getBody().toString()); - return response; - } - - public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException{ + /** + * 根据条件进行滚动查询 + */ + public static List searchQueryScroll(String index, String type, ElasticSearchQuery query) throws IOException { // 首次查询,提交查询条件,endpoint增加‘?scroll=1m’ - List results = new ArrayList<>(); + List results = new ArrayList<>(); String method = "POST"; String entPoint = index.concat("/").concat(type).concat("/").concat("_search").concat("?scroll=1m"); logger.debug(query.getBody().toString()); @@ -109,20 +127,20 @@ JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); EsResponse esResponse = result.toJavaObject(EsResponse.class); String scrollId = result.getString("_scroll_id"); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } // 循环发送scroll请求,直到返回结果为空 entPoint = "_search/scroll"; JSONObject scrollBody = new JSONObject(); - scrollBody.put("scroll","1m"); - scrollBody.put("scroll_id",scrollId); - while (esResponse.getDatas().size() > 0){ + scrollBody.put("scroll", "1m"); + scrollBody.put("scroll_id", scrollId); + while (esResponse.getDatas().size() > 0) { method = "GET"; response = performRequest(method, entPoint, scrollBody.toString()); result = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); esResponse = result.toJavaObject(EsResponse.class); - if(ObjectUtil.isNotEmpty(esResponse.getDatas())){ + if (ObjectUtil.isNotEmpty(esResponse.getDatas())) { results.addAll(esResponse.getDatas()); } } @@ -133,37 +151,43 @@ return results; } - public static Response bulk(String index,String type,String entity) throws IOException{ + + /** + * 批处理 + * + */ + public static Response bulk(String index, String type, String entity) throws IOException { String method = "POST"; StringBuilder endPoint = new StringBuilder(); - if(ObjectUtil.isNotEmpty(index)){ + if (ObjectUtil.isNotEmpty(index)) { endPoint.append(index).append("/"); } - if(ObjectUtil.isNotEmpty(type)){ + if (ObjectUtil.isNotEmpty(type)) { endPoint.append(type).append("/"); } endPoint.append("_bulk"); endPoint.append("?refresh=true"); logger.debug(entity); - return performRequest(method,endPoint.toString(),entity); + return performRequest(method, endPoint.toString(), entity); } + /** + * 请求操作通用类 + */ private static Response performRequest(String method, String endpoint, String entity) throws IOException { //logger.info(method + " " + endpoint); - Request request = new Request(method,endpoint); - if(ObjectUtil.isNotEmpty(entity)){ - request.setEntity(new NStringEntity(entity,"utf-8")); + Request request = new Request(method, endpoint); + if (ObjectUtil.isNotEmpty(entity)) { + request.setEntity(new NStringEntity(entity, "utf-8")); } - if(ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)){ + if (ObjectUtil.isNotEmpty(username) && ObjectUtil.isNotEmpty(password)) { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); String token = username.concat(":").concat(password); String base64encode = Base64.getEncoder().encodeToString(token.getBytes("utf-8")); - builder.addHeader("Authorization", "Basic " + base64encode ); + builder.addHeader("Authorization", "Basic " + base64encode); request.setOptions(builder.build()); } - - Response response = client.performRequest(request); logger.debug(response.toString()); return response; diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java index 923cdd7..30eccdb 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/controller/DataGasController.java @@ -1,105 +1,35 @@ -//package com.casic.missiles.modular.system.controller; -// -//import com.casic.missiles.core.base.controller.ExportController; -//import com.stylefeng.guns.core.result.ResultData; -//import org.springframework.stereotype.Controller; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.ResponseBody; -//import org.springframework.web.bind.annotation.PathVariable; -//import org.springframework.beans.factory.annotation.Autowired; -//import com.casic.missiles.core.page.PageFactory; -//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -//import java.util.List; -//import com.stylefeng.guns.core.common.constant.factory.PageFactory; -//import org.springframework.web.bind.annotation.RequestParam; -//import com.stylefeng.guns.modular.system.model.DataGas; -//import com.stylefeng.guns.modular.system.service.IDataGasService; -// -///** -// * 甲烷数据控制器 -// * -// * @author dev -// * @Date 2023-04-18 15:59:47 -// */ -//@Controller -//@RequestMapping("/dataGas") -//public class DataGasController extends ExportController { -// -// -// @Autowired -// private IDataGasService dataGasService; -// -// -// /** -// * 获取甲烷数据列表 -// */ -// @RequestMapping(value = "/list") -// @ResponseBody -// public Object list(String condition) { -// return dataGasService.selectList(null); -// } -// -// /** -// * 获取甲烷数据分页列表 -// */ -// @RequestMapping(value = "/listPage") -// @ResponseBody -// public Object listPage(String condition) { -// Page page = new PageFactory().defaultPage(); -// EntityWrapper query = new EntityWrapper<>(); -// page = dataGasService.selectPage(page,query); -// return super.packForBT(page); -// } -// /** -// * 新增甲烷数据 -// */ -// @RequestMapping(value = "/add") -// @ResponseBody -// public Object add(DataGas dataGas) { -// dataGasService.insert(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 删除甲烷数据 -// */ -// @RequestMapping(value = "/delete") -// @ResponseBody -// public Object delete(@RequestParam String dataGasId) { -// dataGasService.deleteById(dataGasId); -// return SUCCESS_TIP; -// } -// -// /** -// * 批量删除 -// */ -// @RequestMapping(value = "/batchDelete") -// @ResponseBody -// public Object delete(@RequestParam("dataGasIds") List dataGasIds) { -// dataGasService.deleteBatchIds(dataGasIds); -// return SUCCESS_TIP; -// } -// -// /** -// * 修改甲烷数据 -// */ -// @RequestMapping(value = "/update") -// @ResponseBody -// public Object update(DataGas dataGas) { -// dataGasService.updateById(dataGas); -// return SUCCESS_TIP; -// } -// -// /** -// * 甲烷数据详情 -// */ -// @RequestMapping(value = "/detail/{dataGasId}") -// @ResponseBody -// public Object detail(@PathVariable("dataGasId") String dataGasId) { -// ResultData resultData = new ResultData(); -// resultData.setCode(200); -// resultData.setMessage("查询详情成功"); -// resultData.setData(dataGasService.selectById(dataGasId)); -// return resultData; -// } -//} +package com.casic.missiles.modular.system.controller; + +import com.casic.missiles.core.base.controller.ExportController; +import com.casic.missiles.modular.system.service.IDataGasService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.IOException; +import java.util.List; + +/** + * 甲烷数据控制器 + * + * @author dev + * @Date 2023-04-18 15:59:47 + */ +@RestController +@RequestMapping("/data") +public class DataGasController extends ExportController { + + + @Autowired + private IDataGasService dataGasService; + + + /** + * 获取甲烷数据列表 + */ + @RequestMapping(value = "/hisList") + public Object historicalData(Long monitorId,String startTime,String endTime) throws IOException { + return dataGasService.historicalData(monitorId,startTime,endTime); + } + +} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java new file mode 100644 index 0000000..c354a27 --- /dev/null +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/dto/GasHistoricalDataVO.java @@ -0,0 +1,17 @@ +package com.casic.missiles.modular.system.dto; + +import lombok.Data; + +@Data +public class GasHistoricalDataVO { + + private Long stationId; + private String stationName; + private String monitorId; + private String monitorName; + private String devcode; + private String logtime; + private String direction; + private String pitch; + private String concentration; +} diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java index b2aa6cb..f766aa4 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/IDataGasService.java @@ -1,10 +1,14 @@ package com.casic.missiles.modular.system.service; +import com.casic.missiles.core.es.HitResponse; import com.casic.missiles.es.DataGasEs; +import com.casic.missiles.modular.system.dto.GasHistoricalDataVO; import com.casic.missiles.modular.system.model.DataGas; import com.baomidou.mybatisplus.extension.service.IService; +import org.apache.poi.ss.formula.functions.T; import java.io.IOException; +import java.util.List; /** *

@@ -15,5 +19,9 @@ * @since 2023-04-18 */ public interface IDataGasService extends IService { + void insertDataGasEs(DataGasEs dataGasEs) throws IOException; + + List historicalData(Long monitorId, String startTime, String endTime) throws IOException; + } diff --git a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java index ba3a38d..3833e77 100644 --- a/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java +++ b/casic-data/src/main/java/com/casic/missiles/modular/system/service/impl/DataGasServiceImpl.java @@ -1,15 +1,21 @@ package com.casic.missiles.modular.system.service.impl; +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.casic.missiles.dto.EsResponse; +import com.casic.missiles.core.es.HitResponse; import com.casic.missiles.es.DataGasEs; +import com.casic.missiles.es.ElasticSearchQuery; import com.casic.missiles.es.ElasticSearchUtil; import com.casic.missiles.modular.system.consts.DataConst; import com.casic.missiles.modular.system.dao.DataGasMapper; import com.casic.missiles.modular.system.model.DataGas; import com.casic.missiles.modular.system.service.IDataGasService; +import org.apache.poi.ss.formula.functions.T; import org.springframework.stereotype.Service; import java.io.IOException; +import java.util.List; /** *

@@ -26,4 +32,17 @@ public void insertDataGasEs(DataGasEs dataGasEs) throws IOException { ElasticSearchUtil.addDocumentById(DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, null, dataGasEs); } + + @Override + public List historicalData(Long monitorId, String startTime, String endTime) throws IOException { + ElasticSearchQuery elasticSearchQuery = new ElasticSearchQuery(); + if (ObjectUtils.isNotEmpty(monitorId)) { + elasticSearchQuery.term("monitorId", monitorId); + } + elasticSearchQuery.range("logTime", startTime, endTime); + elasticSearchQuery.size(10000); + elasticSearchQuery.sort("logTime", "DESC"); + List dataGasEsList = ElasticSearchUtil.searchQueryScroll(DataConst.DATA_GAS_ES_INDEX, DataConst.DATA_GAS_ES_TYPE, elasticSearchQuery); + return dataGasEsList; + } }