南昌市物联网技防平台-后台
zengh
2021-11-04 57c72c8438ccb461d5d2f751f710d6908f0e0d32
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.jfpt.equipment.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.jfpt.alarm.service.IAlarmService;
import org.springblade.jfpt.equipment.entity.Equipment;
import org.springblade.jfpt.equipment.entity.EquipmentPlayInfo;
import org.springblade.jfpt.equipment.feign.OneNetStudioApi;
import org.springblade.jfpt.equipment.mapper.EquipmentMapper;
import org.springblade.jfpt.equipment.service.IEquipmentService;
import org.springblade.jfpt.equipment.vo.EquipmentVO;
import org.springblade.jfpt.equipment.vo.EquipmentVOS;
import org.springblade.jfpt.parcel.vo.ConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-07-01
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements IEquipmentService {
    private static final String PARENT_ID = "parentId";
 
    //时间差
    private long TIME_DIFFERENCE = 150*1000;
 
    @Resource
    private OneNetStudioApi oneNetStudioApi;
 
    @Autowired
    private IAlarmService iAlarmService;
 
    @Override
    public IPage<EquipmentVO> selectEquipmentPage(IPage<EquipmentVO> page, EquipmentVO equipment,List childList) {
        return page.setRecords(baseMapper.selectEquipmentPage(page,equipment,childList));
    }
 
    @Override
    public IPage<EquipmentVO> selectPage(IPage<EquipmentVO> page, EquipmentVO equipment,String pid) {
 
        return page.setRecords(baseMapper.selectEquipmentPage(page,equipment,null));
    }
 
 
    @Override
    public IPage<EquipmentVO> selectDeptPages(IPage<EquipmentVO> page, EquipmentVO equipment,String pid,String dxdype) {
 
//        List deptIdList;
//        if (pid != null && !pid.equals("")){
//            deptIdList = SysCache.getDeptChildIds(Long.valueOf(pid));
//        }else{
//            deptIdList = SysCache.getDeptChildIds(null);
//        }
        return page.setRecords(baseMapper.selectDeptPages(page,equipment,pid,dxdype));
    }
 
 
 
    @Override
    public List<EquipmentVOS> tree() {
        return ForestNodeMerger.merge(baseMapper.tree());
    }
 
    @Override
    public List<EquipmentVO> listAll(EquipmentVO equipment) {
        return baseMapper.listAll(equipment);
    }
 
    @Override
    public void updateOne(Equipment equipment) {
        baseMapper.updateOne(equipment);
    }
 
    @Override
    public void saveImg(EquipmentVO equipment) {
        baseMapper.saveImg(equipment);
    }
 
    @Override
    public void updateRW(EquipmentVO equipment) {
        baseMapper.updateRW(equipment);
    }
 
    @Override
    public void s(Equipment equipment) {
        baseMapper.s(equipment);
    }
 
    @Override
    public void updataType(String arr) {
        baseMapper.updataType(arr);
    }
 
    @Override
    public void updataTypeC(String arr) {
        baseMapper.updataTypeC(arr);
    }
 
    @Override
    public List<EquipmentVOS> lazyList(Long parentId, Map<String, Object> param) {
        // 判断点击搜索但是没有查询条件的情况
        if (Func.isEmpty(param.get(PARENT_ID)) && param.size() == 1) {
            parentId = 0L;
        }
        // 判断点击搜索带有查询条件的情况
        if (Func.isEmpty(param.get(PARENT_ID)) && param.size() > 1 && Func.toLong(parentId) == 0L) {
            parentId = null;
        }
        return baseMapper.lazyList(parentId, param);
    }
 
    @Override
    public EquipmentVOS selectInfo(Equipment equipment) {
        return baseMapper.selectInfo(equipment);
    }
 
    @Override
    public List<Map<String, Object>>  selectInfos(String deviceNumber) {
        return baseMapper.selectInfos(deviceNumber);
    }
 
    @Override
    public List<EquipmentVOS> selectList(String deviceType) {
        return baseMapper.selectList(deviceType);
    }
 
    @Override
    public void updateChannel(String heartbeat, String deviceNumber) {
        baseMapper.updateChannel(heartbeat, deviceNumber);
    }
 
    /**
     * 查询在线设备总数
     * @param conditionVo status 状态码   0:本日   1:本周   2:本月
     * @return
     */
    @Override
    public Integer selectEquipmentCount(ConditionVo conditionVo) {
        //更新设备状态
        updateEquipmentDtype();
        //查询在线设备数量
        Integer equipmentCount = baseMapper.selectOnlineEqNum();
        //返回数据
        return equipmentCount;
    }
 
    @Override
    public void updateexpireTime(String expireTime, String deviceNumber) {
        baseMapper.updateexpireTime(expireTime,deviceNumber);
    }
 
    /**
     * 修改设备状态  2021-5-6 arsn修改
     * @param state 设备状态
     * @param reason 原因
     * @param deviceNumber 设备编号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateExstate(String state, String reason, String deviceNumber) {
        if(state.equals("系统故障")){
            String dType = "3";
            baseMapper.updateExstate(dType, reason, deviceNumber);
        }
        if(state.equals("一键报警")){
            String dType = "2";
            baseMapper.updateExstate(dType, reason, deviceNumber);
        }
        if(state.equals("处理完成")){
            //查询当前设备是否还存在其他报警
            int eqCount = iAlarmService.selectEqCount(deviceNumber);
            //只有一条记录时才去修改设备的状态
            if (eqCount==1 || eqCount==0) {
                String dType = "1";
                baseMapper.updateExstate(dType, reason, deviceNumber);
            }
        }
    }
 
    @Override
    public Integer selectEqCz() {
        return baseMapper.selectEqCz();
    }
 
    @Override
    public Integer selectEqCd() {
        return baseMapper.selectEqCd();
    }
 
    @Override
    public List<Map<String, Object>> selectEqCy(String time) {
        return baseMapper.selectEqCy(time);
    }
 
    @Override
    public Integer selectEqCg() {
        return baseMapper.selectEqCg();
    }
 
    /**
     * 获取各个设备状态统计数量
     * @return
     */
    @Override
    public Map<String, Object> selEqTypeSum() {
        //设备在线数量
        Integer integerz = baseMapper.selectEqCz();
        //设备掉线数量
        Integer integerd = baseMapper.selectEqCd();
        //设备预警数量
        Integer alarmSum = baseMapper.selectEqCys();
        //设备故障数量
        Integer integerg = baseMapper.selectEqCg();
        //封装数据
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("zx", integerz);
        map.put("dx", integerd);
        map.put("yj", alarmSum);
        map.put("gz", integerg);
        //返回数据
        return map;
    }
 
 
    /**
     * 更新设备状态
     */
    @Override
    public void updateEquipmentDtype() {
        //查询所有的设备,不包含故障和警情状态
        List<Equipment> equipment = baseMapper.selAllEq();
        long time = System.currentTimeMillis();
        long hearbeat = 0;
        List<Long> equipmentListz = new ArrayList<>();
        List<Long> equipmentListl = new ArrayList<>();
        for (Equipment e:equipment) {
            try {
                if(null!=e.getHeartbeat()) {
                    hearbeat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(e.getHeartbeat()).getTime();
                }
            } catch (ParseException parseException) {
                parseException.printStackTrace();
            }
            //对比心跳时间,在线
            if (time-hearbeat< TIME_DIFFERENCE){
                if (null==e.getDtype() || !e.getDtype().equals("1")){
                    //需要修改为上线
                    equipmentListz.add(e.getId());
                }
            }
            //不在线
            if (time-hearbeat>=TIME_DIFFERENCE){
                if (null==e.getDtype() || !e.getDtype().equals("0")){
                    //需要修改为下线
                    equipmentListl.add(e.getId());
                }
            }
        }
        //批量更新数据
        if (equipmentListz.size()>0){
            baseMapper.updateEqListz(equipmentListz);
            // 在 onenet 平台上线
            oneNetStudioApi.equipmentUp(equipmentListz);
        }
        if (equipmentListl.size()>0){
            baseMapper.updateEqListl(equipmentListl);
            //去 onenet 平台下线
            oneNetStudioApi.equipmentDown(equipmentListl);
        }
    }
 
    /**
     * 获取设备视频播放的信息
     * @param equipmentPlayInfo 视频播放地址对象,只需包含设备编号,以及视频播放格式即可
     * @return
     */
    @Override
    public EquipmentPlayInfo selectEquimentPlayInfo(EquipmentPlayInfo equipmentPlayInfo) {
        return baseMapper.selectEquimentPlayInfo(equipmentPlayInfo);
    }
 
    /**
     * 新增或修改设备视频播放的信息
     * @param equipmentPlayInfo 视频播放地址对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateEquipmentPlayInfo(EquipmentPlayInfo equipmentPlayInfo) {
        equipmentPlayInfo.setCreateTime(new Date());
        //新增或修改
        int result = null==equipmentPlayInfo.getId()? baseMapper.saveEquipmentPlayInfo(equipmentPlayInfo)
                                                                      :baseMapper.updateEquipmentPlayInfo(equipmentPlayInfo);
        //返回
        return result>0? true:false;
    }
}