package com.moral.api.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.moral.api.entity.Device; import com.moral.api.entity.GovMonitorPoint; import com.moral.api.entity.HistoryHourly; import com.moral.api.entity.Organization; import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.GovMonitorPointMapper; import com.moral.api.service.GovMonitorPointService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.service.OrganizationService; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; import com.moral.util.AQIUtils; import com.moral.util.RegionCodeUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author moral * @since 2021-09-16 */ @Service public class GovMonitorPointServiceImpl extends ServiceImpl implements GovMonitorPointService { @Autowired GovMonitorPointMapper govMonitorPointMapper; @Autowired RedisTemplate redisTemplate; @Resource private OrganizationService organizationService; @Autowired(required = false) private DeviceMapper deviceMapper; @Resource private HistoryHourlyServiceImpl historyHourlyService; @Override public List queryGovMonitorPointAndDataByRegionCode(Integer regionCode,String sensorCode) { String regionCodeStr = RegionCodeUtils.regionCodeConvertToName(regionCode); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(regionCodeStr,regionCode); wrapper.eq("is_delete", Constants.NOT_DELETE); wrapper.select("guid","name","longitude","latitude","station_level"); List govMonitorPoints = govMonitorPointMapper.selectList(wrapper); for (GovMonitorPoint govMonitorPoint : govMonitorPoints) { Object data = redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, govMonitorPoint.getGuid()); Map dataMap = (Map) data; if(data!=null&&dataMap.get(sensorCode)!=null) govMonitorPoint.setData(String.valueOf(dataMap.get(sensorCode))); } return govMonitorPoints; } @Override public List selectGovMonitorPointsByOrgid(Map map) { //根据组织id获取子组织 List organizations = organizationService.getChildrenOrganizationsById(Integer.parseInt(map.get("organization_id").toString())); Set organization_ids = organizations.stream().map(organization -> organization.getId()).collect(Collectors.toSet()); organization_ids.add(Integer.parseInt(map.get("organization_id").toString())); //先获取组织下所有设备 QueryWrapper wrapper_device = new QueryWrapper<>(); wrapper_device.in("organization_id",organization_ids).eq("is_delete",Constants.NOT_DELETE); List devices = deviceMapper.selectList(wrapper_device); //用集合存放所有设备的id Set guids = devices.stream().map(device -> device.getGuid()).collect(Collectors.toSet()); //获取所有政府站点信息 QueryWrapper wrapper_govMonitorPoint = new QueryWrapper<>(); wrapper_govMonitorPoint.eq("is_delete",Constants.NOT_DELETE).in("guid",guids); List govMonitorPointList = govMonitorPointMapper.selectList(wrapper_govMonitorPoint); return govMonitorPointList; } @Override public Map queryGovMonitorPointHoutlyDatyByGuidsAndOrgid(Map map) { String[] guids = map.get("guids").toString().split(","); Integer orgId = Integer.parseInt(map.get("organization_id").toString()); String date = map.get("date").toString(); String startTime = date+" 00:00:00"; String endTime = date+" 23:00:01"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM try { Date startDate = simpleDateFormat.parse(startTime); Date endDate = simpleDateFormat.parse(endTime); Map resultMap = new HashMap<>(); List govMonitorPointList = new ArrayList(); //声明一个国控站点,存放最后的平均值信息 GovMonitorPoint govMonitorPoint_avg = new GovMonitorPoint(); govMonitorPoint_avg.setId(0); govMonitorPoint_avg.setName(""); govMonitorPoint_avg.setGuid(""); List> data = new ArrayList<>(); Map hour_1 = new HashMap<>(); Map hour_2 = new HashMap<>(); Map hour_3 = new HashMap<>(); Map hour_4 = new HashMap<>(); Map hour_5 = new HashMap<>(); Map hour_6 = new HashMap<>(); Map hour_7 = new HashMap<>(); Map hour_8 = new HashMap<>(); Map hour_9 = new HashMap<>(); Map hour_10 = new HashMap<>(); Map hour_11 = new HashMap<>(); Map hour_12 = new HashMap<>(); Map hour_13 = new HashMap<>(); Map hour_14 = new HashMap<>(); Map hour_15 = new HashMap<>(); Map hour_16 = new HashMap<>(); Map hour_17 = new HashMap<>(); Map hour_18 = new HashMap<>(); Map hour_19 = new HashMap<>(); Map hour_20 = new HashMap<>(); Map hour_21 = new HashMap<>(); Map hour_22 = new HashMap<>(); Map hour_23 = new HashMap<>(); Map hour_24 = new HashMap<>(); Map hour_AVG = new HashMap<>(); for (String guid:guids) { //获取政府站点信息 QueryWrapper wrapper_govMonitorPoint = new QueryWrapper<>(); wrapper_govMonitorPoint.eq("is_delete",Constants.NOT_DELETE).eq("guid",guid); GovMonitorPoint govMonitorPoint = govMonitorPointMapper.selectOne(wrapper_govMonitorPoint); if (!ObjectUtils.isEmpty(govMonitorPoint)){ govMonitorPointList.add(govMonitorPoint); }else { continue; } int govMonitorPointId = govMonitorPoint.getId(); QueryWrapper wrapper_device = new QueryWrapper<>(); wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgId).eq("guid",guid); List devices = deviceMapper.selectList(wrapper_device); List macs = devices.stream().map(device -> device.getMac()).collect(Collectors.toList()); List> oneGovMonitorPointDevicesValue = new ArrayList(); for (String mac:macs) { List valueByMacAndTime = historyHourlyService.getValueByMacAndTime(mac, startDate, endDate); oneGovMonitorPointDevicesValue.add(valueByMacAndTime); } List PM2_5List_daliy = new ArrayList<>(); List PM10List_daliy = new ArrayList<>(); List SO2List_daliy = new ArrayList<>(); List NO2List_daliy = new ArrayList<>(); List COList_daliy = new ArrayList<>(); List O3_1hList_daliy = new ArrayList<>(); Map O3_1hMap = new HashMap<>(); for (int i=0; i<24; i++){ //声明一个Map,存放当前政府站点的六参和臭氧八小时滑动平均值数据 Map oneGovMonitorPointOneHourData = new HashMap<>(); String time_hourly = ""; if (i<10){ time_hourly = date+" 0"+i+":00:00"; }else { time_hourly = date+" "+i+":00:00"; } List PM2_5List = new ArrayList<>(); List PM10List = new ArrayList<>(); List SO2List = new ArrayList<>(); List NO2List = new ArrayList<>(); List COList = new ArrayList<>(); List O3_1hList = new ArrayList<>(); for (List valueByMacAndTime:oneGovMonitorPointDevicesValue) { for (HistoryHourly historyHourly:valueByMacAndTime) { if (simpleDateFormat.format(historyHourly.getTime()).equals(time_hourly)){ JSONObject value = JSONObject.parseObject(historyHourly.getValue()); if (!ObjectUtils.isEmpty(value.get("a34004"))){ PM2_5List.add(Double.parseDouble(value.get("a34004").toString())); } if (!ObjectUtils.isEmpty(value.get("a34002"))){ PM10List.add(Double.parseDouble(value.get("a34002").toString())); } if (!ObjectUtils.isEmpty(value.get("a21026"))){ SO2List.add(Double.parseDouble(value.get("a21026").toString())); } if (!ObjectUtils.isEmpty(value.get("a21004"))){ NO2List.add(Double.parseDouble(value.get("a21004").toString())); } if (!ObjectUtils.isEmpty(value.get("a21005"))){ COList.add(Double.parseDouble(value.get("a21005").toString())); } if (!ObjectUtils.isEmpty(value.get("a05024"))){ O3_1hList.add(Double.parseDouble(value.get("a05024").toString())); } } } } Double PM2_5Avg = (double)Math.round(PM2_5List.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); Double PM10Avg = (double)Math.round(PM10List.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); Double SO2Avg = (double)Math.round(SO2List.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); Double NO2Avg = (double)Math.round(NO2List.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); Double COAvg = new BigDecimal(COList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue(); Double O3_1hAvg = (double)Math.round(O3_1hList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); if (!ObjectUtils.isEmpty(PM2_5Avg)){ PM2_5List_daliy.add(PM2_5Avg); oneGovMonitorPointOneHourData.put("a34004_"+govMonitorPointId,PM2_5Avg); }else { oneGovMonitorPointOneHourData.put("a34004_"+govMonitorPointId,null); } if (!ObjectUtils.isEmpty(PM10Avg)){ PM10List_daliy.add(PM10Avg); oneGovMonitorPointOneHourData.put("a34002_"+govMonitorPointId,PM10Avg); }else { oneGovMonitorPointOneHourData.put("a34002_"+govMonitorPointId,null); } if (!ObjectUtils.isEmpty(SO2Avg)){ SO2List_daliy.add(SO2Avg); oneGovMonitorPointOneHourData.put("a21026_"+govMonitorPointId,SO2Avg); }else { oneGovMonitorPointOneHourData.put("a21026_"+govMonitorPointId,null); } if (!ObjectUtils.isEmpty(NO2Avg)){ NO2List_daliy.add(NO2Avg); oneGovMonitorPointOneHourData.put("a21004_"+govMonitorPointId,NO2Avg); }else { oneGovMonitorPointOneHourData.put("a21004_"+govMonitorPointId,null); } if (!ObjectUtils.isEmpty(COAvg)){ COList_daliy.add(COAvg); oneGovMonitorPointOneHourData.put("a21005_"+govMonitorPointId,COAvg); }else { oneGovMonitorPointOneHourData.put("a21005_"+govMonitorPointId,null); } if (!ObjectUtils.isEmpty(O3_1hAvg)){ O3_1hList_daliy.add(O3_1hAvg); oneGovMonitorPointOneHourData.put("a05024_"+govMonitorPointId,O3_1hAvg); O3_1hMap.put(i,O3_1hAvg); }else { oneGovMonitorPointOneHourData.put("a05024_"+govMonitorPointId,null); } Double O3_8h = null; if (i>=7){ List O3_8hList = new ArrayList<>(); for (Integer key:O3_1hMap.keySet()){ if (i-8aDouble).summaryStatistics().getAverage()); } oneGovMonitorPointOneHourData.put("O3_8h_"+govMonitorPointId,O3_8h); switch (i){ case 0:hour_1.putAll(oneGovMonitorPointOneHourData); break; case 1:hour_2.putAll(oneGovMonitorPointOneHourData); break; case 2:hour_3.putAll(oneGovMonitorPointOneHourData); break; case 3:hour_4.putAll(oneGovMonitorPointOneHourData); break; case 4:hour_5.putAll(oneGovMonitorPointOneHourData); break; case 5:hour_6.putAll(oneGovMonitorPointOneHourData); break; case 6:hour_7.putAll(oneGovMonitorPointOneHourData); break; case 7:hour_8.putAll(oneGovMonitorPointOneHourData); break; case 8:hour_9.putAll(oneGovMonitorPointOneHourData); break; case 9:hour_10.putAll(oneGovMonitorPointOneHourData); break; case 10:hour_11.putAll(oneGovMonitorPointOneHourData); break; case 11:hour_12.putAll(oneGovMonitorPointOneHourData); break; case 12:hour_13.putAll(oneGovMonitorPointOneHourData); break; case 13:hour_14.putAll(oneGovMonitorPointOneHourData); break; case 14:hour_15.putAll(oneGovMonitorPointOneHourData); break; case 15:hour_16.putAll(oneGovMonitorPointOneHourData); break; case 16:hour_17.putAll(oneGovMonitorPointOneHourData); break; case 17:hour_18.putAll(oneGovMonitorPointOneHourData); break; case 18:hour_19.putAll(oneGovMonitorPointOneHourData); break; case 19:hour_20.putAll(oneGovMonitorPointOneHourData); break; case 20:hour_21.putAll(oneGovMonitorPointOneHourData); break; case 21:hour_22.putAll(oneGovMonitorPointOneHourData); break; case 22:hour_23.putAll(oneGovMonitorPointOneHourData); break; case 23:hour_24.putAll(oneGovMonitorPointOneHourData); break; default: break; } } } Map> replaceDataMap = new HashMap<>(); replaceDataMap.put(0,hour_1); replaceDataMap.put(1,hour_2); replaceDataMap.put(2,hour_3); replaceDataMap.put(3,hour_4); replaceDataMap.put(4,hour_5); replaceDataMap.put(5,hour_6); replaceDataMap.put(6,hour_7); replaceDataMap.put(7,hour_8); replaceDataMap.put(8,hour_9); replaceDataMap.put(9,hour_10); replaceDataMap.put(10,hour_11); replaceDataMap.put(11,hour_12); replaceDataMap.put(12,hour_13); replaceDataMap.put(13,hour_14); replaceDataMap.put(14,hour_15); replaceDataMap.put(15,hour_16); replaceDataMap.put(16,hour_17); replaceDataMap.put(17,hour_18); replaceDataMap.put(18,hour_19); replaceDataMap.put(19,hour_20); replaceDataMap.put(20,hour_21); replaceDataMap.put(21,hour_22); replaceDataMap.put(22,hour_23); replaceDataMap.put(23,hour_24); replaceDataMap.put(24,hour_AVG); for (int i=0;i<25;i++){ List PM2_5AllList = new ArrayList<>(); List PM10AllList = new ArrayList<>(); List SO2AllList = new ArrayList<>(); List NO2AllList = new ArrayList<>(); List COAllList = new ArrayList<>(); List O3_1hAllList = new ArrayList<>(); List O3_8hAllList = new ArrayList<>(); for (String guid:guids) { //获取政府站点信息 QueryWrapper wrapper_govMonitorPoint = new QueryWrapper<>(); wrapper_govMonitorPoint.eq("is_delete",Constants.NOT_DELETE).eq("guid",guid); GovMonitorPoint govMonitorPoint = govMonitorPointMapper.selectOne(wrapper_govMonitorPoint); if (ObjectUtils.isEmpty(govMonitorPoint)){ continue; } int govMonitorPointId = govMonitorPoint.getId(); Map replaceMap = new HashMap<>(); replaceMap = replaceDataMap.get(i); if (!ObjectUtils.isEmpty(replaceMap.get("a34004_"+govMonitorPointId))){ PM2_5AllList.add((double)replaceMap.get("a34004_"+govMonitorPointId)); } if (!ObjectUtils.isEmpty(replaceMap.get("a34002_"+govMonitorPointId))){ PM10AllList.add((double)replaceMap.get("a34002_"+govMonitorPointId)); } if (!ObjectUtils.isEmpty(replaceMap.get("a21026_"+govMonitorPointId))){ SO2AllList.add((double)replaceMap.get("a21026_"+govMonitorPointId)); } if (!ObjectUtils.isEmpty(replaceMap.get("a21004_"+govMonitorPointId))){ NO2AllList.add((double)replaceMap.get("a21004_"+govMonitorPointId)); } if (!ObjectUtils.isEmpty(replaceMap.get("a21005_"+govMonitorPointId))){ COAllList.add((double)replaceMap.get("a21005_"+govMonitorPointId)); } if (!ObjectUtils.isEmpty(replaceMap.get("a05024_"+govMonitorPointId))){ O3_1hAllList.add((double)replaceMap.get("a05024_"+govMonitorPointId)); } if (!ObjectUtils.isEmpty(replaceMap.get("O3_8h_"+govMonitorPointId))){ O3_8hAllList.add((double)replaceMap.get("O3_8h_"+govMonitorPointId)); } } Double PM2_5 = null; Double PM10 = null; Double SO2 = null; Double NO2 = null; Double CO = null; Double O3_1h = null; Double O3_8h = null; if (PM2_5AllList.size()>0){ PM2_5 = (double)Math.round(PM2_5AllList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (PM10AllList.size()>0){ PM10 = (double)Math.round(PM10AllList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (SO2AllList.size()>0){ SO2 = (double)Math.round(SO2AllList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (NO2AllList.size()>0){ NO2 = (double)Math.round(NO2AllList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (COAllList.size()>0){ CO = new BigDecimal(COAllList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue(); } if (O3_1hAllList.size()>0){ O3_1h = (double)Math.round(O3_1hAllList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (O3_8hAllList.size()>0){ O3_8h = (double)Math.round(O3_8hAllList.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } Map AQIMap = new HashMap<>(); if (!ObjectUtils.isEmpty(PM2_5)){ AQIMap.put("a34004",PM2_5); } if (!ObjectUtils.isEmpty(PM2_5)){ AQIMap.put("a34002",PM10); } if (!ObjectUtils.isEmpty(PM2_5)){ AQIMap.put("a21026",SO2); } if (!ObjectUtils.isEmpty(PM2_5)){ AQIMap.put("a21004",NO2); } if (!ObjectUtils.isEmpty(PM2_5)){ AQIMap.put("a21005",CO); } if (!ObjectUtils.isEmpty(PM2_5)){ AQIMap.put("a05024",O3_1h); } Map AQIResultMap = new HashMap<>(); if (AQIMap.size()!=0){ AQIResultMap = AQIUtils.hourlyAqi_pollutant(AQIMap); } Map avgMap = new HashMap<>(); avgMap.put("a34004_"+"0",PM2_5); avgMap.put("a34002_"+"0",PM10); avgMap.put("a21026_"+"0",SO2); avgMap.put("a21004_"+"0",NO2); 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")); }else { avgMap.put("aqi",""); } if (!ObjectUtils.isEmpty(AQIResultMap.get("aqi"))){ String pollutant = AQIResultMap.get("pollutant").toString(); pollutant = pollutant.replace("[",""); pollutant = pollutant.replace("]",""); avgMap.put("pollutant",pollutant); }else { avgMap.put("aqi",""); } //System.out.println("==============="+pollutant); //avgMap.put("pullutant",StringUtils.join(aqi_pollutantMap.get("pollutant").toArray(), separator)); switch (i){ case 0:hour_1.putAll(avgMap); break; case 1:hour_2.putAll(avgMap); break; case 2:hour_3.putAll(avgMap); break; case 3:hour_4.putAll(avgMap); break; case 4:hour_5.putAll(avgMap); break; case 5:hour_6.putAll(avgMap); break; case 6:hour_7.putAll(avgMap); break; case 7:hour_8.putAll(avgMap); break; case 8:hour_9.putAll(avgMap); break; case 9:hour_10.putAll(avgMap); break; case 10:hour_11.putAll(avgMap); break; case 11:hour_12.putAll(avgMap); break; case 12:hour_13.putAll(avgMap); break; case 13:hour_14.putAll(avgMap); break; case 14:hour_15.putAll(avgMap); break; case 15:hour_16.putAll(avgMap); break; case 16:hour_17.putAll(avgMap); break; case 17:hour_18.putAll(avgMap); break; case 18:hour_19.putAll(avgMap); break; case 19:hour_20.putAll(avgMap); break; case 20:hour_21.putAll(avgMap); break; case 21:hour_22.putAll(avgMap); break; case 22:hour_23.putAll(avgMap); break; case 23:hour_24.putAll(avgMap); break; default: break; } } hour_1.put("time","1时"); hour_2.put("time","2时"); hour_3.put("time","3时"); hour_4.put("time","4时"); hour_5.put("time","5时"); hour_6.put("time","6时"); hour_7.put("time","7时"); hour_8.put("time","8时"); hour_9.put("time","9时"); hour_10.put("time","10时"); hour_11.put("time","11时"); hour_12.put("time","12时"); hour_13.put("time","13时"); hour_14.put("time","14时"); hour_15.put("time","15时"); hour_16.put("time","16时"); hour_17.put("time","17时"); hour_18.put("time","18时"); hour_19.put("time","19时"); hour_20.put("time","20时"); hour_21.put("time","21时"); hour_22.put("time","22时"); hour_23.put("time","23时"); hour_24.put("time","0时"); hour_AVG.put("time","avg"); data.add(hour_1); data.add(hour_2); data.add(hour_3); data.add(hour_4); data.add(hour_5); data.add(hour_6); data.add(hour_7); data.add(hour_8); data.add(hour_9); data.add(hour_10); data.add(hour_11); data.add(hour_12); data.add(hour_13); data.add(hour_14); data.add(hour_15); data.add(hour_16); data.add(hour_17); data.add(hour_18); data.add(hour_19); data.add(hour_20); data.add(hour_21); data.add(hour_22); data.add(hour_23); data.add(hour_24); resultMap.put("title",date+"月报"); resultMap.put("data",data); resultMap.put("govMonitorPoint",govMonitorPointList); return resultMap; }catch (Exception e){ e.printStackTrace(); } return null; } }