package com.qmth.qrzk.dao.repositories;

import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.qmth.qrzk.supplypaper.model.SupplyCourse;

/**
 * 供卷课程
 * @author zhengmin
 *
 */
public interface SupplyCourseRepo extends JpaRepository<SupplyCourse, Long> {

	/**
	 * 通过批次id查找供卷课程
	 * @param batchId
	 * @return
	 */
	List<SupplyCourse> findByBatchId(Integer batchId,Sort sort);
	/**
	 * 查找指定批次和课程代码的供卷课程
	 * @param batchId
	 * @param courseCode
	 * @return
	 */
	List<SupplyCourse> findByBatchIdAndCourseCode(Integer batchId,String courseCode);
	
	/**
	 * 根据批次和试卷id查找
	 * @param batchId
	 * @param paperId
	 * @return
	 */
	List<SupplyCourse> findByBatchIdAndPaperId(Integer batchId,Long paperId);
	
	/**
	 * 根据批次和试卷查询数量
	 * @param batchId
	 * @param paperId
	 * @return
	 */
	int countByBatchIdAndPaperId(int batchId,Long paperId);
	
	/**
	 * 根据批次和试卷状态查询数量
	 * @param batchId
	 * @param status
	 * @return
	 */
	@Query(value="SELECT COUNT(*) FROM qr_supply_course sc LEFT OUTER JOIN qr_paper p ON p.id = sc.paper_id where " +
			"sc.batch_id = ?1 AND p.status = ?2",nativeQuery=true)
	int countByBatchIdAndPaperStatus(int batchId,String status);
	
	@Modifying
	@Query("delete from SupplyCourse where batchId = ?1")
	void deleteFromBatch(Long batchId);
	
	/**
	 * 根据试卷id查找
	 * @param paperId
	 * @return
	 */
	List<SupplyCourse> findByPaperId(Long paperId);
	
}
