| | |
| | | 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.RedisConstants; |
| | | import com.moral.util.AQIUtils; |
| | | import com.moral.util.AmendUtils; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | 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) { |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |