screen-api/src/main/java/com/moral/api/mapper/HistoryHourlyMapper.java
@@ -21,7 +21,4 @@ List<Map<String,Object>> selectDataByMacsAndTime(@Param("timeUnits") String timeUnits, @Param("list")List list, @Param("time")String time); Double getSensorSumByMacs(@Param("sensorCode") String sensorCode, @Param("macs") List<String> macs, @Param("time") String time, @Param("timeUnits") String timeUnits); Double getSensorAvgByMacs(@Param("sensorCode") String sensorCode, @Param("macs") List<String> macs, @Param("time") String time, @Param("timeUnits") String timeUnits); } screen-api/src/main/java/com/moral/api/service/HistoryDailyService.java
@@ -65,4 +65,11 @@ **/ List<Map<String, Object>> getThermodynamicDiagramDataByOrgIdSensorCodeTimeSlot(Map<String, Object> map); /** * @description: 通过设备和时间获取天数据 * @param macs List<String> * @param time String 例:2021-12 * */ List<HistoryDaily> getValueByMacs(List<String> macs, String time); } screen-api/src/main/java/com/moral/api/service/HistoryHourlyService.java
@@ -64,10 +64,11 @@ */ List<HistoryHourly> getValueByMacAndTime(String mac, Date startDate, Date endDate); //获取小时数据中这些设备该因子累加值 Double getSensorSumByMacs(String sensorCode, List macs, String time); //获取小时数据中这些设备该因子平均值 Double getSensorAvgByMacs(String sensorCode, List macs, String time); /** * @description: 通过设备和时间获取小时数据 * @param macs List<String> * @param time String 例:2021-12-23 * */ List<HistoryHourly> getValueByMacs(List<String> macs, String time); } screen-api/src/main/java/com/moral/api/service/HistoryMonthlyService.java
@@ -52,4 +52,11 @@ *@Date: 2021/12/16 15:16 **/ List<Map<String, Object>> getThermodynamicDiagramDataByOrgIdSensorCodeTimeSlot(Map<String, Object> map); /** * @description: 通过设备和时间获取月数据 * @param macs List<String> * @param time String 例:2021 * */ List<HistoryMonthly> getValueByMacs(List<String> macs, String time); } screen-api/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
@@ -14,8 +14,8 @@ 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; @@ -62,10 +62,10 @@ @Override public HistoryDaily getHistoryDailyByMacAndDate(String mac, Date date) { QueryWrapper<HistoryDaily> wrapper = new QueryWrapper<>(); wrapper.eq("mac",mac); wrapper.eq("time",date); wrapper.eq("mac", mac); wrapper.eq("time", date); List<HistoryDaily> historyDailies = historyDailyMapper.selectList(wrapper); if(ObjectUtils.isEmpty(historyDailies)) if (ObjectUtils.isEmpty(historyDailies)) return null; return historyDailies.get(0); } @@ -74,12 +74,12 @@ @Override public Map<String, HistoryDaily> getHistoryDailyByMacsAndDate(List<String> mac, Date date) { QueryWrapper<HistoryDaily> wrapper = new QueryWrapper<>(); wrapper.in("mac",mac); wrapper.eq("time",date); wrapper.in("mac", mac); wrapper.eq("time", date); List<HistoryDaily> historyDailies = historyDailyMapper.selectList(wrapper); Map<String,HistoryDaily> map = new HashMap<>(); Map<String, HistoryDaily> map = new HashMap<>(); for (HistoryDaily historyDaily : historyDailies) { map.put(historyDaily.getMac(),historyDaily); map.put(historyDaily.getMac(), historyDaily); } return map; } @@ -87,8 +87,8 @@ @Override public List<HistoryDaily> getHistoryDailyByMacAndTimeSlot(String mac, Date startDate, Date endDate) { QueryWrapper<HistoryDaily> wrapper = new QueryWrapper<>(); wrapper.in("mac",mac); wrapper.between("time",startDate,endDate); wrapper.in("mac", mac); wrapper.between("time", startDate, endDate); List<HistoryDaily> historyDailies = historyDailyMapper.selectList(wrapper); return historyDailies; } @@ -103,8 +103,8 @@ //循环集合 //所有子组织 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()); } } @@ -113,38 +113,38 @@ //声明一个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,10)+" 00:00:00"; resultMap.put("time",time); String time = parameters.get("time").toString().substring(0, 10) + " 00:00:00"; resultMap.put("time", time); QueryWrapper<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.eq("time",time); historyDailyQueryWrapper.eq("time", time); historyDailyQueryWrapper.in("mac", deviceMacList); List<HistoryDaily> historyDailies = historyDailyMapper.selectList(historyDailyQueryWrapper); List<Object> list = new ArrayList<>(); for (HistoryDaily historyDailyData:historyDailies) { for (HistoryDaily historyDailyData : historyDailies) { List<Object> list1 = new ArrayList<>(); String mac = historyDailyData.getMac(); Device device = deviceMap.get(mac); @@ -158,17 +158,17 @@ 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); @@ -187,7 +187,7 @@ 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())); @@ -197,7 +197,7 @@ List centerPoint = new ArrayList(); centerPoint.add(centerPoint400.getLongitude()); centerPoint.add(centerPoint400.getLatitude()); resultMap.put("centerPoint",centerPoint); resultMap.put("centerPoint", centerPoint); return resultMap; } @@ -210,8 +210,8 @@ //循环集合 //所有子组织 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()); } } @@ -220,33 +220,33 @@ //声明一个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); @@ -270,7 +270,7 @@ 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())); @@ -282,16 +282,16 @@ centerPoint.add(centerPoint400.getLatitude()); List<Map<String, Object>> resultList = new ArrayList<>(); //获取时间 String endTime = parameters.get("endTime").toString().substring(0,10)+" 00:00:00"; String endTime = parameters.get("endTime").toString().substring(0, 10) + " 00:00:00"; //获取时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); int days = Integer.parseInt(parameters.get("days").toString()); Date newEndTime = new Date(); for (int i=days;i>=0;i--){ for (int i = days; 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); @@ -299,16 +299,16 @@ e.printStackTrace(); } calendar.setTime(newEndTime); calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)-i); String time = df.format(calendar.getTime())+" 00:00:00"; calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - i); String time = df.format(calendar.getTime()) + " 00:00:00"; //存放时间 resultMap.put("time",time); resultMap.put("time", time); QueryWrapper<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.eq("time",time); historyDailyQueryWrapper.eq("time", time); historyDailyQueryWrapper.in("mac", deviceMacList); List<HistoryDaily> historyDailies = historyDailyMapper.selectList(historyDailyQueryWrapper); List<Object> list = new ArrayList<>(); for (HistoryDaily historyDailyData:historyDailies) { for (HistoryDaily historyDailyData : historyDailies) { List<Object> list1 = new ArrayList<>(); String mac = historyDailyData.getMac(); Device device = deviceMap.get(mac); @@ -322,11 +322,20 @@ list1.add(level); list.add(list1); } resultMap.put("list",list); resultMap.put("list", list); resultList.add(resultMap); } return resultList; } @Override public List<HistoryDaily> getValueByMacs(List<String> macs, String time) { QueryWrapper<HistoryDaily> queryWrapper = new QueryWrapper<>(); queryWrapper.select("time", "value") .likeRight("time", time) .in("mac", macs); return historyDailyMapper.selectList(queryWrapper); } } screen-api/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
@@ -14,9 +14,9 @@ import com.moral.pojo.AQI; import com.moral.util.AQIUtils; 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; @@ -68,11 +68,11 @@ } /** *@Description: 通过组织id和因子编码获取上一个小时数据 *@Param: [map] *@return: java.util.Map<java.lang.String,java.lang.Object> *@Author: lizijie *@Date: 2021/12/7 10:07 * @Description: 通过组织id和因子编码获取上一个小时数据 * @Param: [map] * @return: java.util.Map<java.lang.String, java.lang.Object> * @Author: lizijie * @Date: 2021/12/7 10:07 **/ @Override public Map<String, Object> getLastHourDataByOrgIdAndSensorCode(Map parameters) { @@ -84,8 +84,8 @@ //循环集合 //所有子组织 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()); } } @@ -94,36 +94,36 @@ //声明一个list,存放设备mac List<String> deviceMacList = new ArrayList<>(); //声明一个map,Mac作为key,device作为value Map<String,Device> deviceMap = new HashMap<>(); for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) { Map<String, Device> deviceMap = new HashMap<>(); 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); } }else { } else { continue; } } //获取时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH"); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-1); String time = df.format(calendar.getTime())+":00:00"; resultMap.put("time",time); String timeUnits = DateUtils.dateToDateString(calendar.getTime(), DateUtils.yyyyMM_EN); List<Map<String,Object>> historyHourlyDatas = new ArrayList<>(); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1); String time = df.format(calendar.getTime()) + ":00:00"; resultMap.put("time", time); String timeUnits = DateUtils.dateToDateString(calendar.getTime(), DateUtils.yyyyMM_EN); List<Map<String, Object>> historyHourlyDatas = new ArrayList<>(); historyHourlyDatas = historyHourlyMapper.selectDataByMacsAndTime(timeUnits, deviceMacList, time); List<Object> list = new ArrayList<>(); List<Double> longitudeList = new ArrayList<>(); List<Double> latitudeList = new ArrayList<>(); for (Map historyHourlyData:historyHourlyDatas) { for (Map historyHourlyData : historyHourlyDatas) { List<Object> list1 = new ArrayList<>(); String mac = historyHourlyData.get("mac").toString(); Device device = deviceMap.get(mac); @@ -139,17 +139,17 @@ 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(latitudeMax); bound.add(longitudeMax); bound.add(latitudeMin); resultMap.put("bound",bound); resultMap.put("bound", bound); List<List> bound1 = new ArrayList<>(); List<Double> left_up = new ArrayList<>(); left_up.add(latitudeMax); @@ -168,7 +168,7 @@ 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())); @@ -178,7 +178,7 @@ List centerPoint = new ArrayList(); centerPoint.add(centerPoint400.getLongitude()); centerPoint.add(centerPoint400.getLatitude()); resultMap.put("centerPoint",centerPoint); resultMap.put("centerPoint", centerPoint); return resultMap; } @@ -192,8 +192,8 @@ //循环集合 //所有子组织 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()); } } @@ -202,41 +202,41 @@ //声明一个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; } } List<Object> list = new ArrayList<>(); //获取时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH"); for (int i=12;i>0;i--){ for (int i = 12; i > 0; i--) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-i); String time = df.format(calendar.getTime())+":00:00"; String timeUnits = DateUtils.dateToDateString(calendar.getTime(), DateUtils.yyyyMM_EN); List<Map<String,Object>> historyHourlyDatas = new ArrayList<>(); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - i); String time = df.format(calendar.getTime()) + ":00:00"; String timeUnits = DateUtils.dateToDateString(calendar.getTime(), DateUtils.yyyyMM_EN); List<Map<String, Object>> historyHourlyDatas = new ArrayList<>(); historyHourlyDatas = historyHourlyMapper.selectDataByMacsAndTime(timeUnits, deviceMacList, time); List<Object> oneHourlyList = new ArrayList<>(); for (Map historyHourlyData:historyHourlyDatas) { for (Map historyHourlyData : historyHourlyDatas) { List<Object> list1 = new ArrayList<>(); String mac = historyHourlyData.get("mac").toString(); Device device = deviceMap.get(mac); @@ -252,17 +252,17 @@ } list.add(oneHourlyList); } 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(latitudeMax); bound.add(longitudeMax); bound.add(latitudeMin); bound.add(longitudeMin); resultMap.put("bound",bound); resultMap.put("bound", bound); List<List> bound1 = new ArrayList<>(); List<Double> left_up = new ArrayList<>(); left_up.add(latitudeMax); @@ -281,7 +281,7 @@ 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())); @@ -291,7 +291,7 @@ List centerPoint = new ArrayList(); centerPoint.add(centerPoint400.getLongitude()); centerPoint.add(centerPoint400.getLatitude()); resultMap.put("centerPoint",centerPoint); resultMap.put("centerPoint", centerPoint); return resultMap; } @@ -305,8 +305,8 @@ //循环集合 //所有子组织 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()); } } @@ -315,37 +315,37 @@ //声明一个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,13)+":00:00"; resultMap.put("time",time); String timeUnits = DateUtils.stringToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN, DateUtils.yyyyMM_EN); List<Map<String,Object>> historyHourlyDatas = new ArrayList<>(); String time = parameters.get("time").toString().substring(0, 13) + ":00:00"; resultMap.put("time", time); String timeUnits = DateUtils.stringToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN, DateUtils.yyyyMM_EN); List<Map<String, Object>> historyHourlyDatas = new ArrayList<>(); historyHourlyDatas = historyHourlyMapper.selectDataByMacsAndTime(timeUnits, deviceMacList, time); List<Object> list = new ArrayList<>(); for (Map historyHourlyData:historyHourlyDatas) { for (Map historyHourlyData : historyHourlyDatas) { List<Object> list1 = new ArrayList<>(); String mac = historyHourlyData.get("mac").toString(); Device device = deviceMap.get(mac); @@ -359,17 +359,17 @@ 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); @@ -388,7 +388,7 @@ 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())); @@ -398,7 +398,7 @@ List centerPoint = new ArrayList(); centerPoint.add(centerPoint400.getLongitude()); centerPoint.add(centerPoint400.getLatitude()); resultMap.put("centerPoint",centerPoint); resultMap.put("centerPoint", centerPoint); return resultMap; } @@ -411,8 +411,8 @@ //循环集合 //所有子组织 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()); } } @@ -421,33 +421,33 @@ //声明一个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); @@ -471,7 +471,7 @@ 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())); @@ -487,11 +487,11 @@ String endTime = parameters.get("endTime").toString(); int hour = Integer.parseInt(parameters.get("hour").toString()); Date newEndTime = new Date(); for (int i=hour;i>=0;i--){ for (int i = hour; 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); @@ -499,15 +499,15 @@ e.printStackTrace(); } calendar.setTime(newEndTime); calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-i); String time = df.format(calendar.getTime())+":00:00"; calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - i); String time = df.format(calendar.getTime()) + ":00:00"; //存放时间 resultMap.put("time",time); String timeUnits = DateUtils.dateToDateString(calendar.getTime(), DateUtils.yyyyMM_EN); List<Map<String,Object>> historyHourlyDatas = new ArrayList<>(); resultMap.put("time", time); String timeUnits = DateUtils.dateToDateString(calendar.getTime(), DateUtils.yyyyMM_EN); List<Map<String, Object>> historyHourlyDatas = new ArrayList<>(); historyHourlyDatas = historyHourlyMapper.selectDataByMacsAndTime(timeUnits, deviceMacList, time); List<Object> oneHourlyList = new ArrayList<>(); for (Map historyHourlyData:historyHourlyDatas) { for (Map historyHourlyData : historyHourlyDatas) { List<Object> list1 = new ArrayList<>(); String mac = historyHourlyData.get("mac").toString(); Device device = deviceMap.get(mac); @@ -521,7 +521,7 @@ list1.add(level); oneHourlyList.add(list1); } resultMap.put("list",oneHourlyList); resultMap.put("list", oneHourlyList); list.add(resultMap); } return list; @@ -545,15 +545,14 @@ } @Override public Double getSensorSumByMacs(String sensorCode, List macs, String time) { String timeUnits = time.substring(0, 7).replace("-", ""); return historyHourlyMapper.getSensorSumByMacs(sensorCode, macs, time, timeUnits); } @Override public Double getSensorAvgByMacs(String sensorCode, List macs, String time) { String timeUnits = time.substring(0, 7).replace("-", ""); return historyHourlyMapper.getSensorAvgByMacs(sensorCode, macs, time, timeUnits); public List<HistoryHourly> getValueByMacs(List<String> macs, String time) { QueryWrapper<HistoryHourly> queryWrapper = new QueryWrapper<>(); queryWrapper.select("time", "value") .likeRight("time", time) .in("mac", macs); Date date = DateUtils.getDate(time, DateUtils.yyyy_MM_dd_EN); List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(date, date, SeparateTableType.MONTH); return multiTableQuery(queryWrapper, tableNames); } /** screen-api/src/main/java/com/moral/api/service/impl/HistoryMonthlyServiceImpl.java
@@ -298,4 +298,13 @@ } return resultList; } @Override public List<HistoryMonthly> getValueByMacs(List<String> macs, String time) { QueryWrapper<HistoryMonthly> queryWrapper = new QueryWrapper<>(); queryWrapper.select("time", "value") .likeRight("time", time) .in("mac", macs); return historyMonthlyMapper.selectList(queryWrapper); } } screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java
@@ -13,16 +13,23 @@ 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.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.service.CityAqiDailyService; import com.moral.api.service.CityAqiMonthlyService; @@ -169,90 +176,109 @@ private List<Map<String, Object>> professionContributionOfYear(Integer orgId, List<String> professions, 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 allMacs = getMacsByOrgId(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.split("-")[1]); yearMonth = yearMonth + "-01 00:00:00"; resultMap.put("time", yearMonth); //所有设备该因子累加值 Double allDeviceSum = null; QueryWrapper<HistoryMonthly> historyMonthlyQueryWrapper = new QueryWrapper<>(); historyMonthlyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") .eq("time", yearMonth) .in("mac", allMacs); Map<String, Object> allDeviceSumMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper); if (!ObjectUtils.isEmpty(allDeviceSumMap)) { allDeviceSum = (Double) allDeviceSumMap.get("result"); List<HistoryMonthly> historyMonthlyList = allDeviceDataMap.get(yearMonth); if (!ObjectUtils.isEmpty(historyMonthlyList)) { allDeviceSum = 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(); } resultMap.put("allDeviceSum", allDeviceSum); //本市数据 //本市值 Double cityValue = null; //根据组织获取区域定位 Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode(); QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>(); cityAqiMonthlyQueryWrapper.select("`value`->'$." + sensorCode + "' AS result") .eq("city_code", locationLevelCode) .eq("time", yearMonth); Map<String, Object> cityValueMap = cityAqiMonthlyService.getMap(cityAqiMonthlyQueryWrapper); if (!ObjectUtils.isEmpty(cityValueMap)) { cityValue = (Double) cityValueMap.get("result"); if (cityAqiMap.get(yearMonth) != null) { Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonth).toString(), Map.class); Object o = dataValue.get(sensorCode); if (o != null) { cityValue = (Double) o; } } resultMap.put("cityValue", cityValue); for (String profession : professions) { Map<String, Object> professionMap = new HashMap<>(); //获取该行业设备 List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList()); //该行业累加值 Double professionSum = null; historyMonthlyQueryWrapper.clear(); historyMonthlyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") .eq("time", yearMonth) .in("mac", professionMacs); Map<String, Object> professionSumMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper); if (!ObjectUtils.isEmpty(professionSumMap)) { professionSum = (Double) professionSumMap.get("result"); } //该行业平均值 Double professionAvg = null; historyMonthlyQueryWrapper.clear(); historyMonthlyQueryWrapper.select("AVG(`value`->'$." + sensorCode + "') AS result") .eq("time", yearMonth) .in("mac", professionMacs); Map<String, Object> professionAvgMap = historyMonthlyService.getMap(historyMonthlyQueryWrapper); if (!ObjectUtils.isEmpty(professionAvgMap)) { professionAvg = (Double) professionAvgMap.get("result"); } //行业贡献率计算 String contributionRate = null; NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (professionSum != null && allDeviceSum != null) { contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%"; } //行业均值 professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); //行业贡献率 professionMap.put("contributionRate", contributionRate); resultMap.put(profession, professionMap); } result.add(resultMap); } for (String profession : professions) { //获取该行业设备 List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList()); //该行业设备当天小时数据 List<HistoryMonthly> professionDeviceData = historyMonthlyService.getValueByMacs(professionMacs, time); //按time分组 Map<String, List<HistoryMonthly>> professionDataMap = professionDeviceData.stream() .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7))); for (Map<String, Object> map : result) { Object allDeviceSum = map.remove("allDeviceSum"); String resultTime = map.get("time").toString(); List<HistoryMonthly> historyMonthlyList = professionDataMap.get(resultTime); //贡献率 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"); //行业贡献率计算 NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (allDeviceSum != null) { contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; } } Map<String, Object> professionMap = new HashMap<>(); professionMap.put("contributionRate", contributionRate); professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); map.put(profession, professionMap); } } return result; } @@ -261,92 +287,110 @@ private List<Map<String, Object>> professionContributionOfMonth(Integer orgId, List<String> professions, 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 allMacs = getMacsByOrgId(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.split("-")[2]); yearMonthDay = yearMonthDay + " 00:00:00"; resultMap.put("time", yearMonthDay); //所有设备该因子累加值 Double allDeviceSum = null; QueryWrapper<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") .eq("time", yearMonthDay) .in("mac", allMacs); Map<String, Object> allDeviceSumMap = historyDailyService.getMap(historyDailyQueryWrapper); if (!ObjectUtils.isEmpty(allDeviceSumMap)) { allDeviceSum = (Double) allDeviceSumMap.get("result"); List<HistoryDaily> historyDailyList = allDeviceDataMap.get(yearMonthDay); if (!ObjectUtils.isEmpty(historyDailyList)) { allDeviceSum = 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(); } resultMap.put("allDeviceSum", allDeviceSum); //本市数据 //本市值 Double cityValue = null; //根据组织获取区域定位 Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode(); QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); cityAqiDailyQueryWrapper.select("`value`->'$." + sensorCode + "' AS result") .eq("city_code", locationLevelCode) .eq("time", yearMonthDay); Map<String, Object> cityValueMap = cityAqiDailyService.getMap(cityAqiDailyQueryWrapper); if (!ObjectUtils.isEmpty(cityValueMap)) { cityValue = (Double) cityValueMap.get("result"); if (cityAqiMap.get(yearMonthDay) != null) { Map<String, Object> dataValue = JSONObject.parseObject(cityAqiMap.get(yearMonthDay).toString(), Map.class); Object o = dataValue.get(sensorCode); if (o != null) { cityValue = (Double) o; } } resultMap.put("cityValue", cityValue); for (String profession : professions) { Map<String, Object> professionMap = new HashMap<>(); //获取该行业设备 List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList()); //该行业累加值 Double professionSum = null; historyDailyQueryWrapper.clear(); historyDailyQueryWrapper.select("SUM(`value`->'$." + sensorCode + "') AS result") .eq("time", yearMonthDay) .in("mac", professionMacs); Map<String, Object> professionSumMap = historyDailyService.getMap(historyDailyQueryWrapper); if (!ObjectUtils.isEmpty(professionSumMap)) { professionSum = (Double) professionSumMap.get("result"); } //该行业平均值 Double professionAvg = null; historyDailyQueryWrapper.clear(); historyDailyQueryWrapper.select("AVG(`value`->'$." + sensorCode + "') AS result") .eq("time", yearMonthDay) .in("mac", professionMacs); Map<String, Object> professionAvgMap = historyDailyService.getMap(historyDailyQueryWrapper); if (!ObjectUtils.isEmpty(professionAvgMap)) { professionAvg = (Double) professionAvgMap.get("result"); } //行业贡献率计算 String contributionRate = null; NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (professionSum != null && allDeviceSum != null) { contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%"; } //行业均值 professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); //行业贡献率 professionMap.put("contributionRate", contributionRate); resultMap.put(profession, professionMap); } result.add(resultMap); } for (String profession : professions) { //获取该行业设备 List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList()); //该行业设备当天小时数据 List<HistoryDaily> professionDeviceData = historyDailyService.getValueByMacs(professionMacs, time); //按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.remove("allDeviceSum"); String resultTime = map.get("time").toString(); List<HistoryDaily> historyDailyList = professionDataMap.get(resultTime); //贡献率 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"); //行业贡献率计算 NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (allDeviceSum != null) { contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; } } Map<String, Object> professionMap = new HashMap<>(); professionMap.put("contributionRate", contributionRate); professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); map.put(profession, professionMap); } } return result; } @@ -354,62 +398,111 @@ private List<Map<String, Object>> professionContributionOfDay(Integer orgId, List<String> professions, 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 allMacs = getMacsByOrgId(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.split(" ")[1]); yearMonthDayHour = yearMonthDayHour + ":00:00"; resultMap.put("time", yearMonthDayHour); //所有设备该因子累加值 Double allDeviceSum = historyHourlyService.getSensorSumByMacs(sensorCode, allMacs, yearMonthDayHour); 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(); } resultMap.put("allDeviceSum", allDeviceSum); //本市数据 //本市值 Double cityValue = null; //根据组织获取区域定位 Integer locationLevelCode = organizationService.getById(orgId).getLocationLevelCode(); QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>(); cityAqiQueryWrapper.select("`value`->'$." + sensorCode + "' AS result") .eq("city_code", locationLevelCode) .eq("time", yearMonthDayHour); Map<String, Object> cityValueMap = cityAqiService.getMap(cityAqiQueryWrapper); if (!ObjectUtils.isEmpty(cityValueMap)) { cityValue = (Double) cityValueMap.get("result"); 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; } } resultMap.put("cityValue", cityValue); for (String profession : professions) { Map<String, Object> professionMap = new HashMap<>(); //获取该行业设备 List<Device> devices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); List<String> professionMacs = devices.stream().map(Device::getMac).collect(Collectors.toList()); //获取该行业设备数据,累加值,平均值 Double professionAvg = historyHourlyService.getSensorAvgByMacs(sensorCode, professionMacs, yearMonthDayHour); Double professionSum = historyHourlyService.getSensorSumByMacs(sensorCode, professionMacs, yearMonthDayHour); //行业贡献率计算 String contributionRate = null; NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (professionSum != null && allDeviceSum != null) { contributionRate = numberFormat.format(professionSum / allDeviceSum * 100) + "%"; } //行业均值 professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); //行业贡献率 professionMap.put("contributionRate", contributionRate); resultMap.put(profession, professionMap); } result.add(resultMap); } for (String profession : professions) { //获取该行业设备 List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession)); List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList()); //该行业设备当天小时数据 List<HistoryHourly> professionDeviceData = historyHourlyService.getValueByMacs(professionMacs, time); //按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.remove("allDeviceSum"); String resultTime = map.get("time").toString(); List<HistoryHourly> historyHourlyList = professionDataMap.get(resultTime); //贡献率 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"); //行业贡献率计算 NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); if (allDeviceSum != null) { contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%"; } } Map<String, Object> professionMap = new HashMap<>(); professionMap.put("contributionRate", contributionRate); professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0)); map.put(profession, professionMap); } } return result; } @@ -424,5 +517,75 @@ 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/resources/mapper/HistoryHourlyMapper.xml
@@ -14,24 +14,4 @@ </foreach> and `time` = #{time} </select> <select id="getSensorSumByMacs" resultType="java.lang.Double"> SELECT SUM(`value`->'$.${sensorCode}') FROM history_hourly_${timeUnits} WHERE `time` = #{time} AND mac IN <foreach collection="macs" item="mac" index="index" open="(" close=")" separator=","> #{mac} </foreach> </select> <select id="getSensorAvgByMacs" resultType="java.lang.Double"> SELECT AVG(`value`->'$.${sensorCode}') FROM history_hourly_${timeUnits} WHERE `time` = #{time} AND mac IN <foreach collection="macs" item="mac" index="index" open="(" close=")" separator=","> #{mac} </foreach> </select> </mapper>