完善历史数据查询接口

This commit is contained in:
fanghongchao
2018-12-14 11:06:54 +08:00
parent 66e5779615
commit 5498e937a4
6 changed files with 1321 additions and 169 deletions
@@ -1,26 +1,23 @@
package com.gzzn.omms.adminapi.controller.history;
import java.util.HashMap;
import java.util.Map;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.gzzn.omms.adminapi.dto.ResponseDto;
import com.gzzn.omms.adminapi.dto.history.HistoryFilghtConditionDto;
import com.gzzn.omms.adminapi.elasticsearch.ElasticsearchUtil;
import com.gzzn.omms.adminapi.elasticsearch.EsPage;
import com.gzzn.omms.adminapi.utils.DateUtils;
import com.gzzn.omms.adminapi.utils.GzznFileUtils;
/**
* 历史航班数据 controller
* @author fhc
@@ -31,50 +28,61 @@ import com.gzzn.omms.adminapi.utils.GzznFileUtils;
public class HistoryFlightDataController {
/**
* 操作日志索引
* 历史航班数据索引
*/
private final String INDEX_NAME = "airplant_hst";
private final String INDEX_NAME = "flight_hts";
/**
* 操作日志类型
* 历史航班数据类型
*/
private final String OPLOG_ES_TYPE = "_doc";
private final String ES_TYPE = "_doc";
@ApiOperation(value="获取历史航班数据",tags="历史航班数据")
@ApiOperation(value="获取历史航班数据(只提供计划时间的出发/到达时间段查询)",tags="历史航班数据")
@PostMapping(value="/hitFlightData/list")
public ResponseDto<EsPage> findAllByGroupCode(@RequestBody HistoryFilghtConditionDto conditions) {
//TODO 修改 ElasticsearchUtil 进行分页查询
public ResponseDto<Object> findAllByGroupCode(@RequestBody HistoryFilghtConditionDto conditions) {
Integer pageIndex = StringUtils.isEmpty(conditions.getPageIndex())?1:conditions.getPageIndex();
Integer pageSize = StringUtils.isEmpty(conditions.getPageSize())?10:conditions.getPageSize();
Long startTimeL = StringUtils.isEmpty(conditions.getStartTime())?DateUtils.getTodayStartTime():DateUtils.dateToStamp(conditions.getStartTime()+" 00:00:00","yyyy-MM-dd HH:mm:ss");
Long endTimeL = StringUtils.isEmpty(conditions.getEndTime())?System.currentTimeMillis():DateUtils.dateToStamp(conditions.getEndTime()+" 23:59:59","yyyy-MM-dd HH:mm:ss");
String startTime = StringUtils.isEmpty(conditions.getStartSODT())?null:DateUtils.swichTimeToEn_ddMMMyyHHmm(conditions.getStartSODT());
String endTime = StringUtils.isEmpty(conditions.getEndSODT())?null:DateUtils.swichTimeToEn_ddMMMyyHHmm(conditions.getEndSODT());
String MVIN = conditions.getMVIN();//到达/离港
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.rangeQuery("log_datetime")
.gt(startTimeL).lt(endTimeL)
.includeLower(true).includeUpper(true));
// if(!StringUtils.isEmpty(keyword)){ //使用模糊搜索 不使用分词
// boolQuery.must(QueryBuilders.boolQuery()
// .should(QueryBuilders.wildcardQuery("summary", "*"+keyword+"*"))
// .should(QueryBuilders.wildcardQuery("real_name", "*"+keyword+"*"))
// .should(QueryBuilders.wildcardQuery("log_content", "*"+keyword+"*")));
// }
EsPage page = ElasticsearchUtil.searchDataPage(INDEX_NAME, OPLOG_ES_TYPE, pageIndex, pageSize, boolQuery, null, null, null);
return ResponseDto.success(page);
//获取前一天的历史数据
String hitStartTime = DateUtils.getNewDateOfDayMixOrMaxTime(new Date(), "Day", -1, "MIN");
String hitEndTime = DateUtils.getNewDateOfDayMixOrMaxTime(new Date(), "Day", -1, "MAX");
boolQuery.must(QueryBuilders.rangeQuery("SODT")
.gte(hitStartTime).lte(hitEndTime));
if(!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
boolQuery.must(QueryBuilders.rangeQuery("SODT")
.gte(startTime).lte(endTime));
}
if(!StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){
boolQuery.must(QueryBuilders.rangeQuery("SODT")
.gte(startTime));
}
if(StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
boolQuery.must(QueryBuilders.rangeQuery("SODT")
.lte(startTime));
}
if(!StringUtils.isEmpty(MVIN)){
boolQuery.must(QueryBuilders.termQuery("MVIN", MVIN));
}
String sortField = "SODT";//计划时间 默认倒序
SortOrder sortOrder = SortOrder.ASC;
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(INDEX_NAME, ES_TYPE, boolQuery, null, null, sortField,sortOrder, null);
return ResponseDto.success(list);
}
@RequestMapping(value="/findTest/{log_type}",method = RequestMethod.GET)
public ResponseDto<EsPage> findTest(@PathVariable String log_type){
//获取查询条件script
String script = GzznFileUtils.getFileContent("/es/findTest.query");
//封装参数 分页等参数全都放入此处
Map<String, Object> params = new HashMap<String, Object>();
params.put("log_type", log_type);
params.put("from", 1);
params.put("size", 10);
//执行查询
return ResponseDto.success(ElasticsearchUtil.searchDataPageByScript(INDEX_NAME, params, script,1,10));
}
// @RequestMapping(value="/findTest/{log_type}",method = RequestMethod.GET)
// public ResponseDto<EsPage> findTest(@PathVariable String log_type){
// //获取查询条件script
// String script = GzznFileUtils.getFileContent("/es/findTest.query");
// //封装参数 分页等参数全都放入此处
// Map<String, Object> params = new HashMap<String, Object>();
// params.put("log_type", log_type);
// params.put("from", 1);
// params.put("size", 10);
// //执行查询
// return ResponseDto.success(ElasticsearchUtil.searchDataPageByScript(INDEX_NAME, params, script,1,10));
// }
}
@@ -12,135 +12,30 @@ import io.swagger.annotations.ApiModelProperty;
@ApiModel(value="历史航班数据条件Dto",description="查询历史航班数据的条件DTO")
public class HistoryFilghtConditionDto {
@ApiModelProperty(value="当前页,默认从1开始",example="1")
private Integer pageIndex;
@ApiModelProperty(value="每页大小,默认10",example="10")
private Integer pageSize;
@ApiModelProperty(value="航空公司_id",example="147576845")
private String airlineId;
@ApiModelProperty(value="航空公司_名",example="长龙航空有限公司")
private String airlineName;
@ApiModelProperty(value="航线类别",example="航线类别类别,I-国际、D-国内、M-混合")
private String routeType;
@ApiModelProperty(value="航班任务",example="航班任务:S=正班、N=包机、E=急救、B=专机、G=通用、J=加班、M=军用、Q=补班、X=其他、A=计划外(原参考分类:正班、加班、补班、中转、计划外、计划外中转、货运、训练...)")
private String flightTask;
@ApiModelProperty(value="出发/到达",example="到达/出发(进港/出港)标志,A-到达、D-出发")
private String arriOrDept;
@ApiModelProperty(value="航班代理",example="航班代理")
private String flightAgentId;
@ApiModelProperty(value="开始时间",example="开始时间(yyyy-mm-dd)")
private String startTime;
@ApiModelProperty(value="结束时间",example="结束时间(yyyy-mm-dd)")
private String endTime;
@ApiModelProperty(value="航班异常状态",example="是否故障(0-否 1-是)")
private String isAbnormal;
private String MVIN;
@ApiModelProperty(value="是否查询航班关联信息",example="是否查询航班关联信息,Y-是、N-否")
private String ifShowtRelation;
@ApiModelProperty(value="是否查询详细信息",example="是否查询详细信息,Y-是、N-否")
private String ifShowtDetail;
@ApiModelProperty(value="是否查询保障信息",example="是否查询保障信息,Y-是、N-否")
private String ifShowtEnsure ;
@ApiModelProperty(value="是否查询航班VIP信息",example="是否查询航班VIP信息,Y-是、N-否")
private String ifShowtVIP;
@ApiModelProperty(value="是否查询航班特服信息",example="是否查询航班特服信息,Y-是、N-否")
private String ifShowtSpecialService;
public Integer getPageIndex() {
return pageIndex;
@ApiModelProperty(value="开始时间",example="开始时间(yyyy-mm-dd HH:mm)")
private String startSODT;
@ApiModelProperty(value="结束时间",example="结束时间(yyyy-mm-dd HH:mm)")
private String endSODT;
public String getMVIN() {
return MVIN;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
public void setMVIN(String mVIN) {
MVIN = mVIN;
}
public Integer getPageSize() {
return pageSize;
public String getStartSODT() {
return startSODT;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
public void setStartSODT(String startSODT) {
this.startSODT = startSODT;
}
public String getAirlineId() {
return airlineId;
public String getEndSODT() {
return endSODT;
}
public void setAirlineId(String airlineId) {
this.airlineId = airlineId;
}
public String getAirlineName() {
return airlineName;
}
public void setAirlineName(String airlineName) {
this.airlineName = airlineName;
}
public String getRouteType() {
return routeType;
}
public void setRouteType(String routeType) {
this.routeType = routeType;
}
public String getFlightTask() {
return flightTask;
}
public void setFlightTask(String flightTask) {
this.flightTask = flightTask;
}
public String getArriOrDept() {
return arriOrDept;
}
public void setArriOrDept(String arriOrDept) {
this.arriOrDept = arriOrDept;
}
public String getFlightAgentId() {
return flightAgentId;
}
public void setFlightAgentId(String flightAgentId) {
this.flightAgentId = flightAgentId;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getIsAbnormal() {
return isAbnormal;
}
public void setIsAbnormal(String isAbnormal) {
this.isAbnormal = isAbnormal;
}
public String getIfShowtRelation() {
return ifShowtRelation;
}
public void setIfShowtRelation(String ifShowtRelation) {
this.ifShowtRelation = ifShowtRelation;
}
public String getIfShowtDetail() {
return ifShowtDetail;
}
public void setIfShowtDetail(String ifShowtDetail) {
this.ifShowtDetail = ifShowtDetail;
}
public String getIfShowtEnsure() {
return ifShowtEnsure;
}
public void setIfShowtEnsure(String ifShowtEnsure) {
this.ifShowtEnsure = ifShowtEnsure;
}
public String getIfShowtVIP() {
return ifShowtVIP;
}
public void setIfShowtVIP(String ifShowtVIP) {
this.ifShowtVIP = ifShowtVIP;
}
public String getIfShowtSpecialService() {
return ifShowtSpecialService;
}
public void setIfShowtSpecialService(String ifShowtSpecialService) {
this.ifShowtSpecialService = ifShowtSpecialService;
public void setEndSODT(String endSODT) {
this.endSODT = endSODT;
}
}
@@ -280,12 +280,13 @@ public class ElasticsearchUtil {
* @param size 文档大小限制
* @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
* @param sortField 排序字段
* @param sortOrder
* @param highlightField 高亮字段
* @return
*/
public static List<Map<String, Object>> searchListData(
String index, String type, QueryBuilder query, Integer size,
String fields, String sortField, String highlightField) {
String fields, String sortField, SortOrder sortOrder, String highlightField) {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
if (!StringUtils.isEmpty(type)) {
@@ -307,7 +308,10 @@ public class ElasticsearchUtil {
searchRequestBuilder.setFetchSource(true);
if (!StringUtils.isEmpty(sortField)) {
searchRequestBuilder.addSort(sortField, SortOrder.DESC);
if(null==sortOrder){
sortOrder = SortOrder.DESC;
}
searchRequestBuilder.addSort(sortField, sortOrder);
}
if (size != null && size > 0) {
@@ -2,7 +2,9 @@ package com.gzzn.omms.adminapi.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.springframework.util.StringUtils;
@@ -34,6 +36,30 @@ public class DateUtils {
return ts;
}
/**
* 将时间转换为date
* @param s
* @param format 格式默认 yyyy-MM-dd HH:mm:ss
* @return
* @throws ParseException
*/
public static Date StringToDate(String s,String format){
format = StringUtils.isEmpty(format)?"yyyy-MM-dd HH:mm:ss":format;
if(s == null)
{
return null;
}
Date date = null;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
date = simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 将时间戳转换为时间字符串
@@ -61,10 +87,21 @@ public class DateUtils {
* @return
*/
public static Long getTodayStartTime(){
String time = DateUtils.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String time = DateUtils.dateToString(new Date(), "yyyy-MM-dd")+" 00:00:00";
return DateUtils.dateToStamp(time,"yyyy-MM-dd HH:mm:ss");
}
/**
* 将时间转换为 ddMMMyyHHmm Locale.ENGLISH 格式
* @param time - yyyy-MM-dd HH:mm
* @return
*/
public static String swichTimeToEn_ddMMMyyHHmm(String time){
Date date = DateUtils.StringToDate(time, "yyyy-MM-dd HH:mm");
String swicthTime = DateUtils.dateToString(date, "ddMMMyyHHmm",Locale.ENGLISH);
return swicthTime;
}
/**
* 将时间为时间字符串
* @param date
@@ -78,5 +115,71 @@ public class DateUtils {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
/**
* 将时间为时间字符串
* @param date
* @param format
* @param 时区
* @return
*/
public static String dateToString(Date date,String format,Locale locale){
if(null==date){
return "";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format,locale);
return simpleDateFormat.format(date);
}
/**
* 取得当前日期的后num的日期
* @param date
* @param type Year/Month/Day
* @param num
* @return
*/
public static Date getNewDate(Date date, String type, int num) {
Calendar cal = (Calendar) Calendar.getInstance();
cal.setTime(date);
if (type.equalsIgnoreCase("Year")) {
cal.add(Calendar.YEAR, num);
} else if (type.equalsIgnoreCase("Month")) {
cal.add(Calendar.MONTH, num);
} else {
cal.add(Calendar.DATE, num);
}
return cal.getTime();
}
/**
* 取得当前日期的后num的日期 所在天的最大或最小时间
* @param date
* @param type Year/Month/Day
* @param num
* @param limit MAX/MIN
* @return format ddMMMyyHHmm
*/
public static String getNewDateOfDayMixOrMaxTime(Date date, String type, int num,String limit) {
Calendar cal = (Calendar) Calendar.getInstance();
cal.setTime(date);
if (type.equalsIgnoreCase("Year")) {
cal.add(Calendar.YEAR, num);
} else if (type.equalsIgnoreCase("Month")) {
cal.add(Calendar.MONTH, num);
} else {
cal.add(Calendar.DATE, num);
}
Date timeOfDay = cal.getTime();
String day = DateUtils.dateToString(timeOfDay, "ddMMMyy",Locale.ENGLISH);
switch (limit) {
case "MAX":
day += "2359";
break;
default:
day += "0000";
break;
}
return day;
}
}