lizijie
2021-08-17 cf311a43c3a6b941738de9ca5735e1acca451fc2
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.*;
import com.moral.api.mapper.*;
import com.moral.api.pojo.vo.device.DeviceVO;
import com.moral.api.service.SpecialDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.CacheUtils;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-11
 */
@Service
public class SpecialDeviceServiceImpl extends ServiceImpl<SpecialDeviceMapper, SpecialDevice> implements SpecialDeviceService {
 
    @Autowired(required = false)
    private SpecialDeviceMapper specialDeviceMapper;
 
    @Autowired(required = false)
    private ManageAccountMapper manageAccountMapper;
 
    @Autowired(required = false)
    private VersionMapper versionMapper;
 
    @Autowired(required = false)
    private SysDictDataMapper sysDictDataMapper;
 
    @Autowired(required = false)
    private SpecialDeviceHistoryMapper specialDeviceHistoryMapper;
 
    @Autowired(required = false)
    private OrganizationMapper organizationMapper;
 
    @Autowired(required = false)
    private RedisTemplate redisTemplate;
 
    @Autowired(required = false)
    private OrganizationUnitAlarmMapper organizationUnitAlarmMapper;
 
    @Autowired(required = false)
    private VersionSensorUnitMapper versionSensorUnitMapper;
 
    @Autowired
    private LogUtils logUtils;
 
    /*
     * 从redis获取设备信息
     * */
    private Map<String, Object> getDeviceInfoFromRedis(String mac) {
        return (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac);
    }
 
    /*
     * 设备信息存入redis
     */
    private void setDeviceInfoToRedis(String mac, Map<String, Object> deviceInfo) {
        redisTemplate.opsForHash().put(RedisConstants.DEVICE, mac, deviceInfo);
    }
 
    /*
     * 从redis删除设备信息
     */
    private void delDeviceInfoFromRedis(String mac) {
        redisTemplate.opsForHash().delete(RedisConstants.DEVICE, mac);
    }
 
