|  |  | 
 |  |  | 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.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.OrganizationService; | 
 |  |  | import com.moral.api.service.ProfessionService; | 
 |  |  |  | 
 |  |  | import com.moral.api.service.SensorService; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.constant.RedisConstants; | 
 |  |  |  | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private OrganizationService organizationService; | 
 |  |  |  | 
 |  |  |     private static Map<String, String> senorMap = new HashMap<>(); | 
 |  |  |     @Autowired | 
 |  |  |     private SensorService sensorService; | 
 |  |  |  | 
 |  |  |     private static final Map<String, String> senorMap = new HashMap<>(); | 
 |  |  |  | 
 |  |  |     static { | 
 |  |  |         senorMap.put(Constants.SENSOR_CODE_PM25, "PM2_5"); | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<Map<String, Object>> professionContribution(Map<String, Object> params) { | 
 |  |  |     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(); | 
 |  |  |         List<Map<String, Object>> result = new ArrayList<>(); | 
 |  |  |         Map<String, Object> result = new HashMap<>(); | 
 |  |  |  | 
 |  |  |         switch (type) { | 
 |  |  |             case "year": | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //贡献率,年 | 
 |  |  |     private List<Map<String, Object>> professionContributionOfYear(Integer orgId, List<String> professions, String time, String sensorCode) { | 
 |  |  |         List<Map<String, Object>> result = new ArrayList<>(); | 
 |  |  |     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<>(); | 
 |  |  | 
 |  |  |                 .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) | 
 |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //所有设备本月所有日数据 | 
 |  |  |         //该组织所有设备信息,本年所有月数据 | 
 |  |  |         List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); | 
 |  |  |         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))); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //本市数据结果集 | 
 |  |  |         Map<String, Object> resultCityDataMap = new HashMap<>(); | 
 |  |  |         List<Map<String, Object>> cityDataList = new ArrayList<>(); | 
 |  |  |         //所有设备每个时间点,和值 | 
 |  |  |         Map<String, Object> allDeviceSumMap = new HashMap<>(); | 
 |  |  |         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(); | 
 |  |  |                 allDeviceSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum"); | 
 |  |  |             } | 
 |  |  |             resultMap.put("allDeviceSum", allDeviceSum); | 
 |  |  |             allDeviceSumMap.put(yearMonth, 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; | 
 |  |  |                 //判断城市aqi是否有该因子数据 | 
 |  |  |                 String sensorName = senorMap.get(sensorCode); | 
 |  |  |                 if (sensorName != null) { | 
 |  |  |                     cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             resultMap.put("cityValue", cityValue); | 
 |  |  |             result.add(resultMap); | 
 |  |  |             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); | 
 |  |  |             //按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); | 
 |  |  |             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(historyMonthlyList)) { | 
 |  |  |                     Supplier<Stream<HistoryMonthly>> streamSupplier = historyMonthlyList::stream; | 
 |  |  |                     professionAvg = calculatedValueOfYear(streamSupplier, sensorCode, "avg"); | 
 |  |  |                     Double professionSum = calculatedValueOfYear(streamSupplier, sensorCode, "sum"); | 
 |  |  |                 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) { | 
 |  |  |                         contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; | 
 |  |  |                     if (allDeviceSum != null && allDeviceSum != 0d) { | 
 |  |  |                         contributionRate = numberFormat.format(professionSum / (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); | 
 |  |  |  | 
 |  |  |                 //行业均值 | 
 |  |  |                 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 List<Map<String, Object>> professionContributionOfMonth(Integer orgId, List<String> professions, String time, String sensorCode) { | 
 |  |  |         List<Map<String, Object>> result = new ArrayList<>(); | 
 |  |  |     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<>(); | 
 |  |  | 
 |  |  |                 .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) | 
 |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //所有设备月日数据 | 
 |  |  |         //该组织所有设备信息,本月所有日数据 | 
 |  |  |         List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); | 
 |  |  |         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))); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //本市数据结果集 | 
 |  |  |         Map<String, Object> resultCityDataMap = new HashMap<>(); | 
 |  |  |         List<Map<String, Object>> cityDataList = new ArrayList<>(); | 
 |  |  |         //所有设备每个时间点,和值 | 
 |  |  |         Map<String, Object> allDeviceSumMap = new HashMap<>(); | 
 |  |  |         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(); | 
 |  |  |                 allDeviceSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum"); | 
 |  |  |             } | 
 |  |  |             resultMap.put("allDeviceSum", allDeviceSum); | 
 |  |  |             allDeviceSumMap.put(yearMonthDay, 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; | 
 |  |  |                 //判断城市aqi是否有该因子数据 | 
 |  |  |                 String sensorName = senorMap.get(sensorCode); | 
 |  |  |                 if (sensorName != null) { | 
 |  |  |                     cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             resultMap.put("cityValue", cityValue); | 
 |  |  |             result.add(resultMap); | 
 |  |  |             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); | 
 |  |  |             //按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); | 
 |  |  |             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(historyDailyList)) { | 
 |  |  |                     Supplier<Stream<HistoryDaily>> streamSupplier = historyDailyList::stream; | 
 |  |  |                     professionAvg = calculatedValueOfMonth(streamSupplier, sensorCode, "avg"); | 
 |  |  |                     Double professionSum = calculatedValueOfMonth(streamSupplier, sensorCode, "sum"); | 
 |  |  |                 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) { | 
 |  |  |                         contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; | 
 |  |  |                     if (allDeviceSum != null && allDeviceSum != 0d) { | 
 |  |  |                         contributionRate = numberFormat.format(professionSum / (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); | 
 |  |  |  | 
 |  |  |                 //行业均值 | 
 |  |  |                 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 List<Map<String, Object>> professionContributionOfDay(Integer orgId, List<String> professions, String time, String sensorCode) { | 
 |  |  |         List<Map<String, Object>> result = new ArrayList<>(); | 
 |  |  |     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<>(); | 
 |  |  | 
 |  |  |                 .eq("id", orgId); | 
 |  |  |         Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); | 
 |  |  |  | 
 |  |  |         //该组织所有设备信息 | 
 |  |  |         List allMacs = getMacsByOrgId(orgId); | 
 |  |  |  | 
 |  |  |         //小时时间点 | 
 |  |  |         List<String> timeLag = DateUtils.getTimeLag(time); | 
 |  |  |         //查询因子上下限 | 
 |  |  |         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) | 
 |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //所有设备当天小时数据 | 
 |  |  |         //该组织所有设备信息,本日所有小时数据 | 
 |  |  |         List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); | 
 |  |  |         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))); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //本市数据结果集 | 
 |  |  |         Map<String, Object> resultCityDataMap = new HashMap<>(); | 
 |  |  |         List<Map<String, Object>> cityDataList = new ArrayList<>(); | 
 |  |  |         //所有设备每个时间点,和值 | 
 |  |  |         Map<String, Object> allDeviceSumMap = new HashMap<>(); | 
 |  |  |         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(); | 
 |  |  |                 allDeviceSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper); | 
 |  |  |             } | 
 |  |  |             resultMap.put("allDeviceSum", allDeviceSum); | 
 |  |  |             allDeviceSumMap.put(yearMonthDayHour, 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; | 
 |  |  |                 //判断城市aqi是否有该因子数据 | 
 |  |  |                 String sensorName = senorMap.get(sensorCode); | 
 |  |  |                 if (sensorName != null) { | 
 |  |  |                     cityValue = Double.parseDouble(dataValue.get(sensorName).toString()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             resultMap.put("cityValue", cityValue); | 
 |  |  |             result.add(resultMap); | 
 |  |  |             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); | 
 |  |  |             //按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); | 
 |  |  |             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(historyHourlyList)) { | 
 |  |  |                     Supplier<Stream<HistoryHourly>> streamSupplier = historyHourlyList::stream; | 
 |  |  |                     professionAvg = calculatedValueOfDay(streamSupplier, sensorCode, "avg"); | 
 |  |  |                     Double professionSum = calculatedValueOfDay(streamSupplier, sensorCode, "sum"); | 
 |  |  |                 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) { | 
 |  |  |                         contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; | 
 |  |  |                     if (allDeviceSum != null && allDeviceSum != 0d) { | 
 |  |  |                         contributionRate = numberFormat.format(professionSum / (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(); | 
 |  |  |                 //行业均值 | 
 |  |  |                 timeDataMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); | 
 |  |  |                 //贡献率 | 
 |  |  |                 timeDataMap.put("name", contributionRate); | 
 |  |  |                 dataList.add(timeDataMap); | 
 |  |  |             } | 
 |  |  |         } 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(); | 
 |  |  |             professionMap.put("data", dataList); | 
 |  |  |             result.put(professionName, professionMap); | 
 |  |  |         } | 
 |  |  |         return result; | 
 |  |  |     } |