package com.mes.service.impl;
|
|
import com.mes.entity.PlcTestTask;
|
import com.mes.mapper.PlcTestTaskMapper;
|
import com.mes.service.PlcTestTaskService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.StringUtils;
|
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* PLC测试任务服务实现类
|
*
|
* @author huang
|
* @date 2025/11/04
|
*/
|
@Slf4j
|
@Service
|
public class PlcTestTaskServiceImpl implements PlcTestTaskService {
|
|
@Autowired
|
private PlcTestTaskMapper plcTestTaskMapper;
|
|
/**
|
* 创建新任务
|
*/
|
@Override
|
public PlcTestTask createTask(PlcTestTask task) {
|
try {
|
if (task == null) {
|
throw new RuntimeException("创建任务失败:task对象不能为空");
|
}
|
|
if (!StringUtils.hasText(task.getProjectId())) {
|
throw new RuntimeException("创建任务失败:projectId不能为空");
|
}
|
|
// 一些必上字段应一次设置
|
if (!StringUtils.hasText(task.getModule())) {
|
task.setModule("default");
|
}
|
|
if (!StringUtils.hasText(task.getOperationMode())) {
|
task.setOperationMode("MANUAL");
|
}
|
if (!StringUtils.hasText(task.getStatus())) {
|
task.setStatus("PENDING");
|
}
|
|
if (task.getStartTime() == null) {
|
task.setStartTime(new Date());
|
}
|
|
if (task.getCreatedAt() == null) {
|
task.setCreatedAt(new Date());
|
}
|
|
if (task.getUpdatedAt() == null) {
|
task.setUpdatedAt(new Date());
|
}
|
|
log.info("即将插入任务:projectId={}, module={}, status={}",
|
task.getProjectId(), task.getModule(), task.getStatus());
|
|
int insertCount = plcTestTaskMapper.insert(task);
|
|
if (insertCount <= 0) {
|
throw new RuntimeException("创建任务失败:数据库插入返回0行");
|
}
|
|
log.info("创建PLC测试任务成功,id: {}, projectId: {}", task.getId(), task.getProjectId());
|
return task;
|
} catch (Exception e) {
|
log.error("创建PLC测试任务失败", e);
|
throw new RuntimeException("创建PLC测试任务失败: " + e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 根据id查询任务
|
*/
|
@Override
|
public PlcTestTask getTaskById(Long id) {
|
if (id == null) {
|
log.warn("查询任务失败:id不能为空");
|
return null;
|
}
|
try {
|
return plcTestTaskMapper.selectById(id);
|
} catch (Exception e) {
|
log.error("查询任务失败,id: {}", id, e);
|
return null;
|
}
|
}
|
|
/**
|
* 查询项目的所有任务
|
*/
|
@Override
|
public List<PlcTestTask> getTasksByProjectId(String projectId) {
|
try {
|
if (!StringUtils.hasText(projectId)) {
|
log.warn("查询任务失败:projectId为空");
|
return null;
|
}
|
|
List<PlcTestTask> tasks = plcTestTaskMapper.selectRunningTasks(projectId);
|
log.debug("查询项目[{}]的任务,共{}条", projectId, tasks != null ? tasks.size() : 0);
|
return tasks;
|
} catch (Exception e) {
|
log.error("查询项目任务失败,projectId: {}", projectId, e);
|
throw new RuntimeException("查询项目任务失败", e);
|
}
|
}
|
|
/**
|
* 查询项目的运行中任务
|
*/
|
@Override
|
public List<PlcTestTask> getRunningTasks(String projectId) {
|
try {
|
if (!StringUtils.hasText(projectId)) {
|
log.warn("查询运行中任务失败:projectId为空");
|
return null;
|
}
|
|
List<PlcTestTask> tasks = plcTestTaskMapper.selectRunningTasks(projectId);
|
log.debug("查询项目[{}]的运行中任务,共{}条", projectId, tasks != null ? tasks.size() : 0);
|
return tasks;
|
} catch (Exception e) {
|
log.error("查询运行中任务失败,projectId: {}", projectId, e);
|
throw new RuntimeException("查询运行中任务失败", e);
|
}
|
}
|
|
/**
|
* 更新任务状态
|
*/
|
@Override
|
public boolean updateTaskStatus(Long id, String status) {
|
if (id == null || !StringUtils.hasText(status)) {
|
log.warn("更新任务状态失败:id或status不能为空");
|
return false;
|
}
|
try {
|
PlcTestTask task = new PlcTestTask();
|
task.setId(id);
|
task.setStatus(status);
|
task.setUpdatedAt(new Date());
|
|
int result = plcTestTaskMapper.updateById(task);
|
log.info("更新任务状态成功,id: {}, status: {}", id, status);
|
return result > 0;
|
} catch (Exception e) {
|
log.error("更新任务状态失败,id: {}, status: {}", id, status, e);
|
return false;
|
}
|
}
|
|
/**
|
* 任务完成
|
*/
|
@Override
|
public boolean completeTask(Long id, String status, Integer duration, String result, String errorMessage) {
|
if (id == null || !StringUtils.hasText(status)) {
|
log.warn("完成任务失败:id或status不能为空");
|
return false;
|
}
|
try {
|
PlcTestTask task = new PlcTestTask();
|
task.setId(id);
|
task.setStatus(status);
|
task.setDuration(duration);
|
task.setResult(result);
|
task.setErrorMessage(errorMessage);
|
task.setEndTime(new Date());
|
task.setUpdatedAt(new Date());
|
|
int updateResult = plcTestTaskMapper.updateById(task);
|
log.info("完成任务成功,id: {}, status: {}", id, status);
|
return updateResult > 0;
|
} catch (Exception e) {
|
log.error("完成任务失败,id: {}, status: {}", id, status, e);
|
return false;
|
}
|
}
|
|
/**
|
* 删除任务
|
*/
|
@Override
|
public boolean deleteTask(Long id) {
|
if (id == null) {
|
log.warn("删除任务失败:id不能为空");
|
return false;
|
}
|
try {
|
int result = plcTestTaskMapper.deleteById(id);
|
log.info("删除任务成功,id: {}", id);
|
return result > 0;
|
} catch (Exception e) {
|
log.error("删除任务失败,id: {}", id, e);
|
return false;
|
}
|
}
|
|
|
}
|