package com.moral.api.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.moral.api.dto.MonitoringStationDTO; import com.moral.api.entity.*; import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.HistoryMonthlyMapper; import com.moral.api.mapper.MonitorPointMapper; import com.moral.api.mapper.SensorMapper; import com.moral.api.pojo.form.device.MonitorPointQueryForm; import com.moral.api.service.DeviceService; import com.moral.api.service.HistoryDailyService; import com.moral.api.service.HistoryHourlyService; import com.moral.api.service.HistoryMonthlyService; import com.moral.api.service.MonitorPointService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.service.OrganizationService; import com.moral.constant.Constants; import com.moral.util.ComprehensiveIndexUtils; import com.moral.util.DateUtils; import com.moral.util.RegionCodeUtils; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import springfox.documentation.schema.Entry; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author moral * @since 2021-07-01 */ @Service public class MonitorPointServiceImpl extends ServiceImpl implements MonitorPointService { @Autowired MonitorPointMapper monitorPointMapper; @Autowired OrganizationService organizationService; @Autowired DeviceService deviceService; @Autowired DeviceMapper deviceMapper; @Autowired SensorMapper sensorMapper; @Autowired HistoryHourlyService historyHourlyService; @Autowired HistoryDailyService historyDailyService; @Autowired HistoryMonthlyService historyMonthlyService; @Autowired HistoryMonthlyMapper historyMonthlyMapper; @Override public List queryByOrgIdAndRegionCode(MonitorPointQueryForm form) { //取参 Integer organizationId = form.getOrganizationId(); Integer regionCode = form.getRegionCode(); String region = null; if (regionCode != null && organizationId!=24) { region = RegionCodeUtils.regionCodeConvertToName(regionCode); } // if (regionCode != null) { // region = RegionCodeUtils.regionCodeConvertToName(regionCode); // } /* //查询子组织 List childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId); for (Organization organization : childrenOrganization) { organizationIds.add(organization.getId()); } organizationIds.add(organizationId);*/ //查询站点 QueryWrapper queryMonitorPointsWrapper = new QueryWrapper<>(); //如果region不为空,就查询当前组织,所选城市下所有站点及设备信息 //如果region为空,则查询当前组织下所有的站点和设备 List organizationIds = deviceMapper.deviceOrgIdList(organizationId); if (region != null){ queryMonitorPointsWrapper.eq(region, regionCode); } queryMonitorPointsWrapper.in("organization_id", organizationIds); queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE); List monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper); //查询站点对应的设备 for (MonitorPoint monitorPoint : monitorPoints) { List devices = deviceService.getDevicesByMonitorPointId(monitorPoint.getId()); monitorPoint.setDevices(devices); } return monitorPoints; } @Override public List queryAllMonitorPoints(Integer organizationId) { //查询子组织 List childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId); List organizationIds = new ArrayList<>(); for (Organization organization : childrenOrganization) { organizationIds.add(organization.getId()); } organizationIds.add(organizationId); //查询站点 QueryWrapper queryMonitorPointsWrapper = new QueryWrapper<>(); queryMonitorPointsWrapper.select("id","name"); queryMonitorPointsWrapper.in("organization_id",organizationIds); queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE); List monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper); return monitorPoints; } @Override public List> getHourlyDataByMonitorPoint(Map map) { int monitorPointId = Integer.parseInt(map.get("monitorPointId").toString()); QueryWrapper monitorPointQueryWrapper = new QueryWrapper<>(); monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE); monitorPointQueryWrapper.eq("id",monitorPointId); MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper); QueryWrapper deviceQueryWrapper = new QueryWrapper<>(); deviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); deviceQueryWrapper.eq("monitor_point_id",monitorPointId); List devices = deviceMapper.selectList(deviceQueryWrapper); if (devices.size()<=0){ return null; } String[] sensors = map.remove("sensors").toString().split(","); List sensorsList = Arrays.asList(sensors); String startTime = map.get("startTime").toString(); String endTime = map.get("endTime").toString(); Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); List> resultList = new ArrayList<>(); for (Device device:devices) { List hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(map.get("startTime").toString(), DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(map.get("endTime").toString(), DateUtils.yyyy_MM_dd_HH_EN)); for (String sensor:sensorsList) { Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json timeValueMap.put("监测站点",monitorPoint.getName()); timeValueMap.put("name",device.getName()); QueryWrapper sensorQueryWrapper = new QueryWrapper<>(); sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE); sensorQueryWrapper.eq("code",sensor); Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper); timeValueMap.put("sensor",sensorEntity.getName()); for (HistoryHourly historyHourly : hourlies) { Date time = historyHourly.getTime(); String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH"); String value = historyHourly.getValue(); JSONObject jsonObject = JSONObject.parseObject(value); if (jsonObject.containsKey(sensor)){ timeValueMap.put(dateStr, jsonObject.get(sensor).toString()); } } //补上无数据时间 Date middleDate = startDate; while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){ if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null) timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN), null); middleDate = DateUtils.addHours(middleDate,1); } resultList.add(timeValueMap); } } return resultList; } /** * 监测站点数据导出 * * @param params * @return */ @Override public List> getHourlyDataDataV3(Map params) { List macs = (List) params.remove("macs"); List times = (List) params.remove("times"); String type = params.get("type").toString(); String startTime = times.get(0); String endTime = times.get(1); // String[] macs = params.remove("macs").toString().split(","); // List macsList = Arrays.asList(macs); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("is_delete",Constants.NOT_DELETE); queryWrapper.in("mac", macs); List devices = deviceMapper.selectList(queryWrapper); if (devices.size()<=0){ return null; } String[] sensors = params.remove("sensors").toString().split(","); List sensorsList = Arrays.asList(sensors); List> resultList = new ArrayList<>(); // List sensorsList = (List) params.remove("sensors"); // String startTime = params.get("startTime").toString(); // String endTime = params.get("endTime").toString(); if (type.equals("hours")){ Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); for (Device device:devices) { List hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN)); for (String sensor:sensorsList) { Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json QueryWrapper monitorPointQueryWrapper = new QueryWrapper<>(); monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE); monitorPointQueryWrapper.eq("id",device.getMonitorPointId()); MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper); timeValueMap.put("监测站点",monitorPoint.getName()); timeValueMap.put("name",device.getName()); QueryWrapper sensorQueryWrapper = new QueryWrapper<>(); sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE); sensorQueryWrapper.eq("code",sensor); Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper); timeValueMap.put("sensor",sensorEntity.getName()); for (HistoryHourly historyHourly : hourlies) { Date time = historyHourly.getTime(); String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH"); String value = historyHourly.getValue(); JSONObject jsonObject = JSONObject.parseObject(value); if (jsonObject.containsKey(sensor)){ timeValueMap.put(dateStr, jsonObject.get(sensor).toString()); } } //补上无数据时间 Date middleDate = startDate; while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){ if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null) timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN), null); middleDate = DateUtils.addHours(middleDate,1); } resultList.add(timeValueMap); } } } else if("day".equals(type)){ Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); List historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate); Map> map = resultMap(historyDailyByMacAndTimeSlot,new ArrayList<>()); Map pointMap = pointMap(devices); Map sensorMap = sensorMap(sensorsList); for (Device device:devices) { for (String sensor:sensorsList) { Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd"); Date js = DateUtils.getDateOfDay(DateUtils.getDate(endTime,"yyyy-MM-dd"),1); Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId())); timeValueMap.put("name",device.getName()); timeValueMap.put("sensor",sensorMap.get(sensor)); ArrayList doubleArrayList = new ArrayList<>(); while (DateUtils.isTimeBefor(js,ks)){ String c = DateUtils.dateToDateString(ks,"yyyy-MM-dd"); String k = c+"_"+device.getMac(); if(map.containsKey(k)){ Object o = map.get(k).get(sensor); timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0); if(Objects.nonNull(o)&& (!Double.valueOf(o.toString()).equals(0d))){ doubleArrayList.add(Double.valueOf(o.toString())); } }else { timeValueMap.put(c,0); } ks = DateUtils.getDateOfDay(ks,1); } Double ListAvg = doubleArrayList.stream().collect(Collectors.averagingDouble(Double::doubleValue)); double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); timeValueMap.put("累计值",rsAvg); resultList.add(timeValueMap); } } }else { QueryWrapper HistoryMonthlyWrapper = new QueryWrapper<>(); HistoryMonthlyWrapper.in("mac",devices.stream().map(Device::getMac).collect(Collectors.toList())); HistoryMonthlyWrapper.between("time",startTime,endTime); List historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper); Map> map = resultMap(new ArrayList<>(),historyMonthlyList); Map pointMap = pointMap(devices); Map sensorMap = sensorMap(sensorsList); for (Device device:devices) { for (String sensor:sensorsList) { Date ks = DateUtils.getDate(startTime,"yyyy-MM"); Date js = DateUtils.addMonths(DateUtils.getDate(endTime,"yyyy-MM"),1); Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId())); timeValueMap.put("name",device.getName()); timeValueMap.put("sensor",sensorMap.get(sensor)); while (DateUtils.isTimeBefor(js,ks)){ String c = DateUtils.dateToDateString(ks,"yyyy-MM"); String k = c+"_"+device.getMac(); if(map.containsKey(k)){ Object o = map.get(k).get(sensor); timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0); }else { timeValueMap.put(c,0); } ks = DateUtils.addMonths(ks,1); } resultList.add(timeValueMap); } } } return resultList; } @Override public List> getHourlyDataDataV3Excel(Map params) { List macs = (List) params.remove("macs"); List times = (List) params.remove("times"); String type = params.get("type").toString(); String startTime = times.get(0); String endTime = times.get(1); // String[] macs = params.remove("macs").toString().split(","); // List macsList = Arrays.asList(macs); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("is_delete",Constants.NOT_DELETE); queryWrapper.in("mac", macs); List devices = deviceMapper.selectList(queryWrapper); if (devices.size()<=0){ return null; } String[] sensors = params.remove("sensors").toString().split(","); List sensorsList = Arrays.asList(sensors); List> resultList = new ArrayList<>(); // List sensorsList = (List) params.remove("sensors"); // String startTime = params.get("startTime").toString(); // String endTime = params.get("endTime").toString(); if (type.equals("hours")){ Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); List hourlies = historyHourlyService.getValueByMacAndTime(macs, DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN)); Map> map = resultMap(hourlies); Map pointMap = pointMap(devices); Map sensorMap = sensorMap(sensorsList); for (Device device:devices) { Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd HH"); Date js = DateUtils.getDateAddHour(DateUtils.getDate(endTime,"yyyy-MM-dd HH"),1); while (DateUtils.isTimeBefor(js,ks)){ Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId())); timeValueMap.put("name",device.getName()); String c = DateUtils.dateToDateString(ks,DateUtils.yyyy_MM_dd_HH_EN); timeValueMap.put("日期",c); for (String sensor:sensorsList) { String k = c+"_"+device.getMac(); Double num = 0d; if(map.containsKey(k)){ Object o = map.get(k).get(sensor); num = Objects.nonNull(o)?Double.valueOf(o.toString()):0d; } timeValueMap.put(sensorMap.get(sensor),num); } resultList.add(timeValueMap); ks = DateUtils.getDateAddHour(ks,1); } } } else if("day".equals(type)){ Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); List historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate); Map> map = resultMap(historyDailyByMacAndTimeSlot,new ArrayList<>()); Map pointMap = pointMap(devices); Map sensorMap = sensorMap(sensorsList); for (Device device:devices) { Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd"); Date js = DateUtils.getDateOfDay(DateUtils.getDate(endTime,"yyyy-MM-dd"),1); String nameSite = pointMap.get(device.getMonitorPointId()); String name = device.getName(); Map timeValueMap = new LinkedHashMap<>(); Map> listMap = new HashMap<>(); int numCount = 0; while (DateUtils.isTimeBefor(js,ks)){ timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json timeValueMap.put("监测站点",nameSite); timeValueMap.put("name",name); String c = DateUtils.dateToDateString(ks,"yyyy-MM-dd"); timeValueMap.put("日期",c); for (String sensor:sensorsList) { String k = c+"_"+device.getMac(); Double num = 0d; if(map.containsKey(k)){ Object o = map.get(k).get(sensor); num = Objects.nonNull(o)?Double.valueOf(o.toString()):0d; if(Objects.nonNull(num)&& !num.equals(0d)){ ArrayList doubleArrayList = Objects.isNull(listMap.get(numCount+"_"+sensorMap.get(sensor)))?new ArrayList<>():listMap.get(numCount+"_"+sensorMap.get(sensor)); doubleArrayList.add(num); listMap.put(numCount+"_"+sensorMap.get(sensor),doubleArrayList); } } timeValueMap.put(sensorMap.get(sensor),num); } resultList.add(timeValueMap); ks = DateUtils.getDateOfDay(ks,1); } timeValueMap = new LinkedHashMap<>(); timeValueMap.put("监测站点",nameSite); timeValueMap.put("name",name); timeValueMap.put("日期","累计值"); for(int z = 0 ;z< sensorsList.size();z++){ double rsAvg = 0L; String son = numCount+"_"+sensorMap.get(sensorsList.get(z)); if(listMap.containsKey(numCount+"_"+sensorMap.get(sensorsList.get(z)))){ if(son.contains("臭氧")){ rsAvg = percentile(listMap.get(numCount+"_"+sensorMap.get(sensorsList.get(z))),90d); }else if(son.contains("一氧化碳")){ rsAvg = percentile(listMap.get(numCount+"_"+sensorMap.get(sensorsList.get(z))),95d); }else { Double ListAvg = listMap.get(numCount+"_"+sensorMap.get(sensorsList.get(z))).stream().collect(Collectors.averagingDouble(Double::doubleValue)); rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); } } timeValueMap.put(sensorMap.get(sensorsList.get(z)),rsAvg); } resultList.add(timeValueMap); numCount++; } }else { QueryWrapper HistoryMonthlyWrapper = new QueryWrapper<>(); HistoryMonthlyWrapper.in("mac",devices.stream().map(Device::getMac).collect(Collectors.toList())); HistoryMonthlyWrapper.between("time",startTime,endTime); List historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper); Map> map = resultMap(new ArrayList<>(),historyMonthlyList); Map pointMap = pointMap(devices); Map sensorMap = sensorMap(sensorsList); for (Device device:devices) { Date ks = DateUtils.getDate(startTime,"yyyy-MM"); Date js = DateUtils.addMonths(DateUtils.getDate(endTime,"yyyy-MM"),1); while (DateUtils.isTimeBefor(js,ks)){ Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId())); timeValueMap.put("name",device.getName()); String c = DateUtils.dateToDateString(ks,"yyyy-MM"); timeValueMap.put("日期",c); for (String sensor:sensorsList) { String k = c+"_"+device.getMac(); Double num = 0d; if(map.containsKey(k)){ Object o = map.get(k).get(sensor); num = Objects.nonNull(o)?Double.valueOf(o.toString()):0d; } timeValueMap.put(sensorMap.get(sensor),num); } resultList.add(timeValueMap); ks = DateUtils.addMonths(ks,1); } /*for (String sensor:sensorsList) { Date ks = DateUtils.getDate(startTime,"yyyy-MM"); Date js = DateUtils.addMonths(DateUtils.getDate(endTime,"yyyy-MM"),1); Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId())); timeValueMap.put("name",device.getName()); timeValueMap.put("sensor",sensorMap.get(sensor)); while (DateUtils.isTimeBefor(js,ks)){ String c = DateUtils.dateToDateString(ks,"yyyy-MM"); String k = c+"_"+device.getMac(); if(map.containsKey(k)){ Object o = map.get(k).get(sensor); timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0); }else { timeValueMap.put(c,0); } ks = DateUtils.addMonths(ks,1); } resultList.add(timeValueMap); }*/ } } return resultList; } @Override public List listMonitoringStationDTO(Map params, int reportType, String startTime, String endTime) { // List macs = Arrays.asList(mac.split(",")); List macs = (List) params.remove("mac"); List resultList = new ArrayList<>(); if(reportType == 1||reportType == 4){ Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN); List historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate); resultList = resultList(historyDailyByMacAndTimeSlot); }else if(reportType == 2){ Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); Date endDate = DateUtils.addDays(startDate,6); List historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate); resultList = resultList(historyDailyByMacAndTimeSlot); }else if(reportType == 3){ Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN); QueryWrapper HistoryMonthlyWrapper = new QueryWrapper<>(); HistoryMonthlyWrapper.in("mac",macs); HistoryMonthlyWrapper.between("time",startDate,endDate); List historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper); List historyDailies = new ArrayList<>(); for(HistoryMonthly h : historyMonthlyList) { HistoryDaily historyDaily = new HistoryDaily(); historyDaily.setMac(h.getMac()); historyDaily.setTime(h.getTime()); historyDaily.setValue(h.getValue()); historyDailies.add(historyDaily); } resultList = resultList(historyDailies); } return resultList; } private List resultList(List historyDailyByMacAndTimeSlot){ List list = new ArrayList<>(); if(CollectionUtils.isEmpty(historyDailyByMacAndTimeSlot)){ return list; } Map>> resultMap = new HashMap<>(); int zz = 0; for(HistoryDaily h : historyDailyByMacAndTimeSlot) { JSONObject jsonObject = JSONObject.parseObject(h.getValue()); Map map = jsonObject.getInnerMap(); Map> maps = Objects.nonNull(resultMap.get(h.getMac()))?resultMap.get(h.getMac()):new HashMap<>(); if(Objects.nonNull(map.get("a34004"))){ ArrayList numResult = Objects.nonNull(maps.get("PM2_5"))?maps.get("PM2_5"):new ArrayList<>(); numResult.add(Double.parseDouble(map.get("a34004").toString())); maps.put("PM2_5",numResult); } if(Objects.nonNull(map.get("a05024"))){ ArrayList numResult = Objects.nonNull(maps.get("O3"))?maps.get("O3"):new ArrayList<>(); numResult.add(Double.parseDouble(map.get("a05024").toString())); maps.put("O3",numResult); } if(Objects.nonNull(map.get("a34002"))){ ArrayList numResult = Objects.nonNull(maps.get("PM10"))?maps.get("PM10"):new ArrayList<>(); numResult.add(Double.parseDouble(map.get("a34002").toString())); maps.put("PM10",numResult); } if(Objects.nonNull(map.get("a21026"))){ ArrayList numResult = Objects.nonNull(maps.get("SO2"))?maps.get("SO2"):new ArrayList<>(); numResult.add(Double.parseDouble(map.get("a21026").toString())); maps.put("SO2",numResult); } if(Objects.nonNull(map.get("a21004"))){ ArrayList numResult = Objects.nonNull(maps.get("NO2"))?maps.get("NO2"):new ArrayList<>(); numResult.add(Double.parseDouble(map.get("a21004").toString())); maps.put("NO2",numResult); } if(Objects.nonNull(map.get("a21005"))){ System.out.println(">>>"+map.toString()); System.out.println(">>>"+map.get("a21005").toString()); ArrayList numResult = Objects.nonNull(maps.get("CO"))?maps.get("CO"):new ArrayList<>(); numResult.add(Double.parseDouble(map.get("a21005").toString())); maps.put("CO",numResult); } if(Objects.nonNull(map.get("a99054"))){ ArrayList numResult = Objects.nonNull(maps.get("TVOC"))?maps.get("TVOC"):new ArrayList<>(); numResult.add(Double.parseDouble(map.get("a99054").toString())); maps.put("TVOC",numResult); } resultMap.put(h.getMac(),maps); zz++; } for(Map.Entry entry : resultMap.entrySet()){ String mapKey = (String) entry.getKey(); Map> mapValue = (Map>)entry.getValue(); Map data = new HashMap<>(); if(mapValue.containsKey("PM2_5")){ Double ListAvg = mapValue.get("PM2_5").stream().collect(Collectors.averagingDouble(Double::doubleValue)); double rsAvg = new BigDecimal(ListAvg).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); data.put("PM2_5",rsAvg); }else { data.put("PM2_5",0); } if(mapValue.containsKey("PM10")){ Double ListAvg = mapValue.get("PM10").stream().collect(Collectors.averagingDouble(Double::doubleValue)); double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); data.put("PM10",rsAvg); }else { data.put("PM10",0); } if(mapValue.containsKey("SO2")){ Double ListAvg = mapValue.get("SO2").stream().collect(Collectors.averagingDouble(Double::doubleValue)); double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); data.put("SO2",rsAvg); }else { data.put("SO2",0); } if(mapValue.containsKey("NO2")){ Double ListAvg = mapValue.get("NO2").stream().collect(Collectors.averagingDouble(Double::doubleValue)); double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); data.put("NO2",rsAvg); }else { data.put("NO2",0); } if(mapValue.containsKey("CO")){ Double ListAvg = percentile(mapValue.get("CO"),95d); double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); data.put("CO",rsAvg); }else { data.put("CO",0); } if(mapValue.containsKey("O3")){ Double ListAvg = percentile(mapValue.get("O3"),90d); double rsAvg = new BigDecimal(ListAvg).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue(); data.put("O3",rsAvg); }else { data.put("O3",0); } if(mapValue.containsKey("TVOC")){ Double ListAvg = mapValue.get("TVOC").stream().collect(Collectors.averagingDouble(Double::doubleValue)); double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); data.put("TVOC",rsAvg); }else { data.put("TVOC",0); } Double resultNum = ComprehensiveIndexUtils.dailyData(data); MonitoringStationDTO monitoringStationDTO = new MonitoringStationDTO(); monitoringStationDTO.setPM25(BigDecimal.valueOf(Double.parseDouble(data.get("PM2_5").toString()))); monitoringStationDTO.setO3(BigDecimal.valueOf(Double.parseDouble(data.get("O3").toString()))); monitoringStationDTO.setTovc(BigDecimal.valueOf(Double.parseDouble(data.get("TVOC").toString()))); monitoringStationDTO.setComposite(BigDecimal.valueOf(resultNum)); monitoringStationDTO.setMac(mapKey); list.add(monitoringStationDTO); } List compositeList = fun3(list,1); List pM25List = fun3(list,2); List o3List = fun3(list,3); List tovcList = fun3(list,4); List resultList = new ArrayList<>(); LambdaQueryChainWrapper wrapper = deviceService.lambdaQuery(); wrapper.eq(Device::getIsDelete,0); Map deviceMap = new HashMap<>(); wrapper.list().forEach(it->deviceMap.put(it.getMac(),it.getName())); int num = 1; for (MonitoringStationDTO m :compositeList ){ MonitoringStationDTO monit = new MonitoringStationDTO(); String mac = m.getMac(); monit.setMac(mac); monit.setName(deviceMap.get(mac)); monit.setComposite(m.getComposite()); monit.setCompositeNum(m.getCompositeNum()); List listPM25 = pM25List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList()); List listO3 = o3List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList()); List listTovc = tovcList.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList()); monit.setO3(o3List.get(listO3.indexOf(mac)).getO3()); monit.setO3Num(o3List.get(listO3.indexOf(mac)).getO3Num()); monit.setPM25(pM25List.get(listPM25.indexOf(mac)).getPM25()); monit.setPM25Num(pM25List.get(listPM25.indexOf(mac)).getPM25Num()); monit.setTovc(tovcList.get(listTovc.indexOf(mac)).getTovc()); monit.setTOVCNum(tovcList.get(listTovc.indexOf(mac)).getTOVCNum()); //num++; resultList.add(monit); } return resultList; } public List fun3(List monList,int type) { List resultList = new ArrayList<>(); List compositeList = new ArrayList<>(); Map> map1 = new HashMap<>(); if(type == 1){ compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getComposite)).collect(Collectors.toList()); map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getComposite,LinkedHashMap::new,Collectors.toList())); }else if(type == 2){ compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getPM25)).collect(Collectors.toList()); map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getPM25,LinkedHashMap::new,Collectors.toList())); }else if(type == 3){ compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getO3)).collect(Collectors.toList()); map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getO3,LinkedHashMap::new,Collectors.toList())); }else if(type == 4){ compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getTovc)).collect(Collectors.toList()); map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getTovc,LinkedHashMap::new,Collectors.toList())); } int index = 1; for (Map.Entry entry : map1.entrySet()) { List mapValue = (List)entry.getValue(); for(MonitoringStationDTO m : mapValue){ MonitoringStationDTO stationDTO = new MonitoringStationDTO(); stationDTO.setMac(m.getMac()); if(type == 1){ stationDTO.setComposite(m.getComposite()); stationDTO.setCompositeNum(index); }else if(type == 2){ stationDTO.setPM25(m.getPM25()); stationDTO.setPM25Num(index); }else if(type == 3){ stationDTO.setO3(m.getO3()); stationDTO.setO3Num(index); }else if(type == 4){ stationDTO.setTovc(m.getTovc()); stationDTO.setTOVCNum(index); } resultList.add(stationDTO); } index = index +mapValue.size(); } return resultList; } private Map> resultMap(List list , List historyMonthlyList){ Map> map = new HashMap<>(); for(HistoryDaily h : list){ String dateStr = DateUtils.dateToDateString( h.getTime(), "yyyy-MM-dd")+"_"+h.getMac(); Map jsonMap = new HashMap<>(); JSONObject jsonObject = JSONObject.parseObject(h.getValue()); jsonMap = jsonObject.getInnerMap(); map.put(dateStr,jsonMap); } for(HistoryMonthly h : historyMonthlyList){ String dateStr = DateUtils.dateToDateString( h.getTime(), "yyyy-MM")+"_"+h.getMac(); Map jsonMap = new HashMap<>(); JSONObject jsonObject = JSONObject.parseObject(h.getValue()); jsonMap = jsonObject.getInnerMap(); map.put(dateStr,jsonMap); } return map; } private Map> resultMap(List list){ Map> map = new HashMap<>(); for(HistoryHourly h : list){ String dateStr = DateUtils.dateToDateString( h.getTime(), DateUtils.yyyy_MM_dd_HH_EN)+"_"+h.getMac(); Map jsonMap = new HashMap<>(); JSONObject jsonObject = JSONObject.parseObject(h.getValue()); jsonMap = jsonObject.getInnerMap(); map.put(dateStr,jsonMap); } return map; } private Map sensorMap(List sensorsList){ Map map = new HashMap<>(); QueryWrapper sensorQueryWrapper = new QueryWrapper<>(); sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE); sensorQueryWrapper.in("code",sensorsList); sensorMapper.selectList(sensorQueryWrapper).forEach(it->map.put(it.getCode(),it.getName())); return map; } private Map pointMap(List devices){ Map map = new HashMap<>(); QueryWrapper monitorPointQueryWrapper = new QueryWrapper<>(); monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE); monitorPointQueryWrapper.in("id",devices.stream().map(Device::getMonitorPointId).collect(Collectors.toList())); monitorPointMapper.selectList(monitorPointQueryWrapper).forEach(it->map.put(it.getId(),it.getName())); return map; } private Double percentile(List date,double num){ if(CollectionUtils.isNotEmpty(date)&&date.size() == 1){ return date.get(0); } Collections.sort(date); double position = (num / 100) * (date.size() - 1); int index = (int) position; // 获取小数部分的位置索引 double fraction = position - index; // 获取百分位值 double percentileValue = date.get(index) + fraction * (date.get(index + 1) - date.get(index)); return new BigDecimal(percentileValue).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); } }