| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.moral.api.config.properties.SpecialCitiesProperties; |
| | | import com.moral.api.entity.CityAqi; |
| | | import com.moral.api.entity.CityAqiDaily; |
| | | import com.moral.api.entity.CityAqiMonthly; |
| | | import com.moral.api.entity.CityAqiYearly; |
| | | import com.moral.api.entity.Forecast; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.entity.SysArea; |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.mapper.CityAqiMapper; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.ForecastMapper; |
| | | import com.moral.api.pojo.dto.cityAQI.CityPollutionLevel; |
| | | import com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent; |
| | | import com.moral.api.pojo.form.aqi.AirQualityComparisonForm; |
| | | import com.moral.api.pojo.vo.cityAQI.AirQualityComparisonVO; |
| | | import com.moral.api.service.CityAqiDailyService; |
| | | import com.moral.api.service.CityAqiMonthlyService; |
| | | import com.moral.api.service.CityAqiService; |
| | | import com.moral.api.service.*; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.CityAqiYearlyService; |
| | | 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.pojo.AQI; |
| | |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.DoubleStream; |
| | |
| | | |
| | | @Autowired |
| | | private SpecialCitiesProperties specialCitiesProperties; |
| | | |
| | | @Autowired |
| | | private CityWeatherService cityWeatherService; |
| | | |
| | | @Autowired |
| | | private OrganizationService organizationService; |
| | | |
| | | @Autowired |
| | | private DeviceMapper deviceMapper; |
| | | |
| | | @Autowired |
| | | private HistoryHourlyService historyHourlyService; |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> measuredCompareForecastOfO3(Map<String, Object> params) { |
| | |
| | | return vos; |
| | | } |
| | | |
| | | //环比数据 |
| | | @Override |
| | | public Map<String, Object> momData(Integer regionCode, Integer size, Integer current, Integer orgId) { |
| | | Map<String,Object> result = new HashMap<>(); |
| | | Page<CityAqi> page = new Page<>(current, size); |
| | | Page<CityAqi> pageNext = new Page<>(current+1, size); |
| | | QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>(); |
| | | cityAqiQueryWrapper.eq("city_code",regionCode); |
| | | Integer count = cityAqiMapper.selectCount(cityAqiQueryWrapper); |
| | | if (count==0){ |
| | | return result; |
| | | } |
| | | cityAqiQueryWrapper.orderByDesc("time"); |
| | | Page<CityAqi> resultPage = cityAqiMapper.selectPage(page, cityAqiQueryWrapper); |
| | | List<CityAqi> cityAqiList = resultPage.getRecords(); |
| | | Page<CityAqi> resultPageNext = cityAqiMapper.selectPage(pageNext, cityAqiQueryWrapper); |
| | | List<CityAqi> cityAqiListNext = resultPageNext.getRecords(); |
| | | List<Map<String,Object>> resultList = new ArrayList<>(); |
| | | if (cityAqiList.size()==0){ |
| | | return result; |
| | | } |
| | | for (int i=0; i<cityAqiList.size(); i++) { |
| | | Map<String,Object> resultMap = new HashMap<>(); |
| | | String hour = cityAqiList.get(i).getTime().toString().substring(11,13); |
| | | String previousHour = null; |
| | | Date previousTtime = null; |
| | | Double PM2_5 = null; |
| | | Double PM10 = null; |
| | | Double O3 = null; |
| | | Integer AQI = null; |
| | | String AQIIndex = null; |
| | | Double TVOC = null; |
| | | Double previousPM2_5 = null; |
| | | Double previousPM10 = null; |
| | | Double previousO3 = null; |
| | | String PM2_5_change = null; |
| | | String PM10_change = null; |
| | | String O3_change = null; |
| | | String value = cityAqiList.get(i).getValue(); |
| | | if (!ObjectUtils.isEmpty(value)){ |
| | | JSONObject jsonObject = JSONObject.parseObject(value); |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))){ |
| | | PM2_5 = Double.parseDouble(jsonObject.get("PM2_5").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))){ |
| | | PM10 = Double.parseDouble(jsonObject.get("PM10").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){ |
| | | O3 = Double.parseDouble(jsonObject.get("O3").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("AQI"))){ |
| | | AQI = Integer.parseInt(jsonObject.get("AQI").toString()); |
| | | AQIIndex = AQIUtils.classCodeOfPollutionByAqi(AQI); |
| | | } |
| | | } |
| | | if (i<cityAqiList.size()-1){ |
| | | previousHour = cityAqiList.get(i+1).getTime().toString().substring(11,13); |
| | | previousTtime = cityAqiList.get(i+1).getTime(); |
| | | String previousValue = cityAqiList.get(i+1).getValue(); |
| | | if (!ObjectUtils.isEmpty(previousValue)){ |
| | | JSONObject jsonObject = JSONObject.parseObject(previousValue); |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))){ |
| | | previousPM2_5 = Double.parseDouble(jsonObject.get("PM2_5").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))){ |
| | | previousPM10 = Double.parseDouble(jsonObject.get("PM10").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){ |
| | | previousO3 = Double.parseDouble(jsonObject.get("O3").toString()); |
| | | } |
| | | } |
| | | }else if (cityAqiListNext.size()>0){ |
| | | previousHour = cityAqiListNext.get(0).getTime().toString().substring(11,13); |
| | | previousTtime = cityAqiListNext.get(0).getTime(); |
| | | String previousValue = cityAqiListNext.get(0).getValue(); |
| | | if (!ObjectUtils.isEmpty(previousValue)){ |
| | | JSONObject jsonObject = JSONObject.parseObject(previousValue); |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))){ |
| | | previousPM2_5 = Double.parseDouble(jsonObject.get("PM2_5").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))){ |
| | | previousPM10 = Double.parseDouble(jsonObject.get("PM10").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){ |
| | | previousO3 = Double.parseDouble(jsonObject.get("O3").toString()); |
| | | } |
| | | } |
| | | }else { |
| | | continue; |
| | | } |
| | | if (!ObjectUtils.isEmpty(PM2_5) && !ObjectUtils.isEmpty(previousPM2_5)){ |
| | | Double difference_PM2_5 = PM2_5-previousPM2_5; |
| | | if (difference_PM2_5==0){ |
| | | PM2_5_change = "持平"; |
| | | } |
| | | if (difference_PM2_5>0){ |
| | | PM2_5_change = "上升"+difference_PM2_5; |
| | | } |
| | | if (difference_PM2_5<0){ |
| | | PM2_5_change = "下降"+Math.abs(difference_PM2_5); |
| | | } |
| | | } |
| | | if (!ObjectUtils.isEmpty(PM10) && !ObjectUtils.isEmpty(previousPM10)){ |
| | | Double difference_PM10 = PM10-previousPM10; |
| | | if (difference_PM10==0){ |
| | | PM10_change = "持平"; |
| | | } |
| | | if (difference_PM10>0){ |
| | | PM10_change = "上升"+difference_PM10; |
| | | } |
| | | if (difference_PM10<0){ |
| | | PM10_change = "下降"+Math.abs(difference_PM10); |
| | | } |
| | | } |
| | | if (!ObjectUtils.isEmpty(O3) && !ObjectUtils.isEmpty(previousO3)){ |
| | | Double difference_O3 = O3-previousO3; |
| | | if (difference_O3==0){ |
| | | O3_change = "持平"; |
| | | } |
| | | if (difference_O3>0){ |
| | | O3_change = "上升"+difference_O3; |
| | | } |
| | | if (difference_O3<0){ |
| | | O3_change = "下降"+Math.abs(difference_O3); |
| | | } |
| | | } |
| | | //定义一个集合,存放所有id |
| | | List<Integer> allOrgId = new ArrayList<>(); |
| | | allOrgId.add(orgId); |
| | | //循环集合 |
| | | //所有子组织 |
| | | List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId); |
| | | if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){ |
| | | for (Organization organization:allChildrenOrganization) { |
| | | allOrgId.add(organization.getId()); |
| | | } |
| | | } |
| | | //集合去重 |
| | | List<Integer> allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList()); |
| | | //声明一个list,存放设备Mac |
| | | List<String> deviceMacList = new ArrayList<>(); |
| | | for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) { |
| | | //根据id查询所属设备 |
| | | QueryWrapper<Device> wrapper_device = new QueryWrapper<>(); |
| | | wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgIdWithoutDuplicates); |
| | | List<Device> devices = new ArrayList<>(); |
| | | devices = deviceMapper.selectList(wrapper_device); |
| | | if (devices.size()>0){ |
| | | for (Device device:devices) { |
| | | String deviceMac = device.getMac(); |
| | | deviceMacList.add(deviceMac); |
| | | } |
| | | }else { |
| | | continue; |
| | | } |
| | | } |
| | | //获取时间 |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String time = df.format(cityAqiList.get(i).getTime()); |
| | | if (deviceMacList.size()>0){ |
| | | List<HistoryHourly> valueByMacs = historyHourlyService.getValueByMacs(deviceMacList, time); |
| | | if (valueByMacs.size()>0){ |
| | | for (HistoryHourly historyHourly:valueByMacs) { |
| | | String value_historyHourly = historyHourly.getValue(); |
| | | if (!ObjectUtils.isEmpty(value_historyHourly)){ |
| | | JSONObject jsonObject = JSONObject.parseObject(value_historyHourly); |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("a99054"))){ |
| | | TVOC = Double.parseDouble(jsonObject.get("a99054").toString()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | resultMap.put("time",time.substring(0,13)); |
| | | resultMap.put("title","【"+hour+"时数据提醒】"); |
| | | String PM2_5_string = subZeroAndDot(PM2_5.toString()); |
| | | String PM10_string = subZeroAndDot(PM10.toString()); |
| | | String O3_string = subZeroAndDot(O3.toString()); |
| | | String PM2_5_change_string = subZeroAndDot(PM2_5_change.toString()); |
| | | String PM10_change_string = subZeroAndDot(PM10_change.toString()); |
| | | String O3_change_string = subZeroAndDot(O3_change.toString()); |
| | | resultMap.put("info",hour+"时,我市AQI:"+AQI+",等级:"+AQIIndex+"。与"+previousHour+"时相比,PM10:"+PM10_string+"微克/立方米,"+PM10_change_string+";PM2.5:"+PM2_5_string+"微克/立方米,"+PM2_5_change_string+";TVOC总量:"+String.format("%.2f", TVOC)+"毫克/立方米;O3:"+O3_string+"微克/立方米,"+O3_change_string+"。"); |
| | | QueryWrapper<CityWeather> cityWeatherQueryWrapper = new QueryWrapper<>(); |
| | | cityWeatherQueryWrapper.eq("city_code",regionCode); |
| | | cityWeatherQueryWrapper.eq("time",cityAqiList.get(i).getTime()); |
| | | CityWeather cityWeather = cityWeatherService.getOne(cityWeatherQueryWrapper); |
| | | Integer temp = null; |
| | | Integer humidity = null; |
| | | String windDir = ""; |
| | | Integer windScale = null; |
| | | if (!ObjectUtils.isEmpty(cityWeather)){ |
| | | String cityWeatherValue = cityWeather.getValue(); |
| | | if (!ObjectUtils.isEmpty(cityWeatherValue)){ |
| | | JSONObject jsonObject = JSONObject.parseObject(cityWeatherValue); |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("temp"))){ |
| | | temp = Integer.parseInt(jsonObject.get("temp").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("humidity"))){ |
| | | humidity = Integer.parseInt(jsonObject.get("humidity").toString()); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("windDir"))){ |
| | | windDir = jsonObject.get("windDir").toString(); |
| | | } |
| | | if (!ObjectUtils.isEmpty(jsonObject.get("windScale"))){ |
| | | windScale = Integer.parseInt(jsonObject.get("windScale").toString()); |
| | | } |
| | | } |
| | | } |
| | | String temp_string = ""; |
| | | if (temp!=null){ |
| | | temp_string = temp.toString(); |
| | | } |
| | | String humidity_string = ""; |
| | | if (humidity!=null){ |
| | | humidity_string = humidity.toString(); |
| | | } |
| | | String windScale_string = ""; |
| | | if (windScale!=null){ |
| | | windScale_string = windScale.toString(); |
| | | } |
| | | resultMap.put("weatherCondition","当前天气状况:温度"+temp_string+"℃,湿度"+humidity_string+"%,"+windDir+windScale_string+"级。"); |
| | | resultMap.put("auther","(七星瓢虫专家组)"); |
| | | resultList.add(resultMap); |
| | | } |
| | | result.put("totel",count); |
| | | result.put("resultList",resultList); |
| | | result.put("current",current); |
| | | int totalPageNumber = count/size; |
| | | if(count%size != 0){ |
| | | totalPageNumber += 1; |
| | | } |
| | | result.put("totalPageNumber",totalPageNumber); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算6参和综合指数对比的百分比 |
| | | * @Param: [data, comparisonData] |
| | |
| | | Double avg = MathUtils.division(sum, num, 2); |
| | | return avg; |
| | | } |
| | | |
| | | /** |
| | | * 使用java正则表达式去掉多余的.与0 |
| | | * @param s |
| | | * @return |
| | | */ |
| | | public static String subZeroAndDot(String s){ |
| | | if(s.indexOf(".") > 0){ |
| | | s = s.replaceAll("0+?$", "");//去掉多余的0 |
| | | s = s.replaceAll("[.]$", "");//如最后一位是.则去掉 |
| | | } |
| | | return s; |
| | | } |
| | | } |