jinpengyong
2021-12-29 48e498136c8784ee79a698da2c852ca3aa0549ab
screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java
@@ -13,12 +13,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -31,6 +27,7 @@
import com.moral.api.entity.HistoryMonthly;
import com.moral.api.entity.Organization;
import com.moral.api.entity.Sensor;
import com.moral.api.entity.SysDictData;
import com.moral.api.service.CityAqiDailyService;
import com.moral.api.service.CityAqiMonthlyService;
import com.moral.api.service.CityAqiService;
@@ -41,6 +38,7 @@
import com.moral.api.service.OrganizationService;
import com.moral.api.service.ProfessionService;
import com.moral.api.service.SensorService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
@@ -84,6 +82,9 @@
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private SensorService sensorService;
    private static Map<String, String> senorMap = new HashMap<>();
@@ -183,7 +184,7 @@
        Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode();
        //该组织所有设备信息
        List allMacs = getMacsByOrgId(orgId);
        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
        List<String> timeLag = DateUtils.getTimeLag(time);
@@ -217,15 +218,7 @@
            Double allDeviceSum = null;
            List<HistoryMonthly> historyMonthlyList = allDeviceDataMap.get(yearMonth);
            if (!ObjectUtils.isEmpty(historyMonthlyList)) {
                allDeviceSum = historyMonthlyList.stream().flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class);
                    Object o = dataValue.get(sensorCode);
                    if (o == null) {
                        return null;
                    }
                    double aDouble = Double.parseDouble(o.toString());
                    return DoubleStream.of(aDouble);
                }).sum();
                allDeviceSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum");
            }
            resultMap.put("allDeviceSum", allDeviceSum);
@@ -234,9 +227,10 @@
            Double cityValue = null;
            if (cityAqiMap.get(yearMonth) != null) {
                Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonth).toString(), Map.class);
                Object o = dataValue.get(sensorCode);
                if (o != null) {
                    cityValue = (Double) o;
                //判断城市aqi是否有该因子数据
                String sensorName = senorMap.get(sensorCode);
                if (sensorName != null) {
                    cityValue = (Double) dataValue.get(sensorName);
                }
            }
            resultMap.put("cityValue", cityValue);
@@ -244,6 +238,18 @@
        }
        for (String profession : professions) {
            //查询行业名称
            String professionName = null;
            Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY);
            List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession");
            for (SysDictData sysDictData : professionInfo) {
                if (sysDictData.getDataKey().equals(profession)) {
                    professionName = sysDictData.getDataValue();
                    break;
                }
            }
            //获取该行业设备
            List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
            List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList());
@@ -264,9 +270,10 @@
                String contributionRate = null;
                Double professionAvg = null;
                if (!ObjectUtils.isEmpty(historyMonthlyList)) {
                    Supplier<Stream<HistoryMonthly>> streamSupplier = historyMonthlyList::stream;
                    professionAvg = calculatedValueOfYear(streamSupplier, sensorCode, "avg");
                    Double professionSum = calculatedValueOfYear(streamSupplier, sensorCode, "sum");
                    //该行业均值计算
                    professionAvg = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "avg");
                    //该行业累加值计算
                    Double professionSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum");
                    //行业贡献率计算
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    numberFormat.setMaximumFractionDigits(2);
@@ -277,7 +284,7 @@
                Map<String, Object> professionMap = new HashMap<>();
                professionMap.put("contributionRate", contributionRate);
                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                map.put(profession, professionMap);
                map.put(professionName, professionMap);
            }
        }
        result.forEach(map -> map.remove("allDeviceSum"));
@@ -295,7 +302,7 @@
        Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode();
        //该组织所有设备信息
        List allMacs = getMacsByOrgId(orgId);
        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
        //日时间点
        List<String> timeLag = DateUtils.getTimeLag(time);
