From cfe961da5a7497e905ad0291b1793b685f30688c Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Fri, 14 Jan 2022 16:29:51 +0800
Subject: [PATCH] update

---
 screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java |  635 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 621 insertions(+), 14 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
index 357a1df..746fcbb 100644
--- a/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
+++ b/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
@@ -3,16 +3,21 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.moral.api.config.properties.SpecialCitiesProperties;
 import com.moral.api.entity.CityAqi;
 import com.moral.api.entity.CityAqiDaily;
+import com.moral.api.entity.CityAqiMonthly;
+import com.moral.api.entity.CityAqiYearly;
 import com.moral.api.entity.Forecast;
 import com.moral.api.entity.Organization;
 import com.moral.api.entity.SysArea;
 import com.moral.api.mapper.CityAqiMapper;
 import com.moral.api.mapper.ForecastMapper;
 import com.moral.api.service.CityAqiDailyService;
+import com.moral.api.service.CityAqiMonthlyService;
 import com.moral.api.service.CityAqiService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.moral.api.service.CityAqiYearlyService;
 import com.moral.api.service.OrganizationService;
 import com.moral.api.service.SysAreaService;
 import com.moral.constant.Constants;
@@ -20,6 +25,7 @@
 import com.moral.pojo.AQI;
 import com.moral.util.AQIUtils;
 import com.moral.util.AmendUtils;
+import com.moral.util.ComprehensiveIndexUtils;
 import com.moral.util.DateUtils;
 
 import com.moral.util.MathUtils;
@@ -29,7 +35,9 @@
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
+import java.text.DecimalFormat;
 import java.util.*;
