用多了MP,mybatis语句的这种sql语句不太熟练,记录一下
查询某一条船的历史轨迹
地址:
GET /xxxx/xxxx/xxxx
参数:
| 参数名 | 类型 | 说明 | 范围 | 是否必需 |
|---|---|---|---|---|
| shipName | 字符串 | 船名 | “001”-”100” | 是 |
| date | 时间戳 | 日期 | 最近一周的某一天,默认当日的前一天 | 否 |
| interval | 字符串 | 时间间隔 | “1”-”60”,默认间隔5分钟,最短间隔1分钟,最长间隔60分钟 | 否 |
@GetMapping(value = "/xxx/xxx/xxx")
public Map getHistoryLocationAndStatus(
@RequestParam(value = "shipName",required = true) String shipName,
@RequestParam(value = "date",required = false) Date date,
@RequestParam(value = "interval",required = false) String interval
)
{
List list = shipManageService.getHistoryLocationAndStatus(shipName, date ,interval);
Map resultMap = new HashMap<>();
resultMap.put("information", list);
resultMap.put("shipName", shipName);
return resultMap;
}
2.service
List getHistoryLocationAndStatus(String shipName, Date date,String interval);
3.impl
@Override
public List getHistoryLocationAndStatus(String shipName, Date date,String interval) {
//判断传过来的时间是否为空,如果为空直接默认当日前一天
if (date == null) {
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,-1);
// String yesterdayDate=dateFormat.format(calendar.getTime());
// System.out.println("yestodayDate:"+yesterdayDate);
date = calendar.getTime();
System.out.println(date);
}else{
//判断时间是否为一周之内的时间,如果不是提示出错
Date nowDate = DateUtils.getNowDate();
long datePoor = DateUtils.getDatePoorDay(date, nowDate);
if(datePoor>6){
throw new RuntimeException("您查询的船只时间已过期,请查询近7日以内的船只历史记录");
}
}
if (StringUtils.isBlank(interval)) {
interval = "5";
}
Date startTime = DateUtils.weeHours(date, 0);
Date endTime = DateUtils.weeHours(date, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sTime = sdf.format(startTime);
String eTime = sdf.format(endTime);
return shipMapper.getHistoryLocationAndStatus(shipName,sTime,eTime,Integer.parseInt(interval));
}
4.mapper
List getHistoryLocationAndStatus(String shipName,String startTime,String endTime,int interval);
5.vo
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ShipStatusVo {
private String shipName;
private Date time;
private String longtitude;
private String latitude;
private String speed;
private String warningInformation;
}
6.xml



