jinpengyong
2021-12-29 48e498136c8784ee79a698da2c852ca3aa0549ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Device;
import com.moral.api.entity.Sensor;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
 
import java.util.HashMap;
 
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-06-28
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
 
    @Autowired
    private DeviceMapper deviceMapper;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Override
    public List<Device> getDevicesByMonitorPointId(Integer monitorPointId) {
        QueryWrapper<Device> wrapper = new QueryWrapper();
        wrapper.eq("monitor_point_id", monitorPointId);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        return deviceMapper.selectList(wrapper);
    }
 
    @Override
    public Map<String, Object> getSensorsByMac(Map<String, Object> params) {
        //设备mac
        List<String> macs = (List<String>) params.remove("macs");
        List<Map<String, Object>> elementLists = new ArrayList<>();
 
        for (String mac : macs) {
            //从redis中获取设备因子信息
            Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac);
            List<Sensor> sensors = device.getVersion().getSensors();
            Map<String, Object> map = new HashMap<>();
            for (Sensor sensor : sensors) {
                String sensorCode = sensor.getCode();
                String name = sensor.getName();
                map.put(sensorCode, name);
            }
            elementLists.add(map);
        }
 
        Map<String, Object> map = elementLists.parallelStream()
                .filter(elementList -> elementList.size() != 0)
                .reduce((a, b) -> {
                    a.keySet().retainAll(b.keySet());
                    return a;
                }).orElse(new HashMap<>());
        return map;
    }
 
    @Override
    public List<Map<String, Object>> getTrendChartData(Map<String, Object> params) {
        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) {
            if ("hour".equals(type)) {
                end = DateUtils.getDateAddDay(start, 1);
                String yearAndMonth = DateUtils.dateToDateString(DateUtils.getDate(start, DateUtils.yyyy_MM_dd_EN), DateUtils.yyyyMM_EN);
                timeUnits = "hourly_" + yearAndMonth;
                dateFormat = "%Y-%m-%d %H";
            } else if ("day".equals(type)) {
                end = DateUtils.getDateAddMonth(start, 1);
                timeUnits = "daily";
                dateFormat = "%Y-%m-%d";
            } else {
                end = DateUtils.getDateAddYear(start, 1);
                timeUnits = "monthly";
                dateFormat = "%Y-%m";
            }
            params.put("timeUnits", timeUnits);
            params.put("start", start);
            params.put("end", end);
            params.put("macs", macs);
            params.put("dateFormat", dateFormat);
            //获取多设备指定因子数据
            List<Map<String, Object>> list = deviceMapper.getTrendChartData(params);
 
            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);
                        }
                    }
                    deviceData.add(valueMap);
                }
                resultMap.put("deviceData", deviceData);
                result.add(resultMap);
            }
        }
        return result;
    }
 
    @Override
    public Device getDeviceByMac(String mac) {
        Map<String, Object> deviceMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac);
        Device device = JSON.parseObject(JSON.toJSONString(deviceMap), Device.class);
        //从map获取organizationId和monitorPointId以及versionId
        Map<String, Object> organizationMap = (Map<String, Object>) deviceMap.get("organization");
        Map<String, Object> monitorPointMap = (Map<String, Object>) deviceMap.get("monitorPoint");
        Map<String, Object> versionMap = (Map<String, Object>) deviceMap.get("version");
        device.setDeviceVersionId((Integer) versionMap.get("id"));
        device.setOrganizationId((Integer) organizationMap.get("id"));
        device.setMonitorPointId((Integer) monitorPointMap.get("id"));
        //如果缓存为空则查询数据库
        if (ObjectUtils.isEmpty(device)) {
            return getDeviceByMacFromDB(mac);
        }
        return device;
    }
 
    @Override
    public List<Map<String, Object>> getDevicesByOrganizationId(Integer orgId) {
        //从数据库获取mac
        List macs = getMacsByOrganizationId(orgId);
        //从redis获取设备详细信息
        List<Map<String, Object>> result = new ArrayList<>();
        for (Object mac : macs) {
            Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac.toString());
            result.add(map);
        }
        return result;
    }
 
    @Override
    public List getMacsByOrganizationId(Integer organizationId) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac")
                .eq("organization_id", organizationId)
                .eq("is_delete", Constants.NOT_DELETE);
        return deviceMapper.selectObjs(queryWrapper);
    }
 
    @Override
    public List getMacsByOrgIdAndRegionCode(Integer organizationId,Integer regionCode) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac")
                .eq("organization_id", organizationId)
                .eq("is_delete", Constants.NOT_DELETE)
                .eq("town_code", regionCode);
        return deviceMapper.selectObjs(queryWrapper);
    }
 
    private Device getDeviceByMacFromDB(String mac) {
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("mac", mac);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        return deviceMapper.selectOne(wrapper);
    }
 
 
}