cjl
2023-07-04 2c8b914f70a8b2425d3ede5941bce1cabe9c2931
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.moral.api.entity.*;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.HistoryMonthlyMapper;
import com.moral.api.mapper.MonitorPointMapper;
import com.moral.api.mapper.SensorMapper;
import com.moral.api.pojo.form.device.MonitorPointQueryForm;
import com.moral.api.service.DeviceService;
import com.moral.api.service.HistoryDailyService;
import com.moral.api.service.HistoryHourlyService;
import com.moral.api.service.HistoryMonthlyService;
import com.moral.api.service.MonitorPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.constant.Constants;
import com.moral.util.DateUtils;
import com.moral.util.RegionCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-07-01
 */
@Service
public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, MonitorPoint> implements MonitorPointService {
 
    @Autowired
    MonitorPointMapper monitorPointMapper;
    @Autowired
    OrganizationService organizationService;
    @Autowired
    DeviceService deviceService;
 
    @Autowired
    DeviceMapper deviceMapper;
 
    @Autowired
    SensorMapper sensorMapper;
 
    @Autowired
    HistoryHourlyService historyHourlyService;
 
    @Autowired
    HistoryDailyService historyDailyService;
 
    @Autowired
    HistoryMonthlyService historyMonthlyService;
    @Autowired
    HistoryMonthlyMapper historyMonthlyMapper;
 
    @Override
    public List<MonitorPoint> queryByOrgIdAndRegionCode(MonitorPointQueryForm form) {
        //取参
        Integer organizationId = form.getOrganizationId();
        Integer regionCode = form.getRegionCode();
        String region = null;
        if (regionCode != null && organizationId!=24) {
            region = RegionCodeUtils.regionCodeConvertToName(regionCode);
        }
//        if (regionCode != null) {
//            region = RegionCodeUtils.regionCodeConvertToName(regionCode);
//        }
 
        //查询子组织
        List<Organization> childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId);
        List<Integer> organizationIds = new ArrayList<>();
        for (Organization organization : childrenOrganization) {
            organizationIds.add(organization.getId());
        }
        organizationIds.add(organizationId);
        //查询站点
        QueryWrapper<MonitorPoint> queryMonitorPointsWrapper = new QueryWrapper<>();
 
        //如果region不为空,就查询当前组织,所选城市下所有站点及设备信息
        //如果region为空,则查询当前组织下所有的站点和设备
        if (region != null){
            queryMonitorPointsWrapper.eq(region, regionCode);
        }
        queryMonitorPointsWrapper.in("organization_id", organizationIds);
        queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper);
        //查询站点对应的设备
        for (MonitorPoint monitorPoint : monitorPoints) {
            List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPoint.getId());
            monitorPoint.setDevices(devices);
        }
        return monitorPoints;
    }
 
    @Override
    public List<MonitorPoint> queryAllMonitorPoints(Integer organizationId) {
        //查询子组织
        List<Organization> childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId);
        List<Integer> organizationIds = new ArrayList<>();
        for (Organization organization : childrenOrganization) {
            organizationIds.add(organization.getId());
        }
        organizationIds.add(organizationId);
        //查询站点
        QueryWrapper<MonitorPoint> queryMonitorPointsWrapper = new QueryWrapper<>();
        queryMonitorPointsWrapper.select("id","name");
        queryMonitorPointsWrapper.in("organization_id",organizationIds);
        queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper);
        return monitorPoints;
    }
 
    @Override
    public List<Map<String, Object>> getHourlyDataByMonitorPoint(Map map) {
        int monitorPointId = Integer.parseInt(map.get("monitorPointId").toString());
        QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
        monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        monitorPointQueryWrapper.eq("id",monitorPointId);
        MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper);
        QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
        deviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        deviceQueryWrapper.eq("monitor_point_id",monitorPointId);
        List<Device> devices = deviceMapper.selectList(deviceQueryWrapper);
        if (devices.size()<=0){
            return null;
        }
        String[] sensors = map.remove("sensors").toString().split(",");
        List<String> sensorsList = Arrays.asList(sensors);
        String startTime = map.get("startTime").toString();
        String endTime = map.get("endTime").toString();
        Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
        Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
        List<Map<String,Object>> resultList = new ArrayList<>();
        for (Device device:devices) {
            List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(map.get("startTime").toString(), DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(map.get("endTime").toString(), DateUtils.yyyy_MM_dd_HH_EN));
            for (String sensor:sensorsList) {
                Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                timeValueMap.put("监测站点",monitorPoint.getName());
                timeValueMap.put("name",device.getName());
                QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
                sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                sensorQueryWrapper.eq("code",sensor);
                Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper);
                timeValueMap.put("sensor",sensorEntity.getName());
                for (HistoryHourly historyHourly : hourlies) {
                    Date time = historyHourly.getTime();
                    String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH");
                    String value = historyHourly.getValue();
                    JSONObject jsonObject = JSONObject.parseObject(value);
                    if (jsonObject.containsKey(sensor)){
                        timeValueMap.put(dateStr, jsonObject.get(sensor).toString());
                    }
                }
                //补上无数据时间
                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);
                }
                resultList.add(timeValueMap);
            }
        }
        return resultList;
    }
 
    /**
     * 监测站点数据导出
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> getHourlyDataDataV3(Map<String, Object> params) {
        List<String> macs = (List<String>) params.remove("macs");
        List<String> times = (List<String>) params.remove("times");
        String type = params.get("type").toString();
        String startTime = times.get(0);
        String endTime = times.get(1);
 
//        String[] macs = params.remove("macs").toString().split(",");
//        List<String> macsList = Arrays.asList(macs);
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",Constants.NOT_DELETE);
        queryWrapper.in("mac", macs);
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        if (devices.size()<=0){
            return null;
        }
        String[] sensors = params.remove("sensors").toString().split(",");
        List<String> sensorsList = Arrays.asList(sensors);
        List<Map<String,Object>> resultList = new ArrayList<>();
 
//        List<String> sensorsList = (List<String>) params.remove("sensors");
//        String startTime = params.get("startTime").toString();
//        String endTime = params.get("endTime").toString();
        if (type.equals("hours")){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            for (Device device:devices) {
                List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN));
                for (String sensor:sensorsList) {
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
                    monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                    monitorPointQueryWrapper.eq("id",device.getMonitorPointId());
                    MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper);
                    timeValueMap.put("监测站点",monitorPoint.getName());
                    timeValueMap.put("name",device.getName());
                    QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
                    sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                    sensorQueryWrapper.eq("code",sensor);
                    Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper);
                    timeValueMap.put("sensor",sensorEntity.getName());
                    for (HistoryHourly historyHourly : hourlies) {
                        Date time = historyHourly.getTime();
                        String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH");
                        String value = historyHourly.getValue();
                        JSONObject jsonObject = JSONObject.parseObject(value);
                        if (jsonObject.containsKey(sensor)){
                            timeValueMap.put(dateStr, jsonObject.get(sensor).toString());
                        }
                    }
                    //补上无数据时间
                    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);
                    }
                    resultList.add(timeValueMap);
                }
            }
        } else  if("day".equals(type)){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate);
            Map<String,Map<String,Object>> map = resultMap(historyDailyByMacAndTimeSlot);
            for (Device device:devices) {
                QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
                monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                monitorPointQueryWrapper.eq("id",device.getMonitorPointId());
                MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper);
                for (String sensor:sensorsList) {
                    Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd");
                    Date js = DateUtils.getDateOfDay(DateUtils.getDate(endTime,"yyyy-MM-dd"),1);
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    timeValueMap.put("监测站点",monitorPoint.getName());
                    timeValueMap.put("name",device.getName());
                    QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
                    sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                    sensorQueryWrapper.eq("code",sensor);
                    Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper);
                    timeValueMap.put("sensor",sensorEntity.getName());
                    ArrayList<Double> doubleArrayList = new ArrayList<>();
                    while (DateUtils.isTimeBefor(js,ks)){
                        String c = DateUtils.dateToDateString(ks,"yyyy-MM-dd");
                        String k = c+"_"+device.getMac();
                        if(map.containsKey(k)){
                            Object o = map.get(k).get(sensor);
                            timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0);
                        }else {
                            timeValueMap.put(c,0);
                        }
                        ks = DateUtils.getDateOfDay(ks,1);
                    }
 
                    /*for (HistoryDaily historyDaily : historyDailyByMacAndTimeSlot) {
                        Date time = historyDaily.getTime();
                        String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd");
                        String value = historyDaily.getValue();
                        JSONObject jsonObject = JSONObject.parseObject(value);
                        doubleArrayList.add(Objects.nonNull(jsonObject)&&Objects.nonNull(jsonObject.get(sensor))?Double.parseDouble(jsonObject.get(sensor).toString()):0);
                        if (jsonObject.containsKey(sensor)){
                            timeValueMap.put(dateStr, jsonObject.get(sensor).toString());
                        }
                    }*/
                    Double ListAvg = doubleArrayList.stream().collect(Collectors.averagingDouble(Double::doubleValue));
                    double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                    timeValueMap.put("累计值",rsAvg);
                    resultList.add(timeValueMap);
                }
            }
        }else {
            for (Device device:devices) {
                QueryWrapper<HistoryMonthly> HistoryMonthlyWrapper = new QueryWrapper<>();
                HistoryMonthlyWrapper.eq("mac",device.getMac());
                HistoryMonthlyWrapper.between("time",startTime,endTime);
                List<HistoryMonthly> historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper);
                for (String sensor:sensorsList) {
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
                    monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                    monitorPointQueryWrapper.eq("id",device.getMonitorPointId());
                    MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper);
                    timeValueMap.put("监测站点",monitorPoint.getName());
                    timeValueMap.put("name",device.getName());
                    QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
                    sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                    sensorQueryWrapper.eq("code",sensor);
                    Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper);
                    timeValueMap.put("sensor",sensorEntity.getName());
                    for (HistoryMonthly historyMonthly : historyMonthlyList) {
                        Date time = historyMonthly.getTime();
                        String dateStr = DateUtils.dateToDateString(time, "yyyy-MM");
                        String value = historyMonthly.getValue();
                        JSONObject jsonObject = JSONObject.parseObject(value);
                        if (jsonObject.containsKey(sensor)){
                            timeValueMap.put(dateStr, jsonObject.get(sensor).toString());
                        }
                    }
                    resultList.add(timeValueMap);
                }
            }
        }
        return resultList;
    }
 
 
    private Map<String,Map<String,Object>> resultMap(List<HistoryDaily> list ){
        Map<String,Map<String,Object>> map = new HashMap<>();
        for(HistoryDaily h : list){
            String dateStr = DateUtils.dateToDateString( h.getTime(), "yyyy-MM-dd")+"_"+h.getMac();
            Map<String,Object> jsonMap = new HashMap<>();
            JSONObject jsonObject = JSONObject.parseObject(h.getValue());
            jsonMap = jsonObject.getInnerMap();
            map.put(dateStr,jsonMap);
        }
        return map;
    }
}