package cn.com.qmth.stmms.biz.exam.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.exam.model.ExamPackage;
import cn.com.qmth.stmms.biz.exam.model.ExamPackagePK;

public interface ExamPackageDao
        extends PagingAndSortingRepository<ExamPackage, ExamPackagePK>, JpaSpecificationExecutor<ExamPackage> {

    @Query("select s from ExamPackage s where s.pk.examId=?1")
    public List<ExamPackage> findByExamId(int examId);

    @Query("select count(s) from ExamPackage s where s.pk.examId=?1")
    public long countByExamId(int examId);

    @Query("select count(s) from ExamPackage s where s.pk.examId=?1 and s.picCount>0")
    public long countByExamIdAndUpload(int examId);

    @Query("select count(s) from ExamPackage s where s.pk.examId=?1 and s.picCount=0")
    public long countByExamIdAndUnUpload(int examId);

    @Query("select s from ExamPackage s where s.pk.examId=?1 and s.pk.code=?2")
    public ExamPackage findByExamIdAndCode(int examId, String code);

    @Query("select s from ExamPackage s where s.pk.examId=?1 and s.picCount>0")
    public List<ExamPackage> findByExamIdAndUpload(int examId);

    @Query("select s from ExamPackage s where s.pk.examId=?1 and s.picCount=0")
    public List<ExamPackage> findByExamIdAndUnUpload(int examId);

    @Modifying
    @Query("delete ExamPackage s where s.pk.examId=?1")
    void deleteByExamId(int examId);

    @Query("select COALESCE(sum(s.picCount),0) from ExamPackage s where s.pk.examId=?1")
    public long sumByExamId(int examId);
}
