|
@@ -100,7 +100,7 @@ public class TaskService implements TaskServiceInterface {
|
|
|
|
|
|
|
|
// ==================== SC-P-10 创建任务校验(S-cfc-12) ====================
|
|
// ==================== SC-P-10 创建任务校验(S-cfc-12) ====================
|
|
|
private static final Set<String> VALID_FREQUENCIES = Collections.unmodifiableSet(
|
|
private static final Set<String> VALID_FREQUENCIES = Collections.unmodifiableSet(
|
|
|
- new HashSet<>(Arrays.asList("none", "daily", "weekly", "monthly", "every_other_day", "hourly")));
|
|
|
|
|
|
|
+ new HashSet<>(Arrays.asList("none", "daily", "weekly", "monthly", "every_other_day", "hourly", "custom")));
|
|
|
/** 幂等窗口:同一创建请求在 30 秒内重复提交仅生成 1 条任务(重复提交不重复发分) */
|
|
/** 幂等窗口:同一创建请求在 30 秒内重复提交仅生成 1 条任务(重复提交不重复发分) */
|
|
|
private static final long TASK_DEDUP_WINDOW_MS = 30_000L;
|
|
private static final long TASK_DEDUP_WINDOW_MS = 30_000L;
|
|
|
/** 按任务去重键持有的 JVM 内锁,保证并发双发时 check-then-insert 原子性 */
|
|
/** 按任务去重键持有的 JVM 内锁,保证并发双发时 check-then-insert 原子性 */
|
|
@@ -345,6 +345,13 @@ public class TaskService implements TaskServiceInterface {
|
|
|
task.setFrequency(dto.getFrequency());
|
|
task.setFrequency(dto.getFrequency());
|
|
|
task.setMaxFrequency(dto.getMaxFrequency());
|
|
task.setMaxFrequency(dto.getMaxFrequency());
|
|
|
|
|
|
|
|
|
|
+ // SC-P-12 task.frequency.config: 自定义周期(frequency=custom)周期单位/间隔/锚点
|
|
|
|
|
+ // 周期单位+间隔按 API_REFERENCE §4.3 以 actionType + actionConfig 声明,
|
|
|
|
|
+ // 此处解析后持久化到 Task 实体,供 RepeatTaskGenerator 按周期锚点推进生成实例。
|
|
|
|
|
+ if ("custom".equals(dto.getFrequency())) {
|
|
|
|
|
+ applyCustomFrequencyPeriod(task, dto);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 完成方式(转为JSON存储)
|
|
// 完成方式(转为JSON存储)
|
|
|
if (dto.getCompleteTypes() != null && !dto.getCompleteTypes().isEmpty()) {
|
|
if (dto.getCompleteTypes() != null && !dto.getCompleteTypes().isEmpty()) {
|
|
|
task.setCompleteTypes(String.join(",", dto.getCompleteTypes()));
|
|
task.setCompleteTypes(String.join(",", dto.getCompleteTypes()));
|
|
@@ -358,7 +365,9 @@ public class TaskService implements TaskServiceInterface {
|
|
|
task.setLatestEnd(dto.getLatestEnd());
|
|
task.setLatestEnd(dto.getLatestEnd());
|
|
|
|
|
|
|
|
// 如果是重复任务,标记为模板
|
|
// 如果是重复任务,标记为模板
|
|
|
- if (!"none".equals(task.getRepeatType())) {
|
|
|
|
|
|
|
+ // SC-P-12 task.frequency.config: frequency=custom 同样是周期任务,须标记为模板,
|
|
|
|
|
+ // 否则 RepeatTaskGenerator(仅处理 is_template=1)永远不会为其生成实例。
|
|
|
|
|
+ if (!"none".equals(task.getRepeatType()) || "custom".equals(task.getFrequency())) {
|
|
|
task.setIsTemplate(1); // 模板任务
|
|
task.setIsTemplate(1); // 模板任务
|
|
|
} else {
|
|
} else {
|
|
|
task.setIsTemplate(0);
|
|
task.setIsTemplate(0);
|
|
@@ -436,6 +445,54 @@ task.setReviewByCategory(dto.getReviewByCategory() != null ? dto.getReviewByCate
|
|
|
return member != null ? member.getId() : null;
|
|
return member != null ? member.getId() : null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * SC-P-12 task.frequency.config: 解析 actionConfig 中自定义周期配置并写回 Task。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>actionConfig JSON 字段约定(API_REFERENCE §4.3 任务配置声明):
|
|
|
|
|
+ * {@code periodUnit} 周期单位(DAY/WEEK/MONTH,默认 DAY),{@code periodInterval} 间隔
|
|
|
|
|
+ * (每 N 个周期单位生成一次实例,默认 1)。锚点 {@code cycleAnchor} 优先取 actionConfig,
|
|
|
|
|
+ * 缺省取模板截止时间或创建时间,保证 RepeatTaskGenerator 能按锚点逐周期推进。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ private void applyCustomFrequencyPeriod(Task task, CreateTaskDTO dto) {
|
|
|
|
|
+ String periodUnit = "DAY";
|
|
|
|
|
+ int periodInterval = 1;
|
|
|
|
|
+ JSONObject actionConfig = null;
|
|
|
|
|
+ if (dto.getActionConfig() != null && !dto.getActionConfig().trim().isEmpty()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ actionConfig = JSON.parseObject(dto.getActionConfig());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("SC-P-12 自定义周期 actionConfig 解析失败,按默认 DAY/1 处理: taskId={}", task.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (actionConfig != null) {
|
|
|
|
|
+ String configuredUnit = actionConfig.getString("periodUnit");
|
|
|
|
|
+ if (configuredUnit != null && !configuredUnit.trim().isEmpty()) {
|
|
|
|
|
+ periodUnit = configuredUnit.trim().toUpperCase(Locale.ROOT);
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer configuredInterval = actionConfig.getInteger("periodInterval");
|
|
|
|
|
+ if (configuredInterval != null && configuredInterval > 0) {
|
|
|
|
|
+ periodInterval = configuredInterval;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ task.setFrequencyUnit(periodUnit);
|
|
|
|
|
+ task.setFrequencyInterval(periodInterval);
|
|
|
|
|
+
|
|
|
|
|
+ Date anchor = null;
|
|
|
|
|
+ if (actionConfig != null && actionConfig.getDate("cycleAnchor") != null) {
|
|
|
|
|
+ anchor = actionConfig.getDate("cycleAnchor");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (anchor == null && task.getDeadline() != null) {
|
|
|
|
|
+ anchor = task.getDeadline();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (anchor == null && task.getCreatedAt() != null) {
|
|
|
|
|
+ anchor = task.getCreatedAt();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (anchor == null) {
|
|
|
|
|
+ anchor = new Date();
|
|
|
|
|
+ }
|
|
|
|
|
+ task.setCycleAnchor(anchor);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 实现接口:获取今日任务列表(按memberId,不过滤维度)
|
|
* 实现接口:获取今日任务列表(按memberId,不过滤维度)
|
|
|
*/
|
|
*/
|
|
@@ -1068,6 +1125,57 @@ public List<Task> getTodayTasks(Long memberId, String dimensionCode) {
|
|
|
.set(Task::getUpdatedAt, new Date()));
|
|
.set(Task::getUpdatedAt, new Date()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * SC-P-12 task.frequency.config: 暂停重复任务模板(暂停后 RepeatTaskGenerator 不再生成实例)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>仅对 {@code is_template=1} 的周期任务有效;重复调用幂等返回 true。
|
|
|
|
|
+ * 非模板任务/任务不存在返回 false,避免误改一次性任务状态。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public boolean pauseTask(Long taskId, Long userId) {
|
|
|
|
|
+ Task task = taskMapper.selectById(taskId);
|
|
|
|
|
+ if (task == null || !task.getCreatorId().equals(userId)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (task.getIsTemplate() == null || task.getIsTemplate() != 1) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("paused".equals(task.getStatus())) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ task.setStatus("paused");
|
|
|
|
|
+ task.setIsPaused(1);
|
|
|
|
|
+ task.setUpdatedAt(new Date());
|
|
|
|
|
+ taskMapper.updateById(task);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * SC-P-12 task.frequency.config: 恢复暂停的重复任务模板(按原周期继续生成实例)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>恢复后状态回到 {@code pending},{@code is_paused=0},RepeatTaskGenerator 下一轮
|
|
|
|
|
+ * 按周期锚点继续推进生成。重复调用幂等返回 true。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public boolean resumeTask(Long taskId, Long userId) {
|
|
|
|
|
+ Task task = taskMapper.selectById(taskId);
|
|
|
|
|
+ if (task == null || !task.getCreatorId().equals(userId)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (task.getIsTemplate() == null || task.getIsTemplate() != 1) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("paused".equals(task.getStatus()) || Integer.valueOf(1).equals(task.getIsPaused())) {
|
|
|
|
|
+ task.setStatus("pending");
|
|
|
|
|
+ task.setIsPaused(0);
|
|
|
|
|
+ task.setUpdatedAt(new Date());
|
|
|
|
|
+ taskMapper.updateById(task);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 非暂停态:幂等返回 true(不改变状态)
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private Map<String, Object> submitTaskForReview(Task task, Long memberId, Date now, String content, String contentType) {
|
|
private Map<String, Object> submitTaskForReview(Task task, Long memberId, Date now, String content, String contentType) {
|
|
|
task.setStatus("pending_review");
|
|
task.setStatus("pending_review");
|
|
|
task.setCompletedAt(now);
|
|
task.setCompletedAt(now);
|