    @Override
    public Map<String, Object> getDataByCondition(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (!map.containsKey("current")||!map.containsKey("size")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        Page<SpecialDevice> page = new Page<>(current,size);
        QueryWrapper<SpecialDevice> wrapper_Condition = new QueryWrapper<>();
        wrapper_Condition.eq("is_delete",Constants.NOT_DELETE);
        if (!ObjectUtils.isEmpty(map.get("organization_id"))){
            wrapper_Condition.eq("organization_id",map.get("organization_id").toString());
        }
        if (!ObjectUtils.isEmpty(map.get("keyword"))){
            wrapper_Condition.and(wc -> wc.like("name",map.get("keyword").toString()).or().like("mac",map.get("keyword").toString()));
            //wrapper_Condition.like("name",map.get("keyword").toString()).or().like("mac",map.get("keyword").toString());
        }
        wrapper_Condition.orderByDesc("create_time");
        Page resultPage = specialDeviceMapper.selectPage(page,wrapper_Condition);
        int totalNumber = specialDeviceMapper.selectCount(wrapper_Condition);
        List<SpecialDevice> specialDevices = resultPage.getRecords();
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> specialDeviceList = new ArrayList<>();
        for (SpecialDevice specialDevice:specialDevices) {
            Map specialDeviceMap = JSON.parseObject(JSON.toJSONString(specialDevice),Map.class);
            String createTime = SDF.format(specialDevice.getCreateTime());
            String updateTime = SDF.format(specialDevice.getUpdateTime());
            specialDeviceMap.put("createTime",createTime);
            specialDeviceMap.put("updateTime",updateTime);
            List<Map<String,Object>> operateList = new ArrayList<>();
            if (!ObjectUtils.isEmpty(specialDevice.getOperateIds()) && specialDevice.getOperateIds()!=null){
                String operateIds = specialDevice.getOperateIds();
                String[] operateIdArr = operateIds.split(",");
                if (operateIdArr.length>0){
                    List<Integer> operateIdList = new ArrayList<>();
                    for (int i = 0; i < operateIdArr.length; i++){
                        operateIdList.add(Integer.parseInt(operateIdArr[i]));
                    }
                    QueryWrapper<ManageAccount> wapper_manageAccount = new QueryWrapper<>();
                    wapper_manageAccount.eq("is_delete",Constants.NOT_DELETE);
                    wapper_manageAccount.in("id",operateIdList);
                    List<ManageAccount> manageAccounts = manageAccountMapper.selectList(wapper_manageAccount);
                    for (ManageAccount manageAccount:manageAccounts) {
                        Map<String,Object> operateMap = new HashMap<>();
                        operateMap.put("id",manageAccount.getId());
                        operateMap.put("name",manageAccount.getUserName());
                        operateList.add(operateMap);
                    }
                }
            }
            specialDeviceMap.put("operates",operateList);
            Map<String,Object> deviceVersionMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDevice.getDeviceVersionId()) && specialDevice.getDeviceVersionId()!=null && !"".equals(specialDevice.getDeviceVersionId())){
                int versionId = Integer.parseInt(specialDevice.getDeviceVersionId().toString());
                QueryWrapper<Version> wapper_version = new QueryWrapper<>();
                wapper_version.eq("is_delete",Constants.NOT_DELETE);
                wapper_version.eq("id",versionId);
                Version version = versionMapper.selectOne(wapper_version);
                if (!ObjectUtils.isEmpty(version)){
                    deviceVersionMap.put("id",version.getId());
                    deviceVersionMap.put("name",version.getName());
                }
            }
            specialDeviceMap.put("version",deviceVersionMap);
            Map<String,Object> specialTypeMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDevice.getSpecialType()) && specialDevice.getSpecialType()!=null && !"".equals(specialDevice.getSpecialType())){
                int specialTypeId = Integer.parseInt(specialDevice.getSpecialType().toString());
                QueryWrapper<SysDictData> wapper_sysDictData = new QueryWrapper<>();
                wapper_sysDictData.eq("is_delete",Constants.NOT_DELETE);
                wapper_sysDictData.eq("dict_type_id",27);
                wapper_sysDictData.eq("dataKey",specialTypeId);
                SysDictData sysDictData = sysDictDataMapper.selectOne(wapper_sysDictData);
                if (!ObjectUtils.isEmpty(sysDictData)){
                    specialTypeMap.put("id",sysDictData.getId());
                    specialTypeMap.put("dataKey",sysDictData.getDataKey());
                    specialTypeMap.put("name",sysDictData.getDataValue());
                }
            }
            specialDeviceMap.put("specialType",specialTypeMap);
            Map<String,Object> organizationMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDevice.getOrganizationId()) && specialDevice.getOrganizationId()!=null && !"".equals(specialDevice.getOrganizationId())){
                int organizationId = Integer.parseInt(specialDevice.getOrganizationId().toString());
                QueryWrapper<Organization> wapper_organization = new QueryWrapper<>();
                wapper_organization.eq("is_delete",Constants.NOT_DELETE);
                wapper_organization.eq("id",organizationId);
                Organization organization = organizationMapper.selectOne(wapper_organization);
                if (!ObjectUtils.isEmpty(organization)){
                    organizationMap.put("id",organization.getId());
                    organizationMap.put("name",organization.getName());
                }
            }
            specialDeviceMap.put("organization",organizationMap);
            List<Map<String,Object>> organizationList = new ArrayList<>();
            QueryWrapper<SpecialDeviceHistory> wapper_specialDeviceHistory = new QueryWrapper<>();
            wapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
            wapper_specialDeviceHistory.eq("mac",specialDevice.getMac());
            List<SpecialDeviceHistory> specialDeviceHistories = specialDeviceHistoryMapper.selectList(wapper_specialDeviceHistory);
            if (!ObjectUtils.isEmpty(specialDeviceHistories)){
                List organizationIdList = new ArrayList();
                for (SpecialDeviceHistory specialDeviceHistory:specialDeviceHistories) {
                    organizationIdList.add(specialDeviceHistory.getOrganizationId());
                }
                QueryWrapper<Organization> wapper_organizationHistory = new QueryWrapper<>();
                wapper_organizationHistory.eq("is_delete",Constants.NOT_DELETE);
                wapper_organizationHistory.in("id",organizationIdList);
                List<Organization> organizations = organizationMapper.selectList(wapper_organizationHistory);
                for (Organization organization:organizations) {
                    Map<String,Object> organizationHistoryMap = new HashMap<>();
                    organizationHistoryMap.put("id",organization.getId());
                    organizationHistoryMap.put("name",organization.getName());
                    organizationList.add(organizationHistoryMap);
                }
            }
            specialDeviceMap.put("organizations",organizationList);
            specialDeviceList.add(specialDeviceMap);
        }
        resultMap.put("specialDevices",specialDeviceList);
        resultMap.put("totalNumber",totalNumber);
        resultMap.put("current",current);
        int totalPageNumber = totalNumber/size;
        if(totalNumber%size != 0){
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber",totalPageNumber);
        return resultMap;
    }
 
    @Transactional
    @Override
    public Map<String, Object> insert(SpecialDevice specialDevice) {
        Map<String,Object> resultMap = new HashMap<>();
        String name = specialDevice.getName();
        String mac = specialDevice.getMac();
        int organizationId = specialDevice.getOrganizationId();
        int deviceVersionId = specialDevice.getDeviceVersionId();
        String operateIds = specialDevice.getOperateIds();
        String specialType = specialDevice.getSpecialType();
        if (ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(mac) || ObjectUtils.isEmpty(specialType) || ObjectUtils.isEmpty(organizationId) || ObjectUtils.isEmpty(deviceVersionId)){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (name.equals("") || mac.equals("") || specialType.equals("")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_NOT_REQUIREMENT.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_NOT_REQUIREMENT.getMsg());
            return resultMap;
        }
        QueryWrapper<SpecialDevice> wrapper_mac = new QueryWrapper<>();
        wrapper_mac.eq("is_delete",Constants.NOT_DELETE);
        wrapper_mac.eq("mac",mac);
        int macNum = specialDeviceMapper.selectCount(wrapper_mac);
        if (macNum>0){
            resultMap.put("code",ResponseCodeEnum.MAC_IS_EXIST.getCode());
            resultMap.put("msg",ResponseCodeEnum.MAC_IS_EXIST.getMsg());
            return resultMap;
        }
        specialDeviceMapper.insert(specialDevice);
        QueryWrapper<SpecialDeviceHistory> wrapper_specialDeviceHistory = new QueryWrapper<>();
        wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
        wrapper_specialDeviceHistory.eq("mac",mac);
        wrapper_specialDeviceHistory.eq("organization_id",organizationId);
        SpecialDeviceHistory specialDeviceHistory = specialDeviceHistoryMapper.selectOne(wrapper_specialDeviceHistory);
        SpecialDeviceHistory insertSpecialDeviceHistory = new SpecialDeviceHistory();
        BeanUtils.copyProperties(specialDevice,insertSpecialDeviceHistory);
        if (ObjectUtils.isEmpty(specialDeviceHistory)){
            specialDeviceHistoryMapper.insert(insertSpecialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("添加了特殊设备历史数据:").append(insertSpecialDeviceHistory.getName()).append(":").append("mac:").append(insertSpecialDeviceHistory.getMac());
            logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
        }else {
            /*UpdateWrapper<SpecialDeviceHistory> wrapper_insertSpecialDeviceHistory = new UpdateWrapper<>();
            wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
            wrapper_specialDeviceHistory.eq("mac",mac);
            wrapper_specialDeviceHistory.eq("organization_id",organizationId);*/
            specialDeviceHistoryMapper.update(insertSpecialDeviceHistory,wrapper_specialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("修改了特殊设备:").append(specialDeviceHistory.getId()).append(":");
            Field[] fields = SpecialDevice.class.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().equals("id")) {
                    continue;
                }
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                String fieldName = field.getName();
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(fieldName, SpecialDeviceHistory.class);
                    Method method = pd.getReadMethod();
                    Object o1 = method.invoke(specialDeviceHistory);
                    Object o2 = method.invoke(insertSpecialDeviceHistory);
                    if (o2 != null) {
                        content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(":");
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
            }
            logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
        }
        Map<String, Object> deviceInfo = selectDeviceInfoById(specialDevice.getId());
        //维护组织型号关系表
        insertOrganizationUnitAlarm(specialDevice.getOrganizationId(), specialDevice.getDeviceVersionId());
        //新增设备信息存入redis
        String mac1 = specialDevice.getMac();
        //从redis中删除设备信息
        delDeviceInfoFromRedis(mac1);
        //设备信息存入redis
        setDeviceInfoToRedis(mac1, deviceInfo);
        //刷新deviceInfo缓存
        CacheUtils.refreshSpecialDeviceAlarmInfo();
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("添加了特殊设备:").append(specialDevice.getName()).append(":").append("mac:").append(mac);
        logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
        resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    @Transactional
    @Override
    public void update(SpecialDevice specialDevice) {
        Integer specialDeviceId = specialDevice.getId();
        SpecialDevice oldSpecialDevice = specialDeviceMapper.selectById(specialDeviceId);
        specialDeviceMapper.updateById(specialDevice);
        SpecialDevice updateSpecialDevice = specialDeviceMapper.selectById(specialDeviceId);
        String oleMac = oldSpecialDevice.getMac();
        //维护组织型号关系表
        Integer oldOrgId = oldSpecialDevice.getOrganizationId();
        Integer newOrgId = updateSpecialDevice.getOrganizationId();
        Integer oldVersionId = oldSpecialDevice.getDeviceVersionId();
        Integer newVersionId = updateSpecialDevice.getDeviceVersionId();
        if (!oldOrgId.equals(newOrgId) || !oldVersionId.equals(newVersionId)) {
            deleteOrganizationUnitAlarm(oldOrgId, oldVersionId);
            insertOrganizationUnitAlarm(newOrgId, newVersionId);
        }
        //从redis中删除设备信息
        delDeviceInfoFromRedis(oleMac);
        Map<String, Object> deviceInfo = selectDeviceInfoById(specialDeviceId);
        //设备信息存入redis
        setDeviceInfoToRedis(updateSpecialDevice.getMac(), deviceInfo);
        //刷新deviceInfo缓存
        CacheUtils.refreshSpecialDeviceAlarmInfo();
        QueryWrapper<SpecialDeviceHistory> wrapper_specialDeviceHistory = new QueryWrapper<>();
        wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
        wrapper_specialDeviceHistory.eq("mac",updateSpecialDevice.getMac());
        wrapper_specialDeviceHistory.eq("organization_id",updateSpecialDevice.getOrganizationId());
        SpecialDeviceHistory specialDeviceHistory = specialDeviceHistoryMapper.selectOne(wrapper_specialDeviceHistory);
        SpecialDeviceHistory updateSpecialDeviceHistory = new SpecialDeviceHistory();
        BeanUtils.copyProperties(updateSpecialDevice,updateSpecialDeviceHistory);
        updateSpecialDeviceHistory.setCreateTime(null);
        updateSpecialDeviceHistory.setUpdateTime(null);
        updateSpecialDeviceHistory.setIsDelete(null);
        if (ObjectUtils.isEmpty(specialDeviceHistory)){
            specialDeviceHistoryMapper.insert(updateSpecialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("添加了特殊设备历史数据:").append(updateSpecialDeviceHistory.getName()).append(":").append("mac:").append(updateSpecialDeviceHistory.getMac());
            logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
        }else {
            /*UpdateWrapper<SpecialDeviceHistory> wrapper_insertSpecialDeviceHistory = new UpdateWrapper<>();
            wrapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
            wrapper_specialDeviceHistory.eq("mac",mac);
            wrapper_specialDeviceHistory.eq("organization_id",organizationId);*/
            specialDeviceHistoryMapper.update(updateSpecialDeviceHistory,wrapper_specialDeviceHistory);
            //操作日志记录
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            StringBuilder content = new StringBuilder();
            content.append("修改了特殊设备:").append(specialDeviceHistory.getId()).append(":");
            Field[] fields = SpecialDevice.class.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().equals("id")) {
                    continue;
                }
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                String fieldName = field.getName();
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(fieldName, SpecialDeviceHistory.class);
                    Method method = pd.getReadMethod();
                    Object o1 = method.invoke(specialDeviceHistory);
                    Object o2 = method.invoke(updateSpecialDeviceHistory);
                    if (o2 != null) {
                        content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(":");
                    }
 
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
            }
            logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
        }
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("修改了特殊设备:").append(specialDeviceId).append(":");
        Field[] fields = SpecialDevice.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals("id")) {
                continue;
            }
            if ("serialVersionUID".equals(field.getName())) {
                continue;
            }
            String fieldName = field.getName();
            PropertyDescriptor pd = null;
            try {
                pd = new PropertyDescriptor(fieldName, SpecialDevice.class);
                Method method = pd.getReadMethod();
                Object o1 = method.invoke(oldSpecialDevice);
                Object o2 = method.invoke(specialDevice);
                if (o2 != null) {
                    content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(":");
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
        logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
    }
 
    @Override
    public void delete(Integer specialDeviceId) {
        SpecialDevice specialDevice = specialDeviceMapper.selectById(specialDeviceId);
        UpdateWrapper<SpecialDevice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",specialDeviceId).set("is_delete",Constants.DELETE);
        specialDeviceMapper.update(null,updateWrapper);
        String mac = specialDevice.getMac();
        //从redis中删除设备信息
        delDeviceInfoFromRedis(mac);
        //维护组织型号关系表
        Integer versionId = specialDevice.getDeviceVersionId();
        Integer orgId = specialDevice.getOrganizationId();
        deleteOrganizationUnitAlarm(orgId, versionId);
        //刷新deviceInfo缓存
        CacheUtils.refreshDeviceAlarmInfo();
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("删除了设备:").append(specialDevice.getName()).append(";").append("mac:").append(mac);
        logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
    }
 
    @Override
    public Map<String, Object> selectDeviceInfoById(Integer deviceId) {
        String mac = specialDeviceMapper.selectById(deviceId).getMac();
        Map<String, Object> specialDeviceInfo = getDeviceInfoFromRedis(mac);
        //先从redis中取
        if (specialDeviceInfo != null) {
            return specialDeviceInfo;
        }
        specialDeviceInfo = new LinkedHashMap<>();
        //DeviceVO device = deviceMapper.selectDeviceInfoById(deviceId);
        SpecialDevice specialDevice = specialDeviceMapper.selectById(deviceId);
        //设备
        specialDeviceInfo.put("id", specialDevice.getId());
        specialDeviceInfo.put("name", specialDevice.getName());
        specialDeviceInfo.put("mac", specialDevice.getMac());
        specialDeviceInfo.put("createTime", DateUtils.dateToDateString(specialDevice.getCreateTime()));
 
        //扩展字段
        //specialDeviceInfo.put("extend", device.getExtend());
 
        //型号
        Map<String, Object> versionInfo = new LinkedHashMap<>();
        Version version = versionMapper.selectById(specialDevice.getDeviceVersionId());
        versionInfo.put("id", version.getId());
        versionInfo.put("name", version.getName());
        specialDeviceInfo.put("version", versionInfo);
 
        //维护人
        List<Map<String, Object>> operatorsInfo = new ArrayList<>();
        String operateIds = specialDevice.getOperateIds();
        if (!ObjectUtils.isEmpty(operateIds)){
            String[] operateIdArr = operateIds.split(",");
            if (operateIdArr.length>0){
                List<Integer> operateIdList = new ArrayList<>();
                for (int i = 0; i < operateIdArr.length; i++){
                    operateIdList.add(Integer.parseInt(operateIdArr[i]));
                }
                QueryWrapper<ManageAccount> wapper_manageAccount = new QueryWrapper<>();
                wapper_manageAccount.eq("is_delete",Constants.NOT_DELETE);
                wapper_manageAccount.in("id",operateIdList);
                List<ManageAccount> manageAccounts = manageAccountMapper.selectList(wapper_manageAccount);
                for (ManageAccount manageAccount:manageAccounts) {
                    Map<String,Object> operateMap = new HashMap<>();
                    operateMap.put("id",manageAccount.getId());
                    operateMap.put("name",manageAccount.getUserName());
                    operatorsInfo.add(operateMap);
                }
            }
        }
        specialDeviceInfo.put("operators", operatorsInfo);
 
        //组织
        Map<String, Object> orgInfo = new LinkedHashMap<>();
        Organization organization = organizationMapper.selectById(specialDevice.getOrganizationId());
        orgInfo.put("id", organization.getId());
        orgInfo.put("name", organization.getName());
        specialDeviceInfo.put("organization", orgInfo);
 
        setDeviceInfoToRedis(mac, specialDeviceInfo);
        return specialDeviceInfo;
    }
 
    private void insertOrganizationUnitAlarm(Integer orgId, Integer versionId) {
        QueryWrapper<OrganizationUnitAlarm> queryOrganizationVersionWrapper = new QueryWrapper<>();
        queryOrganizationVersionWrapper.eq("organization_id", orgId);
        queryOrganizationVersionWrapper.eq("version_id", versionId);
        queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrganizationVersionWrapper);
        if (org.springframework.util.ObjectUtils.isEmpty(organizationUnitAlarms)) {
            QueryWrapper<VersionSensorUnit> queryVersionSensorUnitWrapper = new QueryWrapper<>();
            queryVersionSensorUnitWrapper.eq("version_id", versionId);
            queryVersionSensorUnitWrapper.eq("is_delete", Constants.NOT_DELETE);
            List<VersionSensorUnit> versionSensorUnits = versionSensorUnitMapper.selectList(queryVersionSensorUnitWrapper);
            if (!org.springframework.util.ObjectUtils.isEmpty(versionSensorUnits)) {
                for (VersionSensorUnit versionSensorUnit : versionSensorUnits) {
                    OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm();
                    organizationUnitAlarm.setOrganizationId(orgId);
                    organizationUnitAlarm.setVersionId(versionId);
                    organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode());
                    organizationUnitAlarm.setUnitKey(versionSensorUnit.getUnitKey());
                    organizationUnitAlarm.setShowUnitKey(versionSensorUnit.getUnitKey());
                    organizationUnitAlarmMapper.insert(organizationUnitAlarm);
                }
            }
        }
    }
 
    private void deleteOrganizationUnitAlarm(Integer orgId, Integer versionId) {
        QueryWrapper<SpecialDevice> queryOrganizationVersionWrapper = new QueryWrapper<>();
        queryOrganizationVersionWrapper.eq("organization_id", orgId);
        queryOrganizationVersionWrapper.eq("device_version_id", versionId);
        queryOrganizationVersionWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<SpecialDevice> specialDevices = specialDeviceMapper.selectList(queryOrganizationVersionWrapper);
        if (org.springframework.util.ObjectUtils.isEmpty(specialDevices)) {//如果为空,则组织没有该型号的设备了。
            UpdateWrapper deleteWrapper = new UpdateWrapper();
            deleteWrapper.eq("organization_id", orgId);
            deleteWrapper.eq("version_id", versionId);
            deleteWrapper.eq("is_delete", Constants.NOT_DELETE);
            deleteWrapper.set("is_delete", Constants.DELETE);
            organizationUnitAlarmMapper.update(null, deleteWrapper);
        }
    }
}