jinpengyong
2021-08-04 765460125202c993f737dee3a05d54c66ade2499
screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -1,7 +1,5 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Device;
import com.moral.api.entity.Sensor;
@@ -17,18 +15,11 @@
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
 * <p>
@@ -42,7 +33,7 @@
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Autowired
    DeviceMapper deviceMapper;
    private DeviceMapper deviceMapper;
    @Autowired
    private RedisTemplate redisTemplate;
@@ -88,72 +79,65 @@
        Object type = params.get("type");
        //设备mac
        List<String> macs = (List<String>) params.remove("macs");
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "name").in("mac", macs);
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        //所选时间
        List<String> times = (List<String>) params.remove("times");
        //因子code
        String sensorCode = params.get("sensorCode").toString();
        String end;
        String timeUnits;
        String dateFormat;
        //返回结果集,time=data
        List<Map<String, Object>> result = new ArrayList<>();
        for (String start : times) {
            Map<String, Object> resultMap = new HashMap<>();
            if ("day".equals(type)) {
                end = DateUtils.getDateAddDay(start, 1);
                timeUnits = "hourly";
                dateFormat = "%k";
            } else if ("month".equals(type)) {
                end = DateUtils.getDateAddMonth(start, 1);
                timeUnits = "daily";
                dateFormat = "%e";
            } else {
                end = DateUtils.getDateAddYear(start, 1);
                timeUnits = "monthly";
                dateFormat = "%c";
            }
            params.put("timeUnits", timeUnits);
            params.put("dateFormat", dateFormat);
            params.put("start", start);
            params.put("end", end);
            params.put("macs", macs);
            params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
            //获取多设备指定因子数据
            List<Map<String, Object>> list = deviceMapper.getTrendChartData(params);
            //按time分组
            Map<String, List<Map<String, Object>>> data = list.parallelStream()
                    .collect(Collectors.groupingBy(o -> o.get("time").toString()));
            //TreeMap<String, List<Map<String, Object>>> data = new TreeMap<>(listMap);
            for (Map.Entry<String, List<Map<String, Object>>> entry : data.entrySet()) {
                List<Object> values = new ArrayList<>();
                String time = entry.getKey();
                List<Map<String, Object>> value = entry.getValue();
                if (value.isEmpty()) {
                    continue;
                }
                for (String mac : macs) {
                    boolean flag = false;
                    for (Map<String, Object> map : value) {
                        if (mac.equals(map.get("mac"))) {
                            Object o = map.get(sensorCode);
                            values.add(o);
                            flag = true;
                            break;
            for (String s : DateUtils.getTimeLag(start)) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("time", s);
                List<Map<String, Object>> deviceData = new ArrayList<>();
                for (Device device : devices) {
                    Map<String, Object> valueMap = new HashMap<>();
                    valueMap.put("name", device.getName());
                    valueMap.put("sensorValue", "");
                    for (Map<String, Object> map : list) {
                        Object time = map.get("time");
                        Object sensorValue = map.get(sensorCode);
                        Object mac = map.get("mac");
                        if (s.equals(time) && device.getMac().equals(mac)) {
                            valueMap.put("sensorValue", sensorValue);
                        }
                    }
                    if (!flag) {
                        values.add("");
                    }
                    deviceData.add(valueMap);
                }
                resultMap.put(time, values);
                resultMap.put("deviceData", deviceData);
                result.add(resultMap);
            }
            result.add(resultMap);
        }
        return result;
    }
}