kaiyu
2021-11-03 30c76f1f25ddf6437014ea47bdc206cf0e288ec7
screen-common
修改AQI工具类代码
1 files added
6 files modified
780 ■■■■ changed files
screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java 6 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java 58 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/GovMonitorPointServiceImpl.java 15 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java 4 ●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/pojo/AQI.java 23 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/util/AQIUtils.java 646 ●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java 28 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
@@ -17,6 +17,7 @@
import com.moral.api.service.SysAreaService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.pojo.AQI;
import com.moral.util.AQIUtils;
import com.moral.util.AmendUtils;
import com.moral.util.DateUtils;
@@ -195,7 +196,10 @@
        //计算平均数
        Map<String, Object> sixParamAvg = calculate6ParamAvg(cityAqis);
        //计算累计aqi和首要污染物
        Map<String, Object> result = AQIUtils.hourlyAqi_pollutant(sixParamAvg);
        Map<String, Object> result = new HashMap<>();
        AQI aqi = AQIUtils.hourlyAQI(sixParamAvg);
        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");
screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java
@@ -8,6 +8,7 @@
import com.moral.api.pojo.form.dataDisplay.SensorComparisonDisplayForm;
import com.moral.api.service.*;
import com.moral.constant.Constants;
import com.moral.pojo.AQI;
import com.moral.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -76,21 +77,21 @@
                    macDataMap.put(value, datas.get(0));
            });
            if (macDataMap.size() != 0)
                dtos = calculateReportData(macDataMap, deviceMap, startTime);
                dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime);
        }
        //处理日报请求,默认获取昨天的值
        else if (reportType.equals(Constants.DAILY_REPORT)) {
            //获取数据
            Map<String, HistoryDaily> macDataMap = historyDailyService.getHistoryDailyByMacsAndDate(macs, startTime);
            if (macDataMap.size() != 0)
                dtos = calculateReportData(macDataMap, deviceMap, startTime);
                dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime);
        }
        //处理周报请求,默认获取上周的值
        else if (reportType.equals(Constants.WEEKLY_REPORT)) {
            //获取数据
            Map<String, HistoryWeekly> macDataMap = historyWeeklyService.getHistoryWeeklyByMacsAndDate(macs, startTime);
            if (macDataMap.size() != 0)
                dtos = calculateReportData(macDataMap, deviceMap, startTime, endTime);
                dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime, endTime);
        }
        //处理月报请求
        else if (reportType.equals(Constants.MONTHLY_REPORT)) {
@@ -103,7 +104,7 @@
                    macDataDailyMap.put(mac, dailyDatas);
            });
            if (macDataMonthlyMap.size() != 0)
                dtos = calculateReportData(macDataMonthlyMap, deviceMap, startTime);
                dtos = calculateReportData(macDataMonthlyMap, deviceMap, reportType, startTime);
            //注入综合指数
            injectComprehensiveIndex(dtos, macDataDailyMap);
        }
@@ -212,6 +213,13 @@
        return dtos;
    }
    /**
     * @Description: 根据时间进行排序
     * @Param: [timeValueMap, reportType]
     * @return: java.util.Map<java.lang.String               ,               java.lang.String>
     * @Author: 陈凯裕
     * @Date: 2021/11/3
     */
    private Map<String, String> sortMapByTime(Map<String, String> timeValueMap, String reportType) {
        List<Map.Entry<String, String>> entries = new ArrayList(timeValueMap.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
@@ -259,7 +267,6 @@
            String endTime = DateUtils.dateToDateString(endDate, "yyyy-MM-dd");
            String time = startTime + " - " + endTime;
            //计算数据个数
            Double AQINum = 0d;
            Double PM25Num = 0d;
            Double PM10Num = 0d;
            Double SO2Num = 0d;
@@ -267,8 +274,7 @@
            Double CONum = 0d;
            Double O3Num = 0d;
            Double TVOCNum = 0d;
            //计算AQI,PM2.5,PM10,SO2,NO2,CO,O3,TVOC总值,
            Double AQISum = 0d;
            //计算PM2.5,PM10,SO2,NO2,CO,O3,TVOC总值,
            Double PM25Sum = 0d;
            Double PM10Sum = 0d;
            Double SO2Sum = 0d;
@@ -314,18 +320,6 @@
                    TVOCSum = MathUtils.add(TVOCSum, Double.valueOf(TVOC.toString()));
                    TVOCNum++;
                }
                //加AQI
                int aqi = AQIUtils.hourlyAqi(valueMap);
                if (aqi != 0) {
                    AQISum += Double.valueOf(aqi);
                    AQINum++;
                }
            }
            //计算aqi均值
            if (AQINum != 0d) {
                Double AQID = AmendUtils.sciCal(AQISum / AQINum, 0);
                int AQIAvg = new Double(AQID).intValue();
                dto.setAQI(AQIAvg);
            }
            //计算PM2.5均值
            if (PM25Num != 0d) {
@@ -367,6 +361,16 @@
                Double TVOCAvg = AmendUtils.sciCal(TVOCSum / TVOCNum, 2);
                dto.setA99054(TVOCAvg);
            }
            //计算AQI
            Map<String,Object> sixParamMap = new HashMap<>();
            sixParamMap.put("a05024",dto.getA05024());
            sixParamMap.put("a21004",dto.getA21004());
            sixParamMap.put("a21005",dto.getA21005());
            sixParamMap.put("a34002",dto.getA34002());
            sixParamMap.put("a34004",dto.getA34004());
            sixParamMap.put("a21026",dto.getA21026());
            AQI aqi = AQIUtils.dailyAQI(sixParamMap);
            dto.setAQI(aqi.getAQIValue());
            dto.setDeviceName(deviceName);
            dto.setTime(time);
            dtos.add(dto);
