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

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

    KmtExerciseBook findTopByBookNameAndGradeAndCourseNameAndSemesterAndVersionAndBookYearAndBookType(String bookName, String grade, String courseName, int semester, int version, int bookYear, int bookType);

    @Modifying
    @Query(nativeQuery = true, value = "update kmt_exercise_book set answer_correcter_id = ?1 where id = ?2")
    void updateAnswerCorrecterIdById(String teacherId, String exerciseBookId);

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

    @Modifying(flushAutomatically = true)
    @Query(nativeQuery = true, value = "update kmt_exercise_book set handle_user_id = ?1 where id in (?2)")
    void updateHandleUserIdByIds(String teacherId, List<String> exerciseBookIds);

    @Modifying(flushAutomatically = true)
    @Query(nativeQuery = true, value = "update kmt_exercise_book set auditing_user_id = ?1 where id in (?2)")
    void updateAuditingUserId(String teacherId, List<String> exerciseBookIds);

    @Modifying(flushAutomatically = true)
    @Query(nativeQuery = true, value = "update kmt_exercise_book set analysis_user_id = ?1 where id in (?2)")
    void updateAnalysisUserId(String teacherId, List<String> exerciseBookIds);

    @Modifying(flushAutomatically = true)
    @Query(nativeQuery = true, value = "update kmt_exercise_book set analysis_auditer_id = ?1 where id in (?2)")
    void updateAnalysisAuditerId(String teacherId, List<String> exerciseBookIds);

    List<KmtExerciseBook> findByVersionNameIsNull();

    List<KmtExerciseBook> findByCourseName(String courseName);

    List<KmtExerciseBook> findByCourseNameAndVersion(String courseName, int version);

    List<KmtExerciseBook> findByLibNameIsNotNull();

    /**
     * 更新kmt_exercise_book.status
     *
     * @param exerciseBookId 练习册id
     * @param status status更新为？
     */
    @Modifying(flushAutomatically = true)
    @Query(nativeQuery = true, value = "update kmt_exercise_book set `status` = ?2 where id = ?1 ")
    void updateExerciseBookStatus(String exerciseBookId, String status);
}
