package com.lanyuanxiaoyao.leopard.core.repository;

import com.lanyuanxiaoyao.leopard.core.entity.Task;
import com.lanyuanxiaoyao.service.template.repository.SimpleRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lanyuanxiaoyao
 * @version 20250829
 */
@Repository
public interface TaskRepository extends SimpleRepository<Task> {
  @Modifying
  @Query("""
    update Task task set
      task.status = com.lanyuanxiaoyao.leopard.core.entity.Task.Status.FAILURE,
      task.finishedTime = current timestamp
    where task.status = com.lanyuanxiaoyao.leopard.core.entity.Task.Status.RUNNING""")
  void updateAllRunningTaskToFailure();

  @Transactional(rollbackFor = Throwable.class)
  @Modifying
  @Query("update Task task set task.step = ?2 where task.id = ?1")
  void updateStepById(Long id, Double step);
}
