You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

32 lines
1.5 KiB

package com.mydreamjob.assessment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Optional;
@Repository
public interface AssessmentAttemptRepository extends JpaRepository<AssessmentAttempt, Long> {
Optional<AssessmentAttempt> findByStudentUserIdAndIdempotencyKey(Long studentUserId, String idempotencyKey);
boolean existsByStudentUserIdAndDefinitionIdAndStatusIn(
Long studentUserId, Long definitionId, Collection<String> statuses);
Page<AssessmentAttempt> findByStudentUserIdOrderByCreatedAtDesc(Long studentUserId, Pageable pageable);
/** 幂等置为已完成:仅 DRAFT/IN_PROGRESS → COMPLETED,并释放 active_key;已终态返回 0,供并发提交防护。 */
@Modifying
@Query("update AssessmentAttempt a set a.status = :status, a.submittedAt = :now, "
+ "a.completedAt = :now, a.answeredCount = :answered, a.activeKey = null "
+ "where a.id = :id and a.status in ('DRAFT','IN_PROGRESS')")
int markCompleted(@Param("id") Long id, @Param("status") String status,
@Param("now") LocalDateTime now, @Param("answered") int answered);
}