screen-api/src/main/java/com/moral/api/controller/ProfessionController.java
@@ -44,9 +44,13 @@ return ResultMessage.ok(response); } @ApiOperation(value = "获取该组织该行业所有设备的因子", notes = "获取该组织该行业所有设备的因子") @ApiOperation(value = "获取该组织某些行业所有设备的因子", notes = "获取该组织某些行业所有设备的因子") @GetMapping("getSensorByProfessions") public ResultMessage getProfessions(HttpServletRequest request) { @ApiImplicitParams({ @ApiImplicitParam(name = "organizationId", value = "组织id", required = true, paramType = "query", dataType = "Integer"), @ApiImplicitParam(name = "professions", value = "行业key,多个逗号隔开", required = true, paramType = "query", dataType = "String") }) public ResultMessage getSensorByProfessions(HttpServletRequest request) { Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); if (ObjectUtils.isEmpty(params.get("organizationId")) || ObjectUtils.isEmpty(params.get("professions"))) { return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), screen-api/src/main/java/com/moral/api/controller/RegionController.java
New file @@ -0,0 +1,85 @@ package com.moral.api.controller; import groovy.util.logging.Slf4j; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.moral.api.service.RegionService; import com.moral.constant.ResponseCodeEnum; import com.moral.constant.ResultMessage; import com.moral.util.WebUtils; @Slf4j @Api(tags = "区域贡献率模块") @CrossOrigin(origins = "*", maxAge = 3600) @RequestMapping("region") @RestController public class RegionController { @Autowired private RegionService regionService; @ApiOperation(value = "根据组织id获取所有区域列表", notes = "根据组织id获取所有区域列表") @GetMapping("getRegionsByOrganizationId") public ResultMessage getRegionsByOrganizationId(Integer organizationId) { if (ObjectUtils.isEmpty(organizationId)) { return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); } Set<Map<String, Object>> response = regionService.getRegionsByOrganizationId(organizationId); return ResultMessage.ok(response); } @ApiOperation(value = "获取该组织下所选区域下所有传感器信息", notes = "获取该组织下所选区域下所有传感器信息") @GetMapping("getSensorByRegionCodes") @ApiImplicitParams({ @ApiImplicitParam(name = "organizationId", value = "组织id", required = true, paramType = "query", dataType = "Integer"), @ApiImplicitParam(name = "regionCodes", value = "乡镇区域码,多个逗号隔开", required = true, paramType = "query", dataType = "String") }) public ResultMessage getSensorByProfessions(HttpServletRequest request) { Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); if (ObjectUtils.isEmpty(params.get("organizationId")) || ObjectUtils.isEmpty(params.get("regionCodes"))) { return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); } Set<Map<String, Object>> response = regionService.getSensorByRegionCodesAndOrganizationId(params); return ResultMessage.ok(response); } @ApiOperation(value = "区域贡献率", notes = "区域贡献率") @GetMapping("regionContribution") @ApiImplicitParams({ @ApiImplicitParam(name = "organizationId", value = "组织id", required = true, paramType = "query", dataType = "Integer"), @ApiImplicitParam(name = "regionCodes", value = "乡镇区码,多个逗号隔开", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "type", value = "类型,月(2021-12),日(2021-12-24)", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "time", value = "时间", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "sensorCode", value = "传感器code", required = true, paramType = "query", dataType = "String") }) public ResultMessage regionContribution(HttpServletRequest request) { Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); if (ObjectUtils.isEmpty(params.get("organizationId")) || ObjectUtils.isEmpty(params.get("regionCodes")) || ObjectUtils.isEmpty(params.get("type")) || ObjectUtils.isEmpty(params.get("time")) || ObjectUtils.isEmpty(params.get("sensorCode"))) { return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); } List<Map<String, Object>> response = regionService.regionContribution(params); return ResultMessage.ok(response); } } screen-api/src/main/java/com/moral/api/service/DeviceService.java
@@ -31,4 +31,10 @@ //获取当前组织下所有设备列表 List<Map<String, Object>> getDevicesByOrganizationId(Integer orgId); //获取当前组织下设备mac集合 List<String> getMacsByOrganizationId(Integer organizationId); //获取当前组织,当前乡镇区域下设备mac集合 List getMacsByOrgIdAndRegionCode(Integer organizationId,Integer regionCode); } screen-api/src/main/java/com/moral/api/service/HistoryDailyService.java
@@ -72,4 +72,12 @@ * */ List<HistoryDaily> getValueByMacs(List<String> macs, String time); /** * @description 根据类型计算值 * @param list 数据 * @param sensorCode 因子code * @param type 要计算的类型,最大值:max,最小值:min,平均值:sum,平均值:avg, * */ Double calculatedValue(List<HistoryDaily> list, String sensorCode, String type); } screen-api/src/main/java/com/moral/api/service/HistoryHourlyService.java
@@ -74,10 +74,18 @@ List<HistoryHourly> getValueByMacAndTime(String mac, Date startDate, Date endDate); /** * @description: 通过设备和时间获取小时数据 * @param macs List<String> * @param time String 例:2021-12-23 * */ * @description: 通过设备和时间获取小时数据 */ List<HistoryHourly> getValueByMacs(List<String> macs, String time); /** * @param list 数据 * @param sensorCode 因子code * @param type 要计算的类型,最大值:max,最小值:min,平均值:sum,平均值:avg, * @description 根据类型计算值 */ Double calculatedValue(List<HistoryHourly> list, String sensorCode, String type, Double lower, Double upper); } screen-api/src/main/java/com/moral/api/service/HistoryMonthlyService.java
@@ -59,4 +59,12 @@ * @param time String 例:2021 * */ List<HistoryMonthly> getValueByMacs(List<String> macs, String time); /** * @description 根据类型计算值 * @param list 数据 * @param sensorCode 因子code * @param type 要计算的类型,最大值:max,最小值:min,平均值:sum,平均值:avg, * */ Double calculatedValue(List<HistoryMonthly> list, String sensorCode, String type); } screen-api/src/main/java/com/moral/api/service/RegionService.java
New file @@ -0,0 +1,18 @@ package com.moral.api.service; import java.util.List; import java.util.Map; import java.util.Set; public interface RegionService { //根据组织id获取该组织下所有设备所属乡镇街道列表 Set<Map<String, Object>> getRegionsByOrganizationId(Integer organizationId); //获取该组织下所选区域下所有传感器信息 Set<Map<String, Object>> getSensorByRegionCodesAndOrganizationId(Map<String, Object> params); //区域贡献率 List<Map<String, Object>> regionContribution(Map<String, Object> params); } screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -22,7 +22,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * <p> @@ -166,11 +165,33 @@ @Override public List<Map<String, Object>> getDevicesByOrganizationId(Integer orgId) { //从数据库获取mac QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac").eq("organization_id", orgId).eq("is_delete", Constants.NOT_DELETE); List<Device> devices = deviceMapper.selectList(queryWrapper); List macs = getMacsByOrganizationId(orgId); //从redis获取设备详细信息 return devices.stream().map(device -> (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, device.getMac())).collect(Collectors.toList()); List<Map<String, Object>> result = new ArrayList<>(); for (Object mac : macs) { Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac.toString()); result.add(map); } return result; } @Override public List getMacsByOrganizationId(Integer organizationId) { QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac") .eq("organization_id", organizationId) .eq("is_delete", Constants.NOT_DELETE); return deviceMapper.selectObjs(queryWrapper); } @Override public List getMacsByOrgIdAndRegionCode(Integer organizationId,Integer regionCode) { QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac") .eq("organization_id", organizationId) .eq("is_delete", Constants.NOT_DELETE) .eq("town_code", regionCode); return deviceMapper.selectObjs(queryWrapper); } private Device getDeviceByMacFromDB(String mac) { screen-api/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
@@ -1,7 +1,27 @@ package com.moral.api.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.OptionalDouble; 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.entity.Device; import com.moral.api.entity.GeoCoordinate; import com.moral.api.entity.HistoryDaily; @@ -9,21 +29,11 @@ import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.HistoryDailyMapper; import com.moral.api.service.HistoryDailyService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.service.OrganizationService; import com.moral.api.utils.GetCenterPointFromListOfCoordinates; import com.moral.constant.Constants; import com.moral.util.DateUtils; import com.moral.util.PollutantUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** * <p> @@ -337,5 +347,40 @@ return historyDailyMapper.selectList(queryWrapper); } @Override public Double calculatedValue(List<HistoryDaily> list, String sensorCode, String type) { Supplier<Stream<HistoryDaily>> supplier = list::stream; 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; OptionalDouble optionalDouble = null; if ("sum".equals(type)) { result = doubleStream.sum(); } else { if ("min".equals(type)) { optionalDouble = doubleStream.average(); } else if ("max".equals(type)) { optionalDouble = doubleStream.min(); } else if ("avg".equals(type)) { optionalDouble = doubleStream.max(); } if (optionalDouble.isPresent()) { result = optionalDouble.getAsDouble(); } } return result; } } screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
@@ -1,13 +1,36 @@ package com.moral.api.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.OptionalDouble; 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.config.mybatis.MybatisPlusConfig; import com.moral.api.entity.*; import com.moral.api.entity.Device; import com.moral.api.entity.GeoCoordinate; import com.moral.api.entity.HistoryHourly; import com.moral.api.entity.Organization; import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.HistoryHourlyMapper; import com.moral.api.service.HistoryHourlyService; import com.moral.api.service.OrganizationService; import com.moral.api.service.SensorService; import com.moral.api.utils.GetCenterPointFromListOfCoordinates; import com.moral.constant.Constants; import com.moral.constant.SeparateTableType; @@ -16,16 +39,6 @@ import com.moral.util.DateUtils; import com.moral.util.MybatisPLUSUtils; import com.moral.util.PollutantUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.sql.Array; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** * <p> @@ -598,6 +611,62 @@ return multiTableQuery(queryWrapper, tableNames); } @Override public Double calculatedValue(List<HistoryHourly> list, String sensorCode, String type, Double lower, Double upper) { Supplier<Stream<HistoryHourly>> supplier = list::stream; 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; } //数据有效性判断 Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY); if (!Constants.MARKER_BIT_TRUE.equals(flag)) { return null; } double aDouble = Double.parseDouble(sensorValue.toString()); //剔除超出上下限的数据 if (!ObjectUtils.isEmpty(lower)) { if (aDouble < lower) { return null; } } if (!ObjectUtils.isEmpty(upper)) { if (aDouble > upper) { return null; } } return DoubleStream.of(aDouble); }); Double result = null; OptionalDouble optionalDouble = null; if ("sum".equals(type)) { result = doubleStream.sum(); } else { if ("min".equals(type)) { optionalDouble = doubleStream.average(); } else if ("max".equals(type)) { optionalDouble = doubleStream.min(); } else if ("avg".equals(type)) { optionalDouble = doubleStream.max(); } if (optionalDouble.isPresent()) { result = optionalDouble.getAsDouble(); } } return result; } /** * @Description: 多表查询,传入表名集合,以及条件wrapper,返回数据 * @Param: [wrapper, tableNames] screen-api/src/main/java/com/moral/api/service/impl/HistoryMonthlyServiceImpl.java
@@ -11,6 +11,7 @@ import com.moral.api.utils.GetCenterPointFromListOfCoordinates; import com.moral.constant.Constants; import com.moral.util.PollutantUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @@ -18,7 +19,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.DoubleStream; import java.util.stream.Stream; /** * <p> @@ -307,4 +311,39 @@ .in("mac", macs); return historyMonthlyMapper.selectList(queryWrapper); } @Override public Double calculatedValue(List<HistoryMonthly> list, String sensorCode, String type) { Supplier<Stream<HistoryMonthly>> supplier = list::stream; 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; OptionalDouble optionalDouble = null; if ("sum".equals(type)) { result = doubleStream.sum(); } else { if ("min".equals(type)) { optionalDouble = doubleStream.average(); } else if ("max".equals(type)) { optionalDouble = doubleStream.min(); } else if ("avg".equals(type)) { optionalDouble = doubleStream.max(); } if (optionalDouble.isPresent()) { result = optionalDouble.getAsDouble(); } } return result; } } 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; } } screen-api/src/main/java/com/moral/api/service/impl/RegionServiceImpl.java
New file @@ -0,0 +1,498 @@ 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<String, String> senorMap = new HashMap<>(); static { senorMap.put(Constants.SENSOR_CODE_PM25, "PM2_5"); senorMap.put(Constants.SENSOR_CODE_PM10, "PM10"); senorMap.put(Constants.SENSOR_CODE_SO2, "SO2"); senorMap.put(Constants.SENSOR_CODE_NO2, "NO2"); senorMap.put(Constants.SENSOR_CODE_CO, "CO"); senorMap.put(Constants.SENSOR_CODE_O3, "O3"); } @Override public Set<Map<String, Object>> getRegionsByOrganizationId(Integer organizationId) { QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac") .eq("organization_id", organizationId) .eq("is_delete", Constants.NOT_DELETE); List<Object> macs = deviceService.listObjs(queryWrapper); List<Map<String, Object>> devices = new ArrayList<>(); //从redis获取设备 Map<String, Object> entries = redisTemplate.opsForHash().entries(RedisConstants.DEVICE); entries.forEach((key, value) -> { if (macs.contains(key)) { devices.add((Map<String, Object>) value); } }); return devices.stream() .map(device -> (Map<String, Object>) device.get("town")) .collect(Collectors.toSet()); } @Override public Set<Map<String, Object>> getSensorByRegionCodesAndOrganizationId(Map<String, Object> params) { Set<Map<String, Object>> result = new HashSet<>(); Integer orgId = Integer.parseInt(params.get("organizationId").toString()); List<String> regionCodes = Arrays.asList(params.get("regionCodes").toString().split(",")); //获取区域设备macs QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); queryWrapper.select("mac") .eq("organization_id", orgId) .eq("is_delete", Constants.NOT_DELETE) .in("town_code", regionCodes); List<Object> macs = deviceService.listObjs(queryWrapper); //根据设备mac获取设备传感器信息 for (Object mac : macs) { Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac.toString()); List<Sensor> sensors = device.getVersion().getSensors(); sensors.forEach(sensor -> { Map<String, Object> sensorMap = new HashMap<>(); sensorMap.put("sensorCode", sensor.getCode()); sensorMap.put("sensorName", sensor.getName()); result.add(sensorMap); }); } return result; } @Override public List<Map<String, Object>> regionContribution(Map<String, Object> params) { int orgId = Integer.parseInt(params.get("organizationId").toString()); List<String> 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<Map<String, Object>> 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<Map<String, Object>> regionContributionOfYear(Integer orgId, List<String> regionCodes, String time, String sensorCode) { List<Map<String, Object>> result = new ArrayList<>(); //根据组织获取区域定位 QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); organizationQueryWrapper.select("location_level_code") .eq("id", orgId); Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); //该组织所有设备信息 List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); List<String> timeLag = DateUtils.getTimeLag(time); //本市本年所有月数据 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<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))); 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 = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum"); } resultMap.put("allDeviceSum", allDeviceSum); //本市值 Double cityValue = null; if (cityAqiMap.get(yearMonth) != null) { Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonth).toString(), Map.class); //判断城市aqi是否有该因子数据 String sensorName = senorMap.get(sensorCode); if (sensorName != null) { cityValue = (Double) dataValue.get(sensorName); } } resultMap.put("cityValue", cityValue); result.add(resultMap); } for (String regionCode : regionCodes) { QueryWrapper<SysArea> queryWrapper = new QueryWrapper<>(); queryWrapper.select("area_name").eq("area_code", Integer.parseInt(regionCode)); String regionName = sysAreaService.getOne(queryWrapper).getAreaName(); //获取乡镇区域下所有设备 List<String> regionMacs = deviceService.getMacsByOrgIdAndRegionCode(orgId, Integer.parseInt(regionCode)); //该乡镇区域设备本年所有月数据 List<HistoryMonthly> regionDeviceData = historyMonthlyService.getValueByMacs(regionMacs, time); //按time分组 Map<String, List<HistoryMonthly>> regionDataMap = regionDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); for (Map<String, Object> map : result) { Object allDeviceSum = map.get("allDeviceSum"); String resultTime = map.get("time").toString(); List<HistoryMonthly> 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<String, Object> 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<Map<String, Object>> regionContributionOfMonth(Integer orgId, List<String> regionCodes, String time, String sensorCode) { List<Map<String, Object>> result = new ArrayList<>(); //根据组织获取区域定位 QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); organizationQueryWrapper.select("location_level_code") .eq("id", orgId); Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); //该组织所有设备信息 List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); //日时间点 List<String> timeLag = DateUtils.getTimeLag(time); //本市本月所有日数据 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<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))); 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 = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum"); } resultMap.put("allDeviceSum", allDeviceSum); //本市值 Double cityValue = null; if (cityAqiMap.get(yearMonthDay) != null) { Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDay).toString(), Map.class); //判断城市aqi是否有该因子数据 String sensorName = senorMap.get(sensorCode); if (sensorName != null) { cityValue = (Double) dataValue.get(sensorName); } } resultMap.put("cityValue", cityValue); result.add(resultMap); } for (String regionCode : regionCodes) { QueryWrapper<SysArea> queryWrapper = new QueryWrapper<>(); queryWrapper.select("area_name").eq("area_code", Integer.parseInt(regionCode)); String regionName = sysAreaService.getOne(queryWrapper).getAreaName(); //获取乡镇区域下所有设备 List<String> regionMacs = deviceService.getMacsByOrgIdAndRegionCode(orgId, Integer.parseInt(regionCode)); //该行业设备本月所有日数据 List<HistoryDaily> professionDeviceData = historyDailyService.getValueByMacs(regionMacs, 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.get("allDeviceSum"); String resultTime = map.get("time").toString(); List<HistoryDaily> 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<String, Object> 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<Map<String, Object>> regionContributionOfDay(Integer orgId, List<String> regionCodes, String time, String sensorCode) { List<Map<String, Object>> result = new ArrayList<>(); //根据组织获取区域定位 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 = sensor.getLower(); Double sensorUpper = sensor.getUpper(); //该组织所有设备信息 List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); //小时时间点 List<String> timeLag = DateUtils.getTimeLag(time); //本市本日所有小时数据 QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>(); cityAqiQueryWrapper.select("time", "value") .eq("city_code", locationLevelCode) .likeRight("time", time); List<Map<String, Object>> cityAqis = cityAqiService.listMaps(cityAqiQueryWrapper); Map<String, Object> cityAqiMap = new HashMap<>(); if (!ObjectUtils.isEmpty(cityAqis)) { for (Map<String, Object> cityAqi : cityAqis) { cityAqiMap.put(cityAqi.get("time").toString().substring(0, 13), cityAqi.get("value")); } } //所有设备本日所有小时数据 List<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))); 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 = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper); } resultMap.put("allDeviceSum", 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()); } } resultMap.put("cityValue", cityValue); result.add(resultMap); } for (String regionCode : regionCodes) { QueryWrapper<SysArea> queryWrapper = new QueryWrapper<>(); queryWrapper.select("area_name").eq("area_code", Integer.parseInt(regionCode)); String regionName = sysAreaService.getOne(queryWrapper).getAreaName(); //获取乡镇区域下所有设备 List<String> regionMacs = deviceService.getMacsByOrgIdAndRegionCode(orgId, Integer.parseInt(regionCode)); //该行业设备本日所有小时数据 List<HistoryHourly> professionDeviceData = historyHourlyService.getValueByMacs(regionMacs, 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.get("allDeviceSum"); String resultTime = map.get("time").toString(); List<HistoryHourly> 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<String, Object> 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; } } screen-job/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
@@ -61,7 +61,7 @@ //存入数据库是time字段实际时间 Date dataTime = DateUtils.addHours(time, -1); Date start = null; if (DateUtils.getHour(time) >= 8) { if (DateUtils.getHour(time) >= 8 || DateUtils.getHour(time) == 0) { start = DateUtils.addHours(time, -8); } @@ -112,7 +112,7 @@ params.put("value", JSONObject.toJSONString(aqi)); cityAqis.add(params); if (cityAqis.size() >= 6) { OptionalDouble average = cityAqis.parallelStream().flatMapToDouble(v -> { OptionalDouble average = cityAqis.stream().flatMapToDouble(v -> { Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); double o3 = Double.parseDouble(dataValue.get("O3").toString()); return DoubleStream.of(o3);