- TaskInfo DTO 新增 materialUrl 字段 - StudySessionResponse 新增 materialUrl 和 taskId 字段 - StudySessionsServiceImpl 在学习会话响应中填充 materialUrl 和 taskId - TasksServiceImpl 更新任务时保留原有 taskNum - 修复 TaskInfoResponse 注释格式错误
162 lines
7.3 KiB
Java
162 lines
7.3 KiB
Java
package com.guo.learningprogresstracker.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.guo.learningprogresstracker.dto.PriorityDto;
|
|
import com.guo.learningprogresstracker.dto.TaskInfo;
|
|
import com.guo.learningprogresstracker.dto.request.CreateTaskApplicationRequest;
|
|
import com.guo.learningprogresstracker.dto.request.TaskRequest;
|
|
import com.guo.learningprogresstracker.dto.request.UpdateTaskApplicationRequest;
|
|
import com.guo.learningprogresstracker.dto.response.TaskInfoResponse;
|
|
import com.guo.learningprogresstracker.entity.TaskApplicationEntity;
|
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
|
import com.guo.learningprogresstracker.enums.TaskApplicationStatusEnum;
|
|
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
|
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
|
import com.guo.learningprogresstracker.mapStruct.RequestConvert;
|
|
import com.guo.learningprogresstracker.mapStruct.TaskConvert;
|
|
import com.guo.learningprogresstracker.mapper.TaskApplicationMapper;
|
|
import com.guo.learningprogresstracker.service.PriorityWeightsService;
|
|
import com.guo.learningprogresstracker.service.TasksService;
|
|
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
|
import com.guo.learningprogresstracker.utils.CalculatedPriorityTool;
|
|
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* 针对表【tasks(存储学习任务的基本信息,包括优先级的多维度计算)】的数据库操作Service实现
|
|
*/
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
@Slf4j
|
|
public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
|
|
implements TasksService {
|
|
|
|
private final TasksMapper tasksMapper;
|
|
|
|
private final TaskApplicationMapper taskApplicationMapper;
|
|
|
|
private final PriorityWeightsService priorityWeightsService;
|
|
|
|
@Override
|
|
public Page<TaskInfo> taskList(Integer pageNum, Integer pageSize) {
|
|
Page<TaskEntity> page = tasksMapper.selectPage(new Page<TaskEntity>(pageNum, pageSize),
|
|
Wrappers.lambdaQuery(TaskEntity.class)
|
|
.orderByDesc(TaskEntity::getCalculatedPriority));
|
|
Page<TaskInfo> response = TaskConvert.MAPPER.toTaskInfoPage(page);
|
|
return response;
|
|
}
|
|
|
|
@Override
|
|
public String addTask(TaskRequest taskRequest) throws ErrorParameterException {
|
|
TaskEntity task = TaskConvert.MAPPER.taskRequestToTaskEntity(taskRequest);
|
|
|
|
if (this.exists(Wrappers.lambdaQuery(TaskEntity.class)
|
|
.eq(TaskEntity::getTaskName, task.getTaskName()))) {
|
|
throw new ErrorParameterException(String.format("任务名[%s]重复", task.getTaskName()));
|
|
}
|
|
|
|
task.setTaskNum(GenerateNumTool.generateNum("TASK"));
|
|
PriorityDto priorityDto = RequestConvert.MAPPER.taskRequestToPriorityDto(taskRequest);
|
|
task.setCalculatedPriority(CalculatedPriorityTool.calculatedPriority(priorityDto, priorityWeightsService.getWeights()));
|
|
this.save(task);
|
|
return task.getTaskNum();
|
|
}
|
|
|
|
@Override
|
|
public TaskInfoResponse getTask(String taskId) {
|
|
TaskEntity taskEntity = this.getById(taskId);
|
|
return TaskConvert.MAPPER.taskEntityToTaskInfoResponse(taskEntity);
|
|
}
|
|
|
|
@Override
|
|
public void updateTask(String taskId, TaskRequest updatedTask) {
|
|
TaskEntity existingTask = this.getById(taskId);
|
|
if (existingTask == null) {
|
|
throw new IllegalArgumentException("任务不存在: " + taskId);
|
|
}
|
|
TaskEntity taskEntity = TaskConvert.MAPPER.taskRequestToTaskEntity(updatedTask);
|
|
int id;
|
|
try {
|
|
id = Integer.parseInt(taskId);
|
|
} catch (NumberFormatException e) {
|
|
throw new IllegalArgumentException("任务ID格式错误: " + taskId);
|
|
}
|
|
taskEntity.setId(id);
|
|
taskEntity.setTaskNum(existingTask.getTaskNum());
|
|
// 维度数据可能变化,更新时重算优先级
|
|
PriorityDto priorityDto = RequestConvert.MAPPER.taskRequestToPriorityDto(updatedTask);
|
|
taskEntity.setCalculatedPriority(CalculatedPriorityTool.calculatedPriority(priorityDto, priorityWeightsService.getWeights()));
|
|
this.updateById(taskEntity);
|
|
}
|
|
|
|
@Override
|
|
public void deleteTask(String taskId) {
|
|
TaskEntity existingTask = this.getById(taskId);
|
|
if (existingTask == null) {
|
|
throw new IllegalArgumentException("任务不存在: " + taskId);
|
|
}
|
|
this.removeById(taskId);
|
|
}
|
|
|
|
@Override
|
|
public List<TaskApplicationEntity> getApplications(String taskNum) throws NotFindEntitiesException {
|
|
ensureTaskExists(taskNum);
|
|
return taskApplicationMapper.selectList(
|
|
Wrappers.<TaskApplicationEntity>lambdaQuery()
|
|
.eq(TaskApplicationEntity::getTaskNum, taskNum)
|
|
.orderByDesc(TaskApplicationEntity::getLastModifiedTime)
|
|
.orderByDesc(TaskApplicationEntity::getCreatedTime));
|
|
}
|
|
|
|
@Override
|
|
public TaskApplicationEntity createApplication(CreateTaskApplicationRequest request) throws NotFindEntitiesException {
|
|
ensureTaskExists(request.getTaskNum());
|
|
TaskApplicationEntity entity = new TaskApplicationEntity();
|
|
entity.setTaskNum(request.getTaskNum());
|
|
entity.setTitle(request.getTitle());
|
|
entity.setDescription(request.getDescription());
|
|
entity.setResourceUrl(request.getResourceUrl());
|
|
entity.setStatus(normalizeApplicationStatus(request.getStatus()));
|
|
taskApplicationMapper.insert(entity);
|
|
return entity;
|
|
}
|
|
|
|
@Override
|
|
public TaskApplicationEntity updateApplication(Integer id, UpdateTaskApplicationRequest request) throws NotFindEntitiesException {
|
|
TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id))
|
|
.orElseThrow(() -> new NotFindEntitiesException("应用场景[" + id + "]不存在"));
|
|
existing.setTitle(request.getTitle());
|
|
existing.setDescription(request.getDescription());
|
|
existing.setResourceUrl(request.getResourceUrl());
|
|
existing.setStatus(normalizeApplicationStatus(request.getStatus()));
|
|
taskApplicationMapper.updateById(existing);
|
|
return existing;
|
|
}
|
|
|
|
@Override
|
|
public void deleteApplication(Integer id) throws NotFindEntitiesException {
|
|
TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id))
|
|
.orElseThrow(() -> new NotFindEntitiesException("应用场景[" + id + "]不存在"));
|
|
taskApplicationMapper.deleteById(existing.getId());
|
|
}
|
|
|
|
private void ensureTaskExists(String taskNum) throws NotFindEntitiesException {
|
|
if (!StringUtils.hasText(taskNum) || !tasksMapper.exists(
|
|
Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, taskNum))) {
|
|
throw new NotFindEntitiesException("任务[" + taskNum + "]不存在");
|
|
}
|
|
}
|
|
|
|
private String normalizeApplicationStatus(String status) {
|
|
return TaskApplicationStatusEnum.fromCodeOrDefault(status).getCode();
|
|
}
|
|
}
|