| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.api.service.SysAreaService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.AQIUtils; |
| | | import com.moral.util.AmendUtils; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import com.moral.util.MathUtils; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> queryTodayAqiAndPollutant(Integer regionCode) { |
| | | //获取今天的9点时间 |
| | | Date startDate = new Date(DateUtils.getTodayTime()); |
| | | Date endDate = new Date(); |
| | | //查询当天数据 |
| | | QueryWrapper<CityAqi> wrapper = new QueryWrapper<>(); |
| | | wrapper.between("time", startDate, endDate); |
| | | wrapper.eq("city_code", regionCode); |
| | | wrapper.select("DISTINCT city_code,time,value"); |
| | | List<CityAqi> cityAqis = cityAqiMapper.selectList(wrapper); |
| | | //计算平均数 |
| | | Map<String, Object> sixParamAvg = calculate6ParamAvg(cityAqis); |
| | | //计算累计aqi和首要污染物 |
| | | Map<String, Object> result = AQIUtils.hourlyAqi_pollutant(sixParamAvg); |
| | | //结果集添加时间 |
| | | CityAqi lastCityAqi = cityAqis.get(cityAqis.size() - 1); |
| | | String time = DateUtils.dateToDateString(lastCityAqi.getTime(), "HH:mm"); |
| | | result.put("time", time); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算6参平均值 |
| | | * @Param: [cityAqiList] |
| | | * @return: java.util.Map<java.lang.String, java.lang.Double> |
| | | * 返回值key为sensorCode,value为值 |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/11/2 |
| | | */ |
| | | private Map<String, Object> calculate6ParamAvg(List<CityAqi> cityAqiList) { |
| | | Double co = calculateSensorAvg(cityAqiList, "co"); |
| | | Double pm2_5 = calculateSensorAvg(cityAqiList, "pm2_5"); |
| | | Double pm10 = calculateSensorAvg(cityAqiList, "pm10"); |
| | | Double so2 = calculateSensorAvg(cityAqiList, "so2"); |
| | | Double no2 = calculateSensorAvg(cityAqiList, "no2"); |
| | | Double o3 = calculateSensorAvg(cityAqiList, "o3"); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put(Constants.SENSOR_CODE_CO, co); |
| | | result.put(Constants.SENSOR_CODE_NO2, no2); |
| | | result.put(Constants.SENSOR_CODE_SO2, so2); |
| | | result.put(Constants.SENSOR_CODE_O3, o3); |
| | | result.put(Constants.SENSOR_CODE_PM25, pm2_5); |
| | | result.put(Constants.SENSOR_CODE_PM10, pm10); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算因子的平均值 |
| | | * @Param: [cityAqiList, sensor] |
| | | * ,sensor是要计算的因子名称 |
| | | * @return: java.lang.Double |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/11/2 |
| | | */ |
| | | private Double calculateSensorAvg(List<CityAqi> cityAqiList, String sensor) { |
| | | Double sum = 0d; |
| | | int num = 0; |
| | | for (CityAqi cityAqi : cityAqiList) { |
| | | String value = cityAqi.getValue(); |
| | | if (value == null) |
| | | continue; |
| | | Map<String, Object> valueMap = JSON.parseObject(value, Map.class); |
| | | Object sensorValueObject = valueMap.get(sensor); |
| | | if (sensorValueObject == null) |
| | | continue; |
| | | Double sensorValue = Double.valueOf(sensorValueObject.toString()); |
| | | sum = MathUtils.add(sum, sensorValue); |
| | | num++; |
| | | } |
| | | if (num == 0) |
| | | return null; |
| | | Double avg = MathUtils.division(sum, num, 2); |
| | | return avg; |
| | | } |
| | | |
| | | /** |
| | | * @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 |
| | | */ |
| | |
| | | sortByField(ranks, "aqi"); |
| | | //日排名结果 |
| | | Map<String, Object> dayMap = rankByField(ranks, cityCode, "aqi", cityCodes.size()); |
| | | if (ObjectUtils.isEmpty(dayMap)) { |
| | | dayMap.put("rank", null); |
| | | dayMap.put("size", null); |
| | | } |
| | | dayMap.put("aqi", dayMap.remove("value")); |
| | | result.put("day", dayMap); |
| | | |
| | |
| | | sortByField(ranks, "compositeIndexMonth"); |
| | | //月排名结果 |
| | | Map<String, Object> monthMap = rankByField(ranks, cityCode, "compositeIndexMonth", cityCodes.size()); |
| | | if (ObjectUtils.isEmpty(monthMap)) { |
| | | monthMap.put("rank", null); |
| | | monthMap.put("size", null); |
| | | } |
| | | monthMap.put("compositeIndex", monthMap.remove("value")); |
| | | result.put("month", monthMap); |
| | | |
| | | |
| | | //年排名,按累计综指排 |
| | | sortByField(ranks, "compositeIndexYear"); |
| | | //年排名结果 |
| | | sortByField(ranks, "compositeIndexYear"); |
| | | Map<String, Object> yearMap = rankByField(ranks, cityCode, "compositeIndexYear", cityCodes.size()); |
| | | if (ObjectUtils.isEmpty(yearMap)) { |
| | | yearMap.put("rank", null); |
| | | yearMap.put("size", null); |
| | | } |
| | | yearMap.put("compositeIndex", yearMap.remove("value")); |
| | | result.put("year", yearMap); |
| | | |
| | | //时间,昨日 |
| | | result.put("time", DateUtils.dateToDateString(yesterday, DateUtils.yyyy_MM_dd_EN)); |
| | | return result; |
| | | } |
| | | |