kaiyu
2022-03-14 5236663d1e40ca9ed0cbc0885cbc67a67c47943f
screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java
@@ -16,14 +16,18 @@
import java.util.Set;
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;
@@ -34,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;
@@ -78,7 +83,10 @@
    @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");
@@ -141,13 +149,13 @@
    }
    @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":
@@ -166,263 +174,423 @@
    }
    //贡献率,年
    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<>();
        //该组织所有设备信息
        List allMacs = getMacsByOrgId(orgId);
        //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) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("time", yearMonth.split("-")[1]);
            yearMonth = yearMonth + "-01 00:00:00";
            //所有设备该因子累加值
            Double allDeviceSum = null;
            QueryWrapper<HistoryMonthly> historyMonthlyQueryWrapper = new QueryWrapper<>();
            historyMonthlyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result")
                    .eq("time", yearMonth)
                    .in("mac", allMacs);
            Map<String, Object> allDeviceSumMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper);
            if (!ObjectUtils.isEmpty(allDeviceSumMap)) {
                allDeviceSum = (Double) allDeviceSumMap.get("result");
            List<HistoryMonthly> historyMonthlyList = allDeviceDataMap.get(yearMonth);
            if (!ObjectUtils.isEmpty(historyMonthlyList)) {
                allDeviceSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum");
            }
            allDeviceSumMap.put(yearMonth, allDeviceSum);
            //本市数据
            //本市值
            Double cityValue = null;
            //根据组织获取区域定位
            Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode();
            QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>();
            cityAqiMonthlyQueryWrapper.select("`value`->'$." + sensorCode + "' AS result")
                    .eq("city_code", locationLevelCode)
                    .eq("time", yearMonth);
            Map<String, Object> cityValueMap = cityAqiMonthlyService.getMap(cityAqiMonthlyQueryWrapper);
            if (!ObjectUtils.isEmpty(cityValueMap)) {
                cityValue = (Double) cityValueMap.get("result");
            if (cityAqiMap.get(yearMonth) != null) {
                Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonth).toString(), Map.class);
                //判断城市aqi是否有该因子数据
                String sensorName = senorMap.get(sensorCode);
                if (sensorName != null) {
                    cityValue = Double.parseDouble(dataValue.get(sensorName).toString());
                }
            }
            resultMap.put("cityValue", cityValue);
            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) {
                Map<String, Object> professionMap = new HashMap<>();
                //获取该行业设备
                List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
                List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList());
        for (String profession : professions) {
            //每个行业数据结果集
            HashMap<String, Object> professionMap = new HashMap<>();
            List<Map<String, Object>> dataList = new ArrayList<>();
                //该行业累加值
                Double professionSum = null;
                historyMonthlyQueryWrapper.clear();
                historyMonthlyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result")
                        .eq("time", yearMonth)
                        .in("mac", professionMacs);
                Map<String, Object> professionSumMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper);
                if (!ObjectUtils.isEmpty(professionSumMap)) {
                    professionSum = (Double) professionSumMap.get("result");
            //查询行业名称
            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());
                }
                //该行业平均值
                Double professionAvg = null;
                historyMonthlyQueryWrapper.clear();
                historyMonthlyQueryWrapper.select("AVG(`value`->'$." + sensorCode + "') AS result")
                        .eq("time", yearMonth)
                        .in("mac", professionMacs);
                Map<String, Object> professionAvgMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper);
                if (!ObjectUtils.isEmpty(professionAvgMap)) {
                    professionAvg = (Double) professionAvgMap.get("result");
                }
                //行业贡献率计算
                List<HistoryMonthly> professionDeviceHistoryMonthlyList = professionDataMap.get(resultTime);
                //贡献率
                String contributionRate = null;
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                if (professionSum != null && allDeviceSum != null) {
                    contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%";
                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) + "%";
                    }
                }
                //行业均值
                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                //行业贡献率
                professionMap.put("contributionRate", contributionRate);
                resultMap.put(profession, professionMap);
                timeDataMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                //贡献率
                timeDataMap.put("name", contributionRate);
                dataList.add(timeDataMap);
            }
            result.add(resultMap);
            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<>();
        //该组织所有设备信息
        List allMacs = getMacsByOrgId(orgId);
        //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) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("time", yearMonthDay.split("-")[2]);
            yearMonthDay = yearMonthDay + " 00:00:00";
            //所有设备该因子累加值
            Double allDeviceSum = null;
            QueryWrapper<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>();
            historyDailyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result")
                    .eq("time", yearMonthDay)
                    .in("mac", allMacs);
            Map<String, Object> allDeviceSumMap = historyDailyService.getMap(historyDailyQueryWrapper);
            if (!ObjectUtils.isEmpty(allDeviceSumMap)) {
                allDeviceSum = (Double) allDeviceSumMap.get("result");
            List<HistoryDaily> historyDailyList = allDeviceDataMap.get(yearMonthDay);
            if (!ObjectUtils.isEmpty(historyDailyList)) {
                allDeviceSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum");
            }
            allDeviceSumMap.put(yearMonthDay, allDeviceSum);
            //本市数据
            //本市值
            Double cityValue = null;
            //根据组织获取区域定位
            Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode();
            QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>();
            cityAqiDailyQueryWrapper.select("`value`->'$." + sensorCode + "' AS result")
                    .eq("city_code", locationLevelCode)
                    .eq("time", yearMonthDay);
            Map<String, Object> cityValueMap = cityAqiDailyService.getMap(cityAqiDailyQueryWrapper);
            if (!ObjectUtils.isEmpty(cityValueMap)) {
                cityValue = (Double) cityValueMap.get("result");
            if (cityAqiMap.get(yearMonthDay) != null) {
                Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDay).toString(), Map.class);
                //判断城市aqi是否有该因子数据
                String sensorName = senorMap.get(sensorCode);
                if (sensorName != null) {
                    cityValue = Double.parseDouble(dataValue.get(sensorName).toString());
                }
            }
            resultMap.put("cityValue", cityValue);
            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) {
                Map<String, Object> professionMap = new HashMap<>();
                //获取该行业设备
                List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
                List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList());
        for (String profession : professions) {
            //每个行业数据结果集
            HashMap<String, Object> professionMap = new HashMap<>();
            List<Map<String, Object>> dataList = new ArrayList<>();
                //该行业累加值
                Double professionSum = null;
                historyDailyQueryWrapper.clear();
                historyDailyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result")
                        .eq("time", yearMonthDay)
                        .in("mac", professionMacs);
                Map<String, Object> professionSumMap = historyDailyService.getMap(historyDailyQueryWrapper);
                if (!ObjectUtils.isEmpty(professionSumMap)) {
                    professionSum = (Double) professionSumMap.get("result");
            //查询行业名称
            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;
                historyDailyQueryWrapper.clear();
                historyDailyQueryWrapper.select("AVG(`value`->'$." + sensorCode + "') AS result")
                        .eq("time", yearMonthDay)
                        .in("mac", professionMacs);
                Map<String, Object> professionAvgMap = historyDailyService.getMap(historyDailyQueryWrapper);
                if (!ObjectUtils.isEmpty(professionAvgMap)) {
                    professionAvg = (Double) professionAvgMap.get("result");
                }
                //行业贡献率计算
                String contributionRate = null;
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                if (professionSum != null && allDeviceSum != null) {
                    contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%";
                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) + "%";
                    }
                }
                //行业均值
                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                //行业贡献率
                professionMap.put("contributionRate", contributionRate);
                resultMap.put(profession, professionMap);
                timeDataMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                //贡献率
                timeDataMap.put("name", contributionRate);
                dataList.add(timeDataMap);
            }
            result.add(resultMap);
            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<>();
        //该组织所有设备信息
        List allMacs = getMacsByOrgId(orgId);
        //1.时间信息结果
        List<String> timeLag = DateUtils.getTimeLag(time);
        for (String yearMonthDayHour : timeLag) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("time", yearMonthDayHour.split(" ")[1]);
            yearMonthDayHour = yearMonthDayHour + ":00:00";
        result.put("time", timeLag);
            //所有设备该因子累加值
            Double allDeviceSum = historyHourlyService.getSensorSumByMacs(sensorCode, allMacs, yearMonthDayHour);
        //根据组织获取区域定位
        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();
        }
            //本市数据
            Double cityValue = null;
            //根据组织获取区域定位
            Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode();
            QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>();
            cityAqiQueryWrapper.select("`value`->'$." + sensorCode + "' AS result")
                    .eq("city_code", locationLevelCode)
                    .eq("time", yearMonthDayHour);
            Map<String, Object> cityValueMap = cityAqiService.getMap(cityAqiQueryWrapper);
            if (!ObjectUtils.isEmpty(cityValueMap)) {
                cityValue = (Double) cityValueMap.get("result");
        //本市本日所有小时数据
        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"));
            }
            resultMap.put("cityValue", cityValue);
        }
            for (String profession : professions) {
                Map<String, Object> professionMap = new HashMap<>();
        //该组织所有设备信息,本日所有小时数据
        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)));
                //获取该行业设备
                List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
                List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList());
                //获取该行业设备数据,累加值,平均值
                Double professionAvg = historyHourlyService.getSensorAvgByMacs(sensorCode, professionMacs, yearMonthDayHour);
                Double professionSum = historyHourlyService.getSensorSumByMacs(sensorCode, professionMacs, yearMonthDayHour);
        //本市数据结果集
        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;
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                if (professionSum != null && allDeviceSum != null) {
                    contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%";
                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) + "%";
                    }
                }
                //行业均值
                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                //行业贡献率
                professionMap.put("contributionRate", contributionRate);
                resultMap.put(profession, professionMap);
                timeDataMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                //贡献率
                timeDataMap.put("name", contributionRate);
                dataList.add(timeDataMap);
            }
            result.add(resultMap);
            professionMap.put("data", dataList);
            result.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);
    }
}