From 55621908416cd328d013d2e23d0d7c2dcfb1f9b1 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Tue, 28 Dec 2021 13:16:11 +0800 Subject: [PATCH] 行业贡献率 --- screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java | 523 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 514 insertions(+), 9 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 d67599f..1bad56a 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 @@ -3,22 +3,49 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.util.ObjectUtils; +import java.text.NumberFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; 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; +import com.moral.api.service.CityAqiService; 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.OrganizationService; import com.moral.api.service.ProfessionService; + import com.moral.constant.Constants; import com.moral.constant.RedisConstants; + +import com.moral.util.AmendUtils; +import com.moral.util.DateUtils; /** * <p> @@ -36,6 +63,38 @@ @Autowired private RedisTemplate redisTemplate; + + @Autowired + private HistoryMonthlyService historyMonthlyService; + + @Autowired + private HistoryDailyService historyDailyService; + + @Autowired + private HistoryHourlyService historyHourlyService; + + @Autowired + private CityAqiMonthlyService cityAqiMonthlyService; + + @Autowired + private CityAqiDailyService cityAqiDailyService; + + @Autowired + private CityAqiService cityAqiService; + + @Autowired + private OrganizationService organizationService; + + private static Map<String, String> senorMap = new HashMap<>(); + + static { + senorMap.put(Constants.SENSOR_CODE_PM25, "PM2_5"); + senorMap.put(Constants.SENSOR_CODE_PM10, "PM10"); + senorMap.put(Constants.SENSOR_CODE_SO2, "SO2"); + senorMap.put(Constants.SENSOR_CODE_NO2, "NO2"); + senorMap.put(Constants.SENSOR_CODE_CO, "CO"); + senorMap.put(Constants.SENSOR_CODE_O3, "O3"); + } @Override public Set<Map<String, Object>> getProfessionsByOrganizationId(Integer organizationId) { @@ -55,6 +114,22 @@ //��������������������������������� Integer orgId = Integer.parseInt(params.get("organizationId").toString()); List<String> professions = Arrays.asList(params.get("professions").toString().split(",")); + List<Device> devices = getDevicesOfProfessions(orgId, professions); + //���redis������������������������ + for (Device device : devices) { + device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, device.getMac()); + List<Sensor> sensors = device.getVersion().getSensors(); + for (Sensor sensor : sensors) { + Map<String, Object> sensorMap = new HashMap<>(); + sensorMap.put("sensorCode", sensor.getCode()); + sensorMap.put("sensorName", sensor.getName()); + result.add(sensorMap); + } + } + return result; + } + + private List<Device> getDevicesOfProfessions(Integer orgId, List<String> professions) { QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac", "profession") .eq("organization_id", orgId) @@ -69,18 +144,448 @@ } return false; }).collect(Collectors.toList()); + return devices; + } - //���redis������������������������ - for (Device device : devices) { - device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, device.getMac()); - List<Sensor> sensors = device.getVersion().getSensors(); - for (Sensor sensor : sensors) { - Map<String, Object> sensorMap = new HashMap<>(); - sensorMap.put("sensorCode", sensor.getCode()); - sensorMap.put("sensorName", sensor.getName()); - result.add(sensorMap); + @Override + public List<Map<String, Object>> professionContribution(Map<String, Object> params) { + int orgId = Integer.parseInt(params.get("organizationId").toString()); + List<String> professions = Arrays.asList(params.get("professions").toString().split(",")); + String type = params.get("type").toString(); + String time = params.get("time").toString(); + String sensorCode = params.get("sensorCode").toString(); + List<Map<String, Object>> result = new ArrayList<>(); + + switch (type) { + case "year": + result = professionContributionOfYear(orgId, professions, time, sensorCode); + break; + case "month": + result = professionContributionOfMonth(orgId, professions, time, sensorCode); + break; + case "day": + result = professionContributionOfDay(orgId, professions, time, sensorCode); + break; + default: + break; + } + return result; + } + + //��������������� + 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); + + + //������������������������������ + 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(); + } + resultMap.put("allDeviceSum", allDeviceSum); + + + //��������� + 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; + } + } + resultMap.put("cityValue", cityValue); + 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.remove("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); + } + } + 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); + + //������������������������������ + 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(); + } + resultMap.put("allDeviceSum", allDeviceSum); + + + //��������� + 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; + } + } + resultMap.put("cityValue", cityValue); + 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.remove("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); + } + } + 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); + + + //������������������������������ + 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; + 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); + 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.remove("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); + } + } + 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(); + } return result; } } -- Gitblit v1.8.0