package com.ruoyi.system.service.impl;
|
|
import com.ruoyi.system.domain.NotifyTask;
|
import com.ruoyi.system.mapper.NotifyTaskMapper;
|
import com.ruoyi.system.service.INotifyTaskService;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.List;
|
|
/**
|
* 通知任务服务实现类
|
*
|
* @author ruoyi
|
* @date 2025-12-07
|
*/
|
@Service
|
public class NotifyTaskServiceImpl implements INotifyTaskService {
|
|
private static final Logger log = LoggerFactory.getLogger(NotifyTaskServiceImpl.class);
|
|
@Autowired
|
private NotifyTaskMapper notifyTaskMapper;
|
|
/**
|
* 查询通知任务
|
*/
|
@Override
|
public NotifyTask selectNotifyTaskById(Long id) {
|
return notifyTaskMapper.selectNotifyTaskById(id);
|
}
|
|
/**
|
* 查询通知任务列表
|
*/
|
@Override
|
public List<NotifyTask> selectNotifyTaskList(NotifyTask notifyTask) {
|
return notifyTaskMapper.selectNotifyTaskList(notifyTask);
|
}
|
|
/**
|
* 查询待处理的通知任务
|
*/
|
@Override
|
public List<NotifyTask> selectPendingNotifyTasks(int limit) {
|
return notifyTaskMapper.selectPendingNotifyTasks(limit);
|
}
|
|
/**
|
* 检查通知任务是否已存在
|
*/
|
@Override
|
public boolean existsNotifyTask(Long taskId, Long userId, String notifyType) {
|
int count = notifyTaskMapper.countByTaskUserType(taskId, userId, notifyType);
|
return count > 0;
|
}
|
|
/**
|
* 创建通知任务(带防重)
|
*/
|
@Override
|
public NotifyTask createNotifyTask(NotifyTask notifyTask) {
|
// 检查是否已存在
|
if (existsNotifyTask(notifyTask.getTaskId(), notifyTask.getUserId(), notifyTask.getNotifyType())) {
|
log.info("通知任务已存在,跳过创建,taskId={}, userId={}, notifyType={}",
|
notifyTask.getTaskId(), notifyTask.getUserId(), notifyTask.getNotifyType());
|
return null;
|
}
|
|
// 设置默认值
|
if (notifyTask.getStatus() == null) {
|
notifyTask.setStatus(NotifyTask.STATUS_PENDING);
|
}
|
if (notifyTask.getRetryCount() == null) {
|
notifyTask.setRetryCount(0);
|
}
|
if (notifyTask.getMaxRetry() == null) {
|
notifyTask.setMaxRetry(3);
|
}
|
|
try {
|
notifyTaskMapper.insertNotifyTask(notifyTask);
|
log.info("创建通知任务成功,id={}, taskId={}, userId={}, notifyType={}",
|
notifyTask.getId(), notifyTask.getTaskId(), notifyTask.getUserId(), notifyTask.getNotifyType());
|
return notifyTask;
|
} catch (Exception e) {
|
// 可能是唯一索引冲突(并发情况)
|
log.warn("创建通知任务失败(可能并发冲突),taskId={}, userId={}, notifyType={}",
|
notifyTask.getTaskId(), notifyTask.getUserId(), notifyTask.getNotifyType(), e);
|
return null;
|
}
|
}
|
|
/**
|
* 批量创建通知任务
|
*/
|
@Override
|
public int createNotifyTasks(List<NotifyTask> notifyTasks) {
|
int successCount = 0;
|
for (NotifyTask notifyTask : notifyTasks) {
|
NotifyTask created = createNotifyTask(notifyTask);
|
if (created != null) {
|
successCount++;
|
}
|
}
|
return successCount;
|
}
|
|
/**
|
* 修改通知任务
|
*/
|
@Override
|
public int updateNotifyTask(NotifyTask notifyTask) {
|
return notifyTaskMapper.updateNotifyTask(notifyTask);
|
}
|
|
/**
|
* 更新通知任务状态为处理中
|
*/
|
@Override
|
public int markProcessing(Long id) {
|
return notifyTaskMapper.updateNotifyTaskStatus(id, NotifyTask.STATUS_PROCESSING, null);
|
}
|
|
/**
|
* 更新通知任务状态为完成
|
*/
|
@Override
|
public int markCompleted(Long id) {
|
return notifyTaskMapper.updateNotifyTaskStatus(id, NotifyTask.STATUS_COMPLETED, null);
|
}
|
|
/**
|
* 更新通知任务状态为失败
|
*/
|
@Override
|
public int markFailed(Long id, String errorMsg) {
|
return notifyTaskMapper.updateNotifyTaskStatus(id, NotifyTask.STATUS_FAILED, errorMsg);
|
}
|
|
/**
|
* 增加重试次数
|
*/
|
@Override
|
public int incrementRetryCount(Long id) {
|
return notifyTaskMapper.incrementRetryCount(id);
|
}
|
|
/**
|
* 删除通知任务
|
*/
|
@Override
|
public int deleteNotifyTaskById(Long id) {
|
return notifyTaskMapper.deleteNotifyTaskById(id);
|
}
|
|
/**
|
* 批量删除通知任务
|
*/
|
@Override
|
public int deleteNotifyTaskByIds(Long[] ids) {
|
return notifyTaskMapper.deleteNotifyTaskByIds(ids);
|
}
|
}
|