|  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.moral.api.entity.*; | 
|---|
|  |  |  | 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.service.*; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.util.*; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.yaml.snakeyaml.nodes.CollectionNode; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | dtos = calculateCustomData(macDataMap, deviceMap, startTime, endTime); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //处理小时报表请求,默认取上一个小时的值 | 
|---|
|  |  |  | else if (reportType.equals(Constants.HOURYLYREPORT)) { | 
|---|
|  |  |  | else if (reportType.equals(Constants.HOURLY_REPORT)) { | 
|---|
|  |  |  | Map<String, HistoryHourly> macDataMap = new HashMap<>(); | 
|---|
|  |  |  | //获取数据 | 
|---|
|  |  |  | macs.forEach(value -> { | 
|---|
|  |  |  | 
|---|
|  |  |  | dtos = calculateReportData(macDataMap, deviceMap, startTime); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //处理日报请求,默认获取昨天的值 | 
|---|
|  |  |  | else if (reportType.equals(Constants.DAILYREPORT)) { | 
|---|
|  |  |  | else if (reportType.equals(Constants.DAILY_REPORT)) { | 
|---|
|  |  |  | //获取数据 | 
|---|
|  |  |  | Map<String, HistoryDaily> macDataMap = historyDailyService.getHistoryDailyByMacsAndDate(macs, startTime); | 
|---|
|  |  |  | if (macDataMap.size() != 0) | 
|---|
|  |  |  | dtos = calculateReportData(macDataMap, deviceMap, startTime); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //处理周报请求,默认获取上周的值 | 
|---|
|  |  |  | else if (reportType.equals(Constants.WEEKLYREPORT)) { | 
|---|
|  |  |  | else if (reportType.equals(Constants.WEEKLY_REPORT)) { | 
|---|
|  |  |  | //获取数据 | 
|---|
|  |  |  | Map<String, HistoryWeekly> macDataMap = historyWeeklyService.getHistoryWeeklyByMacsAndDate(macs, startTime); | 
|---|
|  |  |  | if (macDataMap.size() != 0) | 
|---|
|  |  |  | dtos = calculateReportData(macDataMap, deviceMap, startTime, endTime); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //处理月报请求 | 
|---|
|  |  |  | else if (reportType.equals(Constants.MONTHLYREPORT)) { | 
|---|
|  |  |  | else if (reportType.equals(Constants.MONTHLY_REPORT)) { | 
|---|
|  |  |  | //月报需要获取一个月每天的数据用来算综合指数,其他数据从月表中取出 | 
|---|
|  |  |  | Map<String, List<HistoryDaily>> macDataDailyMap = new HashMap<>(); | 
|---|
|  |  |  | Map<String, HistoryMonthly> macDataMonthlyMap = historyMonthlyService.getHistoryMonthlyByMacsAndDate(macs, startTime); | 
|---|
|  |  |  | 
|---|
|  |  |  | return dtos; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<SensorComparisonDisplayDTO> getSensorComparisonDisplayData(SensorComparisonDisplayForm form) { | 
|---|
|  |  |  | //取参 | 
|---|
|  |  |  | List<String> sensors = form.getSensors(); | 
|---|
|  |  |  | Date startDate = form.getStartDate(); | 
|---|
|  |  |  | Date endDate = form.getEndDate(); | 
|---|
|  |  |  | String mac = form.getMac(); | 
|---|
|  |  |  | String reportType = form.getReportType(); | 
|---|
|  |  |  | Map<String, String> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json | 
|---|
|  |  |  | //处理日报数据 | 
|---|
|  |  |  | if (reportType.equals(Constants.DAILY_REPORT)) { | 
|---|
|  |  |  | List<HistoryHourly> 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<HistoryDaily> 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<SensorComparisonDisplayDTO> dtos = new ArrayList<>(); | 
|---|
|  |  |  | for (String sensor : sensors) { | 
|---|
|  |  |  | SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO(); | 
|---|
|  |  |  | List<Map<String, Object>> dtoTimeValueList = new ArrayList<>(); | 
|---|
|  |  |  | dto.setSensorCode(sensor); | 
|---|
|  |  |  | timeValueMap.forEach((time, valueJson) -> { | 
|---|
|  |  |  | Map<String, Object> listMap = new HashMap<>(); | 
|---|
|  |  |  | //如果对应时间没有数据 | 
|---|
|  |  |  | if (valueJson == null) { | 
|---|
|  |  |  | listMap.put("time", time); | 
|---|
|  |  |  | listMap.put("value", ""); | 
|---|
|  |  |  | dtoTimeValueList.add(listMap); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Map<String, Object> valueMap = JSON.parseObject(valueJson, Map.class); | 
|---|
|  |  |  | Object sensorValueObject = valueMap.get(sensor); | 
|---|
|  |  |  | //如果数据中没有该因子,则放入null | 
|---|
|  |  |  | if (sensorValueObject == null) { | 
|---|
|  |  |  | listMap.put("time", time); | 
|---|
|  |  |  | listMap.put("value", ""); | 
|---|
|  |  |  | dtoTimeValueList.add(listMap); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //如果是小时数据需要判断标记位 | 
|---|
|  |  |  | if (reportType.equals(Constants.DAILY_REPORT)) { | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Map<String, String> sortMapByTime(Map<String, String> timeValueMap, String reportType) { | 
|---|
|  |  |  | List<Map.Entry<String, String>> entries = new ArrayList(timeValueMap.entrySet()); | 
|---|
|  |  |  | Collections.sort(entries, new Comparator<Map.Entry<String, String>>() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> 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<String, String> sortedMap = new LinkedHashMap<>(); | 
|---|
|  |  |  | for (Map.Entry<String, String> entry : entries) { | 
|---|
|  |  |  | sortedMap.put(entry.getKey(), entry.getValue()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return sortedMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 计算自定义报表的数据 | 
|---|