package com.moral.api.service.impl; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.moral.api.entity.*; import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.HistoryMonthlyMapper; import com.moral.api.mapper.OrganizationMapper; import com.moral.api.pojo.dto.dataDisplay.HeatMapDTO; import com.moral.api.pojo.dto.dataDisplay.MonitorPointDataDisplayDTO; 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; import com.moral.util.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.util.*; import java.util.stream.Collectors; /** * @ClassName DataDisplayServiceImpl * @Description TODO * @Author 陈凯裕 * @Date 2021/9/26 9:55 * @Version TODO **/ @Service @Slf4j public class DataDisplayServiceImpl implements DataDisplayService { @Autowired DeviceService deviceService; @Autowired HistoryDailyService historyDailyService; @Autowired HistoryHourlyService historyHourlyService; @Autowired HistoryMonthlyMapper historyMonthlyMapper; @Autowired HistoryWeeklyService historyWeeklyService; @Autowired HistoryMonthlyService historyMonthlyService; @Autowired OrganizationMapper organizationMapper; @Autowired DeviceMapper deviceMapper; @Override public List getMonitorPointDisplayData(MonitorPointDataDisplayForm form) { //取参 // Integer monitorPointId = form.getMacs(); List macs = form.getMacs(); String reportType = form.getReportType(); Date startTime = form.getStartTime(); Date endTime = form.getEndTime(); //根据站点id获取站点下所有的设备 // List devices = deviceService.getDevicesByMonitorPointId(monitorPointId); QueryWrapper deviceQueryWrapper = new QueryWrapper<>(); deviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); deviceQueryWrapper.in("mac", macs); List devices = deviceMapper.selectList(deviceQueryWrapper); Map deviceMap = new HashMap<>(); // List ListMacs = new ArrayList<>(); devices.forEach(value -> { // ListMacs.add(value.getMac()); deviceMap.put(value.getMac(), value); }); List dtos = new ArrayList<>(); if (reportType == null) {//处理自定义请求 Map> macDataMap = new HashMap<>();//key为mac //获取数据 macs.forEach(value -> { //查询数据 List datas = historyDailyService.getHistoryDailyByMacAndTimeSlot(value, startTime, endTime); //如果查询到的数据不为空 if (datas.size() != 0) macDataMap.put(value, datas); }); //如果都没有数据则不进行计算 if (macDataMap.size() != 0) dtos = calculateCustomData(macDataMap, deviceMap, startTime, endTime); } //处理小时报表请求,默认取上一个小时的值 else if (reportType.equals(Constants.HOURLY_REPORT)) { Map> macDataMap = new HashMap<>(); //获取数据 macs.forEach(value -> { List datas = historyHourlyService.getValueByMacAndTime(value, startTime, endTime); if (datas.size() != 0) macDataMap.put(value,datas); }); if (macDataMap.size() != 0) dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime); } //处理日报请求,默认获取昨天的值 else if (reportType.equals(Constants.DAILY_REPORT)) { Map> macDataMap = new HashMap<>(); //获取数据 macs.forEach(value -> { List datas = historyDailyService.getHistoryDailyByMacAndTimeSlot(value, startTime, endTime); if (datas.size() != 0) macDataMap.put(value,datas); }); if (macDataMap.size() != 0) dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime); } //处理周报请求,默认获取上周的值 else if (reportType.equals(Constants.WEEKLY_REPORT)) { //获取数据 Map macDataMap = historyWeeklyService.getHistoryWeeklyByMacsAndDate(macs, startTime); if (macDataMap.size() != 0) dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime, endTime); } //处理月报请求 else if (reportType.equals(Constants.MONTHLY_REPORT)) { //月报需要获取一个月每天的数据用来算综合指数,其他数据从月表中取出 Map> macDataDailyMap = new HashMap<>(); Map> macDataMonthlyMap = new HashMap<>(); macs.forEach(mac -> { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("mac",mac); wrapper.between("time",startTime,endTime); List monthlyList = historyMonthlyMapper.selectList(wrapper); if (!ObjectUtils.isEmpty(monthlyList)) macDataMonthlyMap.put(mac, monthlyList); }); macs.forEach(mac -> { List dailyDatas = historyDailyService.getHistoryDailyByMacAndTimeSlot(mac, startTime, endTime); if (!ObjectUtils.isEmpty(dailyDatas)) macDataDailyMap.put(mac, dailyDatas); }); if (macDataMonthlyMap.size() != 0) dtos = calculateReportData(macDataMonthlyMap, deviceMap, reportType, startTime); //注入综合指数 injectComprehensiveIndex(dtos, macDataDailyMap); } return dtos; } @Override public List getSensorComparisonDisplayData(SensorComparisonDisplayForm form) { //取参 List sensors = form.getSensors(); Date startDate = form.getStartDate(); Date endDate = form.getEndDate(); String mac = form.getMac(); String reportType = form.getReportType(); Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json //处理日报数据 if (reportType.equals(Constants.DAILY_REPORT)) { List hourlies = historyHourlyService.getValueByMacAndTime(mac, startDate, endDate); for (HistoryHourly historyHourly : hourlies) { Date time = historyHourly.getTime(); String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH"); String value = historyHourly.getValue(); timeValueMap.put(dateStr, value); } //如果数据不足则需要补足一天24小时的时间。 for (int i = 0; i < 24; i++) { //构造时间 String time = DateUtils.dateToDateString(startDate, "yyyy-MM-dd"); if (i < 10) time = time + " 0" + i; else time = time + " " + i; if (timeValueMap.get(time) == null) timeValueMap.put(time, null); } } //处理月报数据 else if (reportType.equals(Constants.MONTHLY_REPORT)) { List dailies = historyDailyService.getHistoryDailyByMacAndTimeSlot(mac, startDate, endDate); for (HistoryDaily historyDaily : dailies) { Date time = historyDaily.getTime(); String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd"); String value = historyDaily.getValue(); timeValueMap.put(String.valueOf(dateStr), value); } //如果数据不足则需要补足一个月的时间。 int days = DateUtils.getMonthDay(startDate); for (int i = 1; i <= days; i++) { //构造时间 String time = DateUtils.dateToDateString(startDate, "yyyy-MM"); if (i < 10) time = time + "-0" + i; else time = time + "-" + i; if (timeValueMap.get(time) == null) timeValueMap.put(time, null); } } else return null; //时间排序 timeValueMap = sortMapByTime(timeValueMap, reportType); //封装返回数据 List dtos = new ArrayList<>(); for (String sensor : sensors) { SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO(); List> dtoTimeValueList = new ArrayList<>(); dto.setSensorCode(sensor); timeValueMap.forEach((time, valueJson) -> { Map listMap = new HashMap<>(); //如果对应时间没有数据,则放入空 if (valueJson == null) { listMap.put("time", time); listMap.put("value", ""); dtoTimeValueList.add(listMap); return; } Map valueMap = JSON.parseObject(valueJson, Map.class); Object sensorValueObject = valueMap.get(sensor); //如果数据中没有该因子,则放入空 if (sensorValueObject == null) { listMap.put("time", time); listMap.put("value", ""); dtoTimeValueList.add(listMap); return; } //如果是小时数据需要判断标记位 if (reportType.equals(Constants.DAILY_REPORT)) { //如果标记位不为N,则放入空 if (!Constants.NORMAL_FLAG.equals(valueMap.get(sensor + "-Flag"))) { listMap.put("time", time); listMap.put("value", ""); dtoTimeValueList.add(listMap); return; } } //取出数据 Double sensorValue = Double.parseDouble(sensorValueObject.toString()); //封装数据 listMap.put("time", time); listMap.put("value", sensorValue); dtoTimeValueList.add(listMap); }); dto.setTimeValueList(dtoTimeValueList); dtos.add(dto); } return dtos; } public static void main(String[] args) { List 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 getSensorComparisonDisplayDataV2(Map params) { //取参 List sensors = (List) params.get("sensorCodes"); /*Date startDate = form.getStartDate(); Date endDate = form.getEndDate();*/ //所选时间 List times = (List) 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 macList = new ArrayList<>(); if(type){ macList = deviceService.getMacMonitorPointId(Integer.parseInt(mac)); }else { macList.add(mac); } String reportType = params.get("reportType").toString(); Map timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json //处理月数据 if (reportType.equals(Constants.MONTHLY_REPORT)) { //封装返回数据 List dtos = new ArrayList<>(); Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_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> dtoTimeValueList = new ArrayList<>(); String str = " ROUND(avg(JSON_EXTRACT( value, '$."+s+"' )),2) value "; List monthlies = historyMonthlyMapper.listAll(str.toString(),macList,startTime.substring(0,7)+"-01 00:00:00",endTime.substring(0,7)+"-01 00:00:00"); Map> 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 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 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); } return dtos; } //处理小时数据 else if (reportType.equals(Constants.HOURLY_REPORT)) { Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); List hourlies = historyHourlyService.getValueByMacAndTime(mac, startDate, endDate); for (HistoryHourly historyHourly : hourlies) { Date time = historyHourly.getTime(); String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH"); String value = historyHourly.getValue(); timeValueMap.put(dateStr, value); } //补上无数据时间 Date middleDate = startDate; while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){ if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null) timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN), null); middleDate = DateUtils.addHours(middleDate,1); } } //处理日数据 else if (reportType.equals(Constants.DAILY_REPORT)) { Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN); List dailies = historyDailyService.getHistoryDailyByMacAndTimeSlot(mac, startDate, endDate); for (HistoryDaily historyDaily : dailies) { Date time = historyDaily.getTime(); String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd"); String value = historyDaily.getValue(); timeValueMap.put(String.valueOf(dateStr), value); } //补上无数据时间 Date middleDate = startDate; while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN),DateUtils.yyyy_MM_dd_EN)<=0){ if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN)) == null) timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN), null); middleDate = DateUtils.addDays(middleDate,1); } } else return null; //时间排序 //timeValueMap = sortMapByTime(timeValueMap, reportType); //封装返回数据 List dtos = new ArrayList<>(); for (String sensor : sensors) { SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO(); List> dtoTimeValueList = new ArrayList<>(); if(sensor.equals("a00e12") || sensor.equals("a00e03")){ dto.setCode("1"); }else if (sensor.equals("a01006")){ dto.setCode("2"); }else if (sensor.equals("a99054") || sensor.equals("a21005")){ dto.setCode("3"); }else { dto.setCode("0"); } dto.setSensorCode(sensor); timeValueMap.forEach((time, valueJson) -> { Map listMap = new HashMap<>(); //如果对应时间没有数据,则放入空 if (valueJson == null) { listMap.put("time", time); listMap.put("value", ""); dtoTimeValueList.add(listMap); return; } Map valueMap = JSON.parseObject(valueJson, Map.class); Object sensorValueObject = valueMap.get(sensor); //如果数据中没有该因子,则放入空 if (sensorValueObject == null) { listMap.put("time", time); listMap.put("value", ""); dtoTimeValueList.add(listMap); return; } //如果是小时数据需要判断标记位 if (reportType.equals(Constants.HOURLY_REPORT)) { //如果标记位不为N,则放入空 if (!Constants.NORMAL_FLAG.equals(valueMap.get(sensor + "-Flag"))) { listMap.put("time", time); listMap.put("value", ""); dtoTimeValueList.add(listMap); return; } } //取出数据 Double sensorValue = Double.parseDouble(sensorValueObject.toString()); //封装数据 listMap.put("time", time); listMap.put("value", sensorValue); dtoTimeValueList.add(listMap); }); Collections.sort(dtoTimeValueList, new Comparator>() { public int compare(Map o1, Map o2) { String id1 = (String) o1.get("time"); String id2 = (String) o2.get("time"); return id1.compareTo(id2); } }); dto.setTimeValueList(dtoTimeValueList); dtos.add(dto); } return dtos; } @Override public List getSensorComparisonDisplayDataV3(Map params) { //取参 List sensors = (List) params.get("sensorCodes"); //所选时间 List times = (List) 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 macList = new ArrayList<>(); if(type){ macList = deviceService.getMacMonitorPointId(Integer.parseInt(mac)); }else { macList.add(mac); } String reportType = params.get("reportType").toString(); List 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> dtoTimeValueList = new ArrayList<>(); String str = " ROUND(avg(JSON_EXTRACT( value, '$."+s+"' )),2) value "; List 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> prodMap= historyResultVos.stream().collect(Collectors.groupingBy(HistoryResultVo::getTime)); Date middleDate = startDate; while(DateUtils.isTimeBeforE(endDate,middleDate)){ Map 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 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 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 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; } /** * 热力图显示 * @param code * @param startTime * @param type * @return */ @Override public List getHeatMapData(String code, String startTime, String type, String form) { HashMap map = new HashMap<>(); map.put("start",startTime); map.put("type","$."+ type); ArrayList list = new ArrayList<>(); // ArrayList> rsHeatMap = new ArrayList<>(); ArrayList rsHeatMap = new ArrayList<>(); if (form.equals("hour")){ //小时 Date date1 = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN); List tableNames = MybatisPLUSUtils.getTableNamesByWrapper(date1, date1, SeparateTableType.MONTH); // for (Integer integer : list) { map.put("organizationIds",list); map.put("tableName",tableNames.get(0)); // List> heatMap = deviceMapper.getHeatMap(map); List heatMap = deviceMapper.getHeatMap(map); rsHeatMap.addAll(heatMap); // } }else { //天 // for (Integer integer : list) { map.put("organizationIds",list); // List> heatMap = deviceMapper.getHeatMap(map); List heatMap = deviceMapper.getHeatMap(map); rsHeatMap.addAll(heatMap); // } } return distrinList(rsHeatMap); } @Override public List getHeatMapDataV2(Integer id, String startTime, String type, String form) { HashMap map = new HashMap<>(); map.put("start",startTime); map.put("type","$."+ type); ArrayList list = new ArrayList<>(); list.add(id); ArrayList rsHeatMap = new ArrayList<>(); /* ArrayList list1 = new ArrayList<>(); list1.add("腾鳌镇政府"); list1.add("西四镇政府"); list1.add("孤山镇政府"); list1.add("英落镇政府"); list1.add("马风镇朱红村");*/ if (form.equals("hour")){ //小时 // String[] split = startTime.split("-"); // String s = "_" + split[0] + split[1]; String dateString = DateUtils.stringToDateString(startTime, DateUtils.yyyy_MM_dd_HH_EN, DateUtils.yyyyMM_EN); map.put("organizationIds",list); map.put("tableName","_"+dateString); List heatMap = deviceMapper.getHeatMapV1(map); //过滤数据 List collect1 = heatMap.stream().filter(d -> d.getTime() != null).collect(Collectors.toList()); if (ObjectUtils.isEmpty(collect1)){ return null; } for (HeatMapDTO heatMapDTO : heatMap) { // if (list1.contains(heatMapDTO.getName())){ if (type.equals("a34002") || type.equals("a21026")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?49.0:heatMapDTO.getCount()); }else if (type.equals("a34004")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?20.0:heatMapDTO.getCount()); }else if (type.equals("a21004")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?20.0:heatMapDTO.getCount()); }else if (type.equals("a21005")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?1.0:heatMapDTO.getCount()); }else if (type.equals("a05024")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?138.0:heatMapDTO.getCount()); }else { heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?0.2:heatMapDTO.getCount()); } // }else { // if (ObjectUtils.isEmpty(heatMapDTO.getCount())){ // heatMapDTO.setCount(0.0); // } // } } getHeatMap(heatMap,type); rsHeatMap.addAll(heatMap); }else { //天 map.put("organizationIds",list); List heatMap = deviceMapper.getHeatMapV1(map); List collect1 = heatMap.stream().filter(d -> d.getTime() != null).collect(Collectors.toList()); if (ObjectUtils.isEmpty(collect1)){ return null; } for (HeatMapDTO heatMapDTO : heatMap) { if (type.equals("a34002") || type.equals("a21026")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?49.0:heatMapDTO.getCount()); }else if (type.equals("a34004")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?34.0:heatMapDTO.getCount()); }else if (type.equals("a21004")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?39.0:heatMapDTO.getCount()); }else if (type.equals("a21005")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?1.9:heatMapDTO.getCount()); }else if (type.equals("a05024")){ heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?100.0:heatMapDTO.getCount()); }else { heatMapDTO.setCount(ObjectUtils.isEmpty(heatMapDTO.getCount())?0.4:heatMapDTO.getCount()); } } getHeatMap(heatMap,type); rsHeatMap.addAll(heatMap); } if (id==71){ for (int i = 0; i <6; i++) { HeatMapDTO heatMapDTO = new HeatMapDTO(); heatMapDTO.setCount(0.0); if (i==0){ heatMapDTO.setLat(40.590436); heatMapDTO.setLng(122.861935); heatMapDTO.setMac("1111"); heatMapDTO.setName("1111"); }else if (i==1){ heatMapDTO.setLat(40.636617); heatMapDTO.setLng(123.101544); heatMapDTO.setMac("2222"); heatMapDTO.setName("2222"); }else if (i==2){ heatMapDTO.setLat(40.890881); heatMapDTO.setLng(122.910687); heatMapDTO.setMac("3333"); heatMapDTO.setName("3333"); }else if (i==3){ heatMapDTO.setLat(40.682129); heatMapDTO.setLng(123.105836); heatMapDTO.setMac("4444"); heatMapDTO.setName("4444"); }else if (i==4){ heatMapDTO.setLat(40.890037); heatMapDTO.setLng(123.021151); heatMapDTO.setMac("5555"); heatMapDTO.setName("5555"); }else { heatMapDTO.setLat(41.051333); heatMapDTO.setLng(122.505864); heatMapDTO.setMac("6666"); heatMapDTO.setName("6666"); } rsHeatMap.add(heatMapDTO); } } if (id==73){ //亭湖区 double lat =33.414538; double lng =120.066616; for (int i = 0; i <11 ; i++) { for (int j = 0; j < 20; j++) { HeatMapDTO heatMapDTO = new HeatMapDTO(); heatMapDTO.setCount(0.0); heatMapDTO.setLat(lat); heatMapDTO.setLng(lng); heatMapDTO.setName("1"); heatMapDTO.setMac(i+"1"+j); rsHeatMap.add(heatMapDTO); lng=lng+0.01; if (j==19){ lng=120.066616; } } lat =lat-0.01; } //大丰区 double lat1 =33.214555; double lng1 =120.416805; for (int i = 0; i < 11 ; i++) { for (int j = 0; j < 10; j++) { HeatMapDTO heatMapDTO = new HeatMapDTO(); heatMapDTO.setCount(0.0); heatMapDTO.setLat(lat1); heatMapDTO.setLng(lng1); heatMapDTO.setName("2"); heatMapDTO.setMac(i+"2"+j); rsHeatMap.add(heatMapDTO); lng1=lng1+0.004; if (j==9){ lng1=120.416805; } } lat1 =lat1-0.0022; } } return distrinList(rsHeatMap); } //数据处理 private void getHeatMap(List heatMap,String type) { //数据过滤 List collect1 = heatMap.stream().filter(d -> d.getGroupId() != null).collect(Collectors.toList()); if (ObjectUtils.isEmpty(collect1)){ return; } //数据分类 Map> collect = collect1.parallelStream().collect(Collectors.groupingBy(o ->o.getGroupId())); Set integers = collect.keySet(); int i = 0 ; for (Integer integer : integers) { ArrayList doubleArrayList = new ArrayList<>(); ArrayList geoCoordinates = new ArrayList<>(); List heatMapDTOS = collect.get(integer); for (HeatMapDTO heatMapDTO : heatMapDTOS) { GeoCoordinate geoCoordinate = new GeoCoordinate(); doubleArrayList.add(heatMapDTO.getCount()); // heatMapDTO.setCount(heatMapDTO.getCount()); if (type.equals("a21026")){ heatMapDTO.setCount(1.0); }else { heatMapDTO.setCount(0.0); } geoCoordinate.setLongitude(heatMapDTO.getLng()); geoCoordinate.setLatitude(heatMapDTO.getLat()); geoCoordinates.add(geoCoordinate); } if (!ObjectUtils.isEmpty(doubleArrayList)){ HeatMapDTO heatMapDTO = new HeatMapDTO(); //计算均值集合 double asDouble = doubleArrayList.stream().mapToDouble(Double::valueOf).max().getAsDouble(); //获取中心点 GeoCoordinate centerPoint = GetCenterPointFromListOfCoordinates.getCenterPoint(geoCoordinates); // double rsCode = asDouble - (doubleArrayList.size()*10); double rsCode = asDouble - doubleArrayList.size()-1; // log.info(asDouble+"-----"+rsCode); // heatMapDTO.setCount(rsCode); heatMapDTO.setCount(rsCode); // heatMapDTO.setSum(asDouble); heatMapDTO.setLng(centerPoint.getLongitude()); heatMapDTO.setLat(centerPoint.getLatitude()); heatMapDTO.setMac(i+""); heatMapDTO.setName(integer+""); heatMap.add(heatMapDTO); i++; } } } /** * 字段去重 * @param responseList * @return */ private List distrinList(List responseList){ List rsMap = new ArrayList<>(); Set keysSet = new HashSet(); for (HeatMapDTO heatMapDTO : responseList) { String keys = String.valueOf(heatMapDTO.getMac()); int beforeSize = keysSet.size(); keysSet.add(keys); int afterSize = keysSet.size(); if(afterSize == beforeSize + 1){ rsMap.add(heatMapDTO); } } return rsMap; } /** * @Description: 根据时间进行排序 * @Param: [timeValueMap, reportType] * @return: java.util.Map * @Author: 陈凯裕 * @Date: 2021/11/3 */ private Map sortMapByTime(Map timeValueMap, String reportType) { List> entries = new ArrayList(timeValueMap.entrySet()); Collections.sort(entries, new Comparator>() { @Override public int compare(Map.Entry o1, Map.Entry o2) { if (Constants.DAILY_REPORT.equals(reportType)) { String atime = o1.getKey(); String btime = o2.getKey(); atime = atime.substring(11, 13); btime = btime.substring(11, 13); return Integer.parseInt(atime) - Integer.parseInt(btime); } else if (Constants.MONTHLY_REPORT.equals(reportType)) { String atime = o1.getKey(); String btime = o2.getKey(); atime = atime.substring(8, 10); btime = btime.substring(8, 10); return Integer.parseInt(atime) - Integer.parseInt(btime); } return 0; } }); Map sortedMap = new LinkedHashMap<>(); for (Map.Entry entry : entries) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } /** * @Description: 计算自定义报表的数据 * @Param: [macDatasMap] key为mac,value为数据集合,数据为日数据 * @return: void * @Author: 陈凯裕 * @Date: 2021/9/26 */ private List calculateCustomData(Map> macDataMap, Map deviceMap, Date startDate, Date endDate) { List dtos = new ArrayList<>(); macDataMap.forEach((key, value) -> { MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); dto.setMac(key); //获取设备名称 String deviceName = deviceMap.get(key).getName(); //拼接时间 String startTime = DateUtils.dateToDateString(startDate, "yyyy-MM-dd"); String endTime = DateUtils.dateToDateString(endDate, "yyyy-MM-dd"); String time = startTime + " - " + endTime; //计算数据个数 Double PM25Num = 0d; Double PM10Num = 0d; Double SO2Num = 0d; Double NO2Num = 0d; Double CONum = 0d; Double O3Num = 0d; Double TVOCNum = 0d; //计算PM2.5,PM10,SO2,NO2,CO,O3,TVOC总值, Double PM25Sum = 0d; Double PM10Sum = 0d; Double SO2Sum = 0d; Double NO2Sum = 0d; Double COSum = 0d; Double O3Sum = 0d; Double TVOCSum = 0d; for (HistoryDaily historyDaily : value) { //获取数据map Map valueMap = JSON.parseObject(historyDaily.getValue(), Map.class); Object SO2 = valueMap.get("a21026"); Object NO2 = valueMap.get("a21004"); Object PM10 = valueMap.get("a34002"); Object PM25 = valueMap.get("a34004"); Object CO = valueMap.get("a21005"); Object O3 = valueMap.get("a05024"); Object TVOC = valueMap.get("a99054"); if (SO2 != null) { SO2Sum = MathUtils.add(SO2Sum, Double.valueOf(SO2.toString())); SO2Num++; } if (NO2 != null) { NO2Sum = MathUtils.add(NO2Sum, Double.valueOf(NO2.toString())); NO2Num++; } if (PM10 != null) { PM10Sum = MathUtils.add(PM10Sum, Double.valueOf(PM10.toString())); PM10Num++; } if (CO != null) { COSum = MathUtils.add(COSum, Double.valueOf(CO.toString())); CONum++; } if (PM25 != null) { PM25Sum = MathUtils.add(PM25Sum, Double.valueOf(PM25.toString())); PM25Num++; } if (O3 != null) { O3Sum = MathUtils.add(O3Sum, Double.valueOf(O3.toString())); O3Num++; } if (TVOC != null) { TVOCSum = MathUtils.add(TVOCSum, Double.valueOf(TVOC.toString())); TVOCNum++; } } //计算PM2.5均值 if (PM25Num != 0d) { Double PM25D = AmendUtils.sciCal(PM25Sum / PM25Num, 0); int PM25Avg = new Double(PM25D).intValue(); dto.setA34004(PM25Avg); } //计算PM10均值 if (PM10Num != 0d) { Double PM10D = AmendUtils.sciCal(PM10Sum / PM10Num, 0); int PM10Avg = new Double(PM10D).intValue(); dto.setA34002(PM10Avg); } //计算SO2均值 if (SO2Num != 0d) { Double SO2D = AmendUtils.sciCal(SO2Sum / SO2Num, 0); int SO2Avg = new Double(SO2D).intValue(); dto.setA21026(SO2Avg); } //计算NO2均值 if (NO2Num != 0d) { Double NO2D = AmendUtils.sciCal(NO2Sum / NO2Num, 0); int NO2Avg = new Double(NO2D).intValue(); dto.setA21004(NO2Avg); } //计算CO均值,保留两位小数 if (CONum != 0d) { Double COAvg = AmendUtils.sciCal(COSum / CONum, 2); dto.setA21005(COAvg); } //计算O3均值 if (O3Num != 0d) { Double O3D = AmendUtils.sciCal(O3Sum / O3Num, 0); int O3Avg = new Double(O3D).intValue(); dto.setA05024(O3Avg); } //计算TVOC均值,保留两位小数 if (TVOCNum != 0d) { Double TVOCAvg = AmendUtils.sciCal(TVOCSum / TVOCNum, 2); dto.setA99054(TVOCAvg); } //计算AQI Map sixParamMap = new HashMap<>(); sixParamMap.put("a05024",dto.getA05024()); sixParamMap.put("a21004",dto.getA21004()); sixParamMap.put("a21005",dto.getA21005()); sixParamMap.put("a34002",dto.getA34002()); sixParamMap.put("a34004",dto.getA34004()); sixParamMap.put("a21026",dto.getA21026()); AQI aqi = AQIUtils.dailyAQI(sixParamMap); dto.setAQI(aqi.getAQIValue()); dto.setDeviceName(deviceName); dto.setTime(time); dtos.add(dto); }); return dtos; } /** * @Description: 计算时报,日报, 周报,月报的数据(只包含6参和AQI,TVOC) * @Param: [macDataMap, deviceMap, startDate] * @return: java.util.List * @Author: 陈凯裕 * @Date: 2021/9/28 */ private List calculateReportData(Map macDataMap, Map deviceMap, String reportType, Date... date) { List dtos = new ArrayList<>(); Set strings = macDataMap.keySet(); for (String key : strings) { if (reportType.equals("0")){ List t = (List)macDataMap.get(key); for (HistoryHourly historyHourly : t) { MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); String value = historyHourly.getValue(); Map map = JSON.parseObject(value, Map.class); String time = DateUtils.dateToDateString(historyHourly.getTime(), "yyyy-MM-dd HH:00:00"); injectDataToDto(map, false, dto, reportType); dto.setTime(time); dto.setMac(key); dto.setDeviceName(deviceMap.get(key).getName()); dtos.add(dto); } } if (reportType.equals("1")){ List t = (List)macDataMap.get(key); for (HistoryDaily historyDaily : t) { MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); String value = historyDaily.getValue(); Map map = JSON.parseObject(value, Map.class); String time = DateUtils.dateToDateString(historyDaily.getTime(), "yyyy-MM-dd"); injectDataToDto(map, false, dto, reportType); dto.setTime(time); dto.setMac(key); dto.setDeviceName(deviceMap.get(key).getName()); dtos.add(dto); } } if (reportType.equals("2")){ HistoryWeekly historyWeekly = (HistoryWeekly)macDataMap.get(key); MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); String value = historyWeekly.getValue(); Map map = JSON.parseObject(value, Map.class); String time = DateUtils.dateToDateString(historyWeekly.getTime(), "yyyy-MM-dd"); injectDataToDto(map, false, dto, reportType); dto.setTime(time); dto.setMac(key); dto.setDeviceName(deviceMap.get(key).getName()); dtos.add(dto); } if (reportType.equals("3")){ List t = (List)macDataMap.get(key); for (HistoryMonthly historyMonthly : t) { MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); String value = historyMonthly.getValue(); Map map = JSON.parseObject(value, Map.class); String time = DateUtils.dateToDateString(historyMonthly.getTime(), "yyyy-MM"); injectDataToDto(map, false, dto, reportType); dto.setTime(time); dto.setMac(key); dto.setDeviceName(deviceMap.get(key).getName()); dtos.add(dto); } } } // macDataMap.forEach((key, valueObject) -> { // MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); // dto.setMac(key); // //数据的map // Map valueMap; // List> list; // //获取对象的value属性值 // Object valueO = ClassUtils.getPropertyValue(valueObject, "value"); // if (valueO == null) // return; // String value = (String) valueO; // list = JSON.parseObject(value, List.class); // for (Map map : list) { // String o = map.get("value").toString(); // valueMap = JSON.parseObject(o, Map.class); // injectDataToDto(valueMap, false, dto, reportType); // String deviceName = deviceMap.get(key).getName(); // dto.setDeviceName(deviceName); // String startTime = DateUtils.dateToDateString((Date) map.get("time"), "yyyy-MM-dd HH:mm:ss"); // dto.setTime(startTime); // dtos.add(dto); // } //获取设备名称 //拼接时间,时报的时间需要精确到小时,日报精确到当天,周报需要含有开始和结束时间 // String startTime = ""; // if (valueObject instanceof HistoryHourly && date.length == 1) // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd HH:mm:ss"); // else if (valueObject instanceof HistoryDaily && date.length == 1) // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd"); // else if (valueObject instanceof HistoryWeekly && date.length == 2) // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd") + " -- " + DateUtils.dateToDateString(date[1], "yyyy-MM-dd"); // else if (valueObject instanceof HistoryMonthly && date.length == 1) // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM"); // dto.setTime(startTime); //注入数据 // }); return dtos; } /** * @Description: 向DTO中注入因子数据,如果数据需要检测标记位,则flag传入true,否则传入false * @Param: [valueMap, flag, dto] * @return: void * @Author: 陈凯裕 * @Date: 2021/9/28 */ private void injectDataToDto(Map valueMap, boolean flag, MonitorPointDataDisplayDTO dto, String reportType) { //注入CO String COFlag = (String) valueMap.get("a21005-Flag"); Object COObject = valueMap.get("a21005"); if ((flag == false || Constants.NORMAL_FLAG.equals(COFlag)) && COObject != null) { Double CO = Double.valueOf(COObject.toString()); dto.setA21005(AmendUtils.sciCal(CO, 2)); } //注入SO2 String SO2Flag = (String) valueMap.get("a21026-Flag"); Object SO2Object = valueMap.get("a21026"); if ((flag == false || Constants.NORMAL_FLAG.equals(SO2Flag)) && SO2Object != null) { Double SO2 = Double.valueOf(SO2Object.toString()); dto.setA21026(new Double(AmendUtils.sciCal(SO2, 0)).intValue()); } //注入NO2 String NO2Flag = (String) valueMap.get("a21004-Flag"); Object NO2Object = valueMap.get("a21004"); if ((flag == false || Constants.NORMAL_FLAG.equals(NO2Flag)) && NO2Object != null) { Double NO2 = Double.valueOf(NO2Object.toString()); dto.setA21004(new Double(AmendUtils.sciCal(NO2, 0)).intValue()); } //注入PM10 String PM10Flag = (String) valueMap.get("a34002-Flag"); Object PM10Object = valueMap.get("a34002"); if ((flag == false || Constants.NORMAL_FLAG.equals(PM10Flag)) && PM10Object != null) { Double PM10 = Double.valueOf(PM10Object.toString()); dto.setA34002(new Double(AmendUtils.sciCal(PM10, 0)).intValue()); } //注入TVOC String TVOCFlag = (String) valueMap.get("a99054-Flag"); Object TVOCObject = valueMap.get("a99054"); if ((flag == false || Constants.NORMAL_FLAG.equals(TVOCFlag)) && TVOCObject != null) { Double TVOC = Double.valueOf(TVOCObject.toString()); dto.setA99054(AmendUtils.sciCal(TVOC, 2)); } //注入PM2.5 String PM25Flag = (String) valueMap.get("a34004-Flag"); Object PM25Object = valueMap.get("a34004"); if ((flag == false || Constants.NORMAL_FLAG.equals(PM25Flag)) && PM25Object != null) { Double PM25 = Double.valueOf(PM25Object.toString()); dto.setA34004(new Double(AmendUtils.sciCal(PM25, 0)).intValue()); } //注入O3 String O3Flag = (String) valueMap.get("a05024-Flag"); Object O3Object = valueMap.get("a05024"); if ((flag == false || Constants.NORMAL_FLAG.equals(O3Flag)) && O3Object != null) { Double O3 = Double.valueOf(O3Object.toString()); dto.setA05024(new Double(AmendUtils.sciCal(O3, 0)).intValue()); } //注入AQI Integer aqi; if (reportType.equals(Constants.HOURLY_REPORT)) { aqi = AQIUtils.hourlyAQI(valueMap).getAQIValue(); } else { aqi = AQIUtils.dailyAQI(valueMap).getAQIValue(); } dto.setAQI(aqi); } /** * @Description: 注入综合指数数据 * @Param: [dtos, macDataDailyMap] * @return: void * @Author: 陈凯裕 * @Date: 2021/9/29 */ private void injectComprehensiveIndex(List dtos, Map> macDataDailyMap) { Map macDtoMap = new HashMap<>(); dtos.forEach(value -> macDtoMap.put(value.getMac(), value)); macDataDailyMap.forEach((key, value) -> { List> list = new ArrayList<>(); for (HistoryDaily historyDaily : value) { Map valueMap = JSON.parseObject(historyDaily.getValue(), Map.class); list.add(valueMap); } Double comIndex = ComprehensiveIndexUtils.monthData(list); MonitorPointDataDisplayDTO dto = macDtoMap.get(key); if (dto != null) dto.setComIndex(comIndex); }); } private boolean subStr(String mac){ if(StringUtils.isNotEmpty(mac)&&mac.contains(",avg")){ return true; } return false; } }