xufenglei
2018-06-29 e45bad6cf75f6da4e8002020755cc07a55892e14
src/main/java/com/moral/service/impl/HistoryServiceImpl.java
@@ -1,7 +1,17 @@
package com.moral.service.impl;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
@@ -93,31 +103,58 @@
      ValidateUtil.notNull(parameters.get("accountId"), "param.is.null");
      String regionCode = parameters.get("regionCode").toString();
      String regionType = "village_code";
      String regionType = "village";
      String nextLevel = "";
      if (regionCode.length() == 6) {
         if (regionCode.endsWith("0000")) {
            regionType = "province_code";
            regionType = "province";
            nextLevel = "city";
         } else if (regionCode.endsWith("00")) {
            regionType = "city_code";
            regionType = "city";
            nextLevel = "area";
         } else {
            regionType = "area_code";
            regionType = "area";
            nextLevel = "town";
         }
      } else {
         if (regionCode.endsWith("000")) {
            regionType = "town_code";
            regionType = "town";
            nextLevel = "village";
         }
      }
      parameters.put("regionType", regionType);
      parameters.put("nextLevel", nextLevel);
      parameters.put("start", LocalDateTime.now().minusMinutes(5));
      LocalDate localDate = LocalDate.now();
      Object timeType = parameters.get("timeType");
      String table = "history";
      String column = "value";
      if ("month".equals(timeType)) {
         if (1 != localDate.getDayOfMonth()) {
            table = "history_minutely";
            column = "json";
         }
         parameters.put("start", localDate.with(TemporalAdjusters.firstDayOfMonth()));
      } else if ("day".equals(timeType)) {
         parameters.put("start", localDate);
      } else if ("hour".equals(timeType)) {
         parameters.put("start", LocalDateTime.now().truncatedTo(ChronoUnit.HOURS));
      } else {
         parameters.put("start", LocalDateTime.now().minusMinutes(5));
      }
      parameters.put("table", table);
      parameters.put("column", column);
      String dimension = "";
      if ("discharge".equals(parameters.get("dimension"))) {
         dimension = "discharge";
         parameters.put("dimension","monitorPoint");
      }
      
      accountService.setOrgIdsByAccount(parameters);
      List<Map<String, Object>> result = historyMapper.getRegionRankingData(parameters);
      if ("profession".equals(parameters.get("dimension"))) {
         List<Profession> professions = professionMapper.selectAll();
         for (Map<String, Object> map : result) {
@@ -146,6 +183,7 @@
               if (map.get("code").equals(region.get("code"))) {
                  map.put("name", region.get("name"));
                  regions.remove(region);
                  map.remove("code");
                  break;
               }
            }
@@ -157,6 +195,26 @@
            result.add(map);
         }
      }
      if ("discharge".equals(dimension)) {
         parameters.put("year", LocalDate.now().getYear());
         List<Map<String, Object>> discharges = historyMapper.getDischargeByRegion(parameters);
         for (Map<String, Object> map : result) {
            for (Map<String, Object> discharge : discharges) {
               if (map.get("name").equals(discharge.get("name"))) {
                  map.put("value", discharge.get("value"));
                  discharges.remove(discharge);
                  break;
               }
            }
         }
         Collections.sort(result, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
               return Double.compare(((Double) o2.get("avg"))- Double.valueOf((String) o2.get("value")),
                     ((Double) o1.get("avg")) - Double.valueOf((String) o1.get("value")));
            }
         });
      }
      return result;
   }
}