package com.ssj.dao.kmt.kmt.dao;

import com.ssj.bean.kmt.kmt.domain.KmtQuestionTemp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface KmtQuestionTempDao extends JpaRepository<KmtQuestionTemp, String> {
	
	@Query(nativeQuery = true, value = "  SELECT * FROM `kmt_question_temp` t where t.course_name =?3 AND t.adduser=?4 and   (t.state=?5 or t.state is NULL) ORDER BY t.create_time ASC  LIMIT ?1,?2 ")
	List<KmtQuestionTemp> findQuList(Integer page, Integer pageSize, String courseName, String userName, Integer state);
	
	@Query(nativeQuery = true, value = "  SELECT * FROM `kmt_question_temp` t where t.course_name =?3   AND (t.state=?4 or t.state is NULL) ORDER BY t.create_time ASC   LIMIT ?1,?2 ")
	List<KmtQuestionTemp> findQuList(Integer page, Integer pageSize, String courseName, Integer state);
	
	
	@Query(nativeQuery = true, value = "  SELECT * FROM `kmt_question_temp` t where  t.course_name =?1 AND t.adduser is null  AND (t.state=1 or t.state is NULL)  ")
	List<KmtQuestionTemp> findQuList(String courseName);
	
	@Query(nativeQuery = true, value = "  SELECT * FROM `kmt_question_temp` t where  t.state=?1  ORDER BY t.create_time ASC ")
	List<KmtQuestionTemp> findQuByStateList(Integer state);
	
	

	@Query(nativeQuery = true, value = "UPDATE  `kmt_question_temp` t SET t.adduser =?2 where t.id in(  	SELECT * from ( SELECT id FROM `kmt_question_temp` p where p.adduser IS NULL AND p.course_name=?1 AND (p.state=1 or p.state is NULL) LIMIT 0,?3 ) as tmp ) ")
	@Modifying
	void updateTempUser(String courseName, String username, Integer num);


}
