package com.moral.api.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; 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.SysArea; 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.RegionService; import com.moral.api.service.SensorService; import com.moral.api.service.SysAreaService; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; import com.moral.util.AmendUtils; import com.moral.util.DateUtils; @Service public class RegionServiceImpl implements RegionService { @Autowired private DeviceService deviceService; @Autowired private RedisTemplate redisTemplate; @Autowired private OrganizationService organizationService; @Autowired private CityAqiMonthlyService cityAqiMonthlyService; @Autowired private CityAqiDailyService cityAqiDailyService; @Autowired private CityAqiService cityAqiService; @Autowired private HistoryMonthlyService historyMonthlyService; @Autowired private HistoryDailyService historyDailyService; @Autowired private HistoryHourlyService historyHourlyService; @Autowired private SysAreaService sysAreaService; @Autowired private SensorService sensorService; private static Map 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> getRegionsByOrganizationId(Integer organizationId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac") .eq("organization_id", organizationId) .eq("is_delete", Constants.NOT_DELETE); List macs = deviceService.listObjs(queryWrapper); List> devices = new ArrayList<>(); //从redis获取设备 Map entries = redisTemplate.opsForHash().entries(RedisConstants.DEVICE); entries.forEach((key, value) -> { if (macs.contains(key)) { devices.add((Map) value); } }); return devices.stream() .map(device -> (Map) device.get("town")) .collect(Collectors.toSet()); } @Override public Set> getSensorByRegionCodesAndOrganizationId(Map params) { Set> result = new HashSet<>(); Integer orgId = Integer.parseInt(params.get("organizationId").toString()); List regionCodes = Arrays.asList(params.get("regionCodes").toString().split(",")); //获取区域设备macs QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac") .eq("organization_id", orgId) .eq("is_delete", Constants.NOT_DELETE) .in("town_code", regionCodes); List macs = deviceService.listObjs(queryWrapper); //根据设备mac获取设备传感器信息 for (Object mac : macs) { Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac.toString()); List sensors = device.getVersion().getSensors(); sensors.forEach(sensor -> { Map sensorMap = new HashMap<>(); sensorMap.put("sensorCode", sensor.getCode()); sensorMap.put("sensorName", sensor.getName()); result.add(sensorMap); }); } return result; } @Override public List> regionContribution(Map params) { int orgId = Integer.parseInt(params.get("organizationId").toString()); List regionCodes = Arrays.asList(params.get("regionCodes").toString().split(",")); String type = params.get("type").toString(); String time = params.get("time").toString(); String sensorCode = params.get("sensorCode").toString(); List> result = new ArrayList<>(); switch (type) { case "year": result = regionContributionOfYear(orgId, regionCodes, time, sensorCode); break; case "month": result = regionContributionOfMonth(orgId, regionCodes, time, sensorCode); break; case "day": result = regionContributionOfDay(orgId, regionCodes, time, sensorCode); break; default: break; } return result; } //贡献率,年 private List> regionContributionOfYear(Integer orgId, List regionCodes, String time, String sensorCode) { List> result = new ArrayList<>(); //根据组织获取区域定位 QueryWrapper organizationQueryWrapper = new QueryWrapper<>(); organizationQueryWrapper.select("location_level_code") .eq("id", orgId); Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); //该组织所有设备信息 List allMacs = deviceService.getMacsByOrganizationId(orgId); List timeLag = DateUtils.getTimeLag(time); //本市本年所有月数据 QueryWrapper cityAqiMonthlyQueryWrapper = new QueryWrapper<>(); cityAqiMonthlyQueryWrapper.select("time", "value") .eq("city_code", locationLevelCode) .likeRight("time", time); List> cityAqis = cityAqiMonthlyService.listMaps(cityAqiMonthlyQueryWrapper); Map cityAqiMap = new HashMap<>(); if (!ObjectUtils.isEmpty(cityAqis)) { for (Map cityAqi : cityAqis) { cityAqiMap.put(cityAqi.get("time").toString().substring(0, 7), cityAqi.get("value")); } } //所有设备本年所有月数据 List allDeviceData = historyMonthlyService.getValueByMacs(allMacs, time); //按time分组 Map> allDeviceDataMap = allDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); for (String yearMonth : timeLag) { Map resultMap = new HashMap<>(); resultMap.put("time", yearMonth); //所有设备该因子累加值 Double allDeviceSum = null; List historyMonthlyList = allDeviceDataMap.get(yearMonth); if (!ObjectUtils.isEmpty(historyMonthlyList)) { allDeviceSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum"); } resultMap.put("allDeviceSum", allDeviceSum); //本市值 Double cityValue = null; if (cityAqiMap.get(yearMonth) != null) { Map dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonth).toString(), Map.class); //判断城市aqi是否有该因子数据 String sensorName = senorMap.get(sensorCode); if (sensorName != null) { cityValue = (Double) dataValue.get(sensorName); } } resultMap.put("cityValue", cityValue); result.add(resultMap); } for (String regionCode : regionCodes) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("area_name").eq("area_code", Integer.parseInt(regionCode)); String regionName = sysAreaService.getOne(queryWrapper).getAreaName(); //获取乡镇区域下所有设备 List regionMacs = deviceService.getMacsByOrgIdAndRegionCode(orgId, Integer.parseInt(regionCode)); //该乡镇区域设备本年所有月数据 List regionDeviceData = historyMonthlyService.getValueByMacs(regionMacs, time); //按time分组 Map> regionDataMap = regionDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); for (Map map : result) { Object allDeviceSum = map.get("allDeviceSum"); String resultTime = map.get("time").toString(); List historyMonthlyList = regionDataMap.get(resultTime); //贡献率 String contributionRate = null; Double regionAvg = null; if (!ObjectUtils.isEmpty(historyMonthlyList)) { regionAvg = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "avg"); Double regionSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum"); //行业贡献率计算 NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (allDeviceSum != null) { contributionRate = numberFormat.format(regionSum / ((Double) allDeviceSum) * 100) + "%"; System.out.println(regionSum + "===" + allDeviceSum); } } Map professionMap = new HashMap<>(); professionMap.put("contributionRate", contributionRate); professionMap.put("value", regionAvg == null ? null : AmendUtils.sciCal(regionAvg, 0)); map.put(regionName, professionMap); } } result.forEach(map -> map.remove("allDeviceSum")); return result; } //贡献率,月 private List> regionContributionOfMonth(Integer orgId, List regionCodes, String time, String sensorCode) { List> result = new ArrayList<>(); //根据组织获取区域定位 QueryWrapper organizationQueryWrapper = new QueryWrapper<>(); organizationQueryWrapper.select("location_level_code") .eq("id", orgId); Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); //该组织所有设备信息 List allMacs = deviceService.getMacsByOrganizationId(orgId); //日时间点 List timeLag = DateUtils.getTimeLag(time); //本市本月所有日数据 QueryWrapper cityAqiDailyQueryWrapper = new QueryWrapper<>(); cityAqiDailyQueryWrapper.select("time", "value") .eq("city_code", locationLevelCode) .likeRight("time", time); List> cityAqis = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper); Map cityAqiMap = new HashMap<>(); if (!ObjectUtils.isEmpty(cityAqis)) { for (Map cityAqi : cityAqis) { cityAqiMap.put(cityAqi.get("time").toString().substring(0, 10), cityAqi.get("value")); } } //所有设备本月所有日数据 List allDeviceData = historyDailyService.getValueByMacs(allMacs, time); //按time分组 Map> allDeviceDataMap = allDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10))); for (String yearMonthDay : timeLag) { Map resultMap = new HashMap<>(); resultMap.put("time", yearMonthDay); //所有设备该因子累加值 Double allDeviceSum = null; List historyDailyList = allDeviceDataMap.get(yearMonthDay); if (!ObjectUtils.isEmpty(historyDailyList)) { allDeviceSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum"); } resultMap.put("allDeviceSum", allDeviceSum); //本市值 Double cityValue = null; if (cityAqiMap.get(yearMonthDay) != null) { Map dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDay).toString(), Map.class); //判断城市aqi是否有该因子数据 String sensorName = senorMap.get(sensorCode); if (sensorName != null) { cityValue = (Double) dataValue.get(sensorName); } } resultMap.put("cityValue", cityValue); result.add(resultMap); } for (String regionCode : regionCodes) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("area_name").eq("area_code", Integer.parseInt(regionCode)); String regionName = sysAreaService.getOne(queryWrapper).getAreaName(); //获取乡镇区域下所有设备 List regionMacs = deviceService.getMacsByOrgIdAndRegionCode(orgId, Integer.parseInt(regionCode)); //该行业设备本月所有日数据 List professionDeviceData = historyDailyService.getValueByMacs(regionMacs, time); //按time分组 Map> professionDataMap = professionDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10))); for (Map map : result) { Object allDeviceSum = map.get("allDeviceSum"); String resultTime = map.get("time").toString(); List historyDailyList = professionDataMap.get(resultTime); //贡献率 String contributionRate = null; Double regionAvg = null; if (!ObjectUtils.isEmpty(historyDailyList)) { //该行业均值计算 regionAvg = historyDailyService.calculatedValue(historyDailyList, sensorCode, "avg"); //该行业累加值计算 Double regionSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum"); //行业贡献率计算 NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (allDeviceSum != null) { contributionRate = numberFormat.format(regionSum / ((Double) allDeviceSum) * 100) + "%"; } } Map professionMap = new HashMap<>(); professionMap.put("contributionRate", contributionRate); professionMap.put("value", regionAvg == null ? null : AmendUtils.sciCal(regionAvg, 0)); map.put(regionName, professionMap); } } result.forEach(map -> map.remove("allDeviceSum")); return result; } private List> regionContributionOfDay(Integer orgId, List regionCodes, String time, String sensorCode) { List> result = new ArrayList<>(); //根据组织获取区域定位 QueryWrapper organizationQueryWrapper = new QueryWrapper<>(); organizationQueryWrapper.select("location_level_code") .eq("id", orgId); Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); //查询因子上下限 QueryWrapper 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 = deviceService.getMacsByOrganizationId(orgId); //小时时间点 List timeLag = DateUtils.getTimeLag(time); //本市本日所有小时数据 QueryWrapper cityAqiQueryWrapper = new QueryWrapper<>(); cityAqiQueryWrapper.select("time", "value") .eq("city_code", locationLevelCode) .likeRight("time", time); List> cityAqis = cityAqiService.listMaps(cityAqiQueryWrapper); Map cityAqiMap = new HashMap<>(); if (!ObjectUtils.isEmpty(cityAqis)) { for (Map cityAqi : cityAqis) { cityAqiMap.put(cityAqi.get("time").toString().substring(0, 13), cityAqi.get("value")); } } //所有设备本日所有小时数据 List allDeviceData = historyHourlyService.getValueByMacs(allMacs, time); //按time分组 Map> allDeviceDataMap = allDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13))); for (String yearMonthDayHour : timeLag) { Map resultMap = new HashMap<>(); resultMap.put("time", yearMonthDayHour); //所有设备该因子累加值 Double allDeviceSum = null; List historyHourlyList = allDeviceDataMap.get(yearMonthDayHour); if (!ObjectUtils.isEmpty(historyHourlyList)) { allDeviceSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper); } resultMap.put("allDeviceSum", allDeviceSum); //本市值 Double cityValue = null; if (cityAqiMap.get(yearMonthDayHour) != null) { Map 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()); } } resultMap.put("cityValue", cityValue); result.add(resultMap); } for (String regionCode : regionCodes) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("area_name").eq("area_code", Integer.parseInt(regionCode)); String regionName = sysAreaService.getOne(queryWrapper).getAreaName(); //获取乡镇区域下所有设备 List regionMacs = deviceService.getMacsByOrgIdAndRegionCode(orgId, Integer.parseInt(regionCode)); //该行业设备本日所有小时数据 List professionDeviceData = historyHourlyService.getValueByMacs(regionMacs, time); //按time分组 Map> professionDataMap = professionDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13))); for (Map map : result) { Object allDeviceSum = map.get("allDeviceSum"); String resultTime = map.get("time").toString(); List historyHourlyList = professionDataMap.get(resultTime); //贡献率 String contributionRate = null; Double regionAvg = null; if (!ObjectUtils.isEmpty(historyHourlyList)) { //该行业均值计算 regionAvg = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "avg", sensorLower, sensorUpper); //该行业累加值计算 Double regionSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper); //行业贡献率计算 NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (allDeviceSum != null) { contributionRate = numberFormat.format(regionSum / ((Double) allDeviceSum) * 100) + "%"; } } Map professionMap = new HashMap<>(); professionMap.put("contributionRate", contributionRate); professionMap.put("value", regionAvg == null ? null : AmendUtils.sciCal(regionAvg, 0)); map.put(regionName, professionMap); } } result.forEach(map -> map.remove("allDeviceSum")); return result; } }