@@ -330,15 +337,7 @@
            Double allDeviceSum = null;
            List<HistoryDaily> historyDailyList = allDeviceDataMap.get(yearMonthDay);
            if (!ObjectUtils.isEmpty(historyDailyList)) {
                allDeviceSum = historyDailyList.stream().flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class);
                    Object o = dataValue.get(sensorCode);
                    if (o == null) {
                        return null;
                    }
                    double aDouble = Double.parseDouble(o.toString());
                    return DoubleStream.of(aDouble);
                }).sum();
                allDeviceSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum");
            }
            resultMap.put("allDeviceSum", allDeviceSum);
@@ -347,9 +346,10 @@
            Double cityValue = null;
            if (cityAqiMap.get(yearMonthDay) != null) {
                Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDay).toString(), Map.class);
                Object o = dataValue.get(sensorCode);
                if (o != null) {
                    cityValue = (Double) o;
                //判断城市aqi是否有该因子数据
                String sensorName = senorMap.get(sensorCode);
                if (sensorName != null) {
                    cityValue = (Double) dataValue.get(sensorName);
                }
            }
            resultMap.put("cityValue", cityValue);
@@ -357,6 +357,16 @@
        }
        for (String profession : professions) {
            String professionName = null;
            Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY);
            List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession");
            for (SysDictData sysDictData : professionInfo) {
                if (sysDictData.getDataKey().equals(profession)) {
                    professionName = sysDictData.getDataValue();
                    break;
                }
            }
            //获取该行业设备
            List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
            List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList());
@@ -377,9 +387,10 @@
                String contributionRate = null;
                Double professionAvg = null;
                if (!ObjectUtils.isEmpty(historyDailyList)) {
                    Supplier<Stream<HistoryDaily>> streamSupplier = historyDailyList::stream;
                    professionAvg = calculatedValueOfMonth(streamSupplier, sensorCode, "avg");
                    Double professionSum = calculatedValueOfMonth(streamSupplier, sensorCode, "sum");
                    //该行业均值计算
                    professionAvg = historyDailyService.calculatedValue(historyDailyList, sensorCode, "avg");
                    //该行业累加值计算
                    Double professionSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum");
                    //行业贡献率计算
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    numberFormat.setMaximumFractionDigits(2);
@@ -390,7 +401,7 @@
                Map<String, Object> professionMap = new HashMap<>();
                professionMap.put("contributionRate", contributionRate);
                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                map.put(profession, professionMap);
                map.put(professionName, professionMap);
            }
        }
        result.forEach(map -> map.remove("allDeviceSum"));
@@ -406,8 +417,15 @@
                .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 = sensor.getLower();
        Double sensorUpper = sensor.getUpper();
        //该组织所有设备信息
        List allMacs = getMacsByOrgId(orgId);
        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
        //小时时间点
        List<String> timeLag = DateUtils.getTimeLag(time);
@@ -443,15 +461,7 @@
            Double allDeviceSum = null;
            List<HistoryHourly> historyHourlyList = allDeviceDataMap.get(yearMonthDayHour);
            if (!ObjectUtils.isEmpty(historyHourlyList)) {
                allDeviceSum = historyHourlyList.stream().flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class);
                    Object o = dataValue.get(sensorCode);
                    if (o == null) {
                        return null;
                    }
                    double aDouble = Double.parseDouble(o.toString());
                    return DoubleStream.of(aDouble);
                }).sum();
                allDeviceSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper);
            }
            resultMap.put("allDeviceSum", allDeviceSum);
@@ -460,9 +470,10 @@
            Double cityValue = null;
            if (cityAqiMap.get(yearMonthDayHour) != null) {
                Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDayHour).toString(), Map.class);
                Object o = dataValue.get(sensorCode);
                if (o != null) {
                    cityValue = (Double) o;
                //判断城市aqi是否有该因子数据
                String sensorName = senorMap.get(sensorCode);
                if (sensorName != null) {
                    cityValue = (Double) dataValue.get(sensorName);
                }
            }
            resultMap.put("cityValue", cityValue);
@@ -470,6 +481,16 @@
        }
        for (String profession : professions) {
            String professionName = null;
            Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY);
            List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession");
            for (SysDictData sysDictData : professionInfo) {
                if (sysDictData.getDataKey().equals(profession)) {
                    professionName = sysDictData.getDataValue();
                    break;
                }
            }
            //获取该行业设备
            List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
            List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList());
