From 22a89c9204b43822563f32b241f063046d284564 Mon Sep 17 00:00:00 2001 From: xiangwbs Date: Wed, 22 Nov 2017 22:27:28 +0800 Subject: [PATCH] merge --- .../java/com/xwbing/service/DemoService.java | 92 +++++++++++++++++++ .../main/java/com/xwbing/util/DateUtil2.java | 1 + 2 files changed, 93 insertions(+) create mode 100644 boot-service/src/main/java/com/xwbing/service/DemoService.java diff --git a/boot-service/src/main/java/com/xwbing/service/DemoService.java b/boot-service/src/main/java/com/xwbing/service/DemoService.java new file mode 100644 index 0000000..edfa153 --- /dev/null +++ b/boot-service/src/main/java/com/xwbing/service/DemoService.java @@ -0,0 +1,92 @@ +package com.xwbing.service; + +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.scheduling.annotation.Scheduled; + +import java.math.BigDecimal; +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class DemoService { + private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + private String chacheTime = ""; + + private static Double formate(Double v1) { + BigDecimal bg = new BigDecimal(v1); + return bg.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue(); + } + + private Double hoursBetween(String startDateTime, String endDateTime) { + LocalDateTime sDateTime = LocalDateTime.parse(startDateTime, dateTimeFormatter); + LocalDateTime eDateTime = LocalDateTime.parse(endDateTime, dateTimeFormatter); + Duration duration = Duration.between(sDateTime, eDateTime); + long m = duration.toMinutes(); + return formate((double) m / 60.0); + } + + private List getGpsByUserName(String username, String stime, String etime) { + return null; + } + + private Map hand(List gpsList) { + Map map = new HashMap<>(); + //存数据库 + JSONObject obj = gpsList.get(0); + // TODO: 2017/11/22 + //获取下一个时间 + String startTime = LocalDateTime.parse((String) obj.get("time"), dateTimeFormatter).plusHours(1).format(dateTimeFormatter); + //过滤 + gpsList = gpsList.stream().filter(jsonObject -> ((String) jsonObject.get("time")).compareTo(startTime) < 0 ? true : false).collect(Collectors.toList()); + map.put("startTime", startTime); + map.put("gpsList", gpsList); + return map; + } + + @Scheduled(cron = "0 0 */1 * * ?") // 每小时执行一次 + public void scheduler() { + LocalDateTime now = LocalDateTime.now(); + String endTime = now.format(dateTimeFormatter);//截止时间 + String startTime;//开始时间 + List gpsList = new ArrayList<>();//所有gps数据 + //获取巡更人员 + List list = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(list)) { + //判断开始时间 + if (StringUtils.isNotEmpty(chacheTime)) { + startTime = chacheTime; + } else { + //从数据库去查 + //数据库无数据 + startTime = LocalDate.now().toString() + " 00:00:00"; + } + while (hoursBetween(startTime, endTime) > 1) { + if (CollectionUtils.isNotEmpty(gpsList)) { + Map hand = hand(gpsList); + startTime = (String) hand.get("startTime"); + gpsList = (List) hand.get("hand"); + } else { + for (JSONObject jsonObject : list) { + gpsList.addAll(getGpsByUserName((String) jsonObject.get("tel"), startTime, endTime)); + } + if (CollectionUtils.isNotEmpty(gpsList)) { + //排序 + //获取处理结果 + Map hand = hand(gpsList); + startTime = (String) hand.get("startTime"); + gpsList = (List) hand.get("hand"); + } + } + } + chacheTime = startTime; + } + } +} diff --git a/boot-service/src/main/java/com/xwbing/util/DateUtil2.java b/boot-service/src/main/java/com/xwbing/util/DateUtil2.java index 0717111..a349b98 100644 --- a/boot-service/src/main/java/com/xwbing/util/DateUtil2.java +++ b/boot-service/src/main/java/com/xwbing/util/DateUtil2.java @@ -349,6 +349,7 @@ public class DateUtil2 { * @param endDateTime 2016-11-12 10:00 * @return */ + public static Double hoursBetween2(String startDateTime, String endDateTime) { LocalDateTime sDateTime = LocalDateTime.parse(startDateTime, getDateFormat(YYYY_MM_DD_HH_MM)); LocalDateTime eDateTime = LocalDateTime.parse(endDateTime, getDateFormat(YYYY_MM_DD_HH_MM));