@@ -382,7 +386,7 @@
     * @Author: 陈凯裕
     * @Date: 2021/9/28
     */
    private <T> List<MonitorPointDataDisplayDTO> calculateReportData(Map<String, T> macDataMap, Map<String, Device> deviceMap, Date... date) {
    private <T> List<MonitorPointDataDisplayDTO> calculateReportData(Map<String, T> macDataMap, Map<String, Device> deviceMap, String reportType, Date... date) {
        List<MonitorPointDataDisplayDTO> dtos = new ArrayList<>();
        macDataMap.forEach((key, valueObject) -> {
            MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO();
@@ -410,7 +414,7 @@
                startTime = DateUtils.dateToDateString(date[0], "yyyy-MM");
            dto.setTime(startTime);
            //注入数据
            injectDataToDto(valueMap, false, dto);
            injectDataToDto(valueMap, false, dto, reportType);
            dtos.add(dto);
        });
        return dtos;
@@ -424,7 +428,7 @@
     * @Author: 陈凯裕
     * @Date: 2021/9/28
     */
    private void injectDataToDto(Map<String, Object> valueMap, boolean flag, MonitorPointDataDisplayDTO dto) {
    private void injectDataToDto(Map<String, Object> valueMap, boolean flag, MonitorPointDataDisplayDTO dto, String reportType) {
        //注入CO
        String COFlag = (String) valueMap.get("a21005-Flag");
        Object COObject = valueMap.get("a21005");
@@ -475,9 +479,13 @@
            dto.setA05024(new Double(AmendUtils.sciCal(O3, 0)).intValue());
        }
        //注入AQI
        int aqi = AQIUtils.hourlyAqi(valueMap);
        if (aqi != 0)
            dto.setAQI(aqi);
        Integer aqi;
        if (reportType.equals(Constants.HOURLY_REPORT)) {
            aqi = AQIUtils.hourlyAQI(valueMap).getAQIValue();
        } else {
            aqi = AQIUtils.dailyAQI(valueMap).getAQIValue();
        }
        dto.setAQI(aqi);
    }
screen-api/src/main/java/com/moral/api/service/impl/GovMonitorPointServiceImpl.java
@@ -14,6 +14,7 @@
import com.moral.api.service.OrganizationService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.pojo.AQI;
import com.moral.util.AQIUtils;
import com.moral.util.RegionCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -492,9 +493,9 @@
                if (!ObjectUtils.isEmpty(PM2_5)){
                    AQIMap.put("a05024",O3_1h);
                }
                Map<String,Object> AQIResultMap = new HashMap<>();
                AQI aqi = new AQI();
                if (AQIMap.size()!=0){
                    AQIResultMap = AQIUtils.hourlyAqi_pollutant(AQIMap);
                    aqi = AQIUtils.hourlyAQI(AQIMap);
                }
                Map<String,Object> avgMap = new HashMap<>();
                avgMap.put("a34004_"+"0",PM2_5);
@@ -504,14 +505,14 @@
                avgMap.put("a21005_"+"0",CO);
                avgMap.put("a05024_"+"0",O3_1h);
                avgMap.put("O3_8h_"+"0",O3_8h);
                if (!ObjectUtils.isEmpty(AQIResultMap.get("aqi"))){
                    avgMap.put("aqi",AQIResultMap.get("aqi"));
                if (!ObjectUtils.isEmpty(aqi.getAQIValue())){
                    avgMap.put("aqi",aqi.getAQIValue());
                }else {
                    avgMap.put("aqi","");
                }
                if (!ObjectUtils.isEmpty(AQIResultMap.get("pollutant"))){
                    if (Integer.parseInt(AQIResultMap.get("aqi").toString())>50){
                        String pollutant = AQIResultMap.get("pollutant").toString();
                if (!ObjectUtils.isEmpty(aqi.getPrimaryPollutantNames())){
                    if (Integer.parseInt(aqi.getAQIValue().toString())>50){
                        String pollutant = aqi.getPrimaryPollutantNames().toString();
                        pollutant = pollutant.replace("[","");
                        pollutant = pollutant.replace("]","");
                        avgMap.put("pollutant",pollutant);
screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
@@ -9,6 +9,7 @@
import com.moral.api.service.HistoryHourlyService;
import com.moral.constant.Constants;
import com.moral.constant.SeparateTableType;
import com.moral.pojo.AQI;
import com.moral.util.AQIUtils;
import com.moral.util.DateUtils;
@@ -49,7 +50,8 @@
            return result;
        }
        Map<String, Object> data = JSONObject.parseObject(value, Map.class);
        result.put("AQI", AQIUtils.hourlyAqi(data));
        AQI aqi = AQIUtils.hourlyAQI(data);
        result.put("AQI", aqi.getAQIValue());
        return result;
    }
screen-common/src/main/java/com/moral/pojo/AQI.java
New file
@@ -0,0 +1,23 @@
package com.moral.pojo;
import lombok.Data;
import java.util.List;
/**
 * @ClassName AQI
 * @Description TODO
 * @Author 陈凯裕
 * @Date 2021/11/3 11:01
 * @Version TODO
 **/
@Data
public class AQI {
    private Integer AQIValue;
    private List<String> primaryPollutantNames;
    private List<String> primaryPollutantCodes;
}
screen-common/src/main/java/com/moral/util/AQIUtils.java
@@ -1,370 +1,352 @@
package com.moral.util;
import org.springframework.util.ObjectUtils;
import com.moral.pojo.AQI;
import java.util.*;
import com.moral.constant.Constants;
public class AQIUtils {
    public static int hourlyAqi(Map<String, Object> map) {
        List<Integer> AQIList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (entry.getValue().toString().equals("")) {
                continue;
            }
            int PM2_5AQI;
            int PM10AQI;
            int SO2AQI;
            int NO2AQI;
            int COAQI;
            int O3AQI;
            switch (key) {
                case Constants.SENSOR_CODE_PM25:
                    PM2_5AQI = PM2_5AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM2_5AQI);
                    break;
                case Constants.SENSOR_CODE_PM10:
                    PM10AQI = PM10AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM10AQI);
                    break;
                case Constants.SENSOR_CODE_SO2:
                    SO2AQI = SO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(SO2AQI);
                    break;
                case Constants.SENSOR_CODE_NO2:
                    NO2AQI = NO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(NO2AQI);
                    break;
                case Constants.SENSOR_CODE_CO:
                    COAQI = COAQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(COAQI);
                    break;
                case Constants.SENSOR_CODE_O3:
                    O3AQI = O3AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(O3AQI);
                    break;
                default:
                    break;
            }
        }
        int AQIMAX = 0;
        if (!ObjectUtils.isEmpty(AQIList)) {
            if (AQIList.size() == 6) {
                AQIMAX = Collections.max(AQIList);
            }
        }
        return AQIMAX;
    }
    /**
     * @Description: 小时AQI首要污染物,map的key为sensorCode,value为数值
     * @Param: [map]
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @Author: 陈凯裕
     * @Date: 2021/10/29
     */
    public static Map<String, Object> hourlyAqi_pollutant(Map<String, Object> map) {
        List<Integer> AQIList = new ArrayList<>();
        Map<String, Integer> AQIMap = new HashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (entry.getValue().toString().equals("")) {
                continue;
            }
            int PM2_5AQI;
            int PM10AQI;
            int SO2AQI;
            int NO2AQI;
            int COAQI;
            int O3AQI;
            switch (key) {
                case Constants.SENSOR_CODE_PM25:
                    PM2_5AQI = PM2_5AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM2_5AQI);
                    AQIMap.put("PM2.5", PM2_5AQI);
                    break;
                case Constants.SENSOR_CODE_PM10:
                    PM10AQI = PM10AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM10AQI);
                    AQIMap.put("PM10", PM10AQI);
                    break;
                case Constants.SENSOR_CODE_SO2:
                    SO2AQI = SO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(SO2AQI);
                    AQIMap.put("SO2", SO2AQI);
                    break;
                case Constants.SENSOR_CODE_NO2:
                    NO2AQI = NO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(NO2AQI);
                    AQIMap.put("NO2", NO2AQI);
                    break;
                case Constants.SENSOR_CODE_CO:
                    COAQI = COAQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(COAQI);
                    AQIMap.put("CO", COAQI);
                    break;
                case Constants.SENSOR_CODE_O3:
                    O3AQI = O3AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(O3AQI);
                    AQIMap.put("O3", O3AQI);
                    break;
                default:
                    break;
            }
        }
        int AQIMAX = 0;
        if (!ObjectUtils.isEmpty(AQIList)) {
            AQIMAX = Collections.max(AQIList);
        }
        List<String> nameList = new ArrayList<>();
        for (String key : AQIMap.keySet()) {
            if (AQIMap.get(key).equals(AQIMAX)) {
                nameList.add(key);
            }
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("aqi", AQIMAX);
        if (AQIMAX > 50)
            resultMap.put("pollutant", nameList);
        return resultMap;
    }
    /*24小时浓度限值*/
    private static Double[] SO2_Daily_Limit_Data = new Double[]{0d, 50d, 150d, 475d, 800d, 1600d, 2100d, 2620d};
    private static Double[] NO2_Daily_Limit_Data = new Double[]{0d, 40d, 80d, 180d, 280d, 565d, 750d, 940d};
    private static Double[] PM10_Daily_Limit_Data = new Double[]{0d, 50d, 150d, 250d, 350d, 420d, 500d, 600d};
    private static Double[] CO_Daily_Limit_Data = new Double[]{0d, 2d, 4d, 14d, 24d, 36d, 48d, 60d};
    private static Double[] PM2_5_Daily_Limit_Data = new Double[]{0d, 35d, 75d, 115d, 150d, 250d, 350d, 500d};
    private static Double[] O3_Daily_Limit_Data = new Double[]{0d, 100d, 160d, 215d, 265d, 800d};
    /*1小时浓度限值*/
    private static Double[] SO2_Hourly_Limit_Data = new Double[]{0d, 150d, 500d, 650d, 800d};
    private static Double[] NO2_Hourly_Limit_Data = new Double[]{0d, 100d, 200d, 700d, 1200d, 2340d, 3090d, 3840d};
    private static Double[] PM10_Hourly_Limit_Data = new Double[]{0d, 50d, 150d, 250d, 350d, 420d, 500d, 600d};
    private static Double[] CO_Hourly_Limit_Data = new Double[]{0d, 5d, 10d, 35d, 60d, 90d, 120d, 150d};
    private static Double[] PM2_5_Hourly_Limit_Data = new Double[]{0d, 35d, 75d, 115d, 150d, 250d, 350d, 500d};
    private static Double[] O3_Hourly_Limit_Data = new Double[]{0d, 160d, 200d, 300d, 400d, 800d, 1000d, 1200d};
    /*空气质量分指数*/
    private static Double[] IAQI_Array = new Double[]{0d,50d,100d,150d,200d,300d,400d,500d};
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("a34004", 11.25);
        map.put("a34002", 18.25);
        map.put("a21026", 55);
        map.put("a21004", 200.01);
        map.put("a21005", 18.94);
        map.put("a05024", 55);
        System.out.println(hourlyAqi_pollutant(map));
        Map<String,Object> map = new HashMap<>();
        map.put("a34002",60);
        map.put("a34004",56);
        map.put("a21004",99);
        map.put("a21005",0.54);
        map.put("a21026",27);
        map.put("a05024",72);
        System.out.println(dailyAQI(map));
    }
    /**
     * @Description: 计算AQI污染等级
     * @Param:
     * @return:
     * @Author: 陈凯裕
     * @Date: 2021/10/29
     */
    public static String classOfPollutionByAqi(Integer aqi) {
        if (aqi > 300)
    * @Description: 计算AQI污染等级
            * @Param:
            * @return:
            * @Author: 陈凯裕
            * @Date: 2021/10/29
            */
    public static String classOfPollutionByAqi(Integer aqi){
        if(aqi>300)
            return Constants.SERVER;
        if (aqi > 200)
        if(aqi>200)
            return Constants.SERIOUS;
        if (aqi > 150)
        if(aqi>150)
            return Constants.MIDDLE;
        if (aqi > 100)
        if(aqi>100)
            return Constants.MILD;
        if (aqi > 50)
        if(aqi>50)
            return Constants.GOOD;
        return Constants.EXCELLENT;
    }
    //PM2.5 IAQI
    private static int PM2_5AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 35) {
            result = 50d / 35d * (value - 0) + 0;
        } else if (value <= 75) {
            result = 50d / 40d * (value - 35) + 50;
        } else if (value <= 115) {
            result = 50d / 40d * (value - 75) + 100;
        } else if (value <= 150) {
            result = 50d / 35d * (value - 115) + 150;
        } else if (value <= 250) {
            result = 100d / 100d * (value - 150) + 200;
        } else if (value <= 350) {
            result = 100d / 100d * (value - 250) + 300;
        } else {
            result = 100d / 150d * (value - 350) + 400;
    /**
     * @Description: 24小时AQI
     * @Param: [paramMap]
     * @return: java.lang.Integer
     * @Author: 陈凯裕
     * @Date: 2021/11/3
     */
    public static AQI dailyAQI(Map<String,Object> paramMap){
        if(paramMap==null)
            return null;
        List<Integer> IAQIList = new ArrayList<>();
        List<String> primaryPollutantNames = new ArrayList<>();
        List<String> primaryPollutantCodes = new ArrayList<>();
        Integer PM2_5IAQI;
        Integer SO2IAQI;
        Integer NO2IAQI;
        Integer COIAQI;
        Integer PM10IAQI;
        Integer O3IAQI;
        if(paramMap.get(Constants.SENSOR_CODE_PM25)!=null){
            Double PM2_5 = Double.valueOf(paramMap.get("a34004").toString());
            PM2_5IAQI = calculateIAQIUniversal(PM2_5,PM2_5_Daily_Limit_Data);
            IAQIList.add(PM2_5IAQI);
            if(PM2_5IAQI>50){
                primaryPollutantCodes.add(Constants.SENSOR_CODE_PM25);
                primaryPollutantNames.add("PM2.5");
            }
        }
        return (int) Math.ceil(result);
    }
    //PM10 IAQI
    private static int PM10AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 50) {
            result = 50d / 50d * (value - 0) + 0;
        } else if (value <= 150) {
            result = 50d / 100d * (value - 50) + 50;
        } else if (value <= 250) {
            result = 50d / 100d * (value - 150) + 100;
        } else if (value <= 350) {
            result = 50d / 100d * (value - 250) + 150;
        } else if (value <= 420) {
            result = 100d / 70d * (value - 350) + 200;
        } else if (value <= 500) {
            result = 100d / 80d * (value - 420) + 300;
        } else {
            result = 100d / 100d * (value - 500) + 400;
        if(paramMap.get(Constants.SENSOR_CODE_SO2)!=null){
            Double SO2 = Double.valueOf(paramMap.get("a21026").toString());
            SO2IAQI = calculateIAQIUniversal(SO2,SO2_Daily_Limit_Data);
            IAQIList.add(SO2IAQI);
            if(SO2IAQI>50){
                primaryPollutantNames.add("SO2");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_SO2);
            }
        }
        return (int) Math.ceil(result);
    }
    //SO2 IAQI
    private static int SO2AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 150) {
            result = 50d / 150d * (value - 0) + 0;
        } else if (value <= 500) {
            result = 50d / 350d * (value - 150) + 50;
        } else if (value <= 650) {
            result = 50d / 150d * (value - 500) + 100;
        } else {
            result = 50d / 150d * (value - 650) + 150;
        if(paramMap.get(Constants.SENSOR_CODE_NO2)!=null){
            Double NO2 = Double.valueOf(paramMap.get("a21004").toString());
            NO2IAQI = calculateIAQIUniversal(NO2,NO2_Daily_Limit_Data);
            IAQIList.add(NO2IAQI);
            if(NO2IAQI>50){
                primaryPollutantNames.add("NO2");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_NO2);
            }
        }
        return (int) Math.ceil(result);
    }
    //NO2 IAQI
    private static int NO2AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 100) {
            result = 50d / 100d * (value - 0) + 0;
        } else if (value <= 200) {
            result = 50d / 100d * (value - 100) + 50;
        } else if (value <= 700) {
            result = 50d / 500d * (value - 200) + 100;
        } else if (value <= 1200) {
            result = 50d / 500d * (value - 700) + 150;
        } else if (value <= 2340) {
            result = 100d / 1140d * (value - 1200) + 200;
        } else if (value <= 3090) {
            result = 100d / 750d * (value - 2340) + 300;
        } else {
            result = 100d / 750d * (value - 3090) + 400;
        if(paramMap.get(Constants.SENSOR_CODE_CO)!=null){
            Double CO = Double.valueOf(paramMap.get("a21005").toString());
            COIAQI = calculateIAQIUniversal(CO,CO_Daily_Limit_Data);
            IAQIList.add(COIAQI);
            if(COIAQI>50){
                primaryPollutantNames.add("CO");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_CO);
            }
        }
        return (int) Math.ceil(result);
    }
    //CO IAQI
    private static int COAQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 5) {
            result = 50d / 5d * (value - 0) + 0;
        } else if (value <= 10) {
            result = 50d / 5d * (value - 5) + 50;
        } else if (value <= 35) {
            result = 50d / 25d * (value - 10) + 100;
        } else if (value <= 60) {
            result = 50d / 25d * (value - 35) + 150;
        } else if (value <= 90) {
            result = 100d / 30d * (value - 60) + 200;
        } else if (value <= 120) {
            result = 100d / 30d * (value - 90) + 300;
        } else {
            result = 100d / 30d * (value - 120) + 400;
        if(paramMap.get(Constants.SENSOR_CODE_PM10)!=null){
            Double PM10 = Double.valueOf(paramMap.get("a34002").toString());
            PM10IAQI = calculateIAQIUniversal(PM10,PM10_Daily_Limit_Data);
            IAQIList.add(PM10IAQI);
            if(PM10IAQI>50){
                primaryPollutantNames.add("PM10");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_PM10);
            }
        }
        return (int) Math.ceil(result);
    }
    //O3 IAQI
    private static int O3AQI(Double value) {
        double result;
        if (value <= 0) {
            result = 0;
        } else if (value <= 100) {
            result = 50d / 100d * (value - 0) + 0;
        } else if (value <= 160) {
            result = 50d / 60d * (value - 100) + 50;
        } else if (value <= 215) {
            result = 50d / 55d * (value - 160) + 100;
        } else if (value <= 265) {
            result = 50d / 150d * (value - 215) + 150;
        } else if (value <= 800) {
            result = 100d / 535d * (value - 265) + 200;
        } else if (value <= 2100) {
            result = 100d / 12d * (value - 800) + 300;
        } else {
            result = 100d / 12d * (value - 48) + 400;
        if(paramMap.get(Constants.SENSOR_CODE_O3)!=null){
            Double O3 = Double.valueOf(paramMap.get("a05024").toString());
            O3IAQI = calculateO3DailyIAQI(O3);
            IAQIList.add(O3IAQI);
            if(O3IAQI>50){
                primaryPollutantNames.add("O3");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_O3);
            }
        }
        return (int) Math.ceil(result);
        AQI aqi= new AQI();
        aqi.setAQIValue(getAQIByIAQIs(IAQIList));
        aqi.setPrimaryPollutantCodes(primaryPollutantCodes);
        aqi.setPrimaryPollutantNames(primaryPollutantNames);
        return aqi;
    }
    /**
     * @Description: 小时AQI首要污染物,map的key为pm2_5,pm10,so2,no2,co,o3,value为数值
     * @Param: [map]
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @Description: 1小时AQI
     * @Param: [paramMap]
     * @return: java.lang.Integer
     * @Author: 陈凯裕
     * @Date: 2021/10/29
     * @Date: 2021/11/3
     */
    public static Map<String, Object> cityAqiPollutant(Map<String, Object> map) {
        List<Integer> AQIList = new ArrayList<>();
        Map<String, Integer> AQIMap = new HashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (entry.getValue().toString().equals("")) {
                continue;
            }
            int PM2_5AQI;
            int PM10AQI;
            int SO2AQI;
            int NO2AQI;
            int COAQI;
            int O3AQI;
            switch (key) {
                case "pm2_5":
                    PM2_5AQI = PM2_5AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM2_5AQI);
                    AQIMap.put("pm2_5", PM2_5AQI);
                    break;
                case "pm10":
                    PM10AQI = PM10AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(PM10AQI);
                    AQIMap.put("pm10", PM10AQI);
                    break;
                case Constants.SENSOR_CODE_SO2:
                    SO2AQI = SO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(SO2AQI);
                    AQIMap.put("so2", SO2AQI);
                    break;
                case Constants.SENSOR_CODE_NO2:
                    NO2AQI = NO2AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(NO2AQI);
                    AQIMap.put("no2", NO2AQI);
                    break;
                case Constants.SENSOR_CODE_CO:
                    COAQI = COAQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(COAQI);
                    AQIMap.put("co", COAQI);
                    break;
                case Constants.SENSOR_CODE_O3:
                    O3AQI = O3AQI(Double.valueOf(entry.getValue().toString()));
                    AQIList.add(O3AQI);
                    AQIMap.put("o3", O3AQI);
                    break;
                default:
                    break;
    public static AQI hourlyAQI(Map<String,Object> paramMap){
        if(paramMap==null)
            return null;
        List<Integer> IAQIList = new ArrayList<>();
        List<String> primaryPollutantNames = new ArrayList<>();
        List<String> primaryPollutantCodes = new ArrayList<>();
        Integer PM2_5IAQI;
        Integer SO2IAQI;
        Integer NO2IAQI;
        Integer COIAQI;
        Integer PM10IAQI;
        Integer O3IAQI;
        if(paramMap.get("a34004")!=null){
            Double PM2_5 = Double.valueOf(paramMap.get("a34004").toString());
            PM2_5IAQI = calculateIAQIUniversal(PM2_5,PM2_5_Hourly_Limit_Data);
            IAQIList.add(PM2_5IAQI);
            if(PM2_5IAQI>50){
                primaryPollutantCodes.add(Constants.SENSOR_CODE_PM25);
                primaryPollutantNames.add("PM2.5");
            }
        }
        int AQIMAX = 0;
        if (!ObjectUtils.isEmpty(AQIList)) {
            AQIMAX = Collections.max(AQIList);
        }
        List<String> nameList = new ArrayList<>();
        for (String key : AQIMap.keySet()) {
            if (AQIMap.get(key).equals(AQIMAX)) {
                nameList.add(key);
        if(paramMap.get("a21026")!=null){
            Double SO2 = Double.valueOf(paramMap.get("a21026").toString());
            SO2IAQI = calculateSO2HourlyIAQI(SO2);
            IAQIList.add(SO2IAQI);
            if(SO2IAQI>50){
                primaryPollutantNames.add("SO2");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_SO2);
            }
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("aqi", AQIMAX);
        if (AQIMAX > 50)
            resultMap.put("pollutant", nameList);
        return resultMap;
        if(paramMap.get("a21004")!=null){
            Double NO2 = Double.valueOf(paramMap.get("a21004").toString());
            NO2IAQI = calculateIAQIUniversal(NO2,NO2_Hourly_Limit_Data);
            IAQIList.add(NO2IAQI);
            if(NO2IAQI>50){
                primaryPollutantNames.add("NO2");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_NO2);
            }
        }
        if(paramMap.get("a21005")!=null){
            Double CO = Double.valueOf(paramMap.get("a21005").toString());
            COIAQI = calculateIAQIUniversal(CO,CO_Hourly_Limit_Data);
            IAQIList.add(COIAQI);
            if(COIAQI>50){
                primaryPollutantNames.add("CO");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_CO);
            }
        }
        if(paramMap.get("a34002")!=null){
            Double PM10 = Double.valueOf(paramMap.get("a34002").toString());
            PM10IAQI = calculateIAQIUniversal(PM10,PM10_Hourly_Limit_Data);
            IAQIList.add(PM10IAQI);
            if(PM10IAQI>50){
                primaryPollutantNames.add("PM10");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_PM10);
            }
        }
        if(paramMap.get("a05024")!=null){
            Double O3 = Double.valueOf(paramMap.get("a05024").toString());
            O3IAQI = calculateIAQIUniversal(O3,O3_Hourly_Limit_Data);
            IAQIList.add(O3IAQI);
            if(O3IAQI>50) {
                primaryPollutantNames.add("O3");
                primaryPollutantCodes.add(Constants.SENSOR_CODE_O3);
            }
        }
        AQI aqi= new AQI();
        aqi.setAQIValue(getAQIByIAQIs(IAQIList));
        aqi.setPrimaryPollutantCodes(primaryPollutantCodes);
        aqi.setPrimaryPollutantNames(primaryPollutantNames);
        return aqi;
    }
    /**
     * @Description: 根据IAQI计算AQI
     * @Param: [IAQIs]
     * @return: int
     * @Author: 陈凯裕
     * @Date: 2021/11/3
     */
    private static Integer getAQIByIAQIs(List<Integer> IAQIs){
        if(IAQIs.size()==0)
            return null;
        Optional<Integer> max = IAQIs.stream().max(Comparator.comparing(Integer::intValue));
        return max.get();
    }
    /**
     * @Description: 除了so2的小时IAQI,O3的日IAQI外的IAQI计算公式
     * @Param: [Cp, limitData]
     * @return: java.lang.Integer
     * @Author: 陈凯裕
     * @Date: 2021/11/2
     */
    private static Integer calculateIAQIUniversal(Double Cp,Double[] limitData){
        int index = getStartIndexFromArray(Cp, limitData);
        Double BPHi;
        Double BPLo;
        Double IAQIHi;
        Double IAQILo;
        if(index==limitData.length-1){
            return 500;
        }else{
            BPHi = limitData[index+1];
            BPLo = limitData[index];
            IAQIHi = IAQI_Array[index+1];
            IAQILo = IAQI_Array[index];
        }
        return calculateIAQIFormula(Cp,BPHi,BPLo,IAQIHi,IAQILo);
    }
    /**
     * @Description: 计算so2的小时IAQI
     * @Param: [Cp, limitData]
     * @return: java.lang.Integer
     * @Author: 陈凯裕
     * @Date: 2021/11/2
     */
    private static Integer calculateSO2HourlyIAQI(Double Cp){
        Double BPHi;
        Double BPLo;
        Double IAQIHi;
        Double IAQILo;
        Double[] limitData = SO2_Hourly_Limit_Data;
        int index = getStartIndexFromArray(Cp, limitData);
        //如果so2超过了小时最大限值,则采用24小时的限值标准,再计算分指数
        if(index ==SO2_Hourly_Limit_Data.length-1){
            limitData = SO2_Daily_Limit_Data;
            index = getStartIndexFromArray(Cp,limitData);
        }
        if(index==limitData.length-1){
            return 500;
        }else{
            BPHi = limitData[index+1];
            BPLo = limitData[index];
            IAQIHi = IAQI_Array[index+1];
            IAQILo = IAQI_Array[index];
        }
        return calculateIAQIFormula(Cp,BPHi,BPLo,IAQIHi,IAQILo);
    }
    /**
     * @Description: 计算O3日的IAQI
     * @Param: [Cp]
     * @return: java.lang.Integer
     * @Author: 陈凯裕
     * @Date: 2021/11/3
     */
    private static Integer calculateO3DailyIAQI(Double Cp){
        Double BPHi;
        Double BPLo;
        Double IAQIHi;
        Double IAQILo;
        Double[] limitData = O3_Daily_Limit_Data;
        int index = getStartIndexFromArray(Cp, limitData);
        //如果so2超过了小时最大限值,则采用24小时的限值标准,再计算分指数
        if(index ==O3_Daily_Limit_Data.length-1){
            limitData = O3_Hourly_Limit_Data;
            index = getStartIndexFromArray(Cp,limitData);
        }
        if(index==limitData.length-1){
            return 500;
        }else{
            BPHi = limitData[index+1];
            BPLo = limitData[index];
            IAQIHi = IAQI_Array[index+1];
            IAQILo = IAQI_Array[index];
        }
        return calculateIAQIFormula(Cp,BPHi,BPLo,IAQIHi,IAQILo);
    }
    /**
     * @Description: 计算IAQI的公式
     * @Param: [Cp, BPHi, BPLo, IAQIHi, IAQILo]
     * @return: java.lang.Integer
     * @Author: 陈凯裕
     * @Date: 2021/11/3
     */
    private static Integer calculateIAQIFormula(Double Cp, Double BPHi, Double BPLo, Double IAQIHi, Double IAQILo) {
        Double tmp1 = MathUtils.sub(IAQIHi, IAQILo);
        Double tmp2 = MathUtils.sub(BPHi, BPLo);
        Double tmp3 = MathUtils.sub(Cp, BPLo);
        Double tmp4 = MathUtils.division(tmp1, tmp2, 2);
        Double tmp5 = MathUtils.mul(tmp4, tmp3);
        Double IAQI = MathUtils.add(tmp5, IAQILo);
        return new Double(AmendUtils.sciCal(IAQI, 0)).intValue();
    }
    /*获取数值对应数组中的索引范围的开始值
     * 例如:数值:5               10
     *       数组:[0,5,15,20]     [0,5,8,12]
     *       结果:1(左闭右开)     2
     *       */
    private static int getStartIndexFromArray(Double num, Double[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            if (num >= array[i] && num < array[i + 1])
                return i;
            if (i == array.length - 2)
                return i + 1;
        }
        return 0;
    }
}
screen-job/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
@@ -10,6 +10,7 @@
import com.moral.api.service.CityConfigService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.pojo.AQI;
import com.moral.util.AQIUtils;
import com.moral.util.AmendUtils;
import com.moral.util.ComprehensiveIndexUtils;
@@ -86,14 +87,14 @@
            Map<String, Object> map = (Map<String, Object>) data.get("data");
            Map<String, Object> aqi = (Map<String, Object>) map.get("aqi");
            aqi.put("pm2_5", aqi.remove("pm25C"));
            aqi.put("PM2_5", aqi.remove("pm25C"));
            aqi.remove("pm25");
            aqi.put("pm10", aqi.remove("pm10C"));
            aqi.put("so2", aqi.remove("so2C"));
            aqi.put("no2", aqi.remove("no2C"));
            aqi.put("co", aqi.remove("coC"));
            aqi.put("o3", aqi.remove("o3C"));
            aqi.put("aqi", aqi.remove("value"));
            aqi.put("PM10", aqi.remove("pm10C"));
            aqi.put("SO2", aqi.remove("so2C"));
            aqi.put("NO2", aqi.remove("no2C"));
            aqi.put("CO", aqi.remove("coC"));
            aqi.put("O3", aqi.remove("o3C"));
            aqi.put("AQI", aqi.remove("value"));
            //o3滑动值计算
            if (start != null) {
@@ -126,9 +127,16 @@
            aqi.put("compositeIndex", compositeIndex);
            //首要污染物计算
            Map<String, Object> pollutant = AQIUtils.cityAqiPollutant(aqi);
            if (!ObjectUtils.isEmpty(pollutant)) {
                aqi.putAll(pollutant);
            Map<String,Object> sixParamMap = new HashMap<>();
            sixParamMap.put("a34002",aqi.get("PM10"));
            sixParamMap.put("a34004",aqi.get("PM2_5"));
            sixParamMap.put("a21004",aqi.get("NO2"));
            sixParamMap.put("a21005",aqi.get("CO"));
            sixParamMap.put("a05024",aqi.get("O3"));
            sixParamMap.put("a21026",aqi.get("SO2"));
            AQI aqiAndPollutant = AQIUtils.hourlyAQI(sixParamMap);
            if (!ObjectUtils.isEmpty(aqiAndPollutant.getPrimaryPollutantNames())) {
                aqi.put("pollutant",aqiAndPollutant.getPrimaryPollutantNames());
            }
            CityAqi cityAqi = new CityAqi();