| | |
| | | package com.ruoyi.device.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.device.mapper.DeviceLedgerMapper; |
| | | import com.ruoyi.device.mapper.MaintenanceTaskMapper; |
| | | import com.ruoyi.device.pojo.DeviceLedger; |
| | | import com.ruoyi.device.pojo.MaintenanceTask; |
| | | import com.ruoyi.device.service.MaintenanceTaskService; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author :yys |
| | |
| | | @Autowired |
| | | private MaintenanceTaskScheduler maintenanceTaskScheduler; |
| | | |
| | | @Autowired |
| | | private DeviceLedgerMapper deviceLedgerMapper; |
| | | |
| | | @Override |
| | | public AjaxResult listPage(Page page, MaintenanceTask maintenanceTask) { |
| | | Page<MaintenanceTask> taskPage = maintenanceTaskMapper.selectPage(page, null); |
| | | // 2. 如果没有数据,直接返回空分页 |
| | | public AjaxResult listPage(Page<MaintenanceTask> page, MaintenanceTask maintenanceTask) { |
| | | // 查询 |
| | | LambdaQueryWrapper<MaintenanceTask> queryWrapper = new LambdaQueryWrapper<>(); |
| | | if (maintenanceTask.getTaskName() != null) { |
| | | queryWrapper.like(MaintenanceTask::getTaskName, maintenanceTask.getTaskName()); |
| | | } |
| | | |
| | | Page<MaintenanceTask> taskPage = maintenanceTaskMapper.selectPage(page, queryWrapper); |
| | | |
| | | if (taskPage.getRecords().isEmpty()) { |
| | | return AjaxResult.success(taskPage); |
| | | } |
| | | |
| | | // 3. 收集所有需要查询的用户ID |
| | | //收集所有需要查询的ID |
| | | Set<Long> userIds = new HashSet<>(); |
| | | Set<Long> allDeviceIds = new HashSet<>(); |
| | | //设备ID列表 |
| | | Map<Long, List<Long>> taskDeviceIdMap = new HashMap<>(); |
| | | |
| | | // 遍历任务,收集登记人ID和设备ID |
| | | for (MaintenanceTask task : taskPage.getRecords()) { |
| | | // 收集登记人ID |
| | | taskPage.getRecords().forEach(task -> { |
| | | if (task.getRegistrantId() != null) { |
| | | userIds.add(task.getRegistrantId()); |
| | | } |
| | | }); |
| | | |
| | | // 收集设备ID并解析 |
| | | if (StringUtils.isNotEmpty(task.getDeviceIds())) { |
| | | try { |
| | | List<Long> deviceIds = JSON.parseArray(task.getDeviceIds(), Long.class); |
| | | if (CollectionUtils.isNotEmpty(deviceIds)) { |
| | | allDeviceIds.addAll(deviceIds); |
| | | taskDeviceIdMap.put(task.getId(), deviceIds); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("解析设备ID列表失败: taskId={}, deviceIds={}", task.getId(), task.getDeviceIds(), e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 4. 批量查询用户信息 |
| | | Map<Long, String> userNickNameMap = new HashMap<>(); |
| | | if (!userIds.isEmpty()) { |
| | | List<SysUser> users = sysUserMapper.selectUserByIds((new ArrayList<>(userIds))); |
| | | List<SysUser> users = sysUserMapper.selectUserByIds(new ArrayList<>(userIds)); |
| | | if (CollectionUtils.isNotEmpty(users)) { |
| | | users.forEach(user -> userNickNameMap.put(user.getUserId(), user.getNickName())); |
| | | } |
| | | taskPage.getRecords().forEach(task -> { |
| | | } |
| | | |
| | | // 5. 批量查询设备信息 |
| | | Map<Long, String> deviceNameMap = new HashMap<>(); |
| | | if (!allDeviceIds.isEmpty()) { |
| | | List<DeviceLedger> devices = deviceLedgerMapper.selectBatchIds(new ArrayList<>(allDeviceIds)); |
| | | if (CollectionUtils.isNotEmpty(devices)) { |
| | | devices.forEach(device -> deviceNameMap.put(device.getId(), device.getDeviceName())); |
| | | } |
| | | } |
| | | |
| | | // 6. 设置返回结果 |
| | | for (MaintenanceTask task : taskPage.getRecords()) { |
| | | // 设置登记人昵称 |
| | | if (task.getRegistrantId() != null) { |
| | | task.setRegistrant(userNickNameMap.getOrDefault(task.getRegistrantId(), "未知用户")); |
| | | } |
| | | }); |
| | | |
| | | // 设置设备名称 |
| | | List<Long> deviceIds = taskDeviceIdMap.get(task.getId()); |
| | | if (CollectionUtils.isNotEmpty(deviceIds)) { |
| | | List<String> deviceNames = deviceIds.stream() |
| | | .map(id -> deviceNameMap.getOrDefault(id, "未知设备")) |
| | | .collect(Collectors.toList()); |
| | | task.setDeviceName(String.join(", ", deviceNames)); |
| | | } else { |
| | | task.setDeviceName("无设备"); |
| | | } |
| | | } |
| | | |
| | | return AjaxResult.success(taskPage); |
| | | } |
| | | |