From 28776b56db3bbd2fbbfd64394e40aa11a6b7ea29 Mon Sep 17 00:00:00 2001 From: cjl <276999030@qq.com> Date: Wed, 15 Nov 2023 17:30:53 +0800 Subject: [PATCH] fix:补充提交 --- screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java | 539 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 522 insertions(+), 17 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..18ae1e2 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,40 @@ 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.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.text.NumberFormat; +import java.util.*; import java.util.stream.Collectors; +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.entity.SysDictData; +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.api.service.SensorService; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; + +import com.moral.util.AmendUtils; +import com.moral.util.DateUtils; /** * <p> @@ -37,14 +55,51 @@ @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; + + @Autowired + private SensorService sensorService; + + private static final 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) { //������������������������������ List<Map<String, Object>> devices = deviceService.getDevicesByOrganizationId(organizationId); Set<Map<String, Object>> result = new HashSet<>(); for (Map<String, Object> device : devices) { - List<Map<String, Object>> professions = (List<Map<String, Object>>) device.get("professions"); - result.addAll(professions); + if(Objects.nonNull(device)&&Objects.nonNull(device.get("professions"))&&device.containsKey("professions")){ + List<Map<String, Object>> professions = (List<Map<String, Object>>) device.get("professions"); + result.addAll(professions); + } } return result; } @@ -55,6 +110,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 +140,452 @@ } 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 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(); + Map<String, Object> result = new HashMap<>(); + + 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 Map<String, Object> professionContributionOfYear(Integer orgId, List<String> professions, String time, String sensorCode) { + Map<String, Object> result = new HashMap<>(); + + //1.������������������ + List<String> timeLag = DateUtils.getTimeLag(time); + result.put("time", timeLag); + + + //������������������������������ + QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); + organizationQueryWrapper.select("location_level_code") + .eq("id", orgId); + Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); + + + //��������������������������� + 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<String> allMacs = deviceService.getMacsByOrganizationId(orgId); + List<HistoryMonthly> allDeviceData = historyMonthlyService.getValueByMacs(allMacs, time); + Map<String, List<HistoryMonthly>> allDeviceDataMap = allDeviceData.stream() + .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); + + + //��������������������� + Map<String, Object> resultCityDataMap = new HashMap<>(); + List<Map<String, Object>> cityDataList = new ArrayList<>(); + //������������������������������������ + Map<String, Object> allDeviceSumMap = new HashMap<>(); + for (String yearMonth : timeLag) { + //������������������������������ + Double allDeviceSum = null; + List<HistoryMonthly> historyMonthlyList = allDeviceDataMap.get(yearMonth); + if (!ObjectUtils.isEmpty(historyMonthlyList)) { + allDeviceSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum"); + } + allDeviceSumMap.put(yearMonth, allDeviceSum); + + + //��������� + Double cityValue = null; + if (cityAqiMap.get(yearMonth) != null) { + Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonth).toString(), Map.class); + //������������aqi������������������������ + String sensorName = senorMap.get(sensorCode); + if (sensorName != null) { + cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); + } + } + Map<String, Object> cityDataMap = new HashMap<>(); + //������������ + cityDataMap.put("value", cityValue); + cityDataList.add(cityDataMap); + } + + //2.������������������ + resultCityDataMap.put("data", cityDataList); + result.put("������", resultCityDataMap); + + + for (String profession : professions) { + //��������������������������� + HashMap<String, Object> professionMap = new HashMap<>(); + List<Map<String, Object>> dataList = new ArrayList<>(); + + + //������������������ + 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); + Map<String, List<HistoryMonthly>> professionDataMap = professionDeviceData.stream() + .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); + + + for (String resultTime : timeLag) { + //��������������������� + Map<String, Object> timeDataMap = new HashMap<>(); + Object o = allDeviceSumMap.get(resultTime); + Double allDeviceSum = null; + if (o != null) { + allDeviceSum = Double.parseDouble(o.toString()); + } + + List<HistoryMonthly> professionDeviceHistoryMonthlyList = professionDataMap.get(resultTime); + //��������� + String contributionRate = null; + Double professionAvg = null; + if (!ObjectUtils.isEmpty(professionDeviceHistoryMonthlyList)) { + //��������������������� + professionAvg = historyMonthlyService.calculatedValue(professionDeviceHistoryMonthlyList, sensorCode, "avg"); + //������������������������ + Double professionSum = historyMonthlyService.calculatedValue(professionDeviceHistoryMonthlyList, sensorCode, "sum"); + //��������������������� + NumberFormat numberFormat = NumberFormat.getInstance(); + numberFormat.setMaximumFractionDigits(2); + if (allDeviceSum != null && allDeviceSum != 0d) { + contributionRate = numberFormat.format(professionSum / (allDeviceSum) * 100) + "%"; + } + } + + //������������ + timeDataMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); + //��������� + timeDataMap.put("name", contributionRate); + dataList.add(timeDataMap); + } + professionMap.put("data", dataList); + result.put(professionName, professionMap); + } + return result; + } + + //��������������� + private Map<String, Object> professionContributionOfMonth(Integer orgId, List<String> professions, String time, String sensorCode) { + Map<String, Object> result = new HashMap<>(); + + //1.������������������ + List<String> timeLag = DateUtils.getTimeLag(time); + result.put("time", timeLag); + + + //������������������������������ + QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); + organizationQueryWrapper.select("location_level_code") + .eq("id", orgId); + Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); + + + //��������������������������� + 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<String> allMacs = deviceService.getMacsByOrganizationId(orgId); + List<HistoryDaily> allDeviceData = historyDailyService.getValueByMacs(allMacs, time); + Map<String, List<HistoryDaily>> allDeviceDataMap = allDeviceData.stream() + .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10))); + + + //��������������������� + Map<String, Object> resultCityDataMap = new HashMap<>(); + List<Map<String, Object>> cityDataList = new ArrayList<>(); + //������������������������������������ + Map<String, Object> allDeviceSumMap = new HashMap<>(); + for (String yearMonthDay : timeLag) { + //������������������������������ + Double allDeviceSum = null; + List<HistoryDaily> historyDailyList = allDeviceDataMap.get(yearMonthDay); + if (!ObjectUtils.isEmpty(historyDailyList)) { + allDeviceSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum"); + } + allDeviceSumMap.put(yearMonthDay, allDeviceSum); + + + //��������� + Double cityValue = null; + if (cityAqiMap.get(yearMonthDay) != null) { + Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDay).toString(), Map.class); + //������������aqi������������������������ + String sensorName = senorMap.get(sensorCode); + if (sensorName != null) { + cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); + } + } + Map<String, Object> cityDataMap = new HashMap<>(); + //������������ + cityDataMap.put("value", cityValue); + cityDataList.add(cityDataMap); + } + + //2.������������������ + resultCityDataMap.put("data", cityDataList); + result.put("������", resultCityDataMap); + + + for (String profession : professions) { + //��������������������������� + HashMap<String, Object> professionMap = new HashMap<>(); + List<Map<String, Object>> dataList = new ArrayList<>(); + + + //������������������ + 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); + Map<String, List<HistoryDaily>> professionDataMap = professionDeviceData.stream() + .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10))); + + + for (String resultTime : timeLag) { + //��������������������� + Map<String, Object> timeDataMap = new HashMap<>(); + Object o = allDeviceSumMap.get(resultTime); + Double allDeviceSum = null; + if (o != null) { + allDeviceSum = Double.parseDouble(o.toString()); + } + + List<HistoryDaily> preofessionDeviceHistoryDailyList = professionDataMap.get(resultTime); + //��������� + String contributionRate = null; + Double professionAvg = null; + if (!ObjectUtils.isEmpty(preofessionDeviceHistoryDailyList)) { + //��������������������� + professionAvg = historyDailyService.calculatedValue(preofessionDeviceHistoryDailyList, sensorCode, "avg"); + //������������������������ + Double professionSum = historyDailyService.calculatedValue(preofessionDeviceHistoryDailyList, sensorCode, "sum"); + //��������������������� + NumberFormat numberFormat = NumberFormat.getInstance(); + numberFormat.setMaximumFractionDigits(2); + if (allDeviceSum != null && allDeviceSum != 0d) { + contributionRate = numberFormat.format(professionSum / (allDeviceSum) * 100) + "%"; + } + } + + //������������ + timeDataMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); + //��������� + timeDataMap.put("name", contributionRate); + dataList.add(timeDataMap); + } + professionMap.put("data", dataList); + result.put(professionName, professionMap); + } + return result; + } + + private Map<String, Object> professionContributionOfDay(Integer orgId, List<String> professions, String time, String sensorCode) { + Map<String, Object> result = new HashMap<>(); + + //1.������������������ + List<String> timeLag = DateUtils.getTimeLag(time); + result.put("time", timeLag); + + + //������������������������������ + QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); + organizationQueryWrapper.select("location_level_code") + .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(); + } + + + //������������������������������ + 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<String> allMacs = deviceService.getMacsByOrganizationId(orgId); + List<HistoryHourly> allDeviceData = historyHourlyService.getValueByMacs(allMacs, time); + Map<String, List<HistoryHourly>> allDeviceDataMap = allDeviceData.stream() + .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13))); + + + //��������������������� + Map<String, Object> resultCityDataMap = new HashMap<>(); + List<Map<String, Object>> cityDataList = new ArrayList<>(); + //������������������������������������ + Map<String, Object> allDeviceSumMap = new HashMap<>(); + for (String yearMonthDayHour : timeLag) { + //������������������������������ + Double allDeviceSum = null; + List<HistoryHourly> historyHourlyList = allDeviceDataMap.get(yearMonthDayHour); + if (!ObjectUtils.isEmpty(historyHourlyList)) { + allDeviceSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper); + } + allDeviceSumMap.put(yearMonthDayHour, allDeviceSum); + + + //��������� + Double cityValue = null; + if (cityAqiMap.get(yearMonthDayHour) != null) { + Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDayHour).toString(), Map.class); + //������������aqi������������������������ + String sensorName = senorMap.get(sensorCode); + if (sensorName != null) { + cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); + } + } + Map<String, Object> cityDataMap = new HashMap<>(); + //������������ + cityDataMap.put("value", cityValue); + cityDataList.add(cityDataMap); + } + + //2.������������������ + resultCityDataMap.put("data", cityDataList); + result.put("������", resultCityDataMap); + + + for (String profession : professions) { + //��������������������������� + HashMap<String, Object> professionMap = new HashMap<>(); + List<Map<String, Object>> dataList = new ArrayList<>(); + + + //������������������ + 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); + Map<String, List<HistoryHourly>> professionDataMap = professionDeviceData.stream() + .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13))); + + + for (String resultTime : timeLag) { + //��������������������� + Map<String, Object> timeDataMap = new HashMap<>(); + Object o = allDeviceSumMap.get(resultTime); + Double allDeviceSum = null; + if (o != null) { + allDeviceSum = Double.parseDouble(o.toString()); + } + + List<HistoryHourly> preofessionDeviceHistoryHourlyList = professionDataMap.get(resultTime); + //��������� + String contributionRate = null; + Double professionAvg = null; + if (!ObjectUtils.isEmpty(preofessionDeviceHistoryHourlyList)) { + //��������������������� + professionAvg = historyHourlyService.calculatedValue(preofessionDeviceHistoryHourlyList, sensorCode, "avg", sensorLower, sensorUpper); + //������������������������ + Double professionSum = historyHourlyService.calculatedValue(preofessionDeviceHistoryHourlyList, sensorCode, "sum", sensorLower, sensorUpper); + //��������������������� + NumberFormat numberFormat = NumberFormat.getInstance(); + numberFormat.setMaximumFractionDigits(2); + if (allDeviceSum != null && allDeviceSum != 0d) { + contributionRate = numberFormat.format(professionSum / (allDeviceSum) * 100) + "%"; + } + } + + //������������ + timeDataMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); + //��������� + timeDataMap.put("name", contributionRate); + dataList.add(timeDataMap); + } + professionMap.put("data", dataList); + result.put(professionName, professionMap); + } return result; } } -- Gitblit v1.8.0