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

import com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion;
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 KmtExerciseQuestionDao extends JpaRepository<KmtExerciseQuestion, String> {

    /**
     * 根据课时id查询题目
     * @param periodId 课时id
     */
    List<KmtExerciseQuestion> findByBookCourseIdOrderByNumAscCreateTimeAsc(String periodId);

    /**
     * 根据单元id查询题目
     * @param unitId 单元id
     */
    List<KmtExerciseQuestion> findByUnitId(String unitId);

    /**
     * 根据练习册id查询题目
     * @param bookId 练习册id
     */
    List<KmtExerciseQuestion> findByBookId(String bookId);

    /**
     * 根据题目id集合查询题目
     */
    List<KmtExerciseQuestion> findByIdIn(List<String> wrongQuestionIdList);

    /**
     * 查询未发布的数量
     * @param bookId
     * @param pageIndexs
     * @return
     */
    @Query(nativeQuery = true, value = "select count(1) from kmt_exercise_questions where book_id =?1 and page_index in (?2) and deal_status != 4")
    int countByBookIdAndPageIndexIn(String bookId, List<Integer> pageIndexs);

    /**
     * 查询未发布的数量
     * @param bookCourseId
     * @return
     */
    @Query(nativeQuery = true, value = "select count(1) from kmt_exercise_questions where book_course_id=?1 and deal_status != 4")
    int countByBookCourseId(String bookCourseId);

    /**
     * 查询页码的单元数量
     * @param bookId
     * @param pageIndex
     * @return
     */
    @Query(nativeQuery = true, value = "select count(distinct  unit_id) from kmt_exercise_questions where book_id=?1 and page_index=?2")
    int countByBookIdAndPageIndex(String bookId, int pageIndex);


    @Query(nativeQuery = true, value = " select book_year from kmt_exercise_book where id=?1 ")
    Integer getBookYear(String bookId);

    @Modifying
    @Query(nativeQuery = true, value = "update kmt_exercise_questions set answer_correcter_id = ?1 where exercise_book_id = ?2")
    void updateAnswerCorrecterIdByExerciseBookId(String teacherId, String exerciseBookId);

    @Modifying
    @Query(nativeQuery = true, value = "update kmt_exercise_questions set answer_correcter_id = ?1 where exercise_book_id in (?2)")
    void updateAnswerCorrecterIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    @Modifying
    @Query(nativeQuery = true, value = "update kmt_exercise_questions set handle_user_id = ?1 where exercise_book_id in (?2)")
    void updateHandleUserIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    @Modifying
    @Query(nativeQuery = true, value = "update kmt_exercise_questions set auditing_user_id = ?1 where exercise_book_id in (?2)")
    void updateAuditingUserIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    @Modifying
    @Query(nativeQuery = true, value = "update kmt_exercise_questions set analysis_user_id = ?1 where exercise_book_id in (?2)")
    void updateAnalysisUserIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    @Modifying
    @Query(nativeQuery = true, value = "update kmt_exercise_questions set analysis_auditer_id = ?1 where exercise_book_id in (?2)")
    void updateAnalysisAuditerIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);
}