package cn.com.qmth.stmms.biz.mark.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import cn.com.qmth.stmms.biz.mark.model.TrialTag;

public interface TrialTagDao extends PagingAndSortingRepository<TrialTag, Integer>, JpaSpecificationExecutor<TrialTag> {

    @Modifying
    @Query("delete from TrialTag s where s.libraryId = ?1")
    public void deleteByLibraryId(Integer libraryId);

    @Modifying
    @Query("delete from TrialTag s where s.libraryId = ?1 and s.markerId=?2")
    public void deleteByLibraryIdAndMarkerId(Integer libraryId, Integer markerId);

    @Modifying
    @Query("delete from TrialTag s where exists "
            + "(select l.id from TrialLibrary l where l.studentId=?1 and s.libraryId=l.id)")
    public void deleteByStudentId(Integer studentId);

    public List<TrialTag> findByLibraryIdOrderByIdAsc(Integer libraryId);

    @Modifying
    @Query("delete from TrialTag s where s.markerId=?1")
    public void deleteByMarkerId(Integer markerId);

    @Modifying
    @Query("delete from TrialTag s where s.libraryId in (select m.id from TrialLibrary m where m.examId=?1 and m.subjectCode=?2 and m.groupNumber=?3)")
    public void deleteByExamAndSubjectAndGroup(Integer examId, String subjectCode, Integer groupNumber);

    @Query("select s from TrialTag s where s.libraryId in "
            + "(select l.id from TrialLibrary l where l.studentId=?1 and s.libraryId=l.id)")
    public List<TrialTag> findByStudentIdOrderByIdAsc(Integer studentId);

}
