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 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 w = new LambdaQueryWrapper<>(); w.orderByDesc(DeviceDefectRecord::getId); w.last("limit 100"); List defects = deviceDefectRecordMapper.selectList(w); List> result = new ArrayList<>(); Map deviceNameMap = new HashMap<>(); try { List 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 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 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()); } }