瀏覽代碼

调整统计逻辑为按照当前时间统计

Levi 3 年之前
父節點
當前提交
d02ad456da

+ 3 - 2
src/main/java/com/ebei/screen/common/util/EnergyUtils.java

@@ -2,6 +2,7 @@ package com.ebei.screen.common.util;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.util.NumberUtil;
 import cn.hutool.http.HttpRequest;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
@@ -230,8 +231,8 @@ public class EnergyUtils {
             return "100%";
         }
         NumberFormat format = NumberFormat.getPercentInstance();
-        Double result = one / two;
-        return format.format(result);
+        format.setMaximumFractionDigits(2);
+        return format.format(NumberUtil.sub(NumberUtil.div(one, two), 1d));
     }
 
     public static String divide(double a, double b, int scale) {

+ 22 - 0
src/main/java/com/ebei/screen/common/util/ParkUtils.java

@@ -300,6 +300,28 @@ public class ParkUtils {
         return maps;
     }
 
+    /**
+     * 计算某个时间点数量
+     *
+     * @param maps    list集合
+     * @param sortKey 排序的key
+     * @return
+     */
+    public static Double sortDateListCount(List<Map> maps, String sortKey) {
+        if (CollUtil.isEmpty(maps)) {
+            return 0D;
+        }
+        if (sortKey.equals(ParkType.EXCEPTION.getSort())) {
+            sortKey = ParkType.OUT.getSort();
+        }
+        if (StringUtils.isNotBlank(sortKey)) {
+            String finalSortKey = sortKey;
+            Long count = maps.stream().filter(m -> m.get(finalSortKey) == null ? false : LocalDateTime.now().minusDays(1).isAfter(LocalDateTimeUtil.parse(m.get(finalSortKey).toString(), DatePattern.NORM_DATETIME_PATTERN))).count();
+            return count.doubleValue();
+        }
+        return 0D;
+    }
+
     /**
      * 获取当前所有的停车场编号
      *

+ 40 - 7
src/main/java/com/ebei/screen/service/impl/ParkSystemServiceImpl.java

@@ -348,14 +348,11 @@ public class ParkSystemServiceImpl implements ParkSystemService {
         times.forEach(x -> result.add(Levi.by("time", x).set("in", 0).set("out", 0).set("exception", 0)));
         parkCodeList.forEach(code -> {
             String format = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.PURE_DATE_PATTERN);
-            String formatOld = LocalDateTimeUtil.format(LocalDateTimeUtil.offset(LocalDateTime.now(), -1, ChronoUnit.DAYS), DatePattern.PURE_DATE_PATTERN);
             JSONObject inObj = mongoTemplate.findById(code + "_" + format, JSONObject.class, ParkType.IN.getCode());
             JSONObject outObj = mongoTemplate.findById(code + "_" + format, JSONObject.class, ParkType.OUT.getCode());
             JSONObject exceptionObj = mongoTemplate.findById(code + "_" + format, JSONObject.class, ParkType.EXCEPTION.getCode());
             JSONObject exceptionData = exceptionObj != null ? exceptionObj.getJSONObject("data") : null;
-            JSONObject exceptionObjOld = mongoTemplate.findById(code + "_" + formatOld, JSONObject.class, ParkType.EXCEPTION.getCode());
             ParkExceptionType.codeList().forEach(x -> exceptionCountResult.set((exceptionObj == null ? 0 : (exceptionObj.getDouble(x + "Count") == null ? 0 : exceptionObj.getDouble(x + "Count").intValue())) + exceptionCountResult.get()));
-            ParkExceptionType.codeList().forEach(x -> exceptionCountOldResult.set((exceptionObjOld == null ? 0 : (exceptionObjOld.getDouble(x + "Count") == null ? 0 : exceptionObjOld.getDouble(x + "Count").intValue())) + exceptionCountOldResult.get()));
             System.out.println("当前车场:" + code + " 异常数量:" + exceptionCountResult);
             List<Map> exceptionResult = new ArrayList<>();
             if (exceptionData != null) {
@@ -369,26 +366,62 @@ public class ParkSystemServiceImpl implements ParkSystemService {
                 return;
             }
             JSONArray inObjList = inObj != null ? inObj.getJSONArray("data") : null;
-            JSONObject inObjOld = mongoTemplate.findById(code + "_" + formatOld, JSONObject.class, ParkType.IN.getCode());
             JSONArray outObjList = outObj != null ? outObj.getJSONArray("data") : null;
-            JSONObject outObjOld = mongoTemplate.findById(code + "_" + formatOld, JSONObject.class, ParkType.OUT.getCode());
             Double inCount = inObj == null ? 0 : inObj.getDouble("count");
-            Double inCountOld = inObjOld == null ? 0 : inObjOld.getDouble("count");
+            Double inCountOld = getTodayFlowAnalysisCount(Levi.by("parkCode", code).set("type", 1));
             Double outCount = outObj == null ? 0 : outObj.getDouble("count");
-            Double outCountOld = outObjOld == null ? 0 : outObjOld.getDouble("count");
+            Double outCountOld = getTodayFlowAnalysisCount(Levi.by("parkCode", code).set("type", 2));
+            Double exceptionCountOld = getTodayFlowAnalysisCount(Levi.by("parkCode", code).set("type", 3));
             inCountResult.set(inCount.intValue() + inCountResult.get());
             inCountOldResult.set(inCountOld.intValue() + inCountOldResult.get());
             outCountResult.set(outCount.intValue() + outCountResult.get());
             outCountOldResult.set(outCountOld.intValue() + outCountOldResult.get());
+            exceptionCountOldResult.set(exceptionCountOld.intValue() + exceptionCountOldResult.get());
             for (int i = 0; i < indexs.size(); i++) {
                 Levi levi = result.get(i);
                 levi.set("in", levi.getInt("in") + (inObjList == null ? 0 : inObjList.getJSONObject(indexs.get(i)).getJSONArray("data").size()));
                 levi.set("out", levi.getInt("out") + (outObjList == null ? 0 : outObjList.getJSONObject(indexs.get(i)).getJSONArray("data").size()));
             }
         });
+        log.info("昨日入场:{},昨日出场:{},昨日异常:{}", inCountOldResult.get().doubleValue(), outCountOldResult.get().doubleValue(), exceptionCountOldResult.get().doubleValue());
         return ResponseBuilder.ok(Levi.by("inCount", inCountResult.get()).set("outCount", outCountResult.get()).set("exceptionCount", exceptionCountResult.get()).set("inPercent", EnergyUtils.getPercent(inCountResult.get().doubleValue(), inCountOldResult.get().doubleValue())).set("outPercent", EnergyUtils.getPercent(outCountResult.get().doubleValue(), outCountOldResult.get().doubleValue())).set("exceptionPercent", EnergyUtils.getPercent(exceptionCountResult.get().doubleValue(), exceptionCountOldResult.get().doubleValue())).set("list", result));
     }
 
+    /**
+     * 获取任意一天当前时间点的数量
+     *
+     * @param params
+     * @return
+     */
+    private Double getTodayFlowAnalysisCount(Levi params) {
+        String parkCode = params.getStr("parkCode");
+        Integer type = params.getInt("type");
+        if (type != 1 && type != 2 && type != 3) {
+            return 0D;
+        }
+        List<String> parkCodeList = Arrays.asList(parkCode);
+        if (StringUtils.isBlank(parkCode)) {
+            parkCodeList = ParkUtils.getParkCodeList();
+        }
+        String format = LocalDateTimeUtil.format(LocalDateTimeUtil.offset(LocalDateTime.now(), -1, ChronoUnit.DAYS), DatePattern.PURE_DATE_PATTERN);
+        List<Map> results = new ArrayList<>();
+        parkCodeList.forEach(code -> {
+            JSONObject obj = mongoTemplate.findById(code + "_" + format, JSONObject.class, ParkType.getTypeByNo(type).getCode());
+            if (obj == null) {
+                return;
+            }
+            // 异常类型
+            if (CommonConstants.EXCEPTION_TYPE.equals(type)) {
+                JSONObject data = obj.getJSONObject("data");
+                data.keySet().forEach(x -> results.addAll((List<Map>) data.get(x)));
+            } else {
+                List<Map> maps = obj.getJSONArray("data").toJavaList(Map.class);
+                maps.forEach(x -> results.addAll((List<Map>) x.get("data")));
+            }
+        });
+        return ParkUtils.sortDateListCount(results, ParkType.getTypeByNo(type).getSort());
+    }
+
     /**
      * 根据停车场编号获取当前车场入场数
      *