ZhuDongming
2020-06-30 67a7a24b47d71dd843dd0b17c5cdf9f519e29d5d
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.moral.service.impl;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.common.bean.PageBean;
import com.moral.common.util.MyBatisBaseMapUtil;
import com.moral.entity.Device;
import com.moral.entity.MonitorPoint;
import com.moral.entity.Sensor;
import com.moral.mapper.SensorMapper;
import com.moral.service.MonitorPointService;
import com.moral.service.SensorService;
 
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
 
@Service
public class SensorServiceImpl implements SensorService {
    @Resource
    SensorMapper sensorMapper;
 
    @Resource
    MonitorPointService monitorPointService;
 
    private static Class ENTITY_CLASS = Sensor.class;
 
    /**
     * 查询所有的传感器类型
     *
     * @return
     */
    @Override
    public List<Sensor> queryAll() {
        return sensorMapper.selectByExample(null);
    }
 
    public PageBean<Sensor> queryByPageBean(PageBean pageBean) {
        return MyBatisBaseMapUtil.queryPage(sensorMapper, pageBean, ENTITY_CLASS);
    }
 
    @Override
    public PageBean queryByVersionId(Integer deviceVersionId) {
        List<Sensor> sensorList = sensorMapper.selectByVersionId(deviceVersionId);
        return new PageBean(sensorList);
    }
 
    @Override
    public List<Sensor> queryListByVersionId(Integer deviceVersionId) {
        return sensorMapper.selectByVersionId(deviceVersionId);
    }
 
    @Override
    public List<Sensor> queryListByVersionNo(Integer versionNo) {
        return sensorMapper.selectByVersionNo(versionNo);
    }
 
    public void addOrModify(Sensor sensor) {
        try {
            if (sensor.getId() == null) {
                sensorMapper.insertSelective(sensor);
            } else {
                sensorMapper.updateByPrimaryKeySelective(sensor);
            }
        } catch (Exception ex) {
            throw ex;
        }
    }
 
    @Override
    public void deleteByIds(Integer... ids) {
        if (ids != null && ids.length > 0) {
            if (ids.length == 1) {
                sensorMapper.deleteByPrimaryKey(ids[0]);
            } else {
                Example example = new Example(ENTITY_CLASS);
                example.or().andIn("id", Arrays.asList(ids));
                sensorMapper.deleteByExample(example);
            }
 
        }
    }
 
    /**
     * 获取当前组织下所有传感器并集
     *
     * @param organizationId
     * @return
     */
    @Override
    public List<Sensor> queryByOrgId(Integer organizationId) {
        return sensorMapper.selectByOrgId(organizationId);
    }
 
    @Override
    public List<Sensor> getAllSensors() {
        return sensorMapper.selectAll();
    }
 
    @Override
    public Map<String, Object> getSensorBySensorKey(String sensorKey) {
        Map<String, Object> map = sensorMapper.getSensorBySensorKey(sensorKey);
        return map;
    }
 
    @Override
    public List<Map<String, Object>> getSensorByDeviceId(String id) {
        List<Map<String, Object>> list = sensorMapper.getSensorByDeviceId(id);
        return list;
    }
 
    @Override
    public List<Map<String, Object>> getSensorByMonitorPointId(String monitor_point_id) {
        List<Map<String, Object>> list = sensorMapper.getSensorByMonitorPointId(monitor_point_id);
        return list;
    }
 
    @Override
    public Map<String, String> getSensorsMap(Map<String, Object> parameters) {
        List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
        Map<String, String> sensorMap = new HashMap<>();
        for (Sensor sensor : sensors) {
            sensorMap.put(sensor.getSensorKey(), sensor.getDescription());
        }
        return sensorMap;
    }
 
    @Override
    public List<Map<String, String>> getSensorsMaps(String mac) {
        List<Sensor> sensors = sensorMapper.getSensorsInfoByMac(mac);
        Map<String, String> sensorsDescriptionMap = new HashMap<>();
        for (Sensor sensor : sensors) {
            sensorsDescriptionMap.put(sensor.getSensorKey(), sensor.getDescription());
        }
        Map<String, String> sensorsUnitMap = new HashMap<>();
        for (Sensor sensor : sensors) {
            sensorsUnitMap.put(sensor.getSensorKey(), sensor.getUnit());
        }
        List<Map<String, String>> sensorsMapList = new ArrayList<>();
        sensorsMapList.add(sensorsDescriptionMap);
        sensorsMapList.add(sensorsUnitMap);
        return sensorsMapList;
    }
 
