| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.CityAqi; |
| | | import com.moral.api.entity.CityAqiDaily; |
| | | 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.CityAqiService; |
| | | 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; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.DoubleStream; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private OrganizationService organizationService; |
| | | |
| | | @Autowired |
| | | private SysAreaService sysAreaService; |
| | | |
| | | @Autowired |
| | | private CityAqiDailyService cityAqiDailyService; |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> measuredCompareForecastOfO3(Map<String, Object> params) { |
| | |
| | | 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 |
| | | */ |
| | |
| | | redisTemplate.opsForHash().put(RedisConstants.CITY_AQI, regionCode, value); |
| | | return JSON.parseObject(value, Map.class); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> provincialRanking(Integer organizationId) { |
| | | //结果集 |
| | | Map<String, Object> result = new HashMap<>(); |
| | | |
| | | Date now = new Date(); |
| | | //昨日 |
| | | 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); |
| | | |
| | | //获取省,市code |
| | | Organization organization = organizationService.getById(organizationId); |
| | | Integer provinceCode = organization.getProvinceCode(); |
| | | Integer cityCode = organization.getCityCode(); |
| | | //获取省内所有city_code |
| | | QueryWrapper<SysArea> wrapper = new QueryWrapper<>(); |
| | | wrapper.select("area_code").eq("parent_code", provinceCode); |
| | | List<Object> cityCodes = sysAreaService.listObjs(wrapper); |
| | | |
| | | List<Map<String, Object>> ranks = new ArrayList<>(); |
| | | for (Object code : cityCodes) { |
| | | Map<String, Object> rankMap = new HashMap<>(); |
| | | rankMap.put("cityCode", code); |
| | | QueryWrapper<CityAqiDaily> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.select("value").eq("city_code", code).eq("time", dateString); |
| | | |
| | | //1.昨日数据 |
| | | CityAqiDaily one = cityAqiDailyService.getOne(queryWrapper); |
| | | if (!ObjectUtils.isEmpty(one)) { |
| | | String value = one.getValue(); |
| | | Map<String, Object> valueMap = JSONObject.parseObject(value, Map.class); |
| | | rankMap.put("aqi", valueMap.get("aqi")); |
| | | } |
| | | |
| | | |
| | | //2.本月累计综合指数计算,截止到昨日 |
| | | queryWrapper.clear(); |
| | | //开始时间:本月1号,结束时间:昨日 |
| | | Date start = DateUtils.addMonths(DateUtils.getFirstDayOfLastMonth(), 1); |
| | | queryWrapper.select("value").eq("city_code", code).ge("time", start).le("time", yesterday); |
| | | List<CityAqiDaily> listMonth = cityAqiDailyService.list(queryWrapper); |
| | | OptionalDouble averageMonth = listMonth.parallelStream().flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Double compositeIndex = Double.parseDouble(dataValue.get("compositeIndex").toString()); |
| | | return DoubleStream.of(compositeIndex); |
| | | }).average(); |
| | | if (averageMonth.isPresent()) { |
| | | //银行家算法修约 |
| | | double compositeIndexAvgMonth = AmendUtils.sciCal(averageMonth.getAsDouble(), 2); |
| | | rankMap.put("compositeIndexMonth", compositeIndexAvgMonth); |
| | | } |
| | | |
| | | //3.本年累计综合指数计算,截止到昨日 |
| | | queryWrapper.clear(); |
| | | //开始时间:本年1.1号,结束时间:昨日 |
| | | String yearStart = DateUtils.dateToDateString(now, DateUtils.yyyy); |
| | | queryWrapper.select("value").eq("city_code", code).ge("time", yearStart).le("time", yesterday); |
| | | List<CityAqiDaily> listYear = cityAqiDailyService.list(queryWrapper); |
| | | OptionalDouble averageYear = listYear.parallelStream().flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Double compositeIndex = Double.parseDouble(dataValue.get("compositeIndex").toString()); |
| | | return DoubleStream.of(compositeIndex); |
| | | }).average(); |
| | | if (averageYear.isPresent()) { |
| | | //银行家算法修约 |
| | | double compositeIndexAvgYear = AmendUtils.sciCal(averageYear.getAsDouble(), 2); |
| | | rankMap.put("compositeIndexYear", compositeIndexAvgYear); |
| | | } |
| | | ranks.add(rankMap); |
| | | } |
| | | |
| | | //日排名,按aqi排序 |
| | | ranks.removeIf(o -> o.get("aqi") == null); |
| | | sortByField(ranks, "aqi"); |
| | | //日排名结果 |
| | | Map<String, Object> dayMap = rankByField(ranks, cityCode, "aqi", cityCodes.size()); |
| | | dayMap.put("aqi", dayMap.remove("value")); |
| | | result.put("day", dayMap); |
| | | |
| | | //月排名,按累计综指排 |
| | | ranks.removeIf(o -> o.get("compositeIndexMonth") == null); |
| | | sortByField(ranks, "compositeIndexMonth"); |
| | | //月排名结果 |
| | | Map<String, Object> monthMap = rankByField(ranks, cityCode, "compositeIndexMonth", cityCodes.size()); |
| | | 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()); |
| | | yearMap.put("compositeIndex", yearMap.remove("value")); |
| | | result.put("year", yearMap); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | //按某字段排序 |
| | | private void sortByField(List<Map<String, Object>> list, String sortField) { |
| | | list.sort((o1, o2) -> { |
| | | double v1 = Double.parseDouble(o1.get(sortField).toString()); |
| | | double v2 = Double.parseDouble(o2.get(sortField).toString()); |
| | | if (v1 > v2) { |
| | | return -1; |
| | | } else if (v1 < v2) { |
| | | return 1; |
| | | } |
| | | return 0; |
| | | }); |
| | | } |
| | | |
| | | //排名 |
| | | private Map<String, Object> rankByField(List<Map<String, Object>> list, Integer cityCode, String rankField, Integer size) { |
| | | Map<String, Object> result = new HashMap<>(); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | Map<String, Object> map = list.get(i); |
| | | if (cityCode == (int) map.get("cityCode")) { |
| | | int rank = i + 1; |
| | | result.put("rank", rank + "/" + size); |
| | | Object value = map.get(rankField); |
| | | result.put("value", value); |
| | | break; |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | } |