jinpengyong
2022-03-17 4b726dfe6a583ecbac1b3ba788d8d44b972db593
screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
@@ -30,7 +30,6 @@
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@@ -368,7 +367,7 @@
                }
            });
            //今日累计O3_8H计算,取每小时O3——8H最大值
            //今日累计O3_8H计算,取每小时O3_8H最大值
            if (!ObjectUtils.isEmpty(doubles)) {
                dataMap.put("O3_8H", Collections.max(doubles));
            }
@@ -454,32 +453,33 @@
        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");
        List<CityAqiDaily> dayData = cityAqiDailyService.list(wrapper);
        for (CityAqiDaily cityAqiDaily : dayData) {
            Map<String, Object> resultMap = JSONObject.parseObject(cityAqiDaily.getValue(), Map.class);
            resultMap.put("O3_8H", resultMap.remove("O3"));
            List<String> primaryPollutantNames = (List<String>) resultMap.get("primaryPollutant");
            String primaryPollutant = "";
            if (!ObjectUtils.isEmpty(primaryPollutantNames)) {
                primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(","));
            }
            value.put("primaryPollutant", primaryPollutant);
            resultMap.put("primaryPollutant", primaryPollutant);
            //城市名
            for (SysArea sysArea : sysAreas) {
                if (dayDatum.get("city_code").equals(sysArea.getAreaCode())) {
                    value.put("cityName", sysArea.getAreaName());
                if (cityAqiDaily.getCityCode().equals(sysArea.getAreaCode())) {
                    resultMap.put("cityName", sysArea.getAreaName());
                    break;
                }
            }
            result.add(value);
            result.add(resultMap);
        }
        return result;
    }
    /**
     * @param sysAreas 所要获取城市集合
     * @param time     所要获取数据的时间 2021-11-01 00:00:00 每月1号
     * @param time     所要获取数据的时间 2021-11-01 00:00:00
     * @return 功能:月排名
     */
    private List<Map<String, Object>> monthRank(List<SysArea> sysAreas, String time) {
@@ -487,123 +487,24 @@
                .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)
        QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>();
        cityAqiMonthlyQueryWrapper.select("city_code", "value")
                .eq("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显示
        List<CityAqiMonthly> list = cityAqiMonthlyService.list(cityAqiMonthlyQueryWrapper);
        for (CityAqiMonthly cityAqiMonthly : list) {
            Map<String, Object> resultMap = JSONObject.parseObject(cityAqiMonthly.getValue(), Map.class);
            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())) {
                if (cityAqiMonthly.getCityCode().equals(sysArea.getAreaCode())) {
                    resultMap.put("cityName", sysArea.getAreaName());
                    break;
                }
            }
            result.add(resultMap);
        });
        }
        return result;
    }
@@ -617,124 +518,23 @@
                .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显示
        QueryWrapper<CityAqiYearly> cityAqiYearlyQueryWrapper = new QueryWrapper<>();
        cityAqiYearlyQueryWrapper.select("city_code", "value")
                .in("city_code", regionCodes)
                .eq("time", time);
        List<CityAqiYearly> list = cityAqiYearlyService.list(cityAqiYearlyQueryWrapper);
        for (CityAqiYearly cityAqiYearly : list) {
            Map<String, Object> resultMap = JSONObject.parseObject(cityAqiYearly.getValue(), Map.class);
            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())) {
                if (cityAqiYearly.getCityCode().equals(sysArea.getAreaCode())) {
                    resultMap.put("cityName", sysArea.getAreaName());
                    break;
                }
            }
            result.add(resultMap);
        });
        }
        return result;
    }
@@ -829,7 +629,7 @@
    /**
     * @Description: 从数据库查询数据
     * @Param: [regionCode]
     * @return: java.util.Map<java.lang.String                                                               ,                                                                                                                               java.lang.Object>
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @Author: 陈凯裕
     * @Date: 2021/10/28
     */
@@ -1244,7 +1044,7 @@
    /**
     * @Description: 计算6参和综合指数对比的百分比
     * @Param: [data, comparisonData]
     * @return: java.util.Map<java.lang.String                               ,                               com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent>
     * @return: java.util.Map<java.lang.String, com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent>
     * @Author: 陈凯裕
     * @Date: 2022/1/17
     */
@@ -1269,15 +1069,15 @@
     */
    private ConcentrationAndPercent contrastParam(Double data, Double comparisonData, String sensor) {
        double percentD = MathUtils.division(data - comparisonData, comparisonData, 4);
        String percent = MathUtils.mul(percentD,100d) + "%";
        String percent = MathUtils.mul(percentD, 100d) + "%";
        ConcentrationAndPercent concentrationAndPercent = new ConcentrationAndPercent();
        concentrationAndPercent.setPercent(percent);
        if (sensor.equals("CO")) {//CO小数点保留一位
            Double CO = AmendUtils.sciCal(data, 1);
            concentrationAndPercent.setConcentration(CO.toString());
        }else if (sensor.equals("compositeIndex")){
        } else if (sensor.equals("compositeIndex")) {
            concentrationAndPercent.setConcentration(data.toString());
        }else{
        } else {
            Double sensorD = AmendUtils.sciCal(data, 0);
            Integer sensorI = new Double(sensorD).intValue();
            concentrationAndPercent.setConcentration(sensorI.toString());
@@ -1303,7 +1103,7 @@
    /**
     * @Description: 根据时间类型查询对应的6参以及综合指数,自定义时间类型用日数据做均值处理
     * @Param: [comparisonType, startDate, endDate, regionCode]
     * @return: java.util.Map<java.lang.String                                                               ,                                                               java.lang.Object>
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @Author: 陈凯裕
     * @Date: 2022/1/17
     */
@@ -1398,7 +1198,7 @@
    /**
     * @Description: 计算6参平均值
     * @Param: [cityAqiList]
     * @return: java.util.Map<java.lang.String                                                               ,                                                                                                                               java.lang.Double>
     * @return: java.util.Map<java.lang.String, java.lang.Double>
     * 返回值key为sensorCode,value为值
     * @Author: 陈凯裕
     * @Date: 2021/11/2