    @Override
    public List<Map<String, String>> getSensorsAllMap() {
        List<Sensor> sensors = sensorMapper.selectAll();
        Map<String, String> sensorsDescriptionMap = new HashMap<>();
        for (Sensor sensor : sensors) {
            sensorsDescriptionMap.put(sensor.getSensorKey(), sensor.getDescription());
        }
        Map<String, String> sensorsUnitMap = new HashMap<>();
        for (Sensor sensor : sensors) {
            sensorsUnitMap.put(sensor.getSensorKey(), sensor.getUnit());
        }
        List<Map<String, String>> sensorsMapList = new ArrayList<>();
        sensorsMapList.add(sensorsDescriptionMap);
        sensorsMapList.add(sensorsUnitMap);
        return sensorsMapList;
    }
 
    @Override
    public List<Sensor> selectSenosrsByOrgId(Map<String, Object> parameters) {
        List<String> macList = new ArrayList<>();
        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsAndDevicesByRegion(parameters);
        for (MonitorPoint m : monitorPoints) {
            for (Device d : m.getDevices()) {
                macList.add(d.getMac());
            }
        }
        parameters.put("macs", macList);
        List<Sensor> sensors = new ArrayList<>();
        if (!CollectionUtils.isEmpty(macList)) {
            sensors = sensorMapper.selectSenosrsByOrgId(parameters);
        } else {
            sensors = sensorMapper.selectAll();
        }
        return sensors;
    }
 
    @Override
    public List<List<String>> listSensorInfos(String[] sensorsResult) {
        List<String> sensorKeys = new ArrayList<>();
        List<String> sensorKeysNames = new ArrayList<>();
        List<String> sensorKeysUnits = new ArrayList<>();
        List<List<String>> sensorInfos = new ArrayList<>();
        for (int index = 0; index < sensorsResult.length; index++) {
            String[] split = sensorsResult[index].split("-");
            String key = split[0].replace("\"", "");
            String name = split[1].replace("\"", "");
            String Unit = split[2].replace("\"", "");
            sensorKeys.add(key);
            sensorKeysNames.add(name);
            sensorKeysUnits.add(Unit);
        }
        sensorInfos.add(sensorKeys);
        sensorInfos.add(sensorKeysNames);
        sensorInfos.add(sensorKeysUnits);
        return sensorInfos;
    }
 
    @Override
    public List<List<Map<String, Object>>> listExcelDatas(List<List<String>> sensorInfos, List<Map<String, Object>> list) {
        List<List<Map<String, Object>>> sheets = new ArrayList<>();
        for (int i = 0; i < sensorInfos.get(0).size(); i++) {
            List<Map<String, Object>> sheet = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                Map<String, Object> data = new LinkedHashMap<>();
                for (Map.Entry<String, Object> kv : list.get(j).entrySet()) {
                    if ("monitorPointName".equals(kv.getKey())) {
                        if ("null".equals(sensorInfos.get(2).get(i))) {
                            data.put("站点名称(单位:)", kv.getValue());
                        } else {
                            data.put("站点名称(单位:" + sensorInfos.get(2).get(i) + ")", kv.getValue());
                        }
                    } else if ("name".equals(kv.getKey())) {
                        data.put("设备名称", kv.getValue());
                    } else {
                        String sensorsValue = kv.getValue().toString();
                        JSONObject jsonObject = JSONObject.parseObject(sensorsValue);
                        if (jsonObject != null) {
                            List<Object> sensorsValueList = (List<Object>) jsonObject.get(sensorInfos.get(0).get(i));
                            if (sensorsValueList != null) {
                                data.put(kv.getKey(), sensorsValueList.get(0));
                            } else {
                                data.put(kv.getKey(), "");
                            }
                        } else {
                            data.put(kv.getKey(), "");
                        }
                    }
                }
                sheet.add(data);
            }
            sheets.add(sheet);
        }
        return sheets;
    }
 
}