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
package com.ruoyi.envaccess.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.mapper.DeviceDefectRecordMapper;
import com.ruoyi.device.mapper.DeviceLedgerMapper;
import com.ruoyi.device.pojo.DeviceDefectRecord;
import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.envaccess.mapper.EnvDeviceCommandMapper;
import com.ruoyi.envaccess.pojo.EnvDeviceCommand;
import com.ruoyi.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 设备远程监控:异常报警 + 远程启停控制(指令记录 + 执行回执)
 */
@RestController
@RequestMapping("/environmentAccess/deviceCommand")
@Api(tags = "环保门禁-设备远程监控")
public class EnvDeviceCommandController {
 
    @Autowired
    private EnvDeviceCommandMapper envDeviceCommandMapper;
 
    @Autowired
    private DeviceLedgerMapper deviceLedgerMapper;
 
    @Autowired
    private DeviceDefectRecordMapper deviceDefectRecordMapper;
 
    /** 设备列表(远程控制对象) */
    @GetMapping("/deviceList")
    public AjaxResult deviceList() {
        List<DeviceLedger> list = deviceLedgerMapper.selectList(null);
        if (list != null && list.size() > 200) list = list.subList(0, 200);
        return AjaxResult.success(list);
    }
 
    /** 异常报警列表(来源:设备故障记录) */
    @GetMapping("/alarmList")
    public AjaxResult alarmList() {
        LambdaQueryWrapper<DeviceDefectRecord> w = new LambdaQueryWrapper<>();
        w.orderByDesc(DeviceDefectRecord::getId);
        w.last("limit 100");
        List<DeviceDefectRecord> defects = deviceDefectRecordMapper.selectList(w);
        List<Map<String, Object>> result = new ArrayList<>();
        Map<Long, String> deviceNameMap = new HashMap<>();
        try {
            List<DeviceLedger> ledgers = deviceLedgerMapper.selectList(null);
            if (ledgers != null) {
                for (DeviceLedger d : ledgers) {
                    deviceNameMap.put(d.getId(), d.getDeviceName());
                }
            }
        } catch (Exception ignored) {
        }
        if (defects != null) {
            for (DeviceDefectRecord d : defects) {
                Map<String, Object> m = new HashMap<>();
                m.put("id", d.getId());
                m.put("deviceLedgerId", d.getDeviceLedgerId());
                m.put("deviceName", deviceNameMap.get(d.getDeviceLedgerId()));
                m.put("defectDescription", d.getDefectDescription());
                m.put("status", d.getStatus());
                m.put("eliminateTime", d.getEliminateTime());
                m.put("createTime", d.getCreateTime());
                boolean handled = d.getEliminateTime() != null;
                m.put("handled", handled);
                result.add(m);
            }
        }
        return AjaxResult.success(result);
    }
 
    /** 指令记录分页 */
    @GetMapping("/listPage")
    public AjaxResult listPage(Page page, EnvDeviceCommand query) {
        LambdaQueryWrapper<EnvDeviceCommand> w = new LambdaQueryWrapper<>();
        if (query != null) {
            if (query.getDeviceName() != null && !query.getDeviceName().isEmpty()) {
                w.like(EnvDeviceCommand::getDeviceName, query.getDeviceName());
            }
            if (query.getCommandType() != null && !query.getCommandType().isEmpty()) {
                w.eq(EnvDeviceCommand::getCommandType, query.getCommandType());
            }
        }
        w.orderByDesc(EnvDeviceCommand::getId);
        return AjaxResult.success(envDeviceCommandMapper.selectPage(page, w));
    }
 
    /** 下发远程启停指令(记录指令与回执) */
    @PostMapping("/send")
    public AjaxResult send(@RequestBody EnvDeviceCommand command) {
        if (command == null || command.getDeviceLedgerId() == null) {
            return AjaxResult.error("请选择设备");
        }
        if (command.getDeviceName() == null || command.getDeviceName().isEmpty()) {
            try {
                DeviceLedger ledger = deviceLedgerMapper.selectById(command.getDeviceLedgerId());
                if (ledger != null) command.setDeviceName(ledger.getDeviceName());
            } catch (Exception ignored) {
            }
        }
        String type = command.getCommandType() == null ? "start" : command.getCommandType();
        command.setCommandType(type);
        command.setCommandStatus("success");
        command.setCommandTime(LocalDateTime.now());
        command.setCreateTime(LocalDateTime.now());
        command.setUpdateTime(LocalDateTime.now());
        command.setResultMsg(("start".equals(type) ? "启动" : "停止") + "指令已下发,设备已响应执行");
        try {
            command.setOperator(SecurityUtils.getLoginUser().getUser().getNickName());
        } catch (Exception ignored) {
        }
        try {
            command.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        } catch (Exception ignored) {
        }
        envDeviceCommandMapper.insert(command);
        return AjaxResult.success(command.getResultMsg());
    }
}