@@ -490,9 +511,10 @@
                String contributionRate = null;
                Double professionAvg = null;
                if (!ObjectUtils.isEmpty(historyHourlyList)) {
                    Supplier<Stream<HistoryHourly>> streamSupplier = historyHourlyList::stream;
                    professionAvg = calculatedValueOfDay(streamSupplier, sensorCode, "avg");
                    Double professionSum = calculatedValueOfDay(streamSupplier, sensorCode, "sum");
                    //该行业均值计算
                    professionAvg = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "avg", sensorLower, sensorUpper);
                    //该行业累加值计算
                    Double professionSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper);
                    //行业贡献率计算
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    numberFormat.setMaximumFractionDigits(2);
@@ -503,92 +525,10 @@
                Map<String, Object> professionMap = new HashMap<>();
                professionMap.put("contributionRate", contributionRate);
                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
                map.put(profession, professionMap);
                map.put(professionName, professionMap);
            }
        }
        result.forEach(map -> map.remove("allDeviceSum"));
        return result;
    }
    //根据组织id获取设备macs
    private List getMacsByOrgId(Integer orgId) {
        //该组织所有设备信息
        QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
        deviceQueryWrapper.select("mac")
                .eq("organization_id", orgId)
                .eq("is_delete", Constants.NOT_DELETE);
        return deviceService.listObjs(deviceQueryWrapper);
    }
    //日累加值,平均值计算
    private Double calculatedValueOfDay(Supplier<Stream<HistoryHourly>> supplier, String sensorCode, String type) {
        DoubleStream doubleStream = supplier.get()
                .flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class);
                    Object sensorValue = dataValue.get(sensorCode);
                    if (ObjectUtils.isEmpty(sensorValue)) {
                        return null;
                    }
                    double aDouble = Double.parseDouble(sensorValue.toString());
                    return DoubleStream.of(aDouble);
                });
        Double result = null;
        if ("avg".equals(type)) {
            OptionalDouble optionalDouble = doubleStream.average();
            if (optionalDouble.isPresent()) {
                result = optionalDouble.getAsDouble();
            }
        } else if ("sum".equals(type)) {
            result = doubleStream.sum();
        }
        return result;
    }
    //月累加值,平均值计算
    private Double calculatedValueOfMonth(Supplier<Stream<HistoryDaily>> supplier, String sensorCode, String type) {
        DoubleStream doubleStream = supplier.get()
                .flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class);
                    Object sensorValue = dataValue.get(sensorCode);
                    if (ObjectUtils.isEmpty(sensorValue)) {
                        return null;
                    }
                    double aDouble = Double.parseDouble(sensorValue.toString());
                    return DoubleStream.of(aDouble);
                });
        Double result = null;
        if ("avg".equals(type)) {
            OptionalDouble optionalDouble = doubleStream.average();
            if (optionalDouble.isPresent()) {
                result = optionalDouble.getAsDouble();
            }
        } else if ("sum".equals(type)) {
            result = doubleStream.sum();
        }
        return result;
    }
    //日累加值,平均值计算
    private Double calculatedValueOfYear(Supplier<Stream<HistoryMonthly>> supplier, String sensorCode, String type) {
        DoubleStream doubleStream = supplier.get()
                .flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject(v.getValue(), Map.class);
                    Object sensorValue = dataValue.get(sensorCode);
                    if (ObjectUtils.isEmpty(sensorValue)) {
                        return null;
                    }
                    double aDouble = Double.parseDouble(sensorValue.toString());
                    return DoubleStream.of(aDouble);
                });
        Double result = null;
        if ("avg".equals(type)) {
            OptionalDouble optionalDouble = doubleStream.average();
            if (optionalDouble.isPresent()) {
                result = optionalDouble.getAsDouble();
            }
        } else if ("sum".equals(type)) {
            result = doubleStream.sum();
        }
        return result;
    }
}