diff --git a/pom.xml b/pom.xml index e7d06b1..79f95b1 100644 --- a/pom.xml +++ b/pom.xml @@ -130,6 +130,12 @@ fastjson 1.2.15 + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.1.0 + diff --git a/src/main/java/com/gzzn/omms/adminapi/controller/history/HistoryFlightDataController.java b/src/main/java/com/gzzn/omms/adminapi/controller/history/HistoryFlightDataController.java new file mode 100644 index 0000000..7792d88 --- /dev/null +++ b/src/main/java/com/gzzn/omms/adminapi/controller/history/HistoryFlightDataController.java @@ -0,0 +1,31 @@ +package com.gzzn.omms.adminapi.controller.history; + +import io.swagger.annotations.ApiOperation; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import com.gzzn.omms.adminapi.dto.PageInfoDto; +import com.gzzn.omms.adminapi.dto.ResponseDto; +import com.gzzn.omms.adminapi.dto.history.HistoryFilghtConditionDto; +import com.gzzn.omms.adminapi.dto.history.HistoryFilghtDataDto; +/** + * 历史航班数据 controller + * @author fhc + * @since 2018-11-30 + * + */ +@RestController +public class HistoryFlightDataController { + + @ApiOperation(value="获取历史航班数据",tags="历史航班数据") + @PostMapping(value="/hitFlightData/list") + public ResponseDto> findAllByGroupCode(@RequestBody HistoryFilghtConditionDto conditions) { + PageInfoDto page = new PageInfoDto(); + + //TODO 利用 ElasticsearchUtil 进行分页查询 + + return ResponseDto.success(page); + } +} diff --git a/src/main/java/com/gzzn/omms/adminapi/dto/PageInfoDto.java b/src/main/java/com/gzzn/omms/adminapi/dto/PageInfoDto.java new file mode 100644 index 0000000..67edcc9 --- /dev/null +++ b/src/main/java/com/gzzn/omms/adminapi/dto/PageInfoDto.java @@ -0,0 +1,149 @@ +package com.gzzn.omms.adminapi.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; +import java.util.Collection; +import java.util.List; + +import com.github.pagehelper.Page; +/** + * + * @author yinsh + * @Description: 分页参数 + * @date 2018年1月12日 下午2:34:16 + * @param + */ +@ApiModel(value="PageInfoDto",description="Api返回分页Dto") +public class PageInfoDto implements Serializable { + private static final long serialVersionUID = 1L; + @ApiModelProperty(value="当前页",example="1") + private int pageNum=1; + @ApiModelProperty(value="每页的数量",example="10") + private int pageSize=10; + @ApiModelProperty(value="总记录数",example="10") + private long total; + @ApiModelProperty(value="总页数",example="1") + private int pages; + //结果集 + private List list; + //是否为第一页 + @ApiModelProperty(hidden=true) + private boolean isFirstPage = false; + //是否为最后一页 + @ApiModelProperty(hidden=true) + private boolean isLastPage = false; + + + public PageInfoDto() { + } + + /** + * 包装Page对象 + * + * @param list + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + public PageInfoDto(List list) { + if (list instanceof Page) { + Page page = (Page) list; + this.pageNum = page.getPageNum(); + this.pageSize = page.getPageSize(); + + this.pages = page.getPages(); + this.list = page; + this.total = page.getTotal(); + } else if (list instanceof Collection) { + this.pageNum = 1; + this.pageSize = list.size(); + + this.pages = 1; + this.list = list; + this.total = list.size(); + } + if (list instanceof Collection) { + //判断页面边界 + judgePageBoudary(); + } + } + + /** + * 判定页面边界 + */ + private void judgePageBoudary() { + isFirstPage = pageNum == 1; + isLastPage = pageNum == pages; + } + + public int getPageNum() { + return pageNum; + } + + public void setPageNum(int pageNum) { + this.pageNum = pageNum; + } + + public int getPageSize() { + return pageSize; + } + + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + + public long getTotal() { + return total; + } + + public void setTotal(long total) { + this.total = total; + } + + public int getPages() { + return pages; + } + + public void setPages(int pages) { + this.pages = pages; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public boolean isIsFirstPage() { + return isFirstPage; + } + + public void setIsFirstPage(boolean isFirstPage) { + this.isFirstPage = isFirstPage; + } + + public boolean isIsLastPage() { + return isLastPage; + } + + public void setIsLastPage(boolean isLastPage) { + this.isLastPage = isLastPage; + } + + @Override + public String toString() { + final StringBuffer sb = new StringBuffer("PageInfo{"); + sb.append("pageNum=").append(pageNum); + sb.append(", pageSize=").append(pageSize); + sb.append(", total=").append(total); + sb.append(", pages=").append(pages); + sb.append(", list=").append(list); + sb.append(", isFirstPage=").append(isFirstPage); + sb.append(", isLastPage=").append(isLastPage); + sb.append(", navigatepageNums="); + sb.append('}'); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/com/gzzn/omms/adminapi/dto/history/HistoryFilghtConditionDto.java b/src/main/java/com/gzzn/omms/adminapi/dto/history/HistoryFilghtConditionDto.java new file mode 100644 index 0000000..a34dd3f --- /dev/null +++ b/src/main/java/com/gzzn/omms/adminapi/dto/history/HistoryFilghtConditionDto.java @@ -0,0 +1,165 @@ +package com.gzzn.omms.adminapi.dto.history; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * 历史航班数据Dto + * @author fhc + * @since 2018-11-30 + * + */ +@ApiModel(value="历史航班数据条件Dto",description="查询历史航班数据的条件DTO") +public class HistoryFilghtConditionDto { + //TODO 更改字段名 + @ApiModelProperty(value="航空公司",example="广东航空") + private String filed_1; + @ApiModelProperty(value="航线类别",example="航线类别类别,I-国际、D-国内、M-混合") + private String filed_2; + @ApiModelProperty(value="航班任务",example="航班任务A") + private String filed_3; + + @ApiModelProperty(value="计划出发时间",example="计划出发时间") + private String filed_4; + @ApiModelProperty(value="计划到达时间",example="计划到达时间") + private String filed_5; + + @ApiModelProperty(value="预计出发时间",example="预计出发时间") + private String filed_6; + @ApiModelProperty(value="预计到达时间",example="预计到达时间") + private String filed_7; + + @ApiModelProperty(value="实际出发时间",example="实际出发时间") + private String filed_8; + @ApiModelProperty(value="实际到达时间",example="实际到达时间") + private String filed_9; + + @ApiModelProperty(value="航班代理",example="航班代理") + private String filed_10; + @ApiModelProperty(value="开始时间",example="开始时间") + private String filed_11; + @ApiModelProperty(value="结束时间",example="结束时间") + private String filed_12; + @ApiModelProperty(value="航班异常状态",example="航班异常状态,1-正常、2-检修") + private String filed_13; + + @ApiModelProperty(value="是否查询航班关联信息",example="是否查询航班关联信息,Y-是、N-否") + private String filed_14; + @ApiModelProperty(value="是否查询详细信息",example="是否查询详细信息,Y-是、N-否") + private String filed_15; + @ApiModelProperty(value="是否查询保障信息",example="是否查询保障信息,Y-是、N-否") + private String filed_16; + @ApiModelProperty(value="是否查询航班VIP信息",example="是否查询航班VIP信息,Y-是、N-否") + private String filed_17; + @ApiModelProperty(value="是否查询航班特服信息",example="是否查询航班特服信息,Y-是、N-否") + private String filed_18; + public String getFiled_1() { + return filed_1; + } + public void setFiled_1(String filed_1) { + this.filed_1 = filed_1; + } + public String getFiled_2() { + return filed_2; + } + public void setFiled_2(String filed_2) { + this.filed_2 = filed_2; + } + public String getFiled_3() { + return filed_3; + } + public void setFiled_3(String filed_3) { + this.filed_3 = filed_3; + } + public String getFiled_4() { + return filed_4; + } + public void setFiled_4(String filed_4) { + this.filed_4 = filed_4; + } + public String getFiled_5() { + return filed_5; + } + public void setFiled_5(String filed_5) { + this.filed_5 = filed_5; + } + public String getFiled_6() { + return filed_6; + } + public void setFiled_6(String filed_6) { + this.filed_6 = filed_6; + } + public String getFiled_7() { + return filed_7; + } + public void setFiled_7(String filed_7) { + this.filed_7 = filed_7; + } + public String getFiled_8() { + return filed_8; + } + public void setFiled_8(String filed_8) { + this.filed_8 = filed_8; + } + public String getFiled_9() { + return filed_9; + } + public void setFiled_9(String filed_9) { + this.filed_9 = filed_9; + } + public String getFiled_10() { + return filed_10; + } + public void setFiled_10(String filed_10) { + this.filed_10 = filed_10; + } + public String getFiled_11() { + return filed_11; + } + public void setFiled_11(String filed_11) { + this.filed_11 = filed_11; + } + public String getFiled_12() { + return filed_12; + } + public void setFiled_12(String filed_12) { + this.filed_12 = filed_12; + } + public String getFiled_13() { + return filed_13; + } + public void setFiled_13(String filed_13) { + this.filed_13 = filed_13; + } + public String getFiled_14() { + return filed_14; + } + public void setFiled_14(String filed_14) { + this.filed_14 = filed_14; + } + public String getFiled_15() { + return filed_15; + } + public void setFiled_15(String filed_15) { + this.filed_15 = filed_15; + } + public String getFiled_16() { + return filed_16; + } + public void setFiled_16(String filed_16) { + this.filed_16 = filed_16; + } + public String getFiled_17() { + return filed_17; + } + public void setFiled_17(String filed_17) { + this.filed_17 = filed_17; + } + public String getFiled_18() { + return filed_18; + } + public void setFiled_18(String filed_18) { + this.filed_18 = filed_18; + } + +} diff --git a/src/main/java/com/gzzn/omms/adminapi/dto/history/HistoryFilghtDataDto.java b/src/main/java/com/gzzn/omms/adminapi/dto/history/HistoryFilghtDataDto.java new file mode 100644 index 0000000..5147905 --- /dev/null +++ b/src/main/java/com/gzzn/omms/adminapi/dto/history/HistoryFilghtDataDto.java @@ -0,0 +1,40 @@ +package com.gzzn.omms.adminapi.dto.history; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * 历史航班数据Dto + * @author fhc + * @since 2018-11-30 + * + */ +@ApiModel(value="历史航班数据Dto",description="历史航班数据Dto") +public class HistoryFilghtDataDto { + + @ApiModelProperty(value="历史数据——1",example="历史数据——1") + private String filed_1; + @ApiModelProperty(value="历史数据——2",example="历史数据——2") + private String filed_2; + @ApiModelProperty(value="历史数据——3",example="历史数据——3") + private String filed_3; + public String getFiled_1() { + return filed_1; + } + public void setFiled_1(String filed_1) { + this.filed_1 = filed_1; + } + public String getFiled_2() { + return filed_2; + } + public void setFiled_2(String filed_2) { + this.filed_2 = filed_2; + } + public String getFiled_3() { + return filed_3; + } + public void setFiled_3(String filed_3) { + this.filed_3 = filed_3; + } + +} diff --git a/src/main/java/com/gzzn/omms/adminapi/elasticsearch/ElasticsearchUtil.java b/src/main/java/com/gzzn/omms/adminapi/elasticsearch/ElasticsearchUtil.java index 6f507a3..b55a218 100644 --- a/src/main/java/com/gzzn/omms/adminapi/elasticsearch/ElasticsearchUtil.java +++ b/src/main/java/com/gzzn/omms/adminapi/elasticsearch/ElasticsearchUtil.java @@ -1,18 +1,35 @@ package com.gzzn.omms.adminapi.elasticsearch; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.UUID; import javax.annotation.PostConstruct; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.get.GetRequestBuilder; +import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; +import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.transport.TransportClient; +import org.elasticsearch.common.text.Text; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; +import org.elasticsearch.search.sort.SortOrder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import com.alibaba.fastjson.JSONObject; @@ -48,6 +65,25 @@ public class ElasticsearchUtil { return indexresponse.isAcknowledged(); } + /** + * 删除索引 + * + * @param index + * @return + */ + public static boolean deleteIndex(String index) { + if (!isIndexExist(index)) { + LOGGER.info("Index is not exits!"); + } + DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index).execute().actionGet(); + if (dResponse.isAcknowledged()) { + LOGGER.info("delete index " + index + " successfully!"); + } else { + LOGGER.info("Fail to delete index " + index); + } + return dResponse.isAcknowledged(); + } + /** * 判断索引是否存在 * @@ -64,6 +100,18 @@ public class ElasticsearchUtil { return inExistsResponse.isExists(); } + /** + * @Author: LX + * @Description: 判断inde下指定type是否存在 + * @Date: 2018/11/6 14:46 + * @Modified by: + */ + public boolean isTypeExist(String index, String type) { + return isIndexExist(index) + ? client.admin().indices().prepareTypesExists(index).setTypes(type).execute().actionGet().isExists() + : false; + } + /** * 数据添加,正定ID * @@ -73,7 +121,7 @@ public class ElasticsearchUtil { * @param id 数据ID * @return */ - private static String addData(JSONObject jsonObject, String index, String type, String id) { + public static String addData(JSONObject jsonObject, String index, String type, String id) { IndexResponse response = client.prepareIndex(index, type, id).setSource(jsonObject).get(); LOGGER.info("addData response status:{},id:{}", response.status().getStatus(), response.getId()); return response.getId(); @@ -91,6 +139,228 @@ public class ElasticsearchUtil { return addData(jsonObject, index, type, ElasticsearchUtil.getUUID32()); } + /** + * 通过ID删除数据 + * + * @param index 索引,类似数据库 + * @param type 类型,类似表 + * @param id 数据ID + */ + public static void deleteDataById(String index, String type, String id) { + + DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet(); + + LOGGER.info("deleteDataById response status:{},id:{}", response.status().getStatus(), response.getId()); + } + + /** + * 通过ID 更新数据 + * + * @param jsonObject 要增加的数据 + * @param index 索引,类似数据库 + * @param type 类型,类似表 + * @param id 数据ID + * @return + */ + public static void updateDataById(JSONObject jsonObject, String index, String type, String id) { + + UpdateRequest updateRequest = new UpdateRequest(); + + updateRequest.index(index).type(type).id(id).doc(jsonObject); + + client.update(updateRequest); + + } + + /** + * 通过ID获取数据 + * + * @param index 索引,类似数据库 + * @param type 类型,类似表 + * @param id 数据ID + * @param fields 需要显示的字段,逗号分隔(缺省为全部字段) + * @return + */ + public static Map searchDataById(String index, String type, String id, String fields) { + + GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id); + + if (!StringUtils.isEmpty(fields)) { + getRequestBuilder.setFetchSource(fields.split(","), null); + } + + GetResponse getResponse = getRequestBuilder.execute().actionGet(); + + return getResponse.getSource(); + } + + + /** + * 使用分词查询,并分页 + * + * @param index 索引名称 + * @param type 类型名称,可传入多个type逗号分隔 + * @param startPage 当前页 从1开始 + * @param pageSize 每页显示条数 + * @param query 查询条件 + * @param fields 需要显示的字段,逗号分隔(缺省为全部字段) + * @param sortField 排序字段 + * @param highlightField 高亮字段 + * @return + */ + public static EsPage searchDataPage(String index, String type, int startPage, int pageSize, QueryBuilder query, String fields, String sortField, String highlightField) { + SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index); + if (!StringUtils.isEmpty(type)) { + searchRequestBuilder.setTypes(type.split(",")); + } + searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH); + + // 需要显示的字段,逗号分隔(缺省为全部字段) + if (!StringUtils.isEmpty(fields)) { + searchRequestBuilder.setFetchSource(fields.split(","), null); + } + + //排序字段 + if (!StringUtils.isEmpty(sortField)) { + searchRequestBuilder.addSort(sortField, SortOrder.DESC); + } + + // 高亮(xxx=111,aaa=222) + if (!StringUtils.isEmpty(highlightField)) { + HighlightBuilder highlightBuilder = new HighlightBuilder(); + + //highlightBuilder.preTags("");//设置前缀 + //highlightBuilder.postTags("");//设置后缀 + + // 设置高亮字段 + highlightBuilder.field(highlightField); + searchRequestBuilder.highlighter(highlightBuilder); + } + + //searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery()); + searchRequestBuilder.setQuery(query); + + // 分页应用 + searchRequestBuilder.setFrom((startPage-1)*pageSize).setSize(pageSize); + + // 设置是否按查询匹配度排序 + searchRequestBuilder.setExplain(true); + + //打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询 + LOGGER.info("\n{}", searchRequestBuilder); + + // 执行搜索,返回搜索响应信息 + SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); + + long totalHits = searchResponse.getHits().totalHits; + long length = searchResponse.getHits().getHits().length; + + LOGGER.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); + + if (searchResponse.status().getStatus() == 200) { + // 解析对象 + List> sourceList = setSearchResponse(searchResponse, highlightField); + + return new EsPage(startPage, pageSize, (int) totalHits, sourceList); + } + + return null; + + } + + + /** + * 使用分词查询 + * + * @param index 索引名称 + * @param type 类型名称,可传入多个type逗号分隔 + * @param query 查询条件 + * @param size 文档大小限制 + * @param fields 需要显示的字段,逗号分隔(缺省为全部字段) + * @param sortField 排序字段 + * @param highlightField 高亮字段 + * @return + */ + public static List> searchListData( + String index, String type, QueryBuilder query, Integer size, + String fields, String sortField, String highlightField) { + + SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index); + if (!StringUtils.isEmpty(type)) { + searchRequestBuilder.setTypes(type.split(",")); + } + + if (!StringUtils.isEmpty(highlightField)) { + HighlightBuilder highlightBuilder = new HighlightBuilder(); + // 设置高亮字段 + highlightBuilder.field(highlightField); + searchRequestBuilder.highlighter(highlightBuilder); + } + + searchRequestBuilder.setQuery(query); + + if (!StringUtils.isEmpty(fields)) { + searchRequestBuilder.setFetchSource(fields.split(","), null); + } + searchRequestBuilder.setFetchSource(true); + + if (!StringUtils.isEmpty(sortField)) { + searchRequestBuilder.addSort(sortField, SortOrder.DESC); + } + + if (size != null && size > 0) { + searchRequestBuilder.setSize(size); + } + + //打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询 + LOGGER.info("\n{}", searchRequestBuilder); + + SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); + + long totalHits = searchResponse.getHits().totalHits; + long length = searchResponse.getHits().getHits().length; + + LOGGER.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); + + if (searchResponse.status().getStatus() == 200) { + // 解析对象 + return setSearchResponse(searchResponse, highlightField); + } + return null; + + } + + + /** + * 高亮结果集 特殊处理 + * + * @param searchResponse + * @param highlightField + */ + private static List> setSearchResponse(SearchResponse searchResponse, String highlightField) { + List> sourceList = new ArrayList>(); + StringBuffer stringBuffer = new StringBuffer(); + for (SearchHit searchHit : searchResponse.getHits().getHits()) { + searchHit.getSourceAsMap().put("id", searchHit.getId()); + + if (!StringUtils.isEmpty(highlightField)) { + + System.out.println("遍历 高亮结果集,覆盖 正常结果集" + searchHit.getSourceAsMap()); + Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments(); + + if (text != null) { + for (Text str : text) { + stringBuffer.append(str.string()); + } + //遍历 高亮结果集,覆盖 正常结果集 + searchHit.getSourceAsMap().put(highlightField, stringBuffer.toString()); + } + } + sourceList.add(searchHit.getSourceAsMap()); + } + return sourceList; + } + private static String getUUID32(){ String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase(); return uuid; diff --git a/src/main/java/com/gzzn/omms/adminapi/elasticsearch/EsPage.java b/src/main/java/com/gzzn/omms/adminapi/elasticsearch/EsPage.java index 2b151df..511bc3a 100644 --- a/src/main/java/com/gzzn/omms/adminapi/elasticsearch/EsPage.java +++ b/src/main/java/com/gzzn/omms/adminapi/elasticsearch/EsPage.java @@ -76,4 +76,60 @@ public class EsPage { } } } + + public int getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(int currentPage) { + this.currentPage = currentPage; + } + + public int getPageSize() { + return pageSize; + } + + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + + public int getRecordCount() { + return recordCount; + } + + public void setRecordCount(int recordCount) { + this.recordCount = recordCount; + } + + public List> getRecordList() { + return recordList; + } + + public void setRecordList(List> recordList) { + this.recordList = recordList; + } + + public int getPageCount() { + return pageCount; + } + + public void setPageCount(int pageCount) { + this.pageCount = pageCount; + } + + public int getBeginPageIndex() { + return beginPageIndex; + } + + public void setBeginPageIndex(int beginPageIndex) { + this.beginPageIndex = beginPageIndex; + } + + public int getEndPageIndex() { + return endPageIndex; + } + + public void setEndPageIndex(int endPageIndex) { + this.endPageIndex = endPageIndex; + } }