jpy
2023-05-27 41a8d1d5ff0192baaba3130f3113a6ccfbcf6a33
screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java
@@ -1,7 +1,9 @@
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.HistoryMonthlyMapper;
import com.moral.api.pojo.dto.dataDisplay.MonitorPointDataDisplayDTO;
import com.moral.api.pojo.dto.dataDisplay.SensorComparisonDisplayDTO;
import com.moral.api.pojo.form.dataDisplay.MonitorPointDataDisplayForm;
@@ -32,6 +34,8 @@
    HistoryDailyService historyDailyService;
    @Autowired
    HistoryHourlyService historyHourlyService;
    @Autowired
    HistoryMonthlyMapper historyMonthlyMapper;
    @Autowired
    HistoryWeeklyService historyWeeklyService;
    @Autowired
@@ -213,6 +217,147 @@
        return dtos;
    }
    @Override
    public List<SensorComparisonDisplayDTO> getSensorComparisonDisplayDataV2(Map<String, Object> params) {
        //取参
        List<String> sensors = (List<String>) params.get("sensorCodes");
        /*Date startDate = form.getStartDate();
        Date endDate = form.getEndDate();*/
        //所选时间
        List<String> times = (List<String>) params.get("times");
        String startTime = times.get(0);
        String endTime = times.get(1);
        String mac = params.get("mac").toString();
        String reportType = params.get("reportType").toString();
        Map<String, String> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
        //处理月数据
        if (reportType.equals(Constants.MONTHLY_REPORT)) {
            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);
            }
            //补上无数据时间
            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);
            }
        }
        //处理小时数据
        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<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);
            }
            //补上无数据时间
            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<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);
            }
            //补上无数据时间
            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<SensorComparisonDisplayDTO> dtos = new ArrayList<>();
        for (String sensor : sensors) {
            SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO();
            List<Map<String, Object>> 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<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);
                //如果数据中没有该因子,则放入空
                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<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> 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;
    }
    /**
     * @Description: 根据时间进行排序
     * @Param: [timeValueMap, reportType]