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 { Optional findByStudentUserIdAndIdempotencyKey(Long studentUserId, String idempotencyKey); boolean existsByStudentUserIdAndDefinitionIdAndStatusIn( Long studentUserId, Long definitionId, Collection statuses); Page 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); }