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

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.ssj.bean.kmt.kmt.domain.KmtExerciseBookCourse;

@Repository
public interface KmtExerciseBookCourseDao extends JpaRepository<KmtExerciseBookCourse, String> {

    /**
     * 查询出一条练习册的记录，获取练习册的名称
     * @param BookId
     * @return
     */
    @Query(nativeQuery = true, value = "select * from kmt_exercise_book_course where book_id=?1 limit 1")
    public KmtExerciseBookCourse findOneByBookId (String BookId);

    /**
     * 查询审核员未分配的练习册
     * @return
     */
    @Query(nativeQuery = true, value = "select t1.id, t1.book_id, t1.course_name, t1.version, t1.grade, t1.analysis_user_id, " +
            " t1.semester, t1.book_name, t1.unit, t1.period, t1.create_time, t1.remark, t1.unit_id, t1.period_sort, " +
            " t1.unit_sort, t1.auditing_user_id, t1.handle_user_id from kmt_exercise_book_course t1 where t1.auditing_user_id is null " +
            " and t1.course_name = ?1 and t1.grade=?2 and t1.version=?3 and t1.semester=?4 order by t1.unit_sort, t1.period_sort limit 0, ?5")
    public List<KmtExerciseBookCourse> findUnAuditingBook (String subject, String grade, int version, int semester, int rows);

    /**
     * 查询加工员未分配的练习册
     * @return
     */
    @Query(nativeQuery = true, value = "select t1.id, t1.book_id, t1.course_name, t1.version, t1.grade, t1.analysis_user_id, " +
            " t1.semester, t1.book_name, t1.unit, t1.period, t1.create_time, t1.remark, t1.unit_id, t1.period_sort, " +
            " t1.unit_sort, t1.auditing_user_id, t1.handle_user_id from kmt_exercise_book_course t1 where t1.handle_user_id is null " +
            " and t1.course_name = ?1 and t1.grade=?2 and t1.version=?3 and t1.semester=?4 order by t1.unit_sort, t1.period_sort limit 0, ?5")
    public List<KmtExerciseBookCourse> findUnHandlingBook (String subject, String grade, int version, int semester, int rows);

    /**
     * 查询用户待加工的练习册列表
     * @param userId
     * @return
     */
    @Query(nativeQuery = true, value = "select distinct t1.id, t1.book_id, t1.course_name, t1.version, t1.grade, " +
            " t1.analysis_user_id, t1.semester, t1.book_name, t1.unit, t1.period, t1.create_time, t1.remark, t1.unit_id, " +
            " t1.period_sort, t1.unit_sort, t1.auditing_user_id, t1.handle_user_id from kmt_exercise_book_course t1 left join kmt_exercise_questions t2" +
            " on t1.id = t2.book_course_id where t1.handle_user_id=?1 and t2.deal_status < 2 order by t1.unit_sort, t1.period_sort limit 0, ?2")
    public List<KmtExerciseBookCourse> findHandBooksByStatusAndUserId (String userId,  int rows);


    /**
     * 查询用户待审核的练习册列表
     * @param userId
     * @return
     */
    @Query(nativeQuery = true, value = "select distinct t1.id, t1.book_id, t1.course_name, t1.version, t1.grade, t1.analysis_user_id, " +
            " t1.semester, t1.book_name, t1.unit, t1.period, t1.create_time, t1.remark, t1.unit_id, t1.period_sort, t1.unit_sort, " +
            " t1.auditing_user_id, t1.handle_user_id  from kmt_exercise_book_course t1 left join kmt_exercise_questions t2" +
            " on t1.id = t2.book_course_id where t1.auditing_user_id=?1 and t2.deal_status=3 order by t1.unit_sort, t1.period_sort limit 0, ?2")
    public List<KmtExerciseBookCourse> findAudeBooksByStatusAndUserId (String userId, int rows);

    /**
     * 查询单元课时列表
     * @param unitId
     * @return
     */
    public List<KmtExerciseBookCourse> findByUnitId (String unitId);

    /**
     * 查询
     * @param bookId
     * @return
     */
    public List<KmtExerciseBookCourse> findByBookId (String bookId);

    /**
     * 根据题目查询科目
     * @param questionId
     * @return
     */
    @Query(nativeQuery = true, value = "select eq.course_name from kmt_exercise_questions eq left join kmt_exercise_book_course ebc on eq.book_course_id=ebc.id where eq.id=?1")
    public String findCourseNameByQuestionId (String questionId);

    @Query(nativeQuery = true, value = "select eb.id book_id,  ebc.book_name, ebc.unit_id, ebc.unit, ebc.id,  ebc.period, ebc.grade," +
            " ebc.course_name, ebc.version, ebc.semester, eb.part_flag remark from kmt_exercise_book_course ebc" +
            " left join kmt_exercise_book eb on ebc.book_id=eb.book_id  where eb.id in(?1) order by eb.id,ebc.unit_sort,ebc.period_sort limit 5000")
    List<KmtExerciseBookCourse> findByBookIdIn(List<String> bookIds);
}
