| | |
| | | import com.moral.api.pojo.dto.dataDisplay.SensorComparisonDisplayDTO; |
| | | import com.moral.api.pojo.form.dataDisplay.MonitorPointDataDisplayForm; |
| | | import com.moral.api.pojo.form.dataDisplay.SensorComparisonDisplayForm; |
| | | import com.moral.api.pojo.vo.historyMonthly.HistoryResultVo; |
| | | import com.moral.api.service.*; |
| | | import com.moral.api.utils.GetCenterPointFromListOfCoordinates; |
| | | import com.moral.api.utils.StringUtils; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.SeparateTableType; |
| | | import com.moral.pojo.AQI; |
| | |
| | | return dtos; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | List<String> sensors = new ArrayList<>(); |
| | | sensors.add("a34004"); |
| | | sensors.add("a34002"); |
| | | sensors.add("a21026"); |
| | | StringBuilder str = new StringBuilder(); |
| | | for(String s : sensors){ |
| | | str.append(" avg(JSON_EXTRACT( value, '$"+s+"' )) pm,"); |
| | | } |
| | | str.deleteCharAt(str.length() - 1); |
| | | System.out.println(str.toString()); |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<SensorComparisonDisplayDTO> getSensorComparisonDisplayDataV2(Map<String, Object> params) { |
| | | //取参 |
| | |
| | | List<String> times = (List<String>) params.get("times"); |
| | | String startTime = times.get(0); |
| | | String endTime = times.get(1); |
| | | String mac = params.get("mac").toString(); |
| | | boolean type = subStr(params.get("mac").toString()); |
| | | String mac = type?params.get("mac").toString().substring(0, params.get("mac").toString().indexOf(",avg")):params.get("mac").toString(); |
| | | List<String> macList = new ArrayList<>(); |
| | | if(type){ |
| | | macList = deviceService.getMacMonitorPointId(Integer.parseInt(mac)); |
| | | }else { |
| | | macList.add(mac); |
| | | } |
| | | |
| | | String reportType = params.get("reportType").toString(); |
| | | Map<String, String> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json |
| | | //处理月数据 |
| | | if (reportType.equals(Constants.MONTHLY_REPORT)) { |
| | | //封装返回数据 |
| | | List<SensorComparisonDisplayDTO> dtos = new ArrayList<>(); |
| | | |
| | | Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_EN); |
| | | Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_EN); |
| | | //List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(mac, startDate, endDate); |
| | | QueryWrapper<HistoryMonthly> historyMonthlyQueryWrapper = new QueryWrapper<>(); |
| | | historyMonthlyQueryWrapper.eq("mac",mac); |
| | | historyMonthlyQueryWrapper.between("time",startTime.substring(0,7)+"-01 00:00:00",endTime.substring(0,7)+"-01 00:00:00"); |
| | | List<HistoryMonthly> monthlies = historyMonthlyMapper.selectList(historyMonthlyQueryWrapper); |
| | | for (HistoryMonthly historyMonthly : monthlies) { |
| | | Date time = historyMonthly.getTime(); |
| | | String dateStr = DateUtils.dateToDateString(time, "yyyy-MM"); |
| | | String value = historyMonthly.getValue(); |
| | | timeValueMap.put(dateStr, value); |
| | | for(String s : sensors){ |
| | | SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO(); |
| | | if(s.equals("a00e12") || s.equals("a00e03")){ |
| | | dto.setCode("1"); |
| | | }else if (s.equals("a01006")){ |
| | | dto.setCode("2"); |
| | | }else if (s.equals("a99054") || s.equals("a21005")){ |
| | | dto.setCode("3"); |
| | | }else { |
| | | dto.setCode("0"); |
| | | } |
| | | List<Map<String, Object>> dtoTimeValueList = new ArrayList<>(); |
| | | String str = " ROUND(avg(JSON_EXTRACT( value, '$."+s+"' )),2) value "; |
| | | List<HistoryResultVo> monthlies = historyMonthlyMapper.listAll(str.toString(),macList,startTime.substring(0,7)+"-01 00:00:00",endTime.substring(0,7)+"-01 00:00:00"); |
| | | Map<String, List<HistoryResultVo>> prodMap= monthlies.stream().collect(Collectors.groupingBy(HistoryResultVo::getTime)); |
| | | //补上无数据时间 |
| | | Date middleDate = startDate; |
| | | while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN),DateUtils.yyyy_MM_EN)<=0){ |
| | | Map<String, Object> resultMap = new LinkedHashMap<>(); |
| | | resultMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN)); |
| | | if (prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN)) == null){ |
| | | resultMap.put("value",null); |
| | | }else { |
| | | List<HistoryResultVo> resultList = prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN)); |
| | | resultMap.put("value", resultList.get(0).getValue()); |
| | | } |
| | | dtoTimeValueList.add(resultMap); |
| | | middleDate = DateUtils.addMonths(middleDate,1); |
| | | } |
| | | |
| | | dto.setTimeValueList(dtoTimeValueList); |
| | | dtos.add(dto); |
| | | } |
| | | //补上无数据时间 |
| | | Date middleDate = startDate; |
| | | while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN),DateUtils.yyyy_MM_EN)<=0){ |
| | | if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN)) == null) |
| | | timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN), null); |
| | | middleDate = DateUtils.addMonths(middleDate,1); |
| | | } |
| | | |
| | | return dtos; |
| | | |
| | | } |
| | | //处理小时数据 |
| | | else if (reportType.equals(Constants.HOURLY_REPORT)) { |
| | |
| | | dtos.add(dto); |
| | | } |
| | | return dtos; |
| | | } |
| | | @Override |
| | | public List<SensorComparisonDisplayDTO> getSensorComparisonDisplayDataV3(Map<String, Object> params) { |
| | | //取参 |
| | | List<String> sensors = (List<String>) params.get("sensorCodes"); |
| | | //所选时间 |
| | | List<String> times = (List<String>) params.get("times"); |
| | | String startTime = times.get(0); |
| | | String endTime = times.get(1); |
| | | boolean type = subStr(params.get("mac").toString()); |
| | | String mac = type?params.get("mac").toString().substring(0, params.get("mac").toString().indexOf(",avg")):params.get("mac").toString(); |
| | | List<String> macList = new ArrayList<>(); |
| | | if(type){ |
| | | macList = deviceService.getMacMonitorPointId(Integer.parseInt(mac)); |
| | | }else { |
| | | macList.add(mac); |
| | | } |
| | | String reportType = params.get("reportType").toString(); |
| | | List<SensorComparisonDisplayDTO> dtos = new ArrayList<>(); |
| | | Date startDate = null; |
| | | Date endDate = null; |
| | | if (reportType.equals(Constants.MONTHLY_REPORT)) { |
| | | startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_EN); |
| | | endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_EN); |
| | | } else if (reportType.equals(Constants.HOURLY_REPORT)) { |
| | | startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | } else if (reportType.equals(Constants.DAILY_REPORT)) { |
| | | startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); |
| | | endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN); |
| | | } |
| | | for(String s : sensors){ |
| | | SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO(); |
| | | if(s.equals("a00e12") || s.equals("a00e03")){ |
| | | dto.setCode("1"); |
| | | }else if (s.equals("a01006")){ |
| | | dto.setCode("2"); |
| | | }else if (s.equals("a99054") || s.equals("a21005")){ |
| | | dto.setCode("3"); |
| | | }else { |
| | | dto.setCode("0"); |
| | | } |
| | | List<Map<String, Object>> dtoTimeValueList = new ArrayList<>(); |
| | | String str = " ROUND(avg(JSON_EXTRACT( value, '$."+s+"' )),2) value "; |
| | | List<HistoryResultVo> historyResultVos = new ArrayList<>(); |
| | | if (reportType.equals(Constants.MONTHLY_REPORT)) { |
| | | historyResultVos = historyMonthlyMapper.listAll(str,macList,startTime.substring(0,7)+"-01 00:00:00",endTime.substring(0,7)+"-01 00:00:00"); |
| | | } else if (reportType.equals(Constants.HOURLY_REPORT)) { |
| | | historyResultVos = historyHourlyService.getAvgValueByMacAndTime(macList,str, startDate, endDate); |
| | | } else if (reportType.equals(Constants.DAILY_REPORT)) { |
| | | historyResultVos = historyDailyService.listAvgResult(str,macList,DateUtils.dateToDateString(startDate,DateUtils.yyyy_MM_dd_EN) ,DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_EN) ); |
| | | } |
| | | Map<String, List<HistoryResultVo>> prodMap= historyResultVos.stream().collect(Collectors.groupingBy(HistoryResultVo::getTime)); |
| | | Date middleDate = startDate; |
| | | while(DateUtils.isTimeBeforE(endDate,middleDate)){ |
| | | Map<String, Object> resultMap = new LinkedHashMap<>(); |
| | | if (reportType.equals(Constants.MONTHLY_REPORT)) { |
| | | resultMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN)); |
| | | if (prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN)) == null){ |
| | | resultMap.put("value",null); |
| | | }else { |
| | | List<HistoryResultVo> resultList = prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN)); |
| | | resultMap.put("value", resultList.get(0).getValue()); |
| | | } |
| | | dtoTimeValueList.add(resultMap); |
| | | middleDate = DateUtils.addMonths(middleDate,1); |
| | | }else if(reportType.equals(Constants.HOURLY_REPORT)) { |
| | | resultMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)); |
| | | if (prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null){ |
| | | resultMap.put("value",null); |
| | | }else { |
| | | List<HistoryResultVo> resultList = prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)); |
| | | resultMap.put("value", resultList.get(0).getValue()); |
| | | } |
| | | dtoTimeValueList.add(resultMap); |
| | | middleDate = DateUtils.addHours(middleDate,1); |
| | | }else if(reportType.equals(Constants.DAILY_REPORT)){ |
| | | resultMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN)); |
| | | if (prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN)) == null){ |
| | | resultMap.put("value",null); |
| | | }else { |
| | | List<HistoryResultVo> resultList = prodMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN)); |
| | | resultMap.put("value", resultList.get(0).getValue()); |
| | | } |
| | | dtoTimeValueList.add(resultMap); |
| | | middleDate = DateUtils.addDays(middleDate,1); |
| | | } |
| | | } |
| | | dto.setSensorCode(s); |
| | | dto.setTimeValueList(dtoTimeValueList); |
| | | dtos.add(dto); |
| | | } |
| | | return dtos; |
| | | } |
| | | |
| | | /** |
| | |
| | | dto.setComIndex(comIndex); |
| | | }); |
| | | } |
| | | |
| | | private boolean subStr(String mac){ |
| | | if(StringUtils.isNotEmpty(mac)&&mac.contains(",avg")){ |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | } |
| | | |