+import java.util.stream.Collectors;
 import java.util.stream.DoubleStream;
 
 /**
@@ -53,13 +61,19 @@
     private RedisTemplate redisTemplate;
 
     @Autowired
-    private OrganizationService organizationService;
-
-    @Autowired
     private SysAreaService sysAreaService;
 
     @Autowired
     private CityAqiDailyService cityAqiDailyService;
+
+    @Autowired
+    private CityAqiMonthlyService cityAqiMonthlyService;
+
+    @Autowired
+    private CityAqiYearlyService cityAqiYearlyService;
+
+    @Autowired
+    private SpecialCitiesProperties specialCitiesProperties;
 
     @Override
     public List<Map<String, Object>> measuredCompareForecastOfO3(Map<String, Object> params) {
@@ -122,6 +136,21 @@
         Map<String, Object> value = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.CITY_AQI, String.valueOf(regionCode));
         if (value == null)
             value = queryCityAqiByRegionCodeFromDB(regionCode);
+        //������������������������������������������������������������
+        if (value == null) {
+            String regionCodeStr = String.valueOf(regionCode);
+            String end = regionCodeStr.substring(regionCodeStr.length() - 2, regionCodeStr.length());
+            if (!end.equals(00)) {
+                regionCodeStr = regionCodeStr.substring(0, regionCodeStr.length() - 2);
+                regionCodeStr += "00";
+                regionCode = Integer.parseInt(regionCodeStr);
+                value = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.CITY_AQI, String.valueOf(regionCode));
+                if (value == null)
+                    value = queryCityAqiByRegionCodeFromDB(regionCode);
+            } else {
+                return null;
+            }
+        }
         //������AQI������������������
         if (value == null || value.get("AQI") == null)
             return null;
@@ -139,6 +168,8 @@
         lastDataWrapper.orderByDesc("time");
         lastDataWrapper.last(true, "limit 1");
         CityAqi cityAqi = cityAqiMapper.selectOne(lastDataWrapper);
+        if (cityAqi == null)
+            return null;
         //���������24������������������
         Date endDate = cityAqi.getTime();
         Date startDate = DateUtils.addHours(endDate, -23);
@@ -163,10 +194,10 @@
             //������������������������
             cityAqis.sort(Comparator.comparing(CityAqi::getTime));
         }
-        //������������������,map���key���HH:mm������������������value���aqi���������
+        //������������������,map���key���yyyy-MM-dd HH:mm������������������value���aqi���������
         Map<String, Object> result = new LinkedHashMap<>();
         for (CityAqi aqi : cityAqis) {
-            String key = DateUtils.dateToDateString(aqi.getTime(), "HH:mm");
+            String key = DateUtils.dateToDateString(aqi.getTime(), "yyyy-MM-dd HH:mm");
             String allDataJson = aqi.getValue();
             if (allDataJson == null) {
                 result.put(key, "");
@@ -198,12 +229,591 @@
         //������������aqi������������������
         Map<String, Object> result = new HashMap<>();
         AQI aqi = AQIUtils.hourlyAQI(sixParamAvg);
-        result.put("aqi",aqi.getAQIValue());
-        result.put("pollutant",aqi.getPrimaryPollutantNames());
+        result.put("aqi", aqi.getAQIValue());
+        result.put("pollutant", aqi.getPrimaryPollutantNames());
         //���������������������
         CityAqi lastCityAqi = cityAqis.get(cityAqis.size() - 1);
         String time = DateUtils.dateToDateString(lastCityAqi.getTime(), "HH:mm");
         result.put("time", time);
+        return result;
+    }
+
+    @Override
+    public List<Map<String, Object>> rankingDetails(Map<String, Object> params) {
+        List<Map<String, Object>> result = new ArrayList<>();
+        int regionCode = Integer.parseInt(params.get("regionCode").toString());
+        String type = params.get("type").toString();
+        String time = null;
+        if (!ObjectUtils.isEmpty(params.get("time"))) {
+            time = params.get("time").toString();
+        }
+        String start = null;
+        String end = null;
+        if (!ObjectUtils.isEmpty(params.get("start")) || !ObjectUtils.isEmpty(params.get("end"))) {
+            start = params.get("start").toString();
+            end = params.get("end").toString();
+        }
+        String cityType = params.get("cityType").toString();
+
+        String s = String.valueOf(regionCode);
+        //���������������,���code
+        Integer curProvinceCode = Integer.parseInt(s.substring(0, 2) + "0000");
+        Integer curCityCode = Integer.parseInt(s.substring(0, 4) + "00");
+
+        QueryWrapper<SysArea> areaWrapper = new QueryWrapper<>();
+
+        List<SysArea> sysAreas;
+        if ("28".equals(cityType)) {
+            //������2+26������
+            sysAreas = specialCitiesProperties.getTwentyEightCities();
+        } else {
+            if ("province".equals(cityType)) {
+                //���������������������
+                areaWrapper.select("area_code", "area_name").eq("parent_code", curProvinceCode);
+            } else {
+                //������������������������
+                areaWrapper.select("area_code", "area_name").eq("parent_code", curCityCode);
+            }
+            sysAreas = sysAreaService.list(areaWrapper);
+        }
+
+        switch (type) {
+            case "today":
+                result = accumulatedTodayRank(sysAreas);
+                break;
+            case "hour":
+                time = new StringBuilder(time).replace(10, 11, " ").toString() + ":00:00";
+                result = hourRank(sysAreas, time);
+                break;
+            case "day":
+                time = time + " 00:00:00";
+                result = dayRank(sysAreas, time);
+                break;
+            case "month":
+                time = time + "-01 00:00:00";
+                result = monthRank(sysAreas, time);
+                break;
+            case "year":
+                time = time + "-01-01 00:00:00";
+                result = yearRank(sysAreas, time);
+                break;
+            case "custom":
+                start = start + " 00:00:00";
+                end = end + " :00:00";
+                result = customRank(sysAreas, start, end);
+                break;
+            default:
+                break;
+        }
+        return result;
+    }
+
+    /**
+     * @param sysAreas ������������������������
+     * @return ���������������������������
+     */
+    private List<Map<String, Object>> accumulatedTodayRank(List<SysArea> sysAreas) {
+        List<Integer> regionCodes = sysAreas.stream()
+                .map(SysArea::getAreaCode)
+                .collect(Collectors.toList());
+
+        List<Map<String, Object>> result = new ArrayList<>();
+        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2", "CO", "O3");
+        Date now = new Date();
+        String today = DateUtils.dateToDateString(now, DateUtils.yyyy_MM_dd_EN);
+        QueryWrapper<CityAqi> wrapper = new QueryWrapper<>();
+        wrapper.select("city_code", "value")
+                .ge("time", today)
+                .in("city_code", regionCodes);
+        List<Map<String, Object>> cumulativeData = cityAqiMapper.selectMaps(wrapper);
+        //���city_code������
+        Map<Integer, List<Map<String, Object>>> data = cumulativeData.parallelStream().collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString())));
+        data.forEach((cityCode, value) -> {
+            List<Double> doubles = new ArrayList<>();
+            for (Map<String, Object> objectMap : value) {
+                Object o = JSONObject.parseObject((String) objectMap.get("value"), Map.class).get("O3_8H");
+                if (!ObjectUtils.isEmpty(o)) {
+                    double v = Double.parseDouble(o.toString());
+                    doubles.add(v);
+                }
+            }
+
+            Map<String, Object> dataMap = new HashMap<>();
+            sensors.forEach(sensor -> {
+                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
+                    Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class);
+                    Object o = sensorValue.get(sensor);
+                    if (ObjectUtils.isEmpty(o)) {
+                        return null;
+                    }
+                    double aDouble = Double.parseDouble(o.toString());
+                    return DoubleStream.of(aDouble);
+                }).average();
+
+                if (optionalDouble.isPresent()) {
+                    //���������������������
+                    double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0);
+                    if ("CO".equals(sensor)) {
+                        sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 1);
+                    }
+                    dataMap.put(sensor, sciCal);
+                }
+            });
+
+            //������������O3_8H������,������������O3������8H���������
+            if (!ObjectUtils.isEmpty(doubles)) {
+                dataMap.put("O3_8H", Collections.max(doubles));
+            }
+
+            //������������aqi,���������������������
+            Map<String, Object> sixParamMap = new HashMap<>();
+            sixParamMap.put(Constants.SENSOR_CODE_PM25, dataMap.get("PM2_5"));
+            sixParamMap.put(Constants.SENSOR_CODE_PM10, dataMap.get("PM10"));
+            sixParamMap.put(Constants.SENSOR_CODE_SO2, dataMap.get("SO2"));
+            sixParamMap.put(Constants.SENSOR_CODE_NO2, dataMap.get("NO2"));
+            sixParamMap.put(Constants.SENSOR_CODE_CO, dataMap.get("CO"));
+            sixParamMap.put(Constants.SENSOR_CODE_O3, dataMap.get("O3"));
+            AQI aqi = AQIUtils.dailyAQI(sixParamMap);
+            dataMap.put("AQI", aqi.getAQIValue());
+            List<String> primaryPollutantNames = aqi.getPrimaryPollutantNames();
+            String primaryPollutant = "";
+            if (!ObjectUtils.isEmpty(primaryPollutantNames)) {
+                primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(","));
+            }
+            dataMap.put("primaryPollutant", primaryPollutant);
+
+            //������������������������������,O3������������O3_8H������
+            Map<String, Object> compositeIndexMap = new HashMap<>(dataMap);
+            compositeIndexMap.put("O3", compositeIndexMap.get("O3_8H"));
+            Double compositeIndex = ComprehensiveIndexUtils.dailyData(compositeIndexMap);
+            dataMap.put("compositeIndex", compositeIndex);
+
+            //���������
+            for (SysArea sysArea : sysAreas) {
+                if (cityCode.equals(sysArea.getAreaCode())) {
+                    dataMap.put("cityName", sysArea.getAreaName());
+                    break;
+                }
+            }
+            result.add(dataMap);
+        });
+        return result;
+    }
+
+    /**
+     * @param sysAreas ������������������������
+     * @param time     ��������������������������� 2021-11-04 13:00:00
+     * @return ���������������������
+     */
+    private List<Map<String, Object>> hourRank(List<SysArea> sysAreas, String time) {
+        List<Integer> regionCodes = sysAreas.stream()
+                .map(SysArea::getAreaCode)
+                .collect(Collectors.toList());
+
+        List<Map<String, Object>> result = new ArrayList<>();
+        QueryWrapper<CityAqi> wrapper = new QueryWrapper<>();
+        wrapper.select("value")
+                .eq("time", time)
+                .in("city_code", regionCodes);
+        List<Map<String, Object>> hourData = cityAqiMapper.selectMaps(wrapper);
+        for (Map<String, Object> hourDatum : hourData) {
+            Map<String, Object> value = JSONObject.parseObject((String) hourDatum.get("value"), Map.class);
+            List<String> primaryPollutantNames = (List<String>) value.get("primaryPollutant");
+            String primaryPollutant = "";
+            if (!ObjectUtils.isEmpty(primaryPollutantNames)) {
+                primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(","));
+            }
+            value.put("primaryPollutant", primaryPollutant);
+            value.remove("pubtime");
+            value.remove("rank");
+            result.add(value);
+        }
+        return result;
+    }
+
+    /**
+     * @param sysAreas ������������������������
+     * @param time     ��������������������������� 2021-11-04 00:00:00
+     * @return ������������������
+     */
+    private List<Map<String, Object>> dayRank(List<SysArea> sysAreas, String time) {
+        List<Integer> regionCodes = sysAreas.stream()
+                .map(SysArea::getAreaCode)
+                .collect(Collectors.toList());
+
+        List<Map<String, Object>> result = new ArrayList<>();
+        QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>();
+        wrapper.select("city_code", "value")
+                .eq("time", time)
+                .in("city_code", regionCodes);
+        List<Map<String, Object>> dayData = cityAqiDailyService.listMaps(wrapper);
+        for (Map<String, Object> dayDatum : dayData) {
+            Map<String, Object> value = JSONObject.parseObject((String) dayDatum.get("value"), Map.class);
+            List<String> primaryPollutantNames = (List<String>) value.get("primaryPollutant");
+            String primaryPollutant = "";
+            if (!ObjectUtils.isEmpty(primaryPollutantNames)) {
+                primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(","));
+            }
+            value.put("primaryPollutant", primaryPollutant);
+
+
+            //���������
+            for (SysArea sysArea : sysAreas) {
+                if (dayDatum.get("city_code").equals(sysArea.getAreaCode())) {
+                    value.put("cityName", sysArea.getAreaName());
+                    break;
+                }
+            }
+            result.add(value);
+        }
+        return result;
+    }
+
+    /**
+     * @param sysAreas ������������������������
+     * @param time     ��������������������������� 2021-11-01 00:00:00 ������1���
+     * @return ������������������
+     */
+    private List<Map<String, Object>> monthRank(List<SysArea> sysAreas, String time) {
+        List<Integer> regionCodes = sysAreas.stream()
+                .map(SysArea::getAreaCode)
+                .collect(Collectors.toList());
+
+        //���������������������������
+        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2");
+        List<Map<String, Object>> result = new ArrayList<>();
+        //������������������,������������������������������city_aqi_monthly������
+        if (!time.substring(0, 7).equals(DateUtils.dateToDateString(new Date(), DateUtils.yyyy_MM_EN))) {
+            QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>();
+            for (Integer regionCode : regionCodes) {
+                cityAqiMonthlyQueryWrapper.clear();
+                cityAqiMonthlyQueryWrapper.select("value")
+                        .eq("city_code", regionCode)
+                        .eq("time", time);
+                CityAqiMonthly cityAqiMonthly = cityAqiMonthlyService.getOne(cityAqiMonthlyQueryWrapper);
+                if (cityAqiMonthly == null) {
+                    continue;
+                }
+                String value = cityAqiMonthly.getValue();
+                Map<String, Object> resultMap = JSONObject.parseObject(value, Map.class);
+
+
+                //���������
+                for (SysArea sysArea : sysAreas) {
+                    if (regionCode.equals(sysArea.getAreaCode())) {
+                        resultMap.put("cityName", sysArea.getAreaName());
+                        break;
+                    }
+                }
+            }
+            return result;
+        }
+
+        QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>();
+        cityAqiDailyQueryWrapper.select("city_code", "value")
+                .ge("time", time)
+                .in("city_code", regionCodes);
+        List<Map<String, Object>> thisMonthData = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper);
+        //���city_code������
+        Map<Integer, List<Map<String, Object>>> thisMonthMap = thisMonthData.parallelStream()
+                .collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString())));
+
+        thisMonthMap.forEach((cityCode, value) -> {
+            Map<String, Object> resultMap = new HashMap<>();
+
+            Map<String, Object> params = new HashMap<>();
+            List<Map<String, Object>> temp = new ArrayList<>();
+            for (Map<String, Object> map : value) {
+                Map<String, Object> sensorsValue = JSONObject.parseObject(map.get("value").toString(), Map.class);
+                Map<String, Object> tempMap = new HashMap<>();
+                tempMap.put(Constants.SENSOR_CODE_CO, sensorsValue.get("CO"));
+                tempMap.put(Constants.SENSOR_CODE_O3, sensorsValue.get("O3"));
+                Map<String, Object> hashMap = new HashMap<>();
+                hashMap.put("value", JSONObject.toJSONString(tempMap));
+                temp.add(hashMap);
+            }
+            params.put("data", temp);
+            //1. CO 95������������������������
+            Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params);
+            if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) {
+                resultMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO));
+            }
+
+            //2. O3 90������������������������
+            Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params);
+            if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) {
+                resultMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3));
+            }
+
+            sensors.forEach(sensor -> {
+                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
+                    Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class);
+                    Object o = sensorValue.get(sensor);
+                    if (ObjectUtils.isEmpty(o)) {
+                        return null;
+                    }
+                    double aDouble = Double.parseDouble(o.toString());
+                    return DoubleStream.of(aDouble);
+                }).average();
+
+                if (optionalDouble.isPresent()) {
+                    //���������������������
+                    double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0);
+                    resultMap.put(sensor, sciCal);
+                }
+            });
+
+            //������������������
+            Double compositeIndex = ComprehensiveIndexUtils.dailyData(resultMap);
+            resultMap.put("compositeIndex", compositeIndex);
+
+            //������O3���O3_8H������
+            resultMap.put("O3_8H", resultMap.remove("O3"));
+
+            //���������������������������
+            Date lastMonth = DateUtils.addMonths(DateUtils.getDate(time), -1);
+            QueryWrapper<CityAqiMonthly> queryWrapper = new QueryWrapper<>();
+            queryWrapper.select("value")
+                    .eq("city_code", cityCode)
+                    .eq("time", DateUtils.dateToDateString(lastMonth));
+            //������������������
+            CityAqiMonthly lastCityAqiMonthly = cityAqiMonthlyService.getOne(queryWrapper);
+            String monthContrast = "";
+            if (lastCityAqiMonthly != null) {
+                Map<String, Object> map = JSONObject.parseObject(lastCityAqiMonthly.getValue(), Map.class);
+                double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString());
+                DecimalFormat decimalFormat = new DecimalFormat("0.00%");
+                monthContrast = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex);
+            }
+            resultMap.put("monthContrast", monthContrast);
+
+            //���������
+            for (SysArea sysArea : sysAreas) {
+                if (cityCode.equals(sysArea.getAreaCode())) {
+                    resultMap.put("cityName", sysArea.getAreaName());
+                    break;
+                }
+            }
+            result.add(resultMap);
+        });
+        return result;
+    }
+
+    /**
+     * @param sysAreas ������������������������
+     * @param time     ��������������������������� 2021-11-01 00:00:00 ������1���1���
+     * @return ������������������
+     */
+    private List<Map<String, Object>> yearRank(List<SysArea> sysAreas, String time) {
+        List<Integer> regionCodes = sysAreas.stream()
+                .map(SysArea::getAreaCode)
+                .collect(Collectors.toList());
+
+        //���������������������������
+        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2");
+        List<Map<String, Object>> result = new ArrayList<>();
+        //���������������,������������������������������city_aqi_monthly������
+        if (!time.substring(0, 4).equals(DateUtils.dateToDateString(new Date(), DateUtils.yyyy))) {
+            QueryWrapper<CityAqiYearly> cityAqiYearlyQueryWrapper = new QueryWrapper<>();
+            for (Integer regionCode : regionCodes) {
+                cityAqiYearlyQueryWrapper.clear();
+                cityAqiYearlyQueryWrapper.select("value")
+                        .eq("city_code", regionCode)
+                        .eq("time", time);
+                CityAqiYearly cityAqiYearly = cityAqiYearlyService.getOne(cityAqiYearlyQueryWrapper);
+                if (cityAqiYearly == null) {
+                    continue;
+                }
+                String value = cityAqiYearly.getValue();
+                Map<String, Object> resultMap = JSONObject.parseObject(value, Map.class);
+
+
+                //���������
+                for (SysArea sysArea : sysAreas) {
+                    if (regionCode.equals(sysArea.getAreaCode())) {
+                        resultMap.put("cityName", sysArea.getAreaName());
+                        break;
+                    }
+                }
+                result.add(resultMap);
+            }
+            return result;
+        }
+
+        QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>();
+        cityAqiDailyQueryWrapper.select("city_code", "value")
+                .ge("time", time)
+                .in("city_code", regionCodes);
+        List<Map<String, Object>> thisMonthData = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper);
+        //���city_code������
+        Map<Integer, List<Map<String, Object>>> thisYearMap = thisMonthData.parallelStream()
+                .collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString())));
+        thisYearMap.forEach((cityCode, value) -> {
+            Map<String, Object> resultMap = new HashMap<>();
+
+            Map<String, Object> params = new HashMap<>();
+            List<Map<String, Object>> temp = new ArrayList<>();
+            for (Map<String, Object> map : value) {
+                Map<String, Object> sensorsValue = JSONObject.parseObject(map.get("value").toString(), Map.class);
+                Map<String, Object> tempMap = new HashMap<>();
+                tempMap.put(Constants.SENSOR_CODE_CO, sensorsValue.get("CO"));
+                tempMap.put(Constants.SENSOR_CODE_O3, sensorsValue.get("O3"));
+                Map<String, Object> hashMap = new HashMap<>();
+                hashMap.put("value", JSONObject.toJSONString(tempMap));
+                temp.add(hashMap);
+            }
+            params.put("data", temp);
+            //1. CO 95������������������������
+            Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params);
+            if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) {
+                resultMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO));
+            }
+
+            //2. O3 90������������������������
+            Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params);
+            if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) {
+                resultMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3));
+            }
+
+            sensors.forEach(sensor -> {
+                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
+                    Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class);
+                    Object o = sensorValue.get(sensor);
+                    if (ObjectUtils.isEmpty(o)) {
+                        return null;
+                    }
+                    double aDouble = Double.parseDouble(o.toString());
+                    return DoubleStream.of(aDouble);
+                }).average();
+
+                if (optionalDouble.isPresent()) {
+                    //���������������������
+                    double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0);
+                    resultMap.put(sensor, sciCal);
+                }
+            });
+
+            //������������������
+            Double compositeIndex = ComprehensiveIndexUtils.dailyData(resultMap);
+            resultMap.put("compositeIndex", compositeIndex);
+
+            //������O3���O3_8H������
+            resultMap.put("O3_8H", resultMap.remove("O3"));
+
+            //���������������������������
+            String lastYear = DateUtils.getDateAddYear(time.substring(0, 4), -1);
+            QueryWrapper<CityAqiYearly> queryWrapper = new QueryWrapper<>();
+            queryWrapper.select("value")
+                    .eq("city_code", cityCode)
+                    .eq("time", lastYear);
+            //������������������
+            CityAqiYearly lastCityAqiYearly = cityAqiYearlyService.getOne(queryWrapper);
+            String yearContrast = "";
+            if (lastCityAqiYearly != null) {
+                Map<String, Object> map = JSONObject.parseObject(lastCityAqiYearly.getValue(), Map.class);
+                double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString());
+                DecimalFormat decimalFormat = new DecimalFormat("0.00%");
+                yearContrast = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex);
+            }
+            resultMap.put("yearContrast", yearContrast);
+
+
+            //���������
+            for (SysArea sysArea : sysAreas) {
+                if (cityCode.equals(sysArea.getAreaCode())) {
+                    resultMap.put("cityName", sysArea.getAreaName());
+                    break;
+                }
+            }
+            result.add(resultMap);
+        });
+        return result;
+    }
+
+    /**
+     * @param sysAreas ������������������������
+     * @param start    ���������������������������������,���������������2021-11-03 00:00:00
+     * @param end      ���������������������������������,���������������2021-11-25 00:00:00
+     * @return ������������������������
+     */
+    private List<Map<String, Object>> customRank(List<SysArea> sysAreas, String start, String end) {
+        List<Integer> regionCodes = sysAreas.stream()
+                .map(SysArea::getAreaCode)
+                .collect(Collectors.toList());
+
+        //���������������������������
+        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2");
+        List<Map<String, Object>> result = new ArrayList<>();
+        QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>();
+        cityAqiDailyQueryWrapper.select("city_code", "value")
+                .ge("time", start)
+                .le("time", end)
+                .in("city_code", regionCodes);
+        List<Map<String, Object>> thisMonthData = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper);
+        //���city_code������
+        Map<Integer, List<Map<String, Object>>> customMap = thisMonthData.parallelStream()
+                .collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString())));
+        customMap.forEach((cityCode, value) -> {
+            Map<String, Object> resultMap = new HashMap<>();
+
+            Map<String, Object> params = new HashMap<>();
+            List<Map<String, Object>> temp = new ArrayList<>();
+            for (Map<String, Object> map : value) {
+                Map<String, Object> sensorsValue = JSONObject.parseObject(map.get("value").toString(), Map.class);
+                Map<String, Object> tempMap = new HashMap<>();
+                tempMap.put(Constants.SENSOR_CODE_CO, sensorsValue.get("CO"));
+                tempMap.put(Constants.SENSOR_CODE_O3, sensorsValue.get("O3"));
+                Map<String, Object> hashMap = new HashMap<>();
+                hashMap.put("value", JSONObject.toJSONString(tempMap));
+                temp.add(hashMap);
+            }
+            params.put("data", temp);
+            //1. CO 95������������������������
+            Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params);
+            if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) {
+                resultMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO));
+            }
+
+            //2. O3 90������������������������
+            Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params);
+            if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) {
+                resultMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3));
+            }
+
+            sensors.forEach(sensor -> {
+                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
+                    Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class);
+                    Object o = sensorValue.get(sensor);
+                    if (ObjectUtils.isEmpty(o)) {
+                        return null;
+                    }
+                    double aDouble = Double.parseDouble(o.toString());
+                    return DoubleStream.of(aDouble);
+                }).average();
+
+                if (optionalDouble.isPresent()) {
+                    //���������������������
+                    double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0);
+                    resultMap.put(sensor, sciCal);
+                }
+            });
+
+            //���������������������
+            Double compositeIndex = ComprehensiveIndexUtils.dailyData(resultMap);
+            resultMap.put("compositeIndex", compositeIndex);
+
+            //������O3���O3_8H������
+            resultMap.put("O3_8H", resultMap.remove("O3"));
+
+            //���������
+            for (SysArea sysArea : sysAreas) {
+                if (cityCode.equals(sysArea.getAreaCode())) {
+                    resultMap.put("cityName", sysArea.getAreaName());
+                    break;
+                }
+            }
+            result.add(resultMap);
+        });
         return result;
     }
 
