3 files added
11 files modified
| | |
| | | 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(), |
| | |
| | | public ResultMessage professionContribution(HttpServletRequest request) { |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | if (ObjectUtils.isEmpty(params.get("organizationId")) |
| | | ||ObjectUtils.isEmpty(params.get("professions")) |
| | | || ObjectUtils.isEmpty(params.get("professions")) |
| | | || ObjectUtils.isEmpty(params.get("type")) |
| | | || ObjectUtils.isEmpty(params.get("time")) |
| | | || ObjectUtils.isEmpty(params.get("sensorCode"))) { |
New file |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | //获取当前组织下所有设备列表 |
| | | List<Map<String, Object>> getDevicesByOrganizationId(Integer orgId); |
| | | |
| | | //获取当前组织下设备mac集合 |
| | | List<String> getMacsByOrganizationId(Integer organizationId); |
| | | |
| | | //获取当前组织,当前乡镇区域下设备mac集合 |
| | | List getMacsByOrgIdAndRegionCode(Integer organizationId,Integer regionCode); |
| | | |
| | | } |
| | |
| | | * */ |
| | | 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); |
| | | |
| | | } |
| | |
| | | List<Map<String, Object>> getThermodynamicDiagramDataByOrgIdSensorCodeTimeslot(Map<String, Object> map); |
| | | |
| | | /** |
| | | *@Description: 获取某一天小时完整数据 |
| | | *@Param: [map] |
| | | *@return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
| | | *@Author: lizijie |
| | | *@Date: 2021/12/28 10:04 |
| | | * @Description: 获取某一天小时完整数据 |
| | | * @Param: [map] |
| | | * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>> |
| | | * @Author: lizijie |
| | | * @Date: 2021/12/28 10:04 |
| | | **/ |
| | | List<Map<String, Object>> getHourCompleteDataByMacSensorCodeDate(Map<String,Object> map); |
| | | List<Map<String, Object>> getHourCompleteDataByMacSensorCodeDate(Map<String, Object> map); |
| | | |
| | | /** |
| | | * @Description: 根据mac和时间获取小时值 |
| | |
| | | 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); |
| | | |
| | | } |
| | |
| | | * @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); |
| | | } |
New file |
| | |
| | | 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); |
| | | |
| | | } |
| | |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @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) { |
| | |
| | | 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; |
| | |
| | | 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> |
| | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | 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; |
| | |
| | | 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> |
| | |
| | | String mac = map.get("mac").toString(); |
| | | String sensorCode = map.get("sensor_code").toString(); |
| | | String date = map.get("date").toString(); |
| | | String dateTime = date.replace("-",""); |
| | | String timeUnits = dateTime.substring(0,6)+"_complete"; |
| | | Map<String,Object> params = new HashMap<>(); |
| | | params.put("timeUnits",timeUnits); |
| | | params.put("mac",mac); |
| | | String dateTime = date.replace("-", ""); |
| | | String timeUnits = dateTime.substring(0, 6) + "_complete"; |
| | | Map<String, Object> params = new HashMap<>(); |
| | | params.put("timeUnits", timeUnits); |
| | | params.put("mac", mac); |
| | | List resultList = new ArrayList(); |
| | | for (int i = 0; i < 24; i++) { |
| | | Map<String,Object> oneHourDateMap = new HashMap<>(); |
| | | Map<String, Object> oneHourDateMap = new HashMap<>(); |
| | | String j; |
| | | if (i<10){ |
| | | j = " 0"+i+":00:00"; |
| | | }else { |
| | | j = " "+i+":00:00"; |
| | | if (i < 10) { |
| | | j = " 0" + i + ":00:00"; |
| | | } else { |
| | | j = " " + i + ":00:00"; |
| | | } |
| | | String time = date+j; |
| | | params.put("time",time); |
| | | String resultTime = time.substring(0,13); |
| | | if (ObjectUtils.isEmpty(historyHourlyMapper.selectHourlyData(params))){ |
| | | oneHourDateMap.put("time",resultTime); |
| | | oneHourDateMap.put("values",new ArrayList<>()); |
| | | String time = date + j; |
| | | params.put("time", time); |
| | | String resultTime = time.substring(0, 13); |
| | | if (ObjectUtils.isEmpty(historyHourlyMapper.selectHourlyData(params))) { |
| | | oneHourDateMap.put("time", resultTime); |
| | | oneHourDateMap.put("values", new ArrayList<>()); |
| | | resultList.add(oneHourDateMap); |
| | | continue; |
| | | } |
| | |
| | | oneHourlyData = historyHourlyMapper.selectHourlyData(params); |
| | | JSONObject js = JSONObject.parseObject(oneHourlyData); |
| | | String sensorDate = js.get(sensorCode).toString(); |
| | | sensorDate = sensorDate.replace("[",""); |
| | | sensorDate = sensorDate.replace("]",""); |
| | | sensorDate = sensorDate.replace("[", ""); |
| | | sensorDate = sensorDate.replace("]", ""); |
| | | String[] split = sensorDate.split(","); |
| | | oneHourDateMap.put("time",resultTime); |
| | | oneHourDateMap.put("values",split); |
| | | oneHourDateMap.put("time", resultTime); |
| | | oneHourDateMap.put("values", split); |
| | | resultList.add(oneHourDateMap); |
| | | } |
| | | return resultList; |
| | |
| | | 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] |
| | |
| | | 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; |
| | |
| | | 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> |
| | |
| | | @Override |
| | | public HistoryMonthly getHistoryMonthlyByMacAndDate(String mac, Date date) { |
| | | QueryWrapper<HistoryMonthly> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("mac",mac); |
| | | wrapper.eq("time",date); |
| | | wrapper.eq("mac", mac); |
| | | wrapper.eq("time", date); |
| | | List<HistoryMonthly> historyMonthlies = historyMonthlyMapper.selectList(wrapper); |
| | | if(ObjectUtils.isEmpty(historyMonthlies)) |
| | | if (ObjectUtils.isEmpty(historyMonthlies)) |
| | | return null; |
| | | return historyMonthlies.get(0); |
| | | } |
| | |
| | | @Override |
| | | public Map<String, HistoryMonthly> getHistoryMonthlyByMacsAndDate(List<String> mac, Date date) { |
| | | QueryWrapper<HistoryMonthly> wrapper = new QueryWrapper<>(); |
| | | wrapper.in("mac",mac); |
| | | wrapper.eq("time",date); |
| | | wrapper.in("mac", mac); |
| | | wrapper.eq("time", date); |
| | | List<HistoryMonthly> historyMonthlies = historyMonthlyMapper.selectList(wrapper); |
| | | Map<String,HistoryMonthly> map = new HashMap<>(); |
| | | Map<String, HistoryMonthly> map = new HashMap<>(); |
| | | for (HistoryMonthly historyMonthly : historyMonthlies) { |
| | | map.put(historyMonthly.getMac(),historyMonthly); |
| | | map.put(historyMonthly.getMac(), historyMonthly); |
| | | } |
| | | return map; |
| | | } |
| | |
| | | //循环集合 |
| | | //所有子组织 |
| | | List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId); |
| | | if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){ |
| | | for (Organization organization:allChildrenOrganization) { |
| | | if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1) { |
| | | for (Organization organization : allChildrenOrganization) { |
| | | allOrgId.add(organization.getId()); |
| | | } |
| | | } |
| | |
| | | //声明一个list,存放设备mac |
| | | List<String> deviceMacList = new ArrayList<>(); |
| | | //声明一个map,Mac作为key,device作为value |
| | | Map<String,Device> deviceMap = new HashMap<>(); |
| | | Map<String, Device> deviceMap = new HashMap<>(); |
| | | List<Double> longitudeList = new ArrayList<>(); |
| | | List<Double> latitudeList = new ArrayList<>(); |
| | | for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) { |
| | | for (Integer orgIdWithoutDuplicates : allOrgIdWithoutDuplicates) { |
| | | //根据id查询所属设备 |
| | | QueryWrapper<Device> wrapper_device = new QueryWrapper<>(); |
| | | wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgIdWithoutDuplicates); |
| | | wrapper_device.eq("is_delete", Constants.NOT_DELETE).eq("organization_id", orgIdWithoutDuplicates); |
| | | List<Device> devices = new ArrayList<>(); |
| | | devices = deviceMapper.selectList(wrapper_device); |
| | | if (devices.size()>0){ |
| | | for (Device device:devices) { |
| | | if (devices.size() > 0) { |
| | | for (Device device : devices) { |
| | | String mac = device.getMac(); |
| | | deviceMacList.add(mac); |
| | | deviceMap.put(mac,device); |
| | | deviceMap.put(mac, device); |
| | | double longitude = device.getLongitude(); |
| | | double latitude = device.getLatitude(); |
| | | longitudeList.add(longitude); |
| | | latitudeList.add(latitude); |
| | | } |
| | | }else { |
| | | } else { |
| | | continue; |
| | | } |
| | | } |
| | | //获取时间 |
| | | String time = parameters.get("time").toString().substring(0,7)+"-01 00:00:00"; |
| | | resultMap.put("time",time); |
| | | String time = parameters.get("time").toString().substring(0, 7) + "-01 00:00:00"; |
| | | resultMap.put("time", time); |
| | | QueryWrapper<HistoryMonthly> historyMonthlyQueryWrapper = new QueryWrapper<>(); |
| | | historyMonthlyQueryWrapper.eq("time",time); |
| | | historyMonthlyQueryWrapper.eq("time", time); |
| | | historyMonthlyQueryWrapper.in("mac", deviceMacList); |
| | | List<HistoryMonthly> historyDailies = historyMonthlyMapper.selectList(historyMonthlyQueryWrapper); |
| | | List<Object> list = new ArrayList<>(); |
| | | for (HistoryMonthly historyDailyData:historyDailies) { |
| | | for (HistoryMonthly historyDailyData : historyDailies) { |
| | | List<Object> list1 = new ArrayList<>(); |
| | | String mac = historyDailyData.getMac(); |
| | | Device device = deviceMap.get(mac); |
| | |
| | | list1.add(level); |
| | | list.add(list1); |
| | | } |
| | | resultMap.put("list",list); |
| | | double latitudeMin = Collections.min(latitudeList)-0.0018; |
| | | double latitudeMax = Collections.max(latitudeList)+0.0018; |
| | | double longitudeMin = Collections.min(longitudeList)-0.2/(111*Math.cos(latitudeMin)); |
| | | double longitudeMax = Collections.max(longitudeList)+0.2/(111*Math.cos(latitudeMin)); |
| | | resultMap.put("list", list); |
| | | double latitudeMin = Collections.min(latitudeList) - 0.0018; |
| | | double latitudeMax = Collections.max(latitudeList) + 0.0018; |
| | | double longitudeMin = Collections.min(longitudeList) - 0.2 / (111 * Math.cos(latitudeMin)); |
| | | double longitudeMax = Collections.max(longitudeList) + 0.2 / (111 * Math.cos(latitudeMin)); |
| | | List<Double> bound = new ArrayList<>(); |
| | | bound.add(longitudeMin); |
| | | bound.add(latitudeMin); |
| | | bound.add(longitudeMax); |
| | | bound.add(latitudeMax); |
| | | resultMap.put("bound",bound); |
| | | resultMap.put("bound", bound); |
| | | List<List> bound1 = new ArrayList<>(); |
| | | List<Double> left_up = new ArrayList<>(); |
| | | left_up.add(latitudeMax); |
| | |
| | | bound1.add(right_down); |
| | | bound1.add(left_down); |
| | | List<GeoCoordinate> geoCoordinates = new ArrayList<>(); |
| | | for (List bo:bound1) { |
| | | for (List bo : bound1) { |
| | | GeoCoordinate g = new GeoCoordinate(); |
| | | g.setLatitude(Double.parseDouble(bo.get(0).toString())); |
| | | g.setLongitude(Double.parseDouble(bo.get(1).toString())); |
| | |
| | | List centerPoint = new ArrayList(); |
| | | centerPoint.add(centerPoint400.getLongitude()); |
| | | centerPoint.add(centerPoint400.getLatitude()); |
| | | resultMap.put("centerPoint",centerPoint); |
| | | resultMap.put("centerPoint", centerPoint); |
| | | return resultMap; |
| | | } |
| | | |
| | |
| | | //循环集合 |
| | | //所有子组织 |
| | | List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId); |
| | | if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){ |
| | | for (Organization organization:allChildrenOrganization) { |
| | | if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1) { |
| | | for (Organization organization : allChildrenOrganization) { |
| | | allOrgId.add(organization.getId()); |
| | | } |
| | | } |
| | |
| | | //声明一个list,存放设备mac |
| | | List<String> deviceMacList = new ArrayList<>(); |
| | | //声明一个map,Mac作为key,device作为value |
| | | Map<String,Device> deviceMap = new HashMap<>(); |
| | | Map<String, Device> deviceMap = new HashMap<>(); |
| | | List<Double> longitudeList = new ArrayList<>(); |
| | | List<Double> latitudeList = new ArrayList<>(); |
| | | for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) { |
| | | for (Integer orgIdWithoutDuplicates : allOrgIdWithoutDuplicates) { |
| | | //根据id查询所属设备 |
| | | QueryWrapper<Device> wrapper_device = new QueryWrapper<>(); |
| | | wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgIdWithoutDuplicates); |
| | | wrapper_device.eq("is_delete", Constants.NOT_DELETE).eq("organization_id", orgIdWithoutDuplicates); |
| | | List<Device> devices = new ArrayList<>(); |
| | | devices = deviceMapper.selectList(wrapper_device); |
| | | if (devices.size()>0){ |
| | | for (Device device:devices) { |
| | | if (devices.size() > 0) { |
| | | for (Device device : devices) { |
| | | String mac = device.getMac(); |
| | | deviceMacList.add(mac); |
| | | deviceMap.put(mac,device); |
| | | deviceMap.put(mac, device); |
| | | double longitude = device.getLongitude(); |
| | | double latitude = device.getLatitude(); |
| | | longitudeList.add(longitude); |
| | | latitudeList.add(latitude); |
| | | } |
| | | }else { |
| | | } else { |
| | | continue; |
| | | } |
| | | } |
| | | double latitudeMin = Collections.min(latitudeList)-0.0018; |
| | | double latitudeMax = Collections.max(latitudeList)+0.0018; |
| | | double longitudeMin = Collections.min(longitudeList)-0.2/(111*Math.cos(latitudeMin)); |
| | | double longitudeMax = Collections.max(longitudeList)+0.2/(111*Math.cos(latitudeMin)); |
| | | double latitudeMin = Collections.min(latitudeList) - 0.0018; |
| | | double latitudeMax = Collections.max(latitudeList) + 0.0018; |
| | | double longitudeMin = Collections.min(longitudeList) - 0.2 / (111 * Math.cos(latitudeMin)); |
| | | double longitudeMax = Collections.max(longitudeList) + 0.2 / (111 * Math.cos(latitudeMin)); |
| | | List<Double> bound = new ArrayList<>(); |
| | | bound.add(longitudeMin); |
| | | bound.add(latitudeMin); |
| | |
| | | bound1.add(right_down); |
| | | bound1.add(left_down); |
| | | List<GeoCoordinate> geoCoordinates = new ArrayList<>(); |
| | | for (List bo:bound1) { |
| | | for (List bo : bound1) { |
| | | GeoCoordinate g = new GeoCoordinate(); |
| | | g.setLatitude(Double.parseDouble(bo.get(0).toString())); |
| | | g.setLongitude(Double.parseDouble(bo.get(1).toString())); |
| | |
| | | centerPoint.add(centerPoint400.getLatitude()); |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | //获取时间 |
| | | String endTime = parameters.get("endTime").toString().substring(0,7)+"-01 00:00:00"; |
| | | String endTime = parameters.get("endTime").toString().substring(0, 7) + "-01 00:00:00"; |
| | | //获取时间 |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM"); |
| | | int months = Integer.parseInt(parameters.get("months").toString()); |
| | | Date newEndTime = new Date(); |
| | | for (int i=months;i>=0;i--){ |
| | | for (int i = months; i >= 0; i--) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | //先存放中心点和边界点 |
| | | resultMap.put("centerPoint",centerPoint); |
| | | resultMap.put("bound",bound); |
| | | resultMap.put("centerPoint", centerPoint); |
| | | resultMap.put("bound", bound); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | try { |
| | | newEndTime = df.parse(endTime); |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | calendar.setTime(newEndTime); |
| | | calendar.set(Calendar.MONTH,calendar.get(Calendar.MONDAY)-i); |
| | | String time = df.format(calendar.getTime())+"-01 00:00:00"; |
| | | calendar.set(Calendar.MONTH, calendar.get(Calendar.MONDAY) - i); |
| | | String time = df.format(calendar.getTime()) + "-01 00:00:00"; |
| | | //存放时间 |
| | | resultMap.put("time",time); |
| | | resultMap.put("time", time); |
| | | QueryWrapper<HistoryMonthly> historyMonthlyQueryWrapper = new QueryWrapper<>(); |
| | | historyMonthlyQueryWrapper.eq("time",time); |
| | | historyMonthlyQueryWrapper.eq("time", time); |
| | | historyMonthlyQueryWrapper.in("mac", deviceMacList); |
| | | List<HistoryMonthly> historyDailies = historyMonthlyMapper.selectList(historyMonthlyQueryWrapper); |
| | | List<Object> list = new ArrayList<>(); |
| | | for (HistoryMonthly historyMonthlyData:historyDailies) { |
| | | for (HistoryMonthly historyMonthlyData : historyDailies) { |
| | | List<Object> list1 = new ArrayList<>(); |
| | | String mac = historyMonthlyData.getMac(); |
| | | Device device = deviceMap.get(mac); |
| | |
| | | list1.add(level); |
| | | list.add(list1); |
| | | } |
| | | resultMap.put("list",list); |
| | | resultMap.put("list", list); |
| | | resultList.add(resultMap); |
| | | } |
| | | return resultList; |
| | |
| | | .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; |
| | | } |
| | | } |
| | |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.OptionalDouble; |
| | | import java.util.Set; |
| | | import java.util.function.Supplier; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.DoubleStream; |
| | | import java.util.stream.Stream; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | |
| | | import com.moral.api.entity.HistoryMonthly; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.entity.SysDictData; |
| | | import com.moral.api.service.CityAqiDailyService; |
| | | import com.moral.api.service.CityAqiMonthlyService; |
| | | import com.moral.api.service.CityAqiService; |
| | |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.api.service.ProfessionService; |
| | | |
| | | import com.moral.api.service.SensorService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private OrganizationService organizationService; |
| | | |
| | | @Autowired |
| | | private SensorService sensorService; |
| | | |
| | | private static Map<String, String> senorMap = new HashMap<>(); |
| | | |
| | |
| | | Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); |
| | | |
| | | //该组织所有设备信息 |
| | | List allMacs = getMacsByOrgId(orgId); |
| | | List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); |
| | | |
| | | List<String> timeLag = DateUtils.getTimeLag(time); |
| | | |
| | |
| | | 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); |
| | | |
| | |
| | | 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); |
| | |
| | | } |
| | | |
| | | 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()); |
| | |
| | | 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); |
| | |
| | | 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")); |
| | |
| | | Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode(); |
| | | |
| | | //该组织所有设备信息 |
| | | List allMacs = getMacsByOrgId(orgId); |
| | | List<String> allMacs = deviceService.getMacsByOrganizationId(orgId); |
| | | |
| | | //日时间点 |
| | | List<String> timeLag = DateUtils.getTimeLag(time); |
| | |
| | | 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); |
| | | |
| | |
| | | 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); |
| | |
| | | } |
| | | |
| | | 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()); |
| | |
| | | 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); |
| | |
| | | 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")); |
| | |
| | | .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); |
| | |
| | | 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); |
| | | |
| | |
| | | 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); |
| | |
| | | } |
| | | |
| | | 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()); |
| | |
| | | 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); |
| | |
| | | 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; |
| | | } |
| | | } |
New file |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | //存入数据库是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); |
| | | } |
| | | |
| | |
| | | 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); |