From caf0e22cb16cc01d3775db11b82787d5ba22c402 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Wed, 29 Dec 2021 16:02:27 +0800 Subject: [PATCH] 行业贡献率因子上下限判断 --- screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java | 251 +++++++++++++++++++------------------------------ 1 files changed, 99 insertions(+), 152 deletions(-) diff --git a/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java index 1bad56a..0a7dcfa 100644 --- a/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java +++ b/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java @@ -13,12 +13,8 @@ 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; @@ -31,6 +27,7 @@ import com.moral.api.entity.HistoryMonthly; import com.moral.api.entity.Organization; import com.moral.api.entity.Sensor; +import com.moral.api.entity.SysDictData; import com.moral.api.service.CityAqiDailyService; import com.moral.api.service.CityAqiMonthlyService; import com.moral.api.service.CityAqiService; @@ -41,6 +38,7 @@ import com.moral.api.service.OrganizationService; import com.moral.api.service.ProfessionService; +import com.moral.api.service.SensorService; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; @@ -84,6 +82,9 @@ @Autowired private OrganizationService organizationService; + + @Autowired + private SensorService sensorService; private static Map<String, String> senorMap = new HashMap<>(); @@ -183,11 +184,11 @@ Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); //��������������������������� - List allMacs = getMacsByOrgId(orgId); + List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); List<String> timeLag = DateUtils.getTimeLag(time); - //������������������������ + //��������������������������� QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>(); cityAqiMonthlyQueryWrapper.select("time", "value") .eq("city_code", locationLevelCode) @@ -201,7 +202,7 @@ } - //��������������������������������� + //��������������������������������� List<HistoryMonthly> allDeviceData = historyMonthlyService.getValueByMacs(allMacs, time); //���time������ Map<String, List<HistoryMonthly>> allDeviceDataMap = allDeviceData.stream() @@ -217,15 +218,7 @@ Double allDeviceSum = null; 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(); + allDeviceSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum"); } resultMap.put("allDeviceSum", allDeviceSum); @@ -234,9 +227,10 @@ Double cityValue = null; 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; + //������������aqi������������������������ + String sensorName = senorMap.get(sensorCode); + if (sensorName != null) { + cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); } } resultMap.put("cityValue", cityValue); @@ -244,12 +238,24 @@ } for (String profession : professions) { + //������������������ + String professionName = null; + Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY); + List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession"); + for (SysDictData sysDictData : professionInfo) { + if (sysDictData.getDataKey().equals(profession)) { + professionName = sysDictData.getDataValue(); + break; + } + } + + //��������������������� 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() @@ -257,29 +263,31 @@ for (Map<String, Object> map : result) { - Object allDeviceSum = map.remove("allDeviceSum"); + 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"); + //��������������������� + professionAvg = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "avg"); + //������������������������ + Double professionSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum"); //��������������������� NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); - if (allDeviceSum != null) { + if (allDeviceSum != null && (Double) allDeviceSum != 0d) { 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); + map.put(professionName, professionMap); } } + result.forEach(map -> map.remove("allDeviceSum")); return result; } @@ -294,13 +302,13 @@ Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); //��������������������������� - List allMacs = getMacsByOrgId(orgId); + List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); //������������ List<String> timeLag = DateUtils.getTimeLag(time); - //������������������������ + //��������������������������� QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); cityAqiDailyQueryWrapper.select("time", "value") .eq("city_code", locationLevelCode) @@ -314,7 +322,7 @@ } - //������������������������ + //��������������������������������� List<HistoryDaily> allDeviceData = historyDailyService.getValueByMacs(allMacs, time); //���time������ Map<String, List<HistoryDaily>> allDeviceDataMap = allDeviceData.stream() @@ -329,15 +337,7 @@ Double allDeviceSum = null; 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(); + allDeviceSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum"); } resultMap.put("allDeviceSum", allDeviceSum); @@ -346,9 +346,10 @@ Double cityValue = null; 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; + //������������aqi������������������������ + String sensorName = senorMap.get(sensorCode); + if (sensorName != null) { + cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); } } resultMap.put("cityValue", cityValue); @@ -356,12 +357,22 @@ } for (String profession : professions) { + String professionName = null; + Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY); + List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession"); + for (SysDictData sysDictData : professionInfo) { + if (sysDictData.getDataKey().equals(profession)) { + professionName = sysDictData.getDataValue(); + break; + } + } + //��������������������� 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() @@ -369,29 +380,31 @@ for (Map<String, Object> map : result) { - Object allDeviceSum = map.remove("allDeviceSum"); + 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"); + //��������������������� + professionAvg = historyDailyService.calculatedValue(historyDailyList, sensorCode, "avg"); + //������������������������ + Double professionSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum"); //��������������������� NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); - if (allDeviceSum != null) { + if (allDeviceSum != null && (Double) allDeviceSum != 0d) { 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); + map.put(professionName, professionMap); } } + result.forEach(map -> map.remove("allDeviceSum")); return result; } @@ -404,14 +417,25 @@ .eq("id", orgId); Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); + //��������������������� + QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); + sensorQueryWrapper.select("lower", "upper").eq("code", sensorCode); + Sensor sensor = sensorService.getOne(sensorQueryWrapper); + Double sensorLower = null; + Double sensorUpper = null; + if (sensor != null) { + sensorLower = sensor.getLower(); + sensorUpper = sensor.getUpper(); + } + //��������������������������� - List allMacs = getMacsByOrgId(orgId); + List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); //��������������� List<String> timeLag = DateUtils.getTimeLag(time); - //������������������������������ + //������������������������������ QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>(); cityAqiQueryWrapper.select("time", "value") .eq("city_code", locationLevelCode) @@ -425,7 +449,7 @@ } - //������������������������������ + //������������������������������������ List<HistoryHourly> allDeviceData = historyHourlyService.getValueByMacs(allMacs, time); //���time������ Map<String, List<HistoryHourly>> allDeviceDataMap = allDeviceData.stream() @@ -441,15 +465,7 @@ 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(); + allDeviceSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper); } resultMap.put("allDeviceSum", allDeviceSum); @@ -458,9 +474,10 @@ Double cityValue = null; 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; + //������������aqi������������������������ + String sensorName = senorMap.get(sensorCode); + if (sensorName != null) { + cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); } } resultMap.put("cityValue", cityValue); @@ -468,12 +485,22 @@ } for (String profession : professions) { + String professionName = null; + Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY); + List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession"); + for (SysDictData sysDictData : professionInfo) { + if (sysDictData.getDataKey().equals(profession)) { + professionName = sysDictData.getDataValue(); + break; + } + } + //��������������������� 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() @@ -481,111 +508,31 @@ for (Map<String, Object> map : result) { - Object allDeviceSum = map.remove("allDeviceSum"); + 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"); + //��������������������� + professionAvg = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "avg", sensorLower, sensorUpper); + //������������������������ + Double professionSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper); //��������������������� NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); - if (allDeviceSum != null) { + if (allDeviceSum != null && (Double) allDeviceSum != 0d) { 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); + map.put(professionName, professionMap); } } - return result; - } - - //������������id������������macs - private List getMacsByOrgId(Integer orgId) { - //��������������������������� - QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>(); - deviceQueryWrapper.select("mac") - .eq("organization_id", orgId) - .eq("is_delete", Constants.NOT_DELETE); - 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(); - } + result.forEach(map -> map.remove("allDeviceSum")); return result; } } -- Gitblit v1.8.0