@@ -282,7 +892,7 @@
     }
 
     @Override
-    public Map<String, Object> provincialRanking(Integer organizationId) {
+    public Map<String, Object> provincialRanking(Integer regionCode) {
         //���������
         Map<String, Object> result = new HashMap<>();
 
@@ -291,10 +901,10 @@
         Date yesterday = DateUtils.dataToTimeStampTime(DateUtils.getDateOfDay(now, -1), DateUtils.yyyy_MM_dd_EN);
         String dateString = DateUtils.dateToDateString(yesterday, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
 
+        String s = String.valueOf(regionCode);
         //���������,���code
-        Organization organization = organizationService.getById(organizationId);
-        Integer provinceCode = organization.getProvinceCode();
-        Integer cityCode = organization.getCityCode();
+        Integer provinceCode = Integer.parseInt(s.substring(0, 2) + "0000");
+        Integer cityCode = Integer.parseInt(s.substring(0, 4) + "00");
         //������������������city_code
         QueryWrapper<SysArea> wrapper = new QueryWrapper<>();
         wrapper.select("area_code").eq("parent_code", provinceCode);
@@ -377,8 +987,6 @@
 
         //������������������������������
         sortByField(ranks, "compositeIndexYear");
-        //���������������
-        sortByField(ranks, "compositeIndexYear");
         Map<String, Object> yearMap = rankByField(ranks, cityCode, "compositeIndexYear", cityCodes.size());
         if (ObjectUtils.isEmpty(yearMap)) {
             yearMap.put("rank", null);
@@ -422,5 +1030,4 @@
         }
         return result;
     }
-
 }

--
Gitblit v1.8.0