package cn.com.qmth.examcloud.tool.dao;

import cn.com.qmth.examcloud.tool.entity.TaskEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
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;

@Repository
public interface TaskRepository extends JpaRepository<TaskEntity, Long>, JpaSpecificationExecutor<TaskEntity> {

    @Query(value = "select id from t_task where status='WAITING' order by id limit 1", nativeQuery = true)
    Long getFirstWaitingTaskId();

    @Transactional
    @Modifying
    @Query(value = "update t_task set status='ERROR' where status='RUNNING'", nativeQuery = true)
    int clearExpiredTaskStatus();

}
