This commit is contained in:
2026-06-17 09:14:42 +08:00
parent 3879cd0067
commit a68a670ecb
27 changed files with 1053 additions and 392 deletions

View File

@@ -1,21 +1,24 @@
package com.maibu.controller;
import com.maibu.core.business.WorkRecord;
import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.core.controller.BaseController;
import com.maibu.core.domain.R;
import com.maibu.service.impl.WorkRecordService;
import com.maibu.utils.MinioUtil;
import com.maibu.utils.json.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.http.MediaType;
import java.io.InputStream;
import java.util.List;
@RestController
@RequestMapping("/iot/workRecord")
public class WorkRecordController {
public class WorkRecordController extends BaseController {
@Autowired
private MinioUtil minioUtil;
@@ -23,15 +26,16 @@ public class WorkRecordController {
@Autowired
private WorkRecordService workRecordService;
@PostMapping("/add")
@PostMapping(value = "/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R addWorkRecord(
@RequestPart("file") MultipartFile file, // 接收上传的图片
@RequestPart("file") MultipartFile file, // 接收上传的图片
@RequestPart("workRecord") WorkRecord workRecord // 接收表单里的 JSON 对象
) {
int i = -1;
WorkRecord workRecord1 = workRecordService.selectWorkRecordByWorkName(workRecord.getWorkName());
// WorkRecord workRecordObj = JsonUtils.parseObject(workRecord, WorkRecord.class);
WorkRecord workRecord1 = workRecordService.selectWorkRecordByWorkNameAndSiteId(workRecord.getWorkName(), workRecord.getSiteId());
if (workRecord1 != null) {
return R.fail("该任务已存在");
}
@@ -49,7 +53,8 @@ public class WorkRecordController {
// 这里可以把 URL 存到 workRecord 里
workRecord.setImgUrl(url);
workRecord.setUserId(getUserId());
workRecord.setOrgId(getLoginUser().getOrgId());
i = workRecordService.addWorkRecord(workRecord);
} catch (Exception e) {
return R.fail("新增失败: " + e.getMessage());
@@ -58,7 +63,6 @@ public class WorkRecordController {
return i <= 0 ? R.fail("新增失败") : R.ok("新增成功");
}
@GetMapping("/selectByUserId")
public R selectWorkRecordByUserId(String userId) {
List<WorkRecord> workRecords = workRecordService.selectWorkRecordByUserId(userId);
@@ -89,4 +93,16 @@ public class WorkRecordController {
return R.ok(workRecords);
}
@GetMapping("/selectById")
public R selectById(@RequestParam Long id) {
WorkRecord workRecord = workRecordService.selectById(id);
return R.ok(workRecord);
}
@GetMapping("/selectByTaskId")
public R selectByTaskId(@RequestParam Long taskId) {
List<LatAndLngEntity> path = workRecordService.selectByTaskId(taskId);
return R.ok(path);
}
}

View File

@@ -1,7 +1,30 @@
package com.maibu.service.impl;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maibu.core.business.*;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import com.maibu.core.business.Device;
import com.maibu.core.business.DevicePlan;
import com.maibu.core.business.DeviceRunningStatusHistory;
import com.maibu.core.business.DeviceStatusRecordDTO;
import com.maibu.core.business.dto.DeviceBindDTO;
import com.maibu.core.domain.AjaxResult;
import com.maibu.core.domain.entity.SysRole;
@@ -9,6 +32,9 @@ import com.maibu.core.domain.entity.SysSite;
import com.maibu.core.domain.model.LoginUser;
import com.maibu.core.enums.DeviceTaskStaus;
import com.maibu.core.redis.RedisCache;
import com.maibu.influxdb.MowerRealTimeData;
import com.maibu.influxdb.util.InfluxSqlBuilder;
import com.maibu.influxdb.util.LambdaUtils;
import com.maibu.mapper.DeviceMapper;
import com.maibu.mapper.DevicePlanMapper;
import com.maibu.mapper.DeviceStatusHistoryMapper;
@@ -19,19 +45,6 @@ import com.maibu.service.IDeviceService;
import com.maibu.service.ISysRoleService;
import com.maibu.utils.SecurityUtils;
import com.maibu.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* 设备Service业务层处理
@@ -59,7 +72,6 @@ public class DeviceServiceImpl implements IDeviceService {
@Autowired
private SysSiteMapper sysSiteMapper;
/**
* 根据设备编号查询设备
*
@@ -88,7 +100,6 @@ public class DeviceServiceImpl implements IDeviceService {
return device;
}
/**
* 查询设备列表
*
@@ -130,46 +141,38 @@ public class DeviceServiceImpl implements IDeviceService {
.map(DevicePlan::getDeviceId)
.collect(Collectors.toSet());
Map<String, List<DeviceRunningStatusHistory>> statusMap =
allStatusList.stream()
.collect(Collectors.groupingBy(DeviceRunningStatusHistory::getDeviceId));
Map<String, List<DeviceRunningStatusHistory>> statusMap = allStatusList.stream()
.collect(Collectors.groupingBy(DeviceRunningStatusHistory::getDeviceId));
deviceList.forEach(x -> {
Integer s = x.getOnlineStatus() == null ? 0 : x.getOnlineStatus();
if (0 == s) {
x.setFeStatus(3); //离线
x.setFeStatus(3); // 离线
} else {
if (!CollectionUtils.isEmpty(taskDeviceIds) && taskDeviceIds.contains(x.getDeviceName())) {
x.setFeStatus(1);
} else {
x.setFeStatus(2); //空闲
x.setFeStatus(2); // 空闲
}
// if (!CollectionUtils.isEmpty(planTasks)) {
// long count = planTasks.stream().filter(y -> y.getDeviceId().equals(x.getDeviceName())).count();
// if (count > 0) {
// x.setFeStatus(1); //任务中
// } else {
// x.setFeStatus(2); //空闲
// }
// } else {
// x.setFeStatus(2); //空闲
// }
}
//经纬度
// 经纬度
String key = "running_status:" + x.getDeviceName();
DeviceStatusRecordDTO recordDTO = redisCache.getCacheObject(key);
DeviceRunningStatusHistory lastRunningStatus = null;
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
List<DeviceRunningStatusHistory> list = recordDTO.getHistories().stream().filter(y -> !"0".equals(y.getQual())).collect(Collectors.toList());
List<DeviceRunningStatusHistory> list = recordDTO.getHistories().stream()
.filter(y -> !"0".equals(y.getQual())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(list)) {
lastRunningStatus = list.get(list.size() - 1);
}
} else {
if (!CollectionUtils.isEmpty(allStatusList)) {
List<DeviceRunningStatusHistory> list = statusMap.get(x.getDeviceName());
// List<DeviceRunningStatusHistory> list = allStatusList.stream().filter(z->z.getDeviceId().equals(x.getDeviceName())).collect(Collectors.toList());
// List<DeviceRunningStatusHistory> list =
// allStatusList.stream().filter(z->z.getDeviceId().equals(x.getDeviceName())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(list)) {
List<DeviceRunningStatusHistory> l = list.stream().filter(y -> !"0".equals(y.getQual())).collect(Collectors.toList());
List<DeviceRunningStatusHistory> l = list.stream().filter(y -> !"0".equals(y.getQual()))
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(l)) {
lastRunningStatus = l.get(l.size() - 1);
}
@@ -183,7 +186,6 @@ public class DeviceServiceImpl implements IDeviceService {
return deviceList;
}
@Override
public List<String> selectSerialNumberByProductId(Long productId) {
return deviceMapper.selectSerialNumberByProductId(productId);
@@ -205,7 +207,6 @@ public class DeviceServiceImpl implements IDeviceService {
return deviceMapper.getDeviceNumsByProductId(productId);
}
/**
* 重置设备状态
*
@@ -227,7 +228,6 @@ public class DeviceServiceImpl implements IDeviceService {
return deviceMapper.selectSerialNumbersByProductId(productId);
}
@Override
public boolean bind(DeviceBindDTO bindDTO, String username) {
try {
@@ -288,7 +288,7 @@ public class DeviceServiceImpl implements IDeviceService {
String roleKey = sysRole.getRoleKey();
if (!StringUtils.isEmpty(roleKey)) {
if ("manager".equals(roleKey) || "siteManager".equals(roleKey) || "admin".equals(roleKey)) {
// device.setSiteId(siteId);
// device.setSiteId(siteId);
} else {
device.setTenantId(loginUser.getUserId());
}
@@ -321,19 +321,20 @@ public class DeviceServiceImpl implements IDeviceService {
DeviceStatusRecordDTO recordDTO = redisCache.getCacheObject(key);
DeviceRunningStatusHistory lastRunningStatus = null;
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
List<DeviceRunningStatusHistory> list = recordDTO.getHistories().stream().filter(y -> !"0".equals(y.getQual())).collect(Collectors.toList());
List<DeviceRunningStatusHistory> list = recordDTO.getHistories().stream()
.filter(y -> !"0".equals(y.getQual())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(list)) {
lastRunningStatus = list.get(list.size() - 1);
}
}
if (lastRunningStatus == null) {
if (!CollectionUtils.isEmpty(allStatusList)) {
Map<String, List<DeviceRunningStatusHistory>> statusMap =
allStatusList.stream()
.collect(Collectors.groupingBy(DeviceRunningStatusHistory::getDeviceId));
Map<String, List<DeviceRunningStatusHistory>> statusMap = allStatusList.stream()
.collect(Collectors.groupingBy(DeviceRunningStatusHistory::getDeviceId));
List<DeviceRunningStatusHistory> list = statusMap.get(x.getDeviceName());
if (!CollectionUtils.isEmpty(list)) {
List<DeviceRunningStatusHistory> l = list.stream().filter(y -> !"0".equals(y.getQual())).collect(Collectors.toList());
List<DeviceRunningStatusHistory> l = list.stream().filter(y -> !"0".equals(y.getQual()))
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(l)) {
lastRunningStatus = l.get(l.size() - 1);
}
@@ -367,16 +368,15 @@ public class DeviceServiceImpl implements IDeviceService {
} else if (!"admin".equals(roleKey)) {
device.setTenantId(loginUser.getUserId());
}
// if ("admin".equals(roleKey)) {
// deviceList = deviceMapper.selectList();
// } else {
// deviceList = deviceMapper.selectDeviceList(device);
// }
// if ("admin".equals(roleKey)) {
// deviceList = deviceMapper.selectList();
// } else {
// deviceList = deviceMapper.selectDeviceList(device);
// }
deviceList = deviceMapper.selectDeviceList(device);
}
}
return deviceList;
}
}

View File

@@ -1,10 +1,14 @@
package com.maibu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maibu.core.business.DevicePlanTask;
import com.maibu.core.business.WorkRecord;
import com.maibu.core.business.path.LatAndLngEntity;
import com.maibu.mapper.DevicePlanTaskMapper;
import com.maibu.mapper.WorkRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
@@ -14,10 +18,12 @@ public class WorkRecordService {
@Autowired
private WorkRecordMapper workRecordMapper;
@Autowired
private DevicePlanTaskMapper devicePlanTaskMapper;
public int addWorkRecord(WorkRecord workRecord) throws Exception {
int i = workRecordMapper.insert(workRecord);
// int i = workRecordMapper.insertWorkRecord(workRecord);
// int i = workRecordMapper.insertWorkRecord(workRecord);
return i;
}
@@ -27,28 +33,55 @@ public class WorkRecordService {
return workRecordMapper.selectOne(queryWrapper);
}
public WorkRecord selectWorkRecordByWorkNameAndSiteId(String workName, Long siteId) {
LambdaQueryWrapper<WorkRecord> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(WorkRecord::getWorkName, workName);
queryWrapper.eq(WorkRecord::getSiteId, siteId);
return workRecordMapper.selectOne(queryWrapper);
}
public List<WorkRecord> selectWorkRecordByUserId(String userId) {
LambdaQueryWrapper<WorkRecord> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(WorkRecord::getUserId, userId);
return workRecordMapper.selectList(queryWrapper);
}
public int deleteWorkRecordByWorkName(String workName) {
LambdaQueryWrapper<WorkRecord> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(WorkRecord::getWorkName, workName);
return workRecordMapper.delete(queryWrapper);
}
public boolean updateWorkRecord(WorkRecord workRecord) {
return workRecordMapper.saveOrUpdate(workRecord);
}
public List<WorkRecord> selectBySiteId(Long siteId) {
LambdaQueryWrapper<WorkRecord> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(WorkRecord::getSiteId, siteId);
return workRecordMapper.selectList(queryWrapper);
List<WorkRecord> list = workRecordMapper.selectList(queryWrapper);
if (!CollectionUtils.isEmpty(list)) {
list.forEach(workRecord -> {
workRecord.setJsonData(null);
});
}
return list;
}
public WorkRecord selectById(Long id) {
return workRecordMapper.selectById(id);
}
public List<LatAndLngEntity> selectByTaskId(Long taskId) {
DevicePlanTask devicePlanTask = devicePlanTaskMapper.selectById(taskId); // 先查询设备任务,确认任务存在
if (devicePlanTask == null) {
return null;
}
Long routeId = devicePlanTask.getRouteId(); // 获取任务关联的路线ID
WorkRecord workRecord = workRecordMapper.selectById(routeId); // 查询路线ID对应的工作记录
if (workRecord != null && workRecord.getJsonData() != null) {
return workRecord.getJsonData().getPath(); // 返回路线数据
}
return null; // 如果没有找到对应的工作记录或路线数据,返回 null
}
}