2026-04-17 17:12:41 +08:00
|
|
|
|
package com.maibu.service;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.core.business.*;
|
2026-06-03 16:53:02 +08:00
|
|
|
|
import com.maibu.core.business.device.NettyDevice;
|
|
|
|
|
|
import com.maibu.core.domain.entity.SysSite;
|
2026-04-17 17:12:41 +08:00
|
|
|
|
import com.maibu.core.domain.model.LoginUser;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.core.enums.DeviceTaskStaus;
|
2026-06-03 16:53:02 +08:00
|
|
|
|
import com.maibu.dto.*;
|
|
|
|
|
|
import com.maibu.manager.DeviceSessionManager;
|
|
|
|
|
|
import com.maibu.mapper.*;
|
|
|
|
|
|
import com.maibu.memory.GlobalMemory;
|
2026-04-30 15:25:58 +08:00
|
|
|
|
import com.maibu.memory.SiteMemory;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.List;
|
2026-06-03 16:53:02 +08:00
|
|
|
|
import java.util.Objects;
|
2026-04-17 10:42:16 +08:00
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
public class DeviceTaskService {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private DevicePlanMapper devicePlanMapper;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private DevicePlanTaskMapper devicePlanTaskMapper;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private DeviceMapper deviceMapper;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private WorkRecordMapper workRecordMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 16:53:02 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
private DeviceSessionManager deviceSessionManager;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private SysSiteMapper sysSiteMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-17 10:42:16 +08:00
|
|
|
|
public int insertOrUpdate(DevicePlan devicePlan, LoginUser loginUser) {
|
|
|
|
|
|
Long id = devicePlan.getId();
|
2026-06-03 16:53:02 +08:00
|
|
|
|
SiteMemory siteMemory = GlobalMemory.getSiteMemory(devicePlan.getOrgId(), devicePlan.getSiteId());
|
2026-04-17 10:42:16 +08:00
|
|
|
|
if (id != null) {
|
|
|
|
|
|
devicePlan.setUpdateBy(loginUser.getUsername());
|
|
|
|
|
|
devicePlan.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
transferDays(devicePlan);
|
|
|
|
|
|
int i = devicePlanMapper.updateById(devicePlan);
|
2026-06-03 16:53:02 +08:00
|
|
|
|
siteMemory.addDevicePlan(devicePlan);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
return i;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
devicePlan.setTaskStaus(DeviceTaskStaus.NEW);
|
|
|
|
|
|
devicePlan.setCreateTime(LocalDateTime.now());
|
|
|
|
|
|
devicePlan.setCreateBy(loginUser.getUsername());
|
|
|
|
|
|
transferDays(devicePlan);
|
|
|
|
|
|
int i = devicePlanMapper.insert(devicePlan);
|
2026-06-03 16:53:02 +08:00
|
|
|
|
siteMemory.addDevicePlan(devicePlan);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
return i;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void transferDays(DevicePlan devicePlan) {
|
|
|
|
|
|
DeviceTaskPlanRule deviceTaskPlanRules = devicePlan.getDeviceTaskPlanRules();
|
|
|
|
|
|
if (deviceTaskPlanRules != null && !CollectionUtils.isEmpty(deviceTaskPlanRules.getDays())) {
|
|
|
|
|
|
List<String> days = new ArrayList<>();
|
|
|
|
|
|
deviceTaskPlanRules.getDays().forEach(x -> {
|
|
|
|
|
|
String day = null;
|
|
|
|
|
|
switch (x) {
|
|
|
|
|
|
case MONDAY:
|
|
|
|
|
|
day = "周一";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TUESDAY:
|
|
|
|
|
|
day = "周二";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case WEDNESDAY:
|
|
|
|
|
|
day = "周三";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case THURSDAY:
|
|
|
|
|
|
day = "周四";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case FRIDAY:
|
|
|
|
|
|
day = "周五";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SATURDAY:
|
|
|
|
|
|
day = "周六";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SUNDAY:
|
|
|
|
|
|
day = "周日";
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
days.add(day);
|
|
|
|
|
|
});
|
|
|
|
|
|
devicePlan.getDeviceTaskPlanRules().setDaysTranslate(days);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<DevicePlan> getDeviceTaskByUser(Long userId) {
|
|
|
|
|
|
if (userId != null) {
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlan> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getUserId, userId);
|
|
|
|
|
|
return transferDevicePlanData(devicePlanMapper.selectList(queryWrapper));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-03 16:53:02 +08:00
|
|
|
|
public List<DevicePlan> getSiteDevicePlan(Long siteId, LocalDate startTime, LocalDate endTime, String deviceId, DeviceTaskStaus taskStaus) {
|
|
|
|
|
|
if (siteId != null) {
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlan> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getSiteId, siteId);
|
|
|
|
|
|
if (taskStaus != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getTaskStaus, taskStaus);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!StringUtils.isEmpty(deviceId)) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getDeviceId, deviceId);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (startTime != null && endTime != null) {
|
|
|
|
|
|
queryWrapper.between(DevicePlan::getCreateTime, startTime, endTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
return transferDevicePlanData(devicePlanMapper.selectList(queryWrapper));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-17 10:42:16 +08:00
|
|
|
|
public List<DevicePlan> getDeviceTask(DeviceTaskQueryDTO dto) {
|
|
|
|
|
|
if (dto != null) {
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlan> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
|
if (dto.getUserId() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getUserId, dto.getUserId());
|
|
|
|
|
|
}
|
2026-06-03 16:53:02 +08:00
|
|
|
|
if (!StringUtils.isEmpty(dto.getDeviceId())) {
|
2026-04-17 10:42:16 +08:00
|
|
|
|
queryWrapper.eq(DevicePlan::getDeviceId, dto.getDeviceId());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getTaskStaus() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getTaskStaus, dto.getTaskStaus());
|
|
|
|
|
|
}
|
2026-06-03 16:53:02 +08:00
|
|
|
|
if (dto.getOrgId() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getOrgId, dto.getOrgId());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getSiteId() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getSiteId, dto.getSiteId());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!StringUtils.isEmpty(dto.getPlanName())) {
|
2026-04-17 10:42:16 +08:00
|
|
|
|
queryWrapper.like(DevicePlan::getPlanName, dto.getPlanName());
|
|
|
|
|
|
}
|
|
|
|
|
|
queryWrapper.eq(DevicePlan::getDelFlag, 0);
|
|
|
|
|
|
List<DevicePlan> list = devicePlanMapper.selectList(queryWrapper);
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(list)) {
|
|
|
|
|
|
list.forEach(x -> {
|
|
|
|
|
|
Device device = deviceMapper.selectDeviceBySerialNumber(x.getDeviceId());
|
|
|
|
|
|
if (device != null) {
|
|
|
|
|
|
x.setDeviceAlias(device.getDeviceAlias());
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
return transferDevicePlanData(list);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int delete(Long id) {
|
|
|
|
|
|
DevicePlan planTask = devicePlanMapper.selectById(id);
|
|
|
|
|
|
if (planTask != null) {
|
2026-06-05 08:51:12 +08:00
|
|
|
|
if (planTask.getOrgId() == null) {
|
|
|
|
|
|
SysSite site = sysSiteMapper.selectById(planTask.getSiteId());
|
|
|
|
|
|
if (site != null) {
|
|
|
|
|
|
planTask.setOrgId(site.getOrgId());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-03 16:53:02 +08:00
|
|
|
|
SiteMemory siteMemory = GlobalMemory.getSiteMemory(planTask.getOrgId(), planTask.getSiteId());
|
2026-06-05 08:51:12 +08:00
|
|
|
|
if (siteMemory != null) {
|
|
|
|
|
|
siteMemory.removeDevicePlan(planTask);
|
|
|
|
|
|
}
|
2026-06-03 16:53:02 +08:00
|
|
|
|
return devicePlanMapper.deleteById(id);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void start(Long id, LoginUser loginUser) {
|
|
|
|
|
|
if (id != null) {
|
|
|
|
|
|
DevicePlan planTask = devicePlanMapper.selectById(id);
|
|
|
|
|
|
if (planTask != null) {
|
|
|
|
|
|
planTask.setTaskStaus(DeviceTaskStaus.EXECUTING);
|
|
|
|
|
|
planTask.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
planTask.setUpdateBy(loginUser.getUsername());
|
|
|
|
|
|
devicePlanMapper.updateById(planTask);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<DevicePlanStatisticsDTO> getDevicePlanStatistics(Long userId) {
|
|
|
|
|
|
List<DevicePlanStatisticsDTO> list = new ArrayList<>();
|
|
|
|
|
|
LocalDate today = LocalDate.now();
|
|
|
|
|
|
LocalDateTime monthStart = LocalDateTime.of(
|
|
|
|
|
|
today.getYear(), // 当前年
|
|
|
|
|
|
today.getMonth(), // 当前月
|
|
|
|
|
|
1, // 当月第一天
|
|
|
|
|
|
0, 0, 0, 0 // 时:分:秒:纳秒(00:00:00.000)
|
|
|
|
|
|
);
|
|
|
|
|
|
LocalDate lastDayOfMonth = today.withDayOfMonth(today.lengthOfMonth()); // 获取当月最后一天
|
|
|
|
|
|
LocalDateTime monthEnd = LocalDateTime.of(
|
|
|
|
|
|
lastDayOfMonth.getYear(),
|
|
|
|
|
|
lastDayOfMonth.getMonth(),
|
|
|
|
|
|
lastDayOfMonth.getDayOfMonth(),
|
|
|
|
|
|
23, 59, 59, 999_999_999 // 时:分:秒:纳秒(23:59:59.999)
|
|
|
|
|
|
);
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlan> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
|
queryWrapper.ge(DevicePlan::getCreateTime, monthStart)
|
|
|
|
|
|
.le(DevicePlan::getCreateTime, monthEnd)
|
2026-06-03 16:53:02 +08:00
|
|
|
|
.eq(DevicePlan::getUserId, userId);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
List<DevicePlan> devicePlans = transferDevicePlanData(devicePlanMapper.selectList(queryWrapper));
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(devicePlans)) {
|
|
|
|
|
|
List<LocalDate> dates = getMonthDays();
|
|
|
|
|
|
for (LocalDate date : dates) {
|
|
|
|
|
|
List<DevicePlan> plans = devicePlans.stream().filter(x -> x.getCreateTime().toLocalDate().equals(date)).collect(Collectors.toList());
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(plans)) {
|
|
|
|
|
|
DevicePlanStatisticsDTO dto = new DevicePlanStatisticsDTO();
|
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M.d");
|
|
|
|
|
|
dto.setName(formatter.format(date));
|
|
|
|
|
|
long count = plans.stream().filter(x -> DeviceTaskStaus.FINISH.equals(x.getTaskStaus())).count();
|
|
|
|
|
|
dto.setCompleted((int) count);
|
|
|
|
|
|
dto.setPlanned(plans.size());
|
|
|
|
|
|
list.add(dto);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<LocalDate> getMonthDays() {
|
|
|
|
|
|
LocalDate today = LocalDate.now();
|
|
|
|
|
|
int totalDays = today.lengthOfMonth();
|
|
|
|
|
|
LocalDate firstDayOfMonth = LocalDate.of(today.getYear(), today.getMonth(), 1);
|
|
|
|
|
|
List<LocalDate> allDaysOfCurrentMonth = new ArrayList<>();
|
|
|
|
|
|
for (int i = 0; i < totalDays; i++) {
|
|
|
|
|
|
// 每次累加1天,获取当月每一天
|
|
|
|
|
|
LocalDate currentDay = firstDayOfMonth.plusDays(i);
|
|
|
|
|
|
allDaysOfCurrentMonth.add(currentDay);
|
|
|
|
|
|
}
|
|
|
|
|
|
return allDaysOfCurrentMonth;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<DevicePlanTaskStatisticsDTO> getDevicePlanTaskStatistics(Long userId) {
|
|
|
|
|
|
List<DevicePlanTaskStatisticsDTO> list = new ArrayList<>();
|
|
|
|
|
|
// 1. 获取当前日期
|
|
|
|
|
|
LocalDate today = LocalDate.now();
|
|
|
|
|
|
// 2. 构造当前月份的起始时间:当月第一天 00:00:00
|
|
|
|
|
|
LocalDateTime monthStart = LocalDateTime.of(
|
|
|
|
|
|
today.getYear(), // 当前年
|
|
|
|
|
|
today.getMonth(), // 当前月
|
|
|
|
|
|
1, // 当月第一天
|
|
|
|
|
|
0, 0, 0, 0 // 时:分:秒:纳秒(00:00:00.000)
|
|
|
|
|
|
);
|
|
|
|
|
|
// 3. 构造当前月份的结束时间:当月最后一天 23:59:59.999
|
|
|
|
|
|
LocalDate lastDayOfMonth = today.withDayOfMonth(today.lengthOfMonth()); // 获取当月最后一天
|
|
|
|
|
|
LocalDateTime monthEnd = LocalDateTime.of(
|
|
|
|
|
|
lastDayOfMonth.getYear(),
|
|
|
|
|
|
lastDayOfMonth.getMonth(),
|
|
|
|
|
|
lastDayOfMonth.getDayOfMonth(),
|
|
|
|
|
|
23, 59, 59, 999_999_999 // 时:分:秒:纳秒(23:59:59.999)
|
|
|
|
|
|
);
|
|
|
|
|
|
// 4. 构建 LambdaQueryWrapper 进行范围查询
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlan> query = new LambdaQueryWrapper<>();
|
2026-06-03 16:53:02 +08:00
|
|
|
|
query.eq(DevicePlan::getUserId, userId);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
List<DevicePlan> devicePlans = transferDevicePlanData(devicePlanMapper.selectList(query));
|
2026-06-03 16:53:02 +08:00
|
|
|
|
if (CollectionUtils.isEmpty(devicePlans)) return new ArrayList<>();
|
2026-04-17 10:42:16 +08:00
|
|
|
|
List<Long> planIds = devicePlans.stream().map(DevicePlan::getId).collect(Collectors.toList());
|
2026-06-03 16:53:02 +08:00
|
|
|
|
if (CollectionUtils.isEmpty(planIds)) return new ArrayList<>();
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlanTask> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
|
// 核心:筛选 createTime 大于等于起始时间,且小于等于结束时间
|
|
|
|
|
|
queryWrapper.ge(DevicePlanTask::getCreateTime, monthStart)
|
|
|
|
|
|
.le(DevicePlanTask::getCreateTime, monthEnd)
|
2026-06-03 16:53:02 +08:00
|
|
|
|
.in(DevicePlanTask::getPlanId, planIds);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
List<DevicePlanTask> devicePlanTasks = transferDeviceTaskData(devicePlanTaskMapper.selectList(queryWrapper));
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(devicePlanTasks)) {
|
|
|
|
|
|
List<LocalDate> dates = getMonthDays();
|
|
|
|
|
|
for (LocalDate date : dates) {
|
|
|
|
|
|
List<DevicePlanTask> planTasks = devicePlanTasks.stream().filter(x -> x.getCreateTime().toLocalDate().equals(date)).collect(Collectors.toList());
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(planTasks)) {
|
|
|
|
|
|
DevicePlanTaskStatisticsDTO dto = new DevicePlanTaskStatisticsDTO();
|
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M.d");
|
|
|
|
|
|
dto.setDate(formatter.format(date));
|
|
|
|
|
|
long finishCount = planTasks.stream().filter(x -> DeviceTaskStaus.FINISH.equals(x.getTaskStaus())).count();
|
|
|
|
|
|
dto.setSuccess((int) finishCount);
|
|
|
|
|
|
long failedCount = planTasks.stream().filter(x -> DeviceTaskStaus.FAILED.equals(x.getTaskStaus())).count();
|
|
|
|
|
|
dto.setFailed((int) failedCount);
|
|
|
|
|
|
long pauseCount = planTasks.stream().filter(x -> DeviceTaskStaus.PAUSE.equals(x.getTaskStaus())).count();
|
|
|
|
|
|
dto.setPaused((int) pauseCount);
|
|
|
|
|
|
list.add(dto);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WorkRecord concurrentDevicePlanTask(String deviceId) {
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlanTask> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
|
queryWrapper.eq(DevicePlanTask::getDeviceId, deviceId)
|
|
|
|
|
|
.eq(DevicePlanTask::getTaskStaus, DeviceTaskStaus.EXECUTING);
|
|
|
|
|
|
List<DevicePlanTask> devicePlanTasks = devicePlanTaskMapper.selectList(queryWrapper);
|
|
|
|
|
|
Long routId = CollectionUtils.isEmpty(devicePlanTasks) ? null : devicePlanTasks.get(0).getRouteId();
|
2026-06-03 16:53:02 +08:00
|
|
|
|
return routId == null ? null : workRecordMapper.selectById(routId);
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<DeviceWorkStatisticsDTO> workStatistics(Long userId) {
|
|
|
|
|
|
//todo update
|
|
|
|
|
|
|
|
|
|
|
|
List<DeviceWorkStatisticsDTO> result = new ArrayList<>();
|
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
|
int times = random.nextInt(100);
|
|
|
|
|
|
int s = random.nextInt(1000);
|
|
|
|
|
|
DeviceWorkStatisticsDTO dto = new DeviceWorkStatisticsDTO();
|
|
|
|
|
|
dto.setLabel("割草次数");
|
|
|
|
|
|
dto.setValue(times);
|
|
|
|
|
|
DeviceWorkStatisticsDTO dto2 = new DeviceWorkStatisticsDTO();
|
|
|
|
|
|
dto2.setLabel("割草时长(s)");
|
|
|
|
|
|
dto2.setValue(s);
|
|
|
|
|
|
DeviceWorkStatisticsDTO dto3 = new DeviceWorkStatisticsDTO();
|
|
|
|
|
|
dto3.setLabel("割草面积(m2)");
|
|
|
|
|
|
dto3.setValue(s * 5);
|
|
|
|
|
|
result.add(dto);
|
|
|
|
|
|
result.add(dto2);
|
|
|
|
|
|
result.add(dto3);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<DevicePlanTask> deviceTaskPool(DeviceTaskQueryDTO dto) {
|
|
|
|
|
|
List<DevicePlanTask> list = new ArrayList<>();
|
2026-06-03 16:53:02 +08:00
|
|
|
|
if (dto.getOrgId() == null) {
|
|
|
|
|
|
SysSite site = sysSiteMapper.selectById(dto.getSiteId());
|
|
|
|
|
|
if (site != null) {
|
|
|
|
|
|
dto.setOrgId(site.getOrgId());
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-03 16:53:02 +08:00
|
|
|
|
SiteMemory siteMemory = GlobalMemory.getSiteMemory(dto.getOrgId(), dto.getSiteId());
|
|
|
|
|
|
siteMemory.devicePlanTaskPrepareMap.values().forEach(list::addAll);
|
|
|
|
|
|
list.addAll(SiteMemory.devicePlanTaskExecuteMap.values());
|
|
|
|
|
|
if (dto.getTaskStaus() != null) {
|
|
|
|
|
|
list = list.stream().filter(x -> x.getTaskStaus().equals(dto.getTaskStaus())).collect(Collectors.toList());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!StringUtils.isEmpty(dto.getDeviceId())) {
|
|
|
|
|
|
list = list.stream().filter(x -> x.getDeviceId().equals(dto.getDeviceId())).collect(Collectors.toList());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getPlanId() != null) {
|
|
|
|
|
|
list = list.stream().filter(x -> x.getPlanId().equals(dto.getPlanId())).collect(Collectors.toList());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getSiteId() != null) {
|
|
|
|
|
|
list = list.stream().filter(x -> x.getSiteId().equals(dto.getSiteId())).collect(Collectors.toList());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getOrgId() != null) {
|
|
|
|
|
|
list = list.stream().filter(x -> x.getOrgId().equals(dto.getOrgId())).collect(Collectors.toList());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!StringUtils.isEmpty(dto.getDeviceId())) {
|
|
|
|
|
|
list = list.stream().filter(x -> x.getDeviceId().equals(dto.getDeviceId())).collect(Collectors.toList());
|
|
|
|
|
|
}
|
2026-04-17 10:42:16 +08:00
|
|
|
|
return transferDeviceTaskData(list);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-03 16:53:02 +08:00
|
|
|
|
public List<DevicePlanTask> getSiteDeviceTaskHistory(DeviceTaskQueryDTO dto) {
|
|
|
|
|
|
LambdaQueryWrapper<DevicePlanTask> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
|
if (dto.getSiteId() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlanTask::getSiteId, dto.getSiteId());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getOrgId() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlanTask::getOrgId, dto.getOrgId());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getTaskStaus() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlanTask::getTaskStaus, dto.getTaskStaus());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.getPlanId() != null) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlanTask::getPlanId, dto.getPlanId());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!StringUtils.isEmpty(dto.getDeviceId())) {
|
|
|
|
|
|
queryWrapper.eq(DevicePlanTask::getDeviceId, dto.getDeviceId());
|
|
|
|
|
|
}
|
|
|
|
|
|
return transferDeviceTaskData(devicePlanTaskMapper.selectList(queryWrapper));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-17 10:42:16 +08:00
|
|
|
|
|
|
|
|
|
|
public List<DevicePlan> transferDevicePlanData(List<DevicePlan> list) {
|
|
|
|
|
|
if (CollectionUtils.isEmpty(list)) return list;
|
|
|
|
|
|
list.forEach(x -> {
|
|
|
|
|
|
DeviceTaskStaus taskStaus = x.getTaskStaus();
|
|
|
|
|
|
x.setTaskStausTranslate(taskStaus.getDescription());
|
|
|
|
|
|
});
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<DevicePlanTask> transferDeviceTaskData(List<DevicePlanTask> list) {
|
|
|
|
|
|
if (CollectionUtils.isEmpty(list)) return list;
|
|
|
|
|
|
list.forEach(x -> {
|
|
|
|
|
|
DeviceTaskStaus taskStaus = x.getTaskStaus();
|
|
|
|
|
|
x.setTaskStausTranslate(taskStaus.getDescription());
|
|
|
|
|
|
});
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-03 16:53:02 +08:00
|
|
|
|
public boolean createDeviceTask(DeviceTaskCommandDTO dto, String username) {
|
|
|
|
|
|
DevicePlanTask task = new DevicePlanTask();
|
|
|
|
|
|
task.setOrgId(dto.getOrgId());
|
|
|
|
|
|
task.setSiteId(dto.getSiteId());
|
|
|
|
|
|
task.setTaskStaus(DeviceTaskStaus.NEW);
|
|
|
|
|
|
task.setDeviceId(dto.getDeviceId());
|
|
|
|
|
|
task.setCreateBy(username);
|
|
|
|
|
|
task.setCreateTime(LocalDateTime.now());
|
|
|
|
|
|
task.setRouteId(dto.getRouteId());
|
|
|
|
|
|
task.setStartTime(dto.getStartTime());
|
|
|
|
|
|
boolean result = devicePlanTaskMapper.saveOrUpdate(task);
|
|
|
|
|
|
if (dto.getOrgId() == null) {
|
|
|
|
|
|
SysSite site = sysSiteMapper.selectById(dto.getSiteId());
|
|
|
|
|
|
if (site != null) {
|
|
|
|
|
|
dto.setOrgId(site.getOrgId());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
SiteMemory siteMemory = GlobalMemory.getSiteMemory(task.getOrgId(), task.getSiteId());
|
|
|
|
|
|
siteMemory.addDevicePlanPrepare(task);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean cancelTask(DeviceTaskCommandDTO dto, String username) {
|
|
|
|
|
|
if (!StringUtils.isEmpty(dto.getDeviceId())) {
|
|
|
|
|
|
NettyDevice device = deviceSessionManager.getDevice(dto.getDeviceId());
|
|
|
|
|
|
if (device != null) {
|
|
|
|
|
|
return device.cancelTask();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Long taskId = dto.getTaskId();
|
2026-06-09 15:30:06 +08:00
|
|
|
|
// Long siteId = dto.getSiteId();
|
|
|
|
|
|
// Long orgId = dto.getOrgId();
|
2026-06-03 16:53:02 +08:00
|
|
|
|
DevicePlanTask devicePlanTask = devicePlanTaskMapper.selectById(taskId);
|
|
|
|
|
|
if (devicePlanTask != null) {
|
2026-06-09 15:30:06 +08:00
|
|
|
|
NettyDevice device = deviceSessionManager.getDevice(devicePlanTask.getDeviceId());
|
|
|
|
|
|
if (device != null) {
|
|
|
|
|
|
return device.cancelTask();
|
|
|
|
|
|
}
|
2026-06-03 16:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//todo 完善
|
|
|
|
|
|
public boolean pauseTask(DeviceTaskCommandDTO dto, String username) {
|
|
|
|
|
|
if (!StringUtils.isEmpty(dto.getDeviceId())) {
|
|
|
|
|
|
NettyDevice device = deviceSessionManager.getDevice(dto.getDeviceId());
|
|
|
|
|
|
if (device != null) {
|
|
|
|
|
|
return device.pauseTask();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Long taskId = dto.getTaskId();
|
|
|
|
|
|
DevicePlanTask devicePlanTask = devicePlanTaskMapper.selectById(taskId);
|
|
|
|
|
|
if (devicePlanTask != null) {
|
|
|
|
|
|
devicePlanTask.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
devicePlanTask.setTaskStaus(DeviceTaskStaus.PAUSE);
|
|
|
|
|
|
devicePlanTask.setUpdateBy(username);
|
|
|
|
|
|
devicePlanTaskMapper.saveOrUpdate(devicePlanTask);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//todo 完善
|
|
|
|
|
|
public boolean recoveryTask(DeviceTaskCommandDTO dto, String username) {
|
|
|
|
|
|
if (!StringUtils.isEmpty(dto.getDeviceId())) {
|
|
|
|
|
|
NettyDevice device = deviceSessionManager.getDevice(dto.getDeviceId());
|
|
|
|
|
|
if (device != null) {
|
|
|
|
|
|
return device.recoveryTask();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Long taskId = dto.getTaskId();
|
|
|
|
|
|
Long siteId = dto.getSiteId();
|
|
|
|
|
|
Long orgId = dto.getOrgId();
|
|
|
|
|
|
DevicePlanTask devicePlanTask = devicePlanTaskMapper.selectById(taskId);
|
|
|
|
|
|
SiteMemory siteMemory = GlobalMemory.getSiteMemory(orgId, siteId);
|
|
|
|
|
|
DevicePlanTask executeTask = siteMemory.getDeviceExecuteTask(devicePlanTask.getDeviceId());
|
|
|
|
|
|
if (Objects.equals(devicePlanTask.getId(), executeTask.getId())) {
|
|
|
|
|
|
devicePlanTask.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
|
devicePlanTask.setTaskStaus(DeviceTaskStaus.EXECUTING);
|
|
|
|
|
|
devicePlanTask.setUpdateBy(username);
|
|
|
|
|
|
devicePlanTaskMapper.saveOrUpdate(devicePlanTask);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-17 10:42:16 +08:00
|
|
|
|
}
|