| | |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.OptionalDouble; |
| | | import java.util.Set; |
| | | import java.util.function.Supplier; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.DoubleStream; |
| | | import java.util.stream.Stream; |
| | | |
| | | 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.CityAqiMonthly; |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.HistoryDaily; |
| | | import com.moral.api.entity.HistoryHourly; |
| | | import com.moral.api.entity.HistoryMonthly; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.service.CityAqiDailyService; |
| | | import com.moral.api.service.CityAqiMonthlyService; |
| | |
| | | private List<Map<String, Object>> professionContributionOfYear(Integer orgId, List<String> professions, String time, String sensorCode) { |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | |
| | | //根据组织获取区域定位 |
| | | QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); |
| | | organizationQueryWrapper.select("location_level_code") |
| | | .eq("id", orgId); |
| | | Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); |
| | | |
| | | //该组织所有设备信息 |
| | | List allMacs = getMacsByOrgId(orgId); |
| | | |
| | | List<String> timeLag = DateUtils.getTimeLag(time); |
| | | |
| | | //本市本年所有月数据 |
| | | QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>(); |
| | | cityAqiMonthlyQueryWrapper.select("time", "value") |
| | | .eq("city_code", locationLevelCode) |
| | | .likeRight("time", time); |
| | | List<Map<String, Object>> cityAqis = cityAqiMonthlyService.listMaps(cityAqiMonthlyQueryWrapper); |
| | | Map<String, Object> cityAqiMap = new HashMap<>(); |
| | | if (!ObjectUtils.isEmpty(cityAqis)) { |
| | | for (Map<String, Object> cityAqi : cityAqis) { |
| | | cityAqiMap.put(cityAqi.get("time").toString().substring(0, 7), cityAqi.get("value")); |
| | | } |
| | | } |
| | | |
| | | |
| | | //所有设备本年所有月数据 |
| | | List<HistoryMonthly> allDeviceData = historyMonthlyService.getValueByMacs(allMacs, time); |
| | | //按time分组 |
| | | Map<String, List<HistoryMonthly>> allDeviceDataMap = allDeviceData.stream() |
| | | .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); |
| | | |
| | | |
| | | for (String yearMonth : timeLag) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | resultMap.put("time", yearMonth.split("-")[1]); |
| | | yearMonth = yearMonth + "-01 00:00:00"; |
| | | resultMap.put("time", yearMonth); |
| | | |
| | | |
| | | //所有设备该因子累加值 |
| | | Double allDeviceSum = null; |
| | | QueryWrapper<HistoryMonthly> historyMonthlyQueryWrapper = new QueryWrapper<>(); |
| | | historyMonthlyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") |
| | | .eq("time", yearMonth) |
| | | .in("mac", allMacs); |
| | | Map<String, Object> allDeviceSumMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(allDeviceSumMap)) { |
| | | allDeviceSum = (Double) allDeviceSumMap.get("result"); |
| | | List<HistoryMonthly> historyMonthlyList = allDeviceDataMap.get(yearMonth); |
| | | if (!ObjectUtils.isEmpty(historyMonthlyList)) { |
| | | allDeviceSum = historyMonthlyList.stream().flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Object o = dataValue.get(sensorCode); |
| | | if (o == null) { |
| | | return null; |
| | | } |
| | | double aDouble = Double.parseDouble(o.toString()); |
| | | return DoubleStream.of(aDouble); |
| | | }).sum(); |
| | | } |
| | | resultMap.put("allDeviceSum", allDeviceSum); |
| | | |
| | | |
| | | //本市数据 |
| | | //本市值 |
| | | Double cityValue = null; |
| | | //根据组织获取区域定位 |
| | | Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode(); |
| | | QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>(); |
| | | cityAqiMonthlyQueryWrapper.select("`value`->'$." + sensorCode + "' AS result") |
| | | .eq("city_code", locationLevelCode) |
| | | .eq("time", yearMonth); |
| | | Map<String, Object> cityValueMap = cityAqiMonthlyService.getMap(cityAqiMonthlyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(cityValueMap)) { |
| | | cityValue = (Double) cityValueMap.get("result"); |
| | | if (cityAqiMap.get(yearMonth) != null) { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonth).toString(), Map.class); |
| | | Object o = dataValue.get(sensorCode); |
| | | if (o != null) { |
| | | cityValue = (Double) o; |
| | | } |
| | | } |
| | | resultMap.put("cityValue", cityValue); |
| | | |
| | | |
| | | for (String profession : professions) { |
| | | Map<String, Object> professionMap = new HashMap<>(); |
| | | |
| | | //获取该行业设备 |
| | | List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); |
| | | List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList()); |
| | | |
| | | |
| | | //该行业累加值 |
| | | Double professionSum = null; |
| | | historyMonthlyQueryWrapper.clear(); |
| | | historyMonthlyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") |
| | | .eq("time", yearMonth) |
| | | .in("mac", professionMacs); |
| | | Map<String, Object> professionSumMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(professionSumMap)) { |
| | | professionSum = (Double) professionSumMap.get("result"); |
| | | } |
| | | |
| | | |
| | | //该行业平均值 |
| | | Double professionAvg = null; |
| | | historyMonthlyQueryWrapper.clear(); |
| | | historyMonthlyQueryWrapper.select("AVG(`value`->'$." + sensorCode + "') AS result") |
| | | .eq("time", yearMonth) |
| | | .in("mac", professionMacs); |
| | | Map<String, Object> professionAvgMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(professionAvgMap)) { |
| | | professionAvg = (Double) professionAvgMap.get("result"); |
| | | } |
| | | |
| | | //行业贡献率计算 |
| | | String contributionRate = null; |
| | | NumberFormat numberFormat = NumberFormat.getInstance(); |
| | | numberFormat.setMaximumFractionDigits(2); |
| | | if (professionSum != null && allDeviceSum != null) { |
| | | contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%"; |
| | | } |
| | | |
| | | //行业均值 |
| | | professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); |
| | | //行业贡献率 |
| | | professionMap.put("contributionRate", contributionRate); |
| | | |
| | | resultMap.put(profession, professionMap); |
| | | } |
| | | result.add(resultMap); |
| | | } |
| | | |
| | | for (String profession : professions) { |
| | | //获取该行业设备 |
| | | List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); |
| | | List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList()); |
| | | |
| | | |
| | | //该行业设备本年所有月数据 |
| | | List<HistoryMonthly> professionDeviceData = historyMonthlyService.getValueByMacs(professionMacs, time); |
| | | //按time分组 |
| | | Map<String, List<HistoryMonthly>> professionDataMap = professionDeviceData.stream() |
| | | .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); |
| | | |
| | | |
| | | for (Map<String, Object> map : result) { |
| | | Object allDeviceSum = map.get("allDeviceSum"); |
| | | String resultTime = map.get("time").toString(); |
| | | List<HistoryMonthly> historyMonthlyList = professionDataMap.get(resultTime); |
| | | //贡献率 |
| | | String contributionRate = null; |
| | | Double professionAvg = null; |
| | | if (!ObjectUtils.isEmpty(historyMonthlyList)) { |
| | | Supplier<Stream<HistoryMonthly>> streamSupplier = historyMonthlyList::stream; |
| | | professionAvg = calculatedValueOfYear(streamSupplier, sensorCode, "avg"); |
| | | Double professionSum = calculatedValueOfYear(streamSupplier, sensorCode, "sum"); |
| | | //行业贡献率计算 |
| | | NumberFormat numberFormat = NumberFormat.getInstance(); |
| | | numberFormat.setMaximumFractionDigits(2); |
| | | if (allDeviceSum != null) { |
| | | contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; |
| | | } |
| | | } |
| | | Map<String, Object> professionMap = new HashMap<>(); |
| | | professionMap.put("contributionRate", contributionRate); |
| | | professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); |
| | | map.put(profession, professionMap); |
| | | } |
| | | } |
| | | result.forEach(map -> map.remove("allDeviceSum")); |
| | | return result; |
| | | } |
| | | |
| | |
| | | private List<Map<String, Object>> professionContributionOfMonth(Integer orgId, List<String> professions, String time, String sensorCode) { |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | |
| | | //根据组织获取区域定位 |
| | | QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); |
| | | organizationQueryWrapper.select("location_level_code") |
| | | .eq("id", orgId); |
| | | Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); |
| | | |
| | | //该组织所有设备信息 |
| | | List allMacs = getMacsByOrgId(orgId); |
| | | |
| | | //日时间点 |
| | | List<String> timeLag = DateUtils.getTimeLag(time); |
| | | |
| | | |
| | | //本市本月所有日数据 |
| | | QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); |
| | | cityAqiDailyQueryWrapper.select("time", "value") |
| | | .eq("city_code", locationLevelCode) |
| | | .likeRight("time", time); |
| | | List<Map<String, Object>> cityAqis = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper); |
| | | Map<String, Object> cityAqiMap = new HashMap<>(); |
| | | if (!ObjectUtils.isEmpty(cityAqis)) { |
| | | for (Map<String, Object> cityAqi : cityAqis) { |
| | | cityAqiMap.put(cityAqi.get("time").toString().substring(0, 10), cityAqi.get("value")); |
| | | } |
| | | } |
| | | |
| | | |
| | | //所有设备本月所有日数据 |
| | | List<HistoryDaily> allDeviceData = historyDailyService.getValueByMacs(allMacs, time); |
| | | //按time分组 |
| | | Map<String, List<HistoryDaily>> allDeviceDataMap = allDeviceData.stream() |
| | | .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10))); |
| | | |
| | | |
| | | for (String yearMonthDay : timeLag) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | resultMap.put("time", yearMonthDay.split("-")[2]); |
| | | yearMonthDay = yearMonthDay + " 00:00:00"; |
| | | |
| | | resultMap.put("time", yearMonthDay); |
| | | |
| | | //所有设备该因子累加值 |
| | | Double allDeviceSum = null; |
| | | QueryWrapper<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>(); |
| | | historyDailyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") |
| | | .eq("time", yearMonthDay) |
| | | .in("mac", allMacs); |
| | | Map<String, Object> allDeviceSumMap = historyDailyService.getMap(historyDailyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(allDeviceSumMap)) { |
| | | allDeviceSum = (Double) allDeviceSumMap.get("result"); |
| | | List<HistoryDaily> historyDailyList = allDeviceDataMap.get(yearMonthDay); |
| | | if (!ObjectUtils.isEmpty(historyDailyList)) { |
| | | allDeviceSum = historyDailyList.stream().flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Object o = dataValue.get(sensorCode); |
| | | if (o == null) { |
| | | return null; |
| | | } |
| | | double aDouble = Double.parseDouble(o.toString()); |
| | | return DoubleStream.of(aDouble); |
| | | }).sum(); |
| | | } |
| | | resultMap.put("allDeviceSum", allDeviceSum); |
| | | |
| | | |
| | | //本市数据 |
| | | //本市值 |
| | | Double cityValue = null; |
| | | //根据组织获取区域定位 |
| | | Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode(); |
| | | QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); |
| | | cityAqiDailyQueryWrapper.select("`value`->'$." + sensorCode + "' AS result") |
| | | .eq("city_code", locationLevelCode) |
| | | .eq("time", yearMonthDay); |
| | | Map<String, Object> cityValueMap = cityAqiDailyService.getMap(cityAqiDailyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(cityValueMap)) { |
| | | cityValue = (Double) cityValueMap.get("result"); |
| | | if (cityAqiMap.get(yearMonthDay) != null) { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDay).toString(), Map.class); |
| | | Object o = dataValue.get(sensorCode); |
| | | if (o != null) { |
| | | cityValue = (Double) o; |
| | | } |
| | | } |
| | | resultMap.put("cityValue", cityValue); |
| | | |
| | | |
| | | |
| | | for (String profession : professions) { |
| | | Map<String, Object> professionMap = new HashMap<>(); |
| | | |
| | | //获取该行业设备 |
| | | List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); |
| | | List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList()); |
| | | |
| | | |
| | | //该行业累加值 |
| | | Double professionSum = null; |
| | | historyDailyQueryWrapper.clear(); |
| | | historyDailyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") |
| | | .eq("time", yearMonthDay) |
| | | .in("mac", professionMacs); |
| | | Map<String, Object> professionSumMap = historyDailyService.getMap(historyDailyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(professionSumMap)) { |
| | | professionSum = (Double) professionSumMap.get("result"); |
| | | } |
| | | |
| | | |
| | | //该行业平均值 |
| | | Double professionAvg = null; |
| | | historyDailyQueryWrapper.clear(); |
| | | historyDailyQueryWrapper.select("AVG(`value`->'$." + sensorCode + "') AS result") |
| | | .eq("time", yearMonthDay) |
| | | .in("mac", professionMacs); |
| | | Map<String, Object> professionAvgMap = historyDailyService.getMap(historyDailyQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(professionAvgMap)) { |
| | | professionAvg = (Double) professionAvgMap.get("result"); |
| | | } |
| | | |
| | | |
| | | //行业贡献率计算 |
| | | String contributionRate = null; |
| | | NumberFormat numberFormat = NumberFormat.getInstance(); |
| | | numberFormat.setMaximumFractionDigits(2); |
| | | if (professionSum != null && allDeviceSum != null) { |
| | | contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%"; |
| | | } |
| | | |
| | | //行业均值 |
| | | professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); |
| | | //行业贡献率 |
| | | professionMap.put("contributionRate", contributionRate); |
| | | |
| | | resultMap.put(profession, professionMap); |
| | | } |
| | | result.add(resultMap); |
| | | } |
| | | |
| | | for (String profession : professions) { |
| | | //获取该行业设备 |
| | | List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); |
| | | List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList()); |
| | | |
| | | |
| | | //该行业设备本月所有日数据 |
| | | List<HistoryDaily> professionDeviceData = historyDailyService.getValueByMacs(professionMacs, time); |
| | | //按time分组 |
| | | Map<String, List<HistoryDaily>> professionDataMap = professionDeviceData.stream() |
| | | .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10))); |
| | | |
| | | |
| | | for (Map<String, Object> map : result) { |
| | | Object allDeviceSum = map.get("allDeviceSum"); |
| | | String resultTime = map.get("time").toString(); |
| | | List<HistoryDaily> historyDailyList = professionDataMap.get(resultTime); |
| | | //贡献率 |
| | | String contributionRate = null; |
| | | Double professionAvg = null; |
| | | if (!ObjectUtils.isEmpty(historyDailyList)) { |
| | | Supplier<Stream<HistoryDaily>> streamSupplier = historyDailyList::stream; |
| | | professionAvg = calculatedValueOfMonth(streamSupplier, sensorCode, "avg"); |
| | | Double professionSum = calculatedValueOfMonth(streamSupplier, sensorCode, "sum"); |
| | | //行业贡献率计算 |
| | | NumberFormat numberFormat = NumberFormat.getInstance(); |
| | | numberFormat.setMaximumFractionDigits(2); |
| | | if (allDeviceSum != null) { |
| | | contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; |
| | | } |
| | | } |
| | | Map<String, Object> professionMap = new HashMap<>(); |
| | | professionMap.put("contributionRate", contributionRate); |
| | | professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); |
| | | map.put(profession, professionMap); |
| | | } |
| | | } |
| | | result.forEach(map -> map.remove("allDeviceSum")); |
| | | return result; |
| | | } |
| | | |
| | | private List<Map<String, Object>> professionContributionOfDay(Integer orgId, List<String> professions, String time, String sensorCode) { |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | |
| | | //根据组织获取区域定位 |
| | | QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); |
| | | organizationQueryWrapper.select("location_level_code") |
| | | .eq("id", orgId); |
| | | Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); |
| | | |
| | | //该组织所有设备信息 |
| | | List allMacs = getMacsByOrgId(orgId); |
| | | |
| | | //小时时间点 |
| | | List<String> timeLag = DateUtils.getTimeLag(time); |
| | | |
| | | |
| | | //本市本日所有小时数据 |
| | | QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>(); |
| | | cityAqiQueryWrapper.select("time", "value") |
| | | .eq("city_code", locationLevelCode) |
| | | .likeRight("time", time); |
| | | List<Map<String, Object>> cityAqis = cityAqiService.listMaps(cityAqiQueryWrapper); |
| | | Map<String, Object> cityAqiMap = new HashMap<>(); |
| | | if (!ObjectUtils.isEmpty(cityAqis)) { |
| | | for (Map<String, Object> cityAqi : cityAqis) { |
| | | cityAqiMap.put(cityAqi.get("time").toString().substring(0, 13), cityAqi.get("value")); |
| | | } |
| | | } |
| | | |
| | | |
| | | //所有设备本日所有小时数据 |
| | | List<HistoryHourly> allDeviceData = historyHourlyService.getValueByMacs(allMacs, time); |
| | | //按time分组 |
| | | Map<String, List<HistoryHourly>> allDeviceDataMap = allDeviceData.stream() |
| | | .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13))); |
| | | |
| | | |
| | | for (String yearMonthDayHour : timeLag) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | resultMap.put("time", yearMonthDayHour.split(" ")[1]); |
| | | yearMonthDayHour = yearMonthDayHour + ":00:00"; |
| | | resultMap.put("time", yearMonthDayHour); |
| | | |
| | | |
| | | //所有设备该因子累加值 |
| | | Double allDeviceSum = historyHourlyService.getSensorSumByMacs(sensorCode, allMacs, yearMonthDayHour); |
| | | Double allDeviceSum = null; |
| | | List<HistoryHourly> historyHourlyList = allDeviceDataMap.get(yearMonthDayHour); |
| | | if (!ObjectUtils.isEmpty(historyHourlyList)) { |
| | | allDeviceSum = historyHourlyList.stream().flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Object o = dataValue.get(sensorCode); |
| | | if (o == null) { |
| | | return null; |
| | | } |
| | | double aDouble = Double.parseDouble(o.toString()); |
| | | return DoubleStream.of(aDouble); |
| | | }).sum(); |
| | | } |
| | | resultMap.put("allDeviceSum", allDeviceSum); |
| | | |
| | | |
| | | //本市数据 |
| | | //本市值 |
| | | Double cityValue = null; |
| | | //根据组织获取区域定位 |
| | | Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode(); |
| | | QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>(); |
| | | cityAqiQueryWrapper.select("`value`->'$." + sensorCode + "' AS result") |
| | | .eq("city_code", locationLevelCode) |
| | | .eq("time", yearMonthDayHour); |
| | | Map<String, Object> cityValueMap = cityAqiService.getMap(cityAqiQueryWrapper); |
| | | if (!ObjectUtils.isEmpty(cityValueMap)) { |
| | | cityValue = (Double) cityValueMap.get("result"); |
| | | if (cityAqiMap.get(yearMonthDayHour) != null) { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDayHour).toString(), Map.class); |
| | | Object o = dataValue.get(sensorCode); |
| | | if (o != null) { |
| | | cityValue = (Double) o; |
| | | } |
| | | } |
| | | resultMap.put("cityValue", cityValue); |
| | | |
| | | |
| | | for (String profession : professions) { |
| | | Map<String, Object> professionMap = new HashMap<>(); |
| | | |
| | | //获取该行业设备 |
| | | List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); |
| | | List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList()); |
| | | |
| | | //获取该行业设备数据,累加值,平均值 |
| | | Double professionAvg = historyHourlyService.getSensorAvgByMacs(sensorCode, professionMacs, yearMonthDayHour); |
| | | Double professionSum = historyHourlyService.getSensorSumByMacs(sensorCode, professionMacs, yearMonthDayHour); |
| | | |
| | | //行业贡献率计算 |
| | | String contributionRate = null; |
| | | NumberFormat numberFormat = NumberFormat.getInstance(); |
| | | numberFormat.setMaximumFractionDigits(2); |
| | | if (professionSum != null && allDeviceSum != null) { |
| | | contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%"; |
| | | } |
| | | |
| | | //行业均值 |
| | | professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); |
| | | //行业贡献率 |
| | | professionMap.put("contributionRate", contributionRate); |
| | | |
| | | resultMap.put(profession, professionMap); |
| | | } |
| | | result.add(resultMap); |
| | | } |
| | | |
| | | for (String profession : professions) { |
| | | //获取该行业设备 |
| | | List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); |
| | | List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList()); |
| | | |
| | | |
| | | //该行业设备本日所有小时数据 |
| | | List<HistoryHourly> professionDeviceData = historyHourlyService.getValueByMacs(professionMacs, time); |
| | | //按time分组 |
| | | Map<String, List<HistoryHourly>> professionDataMap = professionDeviceData.stream() |
| | | .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13))); |
| | | |
| | | |
| | | for (Map<String, Object> map : result) { |
| | | Object allDeviceSum = map.get("allDeviceSum"); |
| | | String resultTime = map.get("time").toString(); |
| | | List<HistoryHourly> historyHourlyList = professionDataMap.get(resultTime); |
| | | //贡献率 |
| | | String contributionRate = null; |
| | | Double professionAvg = null; |
| | | if (!ObjectUtils.isEmpty(historyHourlyList)) { |
| | | Supplier<Stream<HistoryHourly>> streamSupplier = historyHourlyList::stream; |
| | | professionAvg = calculatedValueOfDay(streamSupplier, sensorCode, "avg"); |
| | | Double professionSum = calculatedValueOfDay(streamSupplier, sensorCode, "sum"); |
| | | //行业贡献率计算 |
| | | NumberFormat numberFormat = NumberFormat.getInstance(); |
| | | numberFormat.setMaximumFractionDigits(2); |
| | | if (allDeviceSum != null) { |
| | | contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; |
| | | } |
| | | } |
| | | Map<String, Object> professionMap = new HashMap<>(); |
| | | professionMap.put("contributionRate", contributionRate); |
| | | professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); |
| | | map.put(profession, professionMap); |
| | | } |
| | | } |
| | | result.forEach(map -> map.remove("allDeviceSum")); |
| | | return result; |
| | | } |
| | | |
| | |
| | | return deviceService.listObjs(deviceQueryWrapper); |
| | | } |
| | | |
| | | //日累加值,平均值计算 |
| | | private Double calculatedValueOfDay(Supplier<Stream<HistoryHourly>> supplier, String sensorCode, String type) { |
| | | DoubleStream doubleStream = supplier.get() |
| | | .flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Object sensorValue = dataValue.get(sensorCode); |
| | | if (ObjectUtils.isEmpty(sensorValue)) { |
| | | return null; |
| | | } |
| | | double aDouble = Double.parseDouble(sensorValue.toString()); |
| | | return DoubleStream.of(aDouble); |
| | | }); |
| | | Double result = null; |
| | | if ("avg".equals(type)) { |
| | | OptionalDouble optionalDouble = doubleStream.average(); |
| | | if (optionalDouble.isPresent()) { |
| | | result = optionalDouble.getAsDouble(); |
| | | } |
| | | } else if ("sum".equals(type)) { |
| | | result = doubleStream.sum(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | //月累加值,平均值计算 |
| | | private Double calculatedValueOfMonth(Supplier<Stream<HistoryDaily>> supplier, String sensorCode, String type) { |
| | | DoubleStream doubleStream = supplier.get() |
| | | .flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Object sensorValue = dataValue.get(sensorCode); |
| | | if (ObjectUtils.isEmpty(sensorValue)) { |
| | | return null; |
| | | } |
| | | double aDouble = Double.parseDouble(sensorValue.toString()); |
| | | return DoubleStream.of(aDouble); |
| | | }); |
| | | Double result = null; |
| | | if ("avg".equals(type)) { |
| | | OptionalDouble optionalDouble = doubleStream.average(); |
| | | if (optionalDouble.isPresent()) { |
| | | result = optionalDouble.getAsDouble(); |
| | | } |
| | | } else if ("sum".equals(type)) { |
| | | result = doubleStream.sum(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | //日累加值,平均值计算 |
| | | private Double calculatedValueOfYear(Supplier<Stream<HistoryMonthly>> supplier, String sensorCode, String type) { |
| | | DoubleStream doubleStream = supplier.get() |
| | | .flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class); |
| | | Object sensorValue = dataValue.get(sensorCode); |
| | | if (ObjectUtils.isEmpty(sensorValue)) { |
| | | return null; |
| | | } |
| | | double aDouble = Double.parseDouble(sensorValue.toString()); |
| | | return DoubleStream.of(aDouble); |
| | | }); |
| | | Double result = null; |
| | | if ("avg".equals(type)) { |
| | | OptionalDouble optionalDouble = doubleStream.average(); |
| | | if (optionalDouble.isPresent()) { |
| | | result = optionalDouble.getAsDouble(); |
| | | } |
| | | } else if ("sum".equals(type)) { |
| | | result = doubleStream.sum(); |
| | | } |
| | | return result; |
| | | } |
| | | } |