package com.ssj.dao.sys.homework.dao;

import com.ssj.bean.sys.homework.domain.HomeworkPictureCorrect;
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;

/**
 * @author Mount Liu
 * @date 2019/7/22
 */
@Repository
public interface HomeworkPictureCorrectDao extends JpaRepository<HomeworkPictureCorrect, String> {

    /**
     * 查找作业批改记录
     *
     * @param homeworkPictureId
     * @param type
     * @return
     */
    HomeworkPictureCorrect findTopByHomeworkPictureIdAndType(String homeworkPictureId, int type);

    /**
     * 查找作业批改记录
     *
     * @param homeworkPictureId
     * @param types
     * @return
     */
    HomeworkPictureCorrect findTopByHomeworkPictureIdAndTypeIn(String homeworkPictureId, List<Integer> types);

    /**
     * 查找作业批改记录
     *
     * @param homeworkPictureId
     * @param type
     * @param isCheckError
     * @return
     */
    HomeworkPictureCorrect findTopByHomeworkPictureIdAndTypeAndIsCheckError(String homeworkPictureId, int type, int isCheckError);


    /**
     * 重置批改开始和结束时间
     * @param homeworkPictureId
     * @param type
     */
    @Modifying
    @Query(value = "update scon_homework_picture_correct set start_time =null, end_time=null where homework_picture_id=?1 and type = ?2", nativeQuery = true)
    void updateCorrectTime(String homeworkPictureId, int type);

    /**
     * 重置批改开始和结束时间
     * @param id
     */
    @Modifying
    @Query(value = "update scon_homework_picture_correct set start_time =null and end_time=null where id=?1", nativeQuery = true)
    void updateCorrectTime(String id);

    /**
     * 重置回收站批改开始和结束时间
     * @param beginTime
     */
    @Modifying
    @Query(value = "update scon_homework_picture_correct\n" +
            "set start_time = null\n" +
            "where ((homework_picture_id in (\n" +
            "    select id\n" +
            "    from scon_homework_picture\n" +
            "    where 1 = 1\n" +
            "      and is_delete = 0\n" +
            "      and is_mark in (0,2)\n " +
            "      and create_time >= ?1\n" +
            "      and (teacher_id is null or teacher_id = '')\n" +
            ")\n" +
            "    and type = 8)\n" +
            "    or (\n" +
            "                   homework_picture_id in (\n" +
            "                   select id\n" +
            "                   from scon_homework_picture\n" +
            "                   where 1 = 1\n" +
            "                     and is_delete = 0\n" +
            "                     and is_mark in (0,2)\n" +
            "                     and create_time >= ?1\n" +
            "                     and teacher_id is not null\n" +
            "                     and (know_teacher_id is null or know_teacher_id = '')\n" +
            "                     and is_needing_know =1\n" +
            "                     and is_feedback = 1\n" +
            "               )\n" +
            "               and type = 13\n" +
            "           ))\n" +
            "  and start_time is not null\n" +
            "  and end_time is null", nativeQuery = true)
    void clearRecoveryCorrectionTime(String beginTime);


    @Modifying
    @Query(value = "UPDATE scon_homework_picture_correct\n" +
            "SET start_time = NULL\n" +
            "WHERE\n" +
            "	\n" +
            "			homework_picture_id IN (\n" +
            "				SELECT\n" +
            "					id\n" +
            "				FROM\n" +
            "					scon_homework_picture\n" +
            "				WHERE\n" +
            "					 is_delete = 0\n" +
            "				AND is_mark IN (0, 2)\n" +
            "				AND create_time >= ?1\n" +
            "				AND teacher_id IS NULL\n" +
            "					\n" +
            "      UNION \n" +
            "        SELECT\n" +
            "					id\n" +
            "				FROM\n" +
            "					scon_homework_picture\n" +
            "				WHERE\n" +
            "					 is_delete = 0\n" +
            "				AND is_mark IN (0, 2)\n" +
            "				AND create_time >= ?1\n" +
            "				AND  teacher_id = ''\n" +
            "				\n" +
            "			)\n" +
            "			AND type = 8\n" +
            "	\n" +
            "AND start_time IS NOT NULL\n" +
            "AND end_time IS NULL", nativeQuery = true)
    void clearRecoveryCorrectionTimeV8(String beginTime);


    @Modifying
    @Query(value = "UPDATE scon_homework_picture_correct\n" +
            "SET start_time = NULL\n" +
            "WHERE\n" +
            "	\n" +
            "			homework_picture_id IN (\n" +
            "				SELECT\n" +
            "					id\n" +
            "				FROM\n" +
            "					scon_homework_picture\n" +
            "				WHERE\n" +
            "					 is_delete = 0\n" +
            "				AND is_mark IN (0, 2)\n" +
            "				AND create_time >= ?1\n" +
            "				AND know_teacher_id IS NULL\n" +
            "					\n" +
            "      UNION \n" +
            "        SELECT\n" +
            "					id\n" +
            "				FROM\n" +
            "					scon_homework_picture\n" +
            "				WHERE\n" +
            "					 is_delete = 0\n" +
            "				AND is_mark IN (0, 2)\n" +
            "				AND create_time >= ?1\n" +
            "				AND  know_teacher_id = ''\n" +
            "				\n" +
            "			)\n" +
            "			AND type = 13\n" +
            "	\n" +
            "AND start_time IS NOT NULL\n" +
            "AND end_time IS NULL", nativeQuery = true)
    void clearRecoveryCorrectionTimeV13(String beginTime);


    @Query(nativeQuery = true,value = "select * from scon_homework_picture_correct where type=?1 and homework_picture_id in?2")
    List<HomeworkPictureCorrect> findByTypeAndHomeworkPictureIdIn(int type,List<String>list);

    @Query(nativeQuery = true,value = "select * from scon_homework_picture_correct where homework_id=?1")
    List<HomeworkPictureCorrect> findByHomeworkId(String homeworkId);

    @Query(nativeQuery = true, value = "select t1.*\n" +
            "from scon_homework_picture_correct t1\n" +
            "where t1.homework_id = ?1\n" +
            "  and t1.type = 8\n" +
            "  and exists(\n" +
            "        select 1 from scon_homework_picture t2 where t1.homework_picture_id = t2.id and t2.teacher_id = ?2\n" +
            "    )")
    List<HomeworkPictureCorrect> findByHomeworkIdAndCorrectTeacherIdType(String homeworkId, String teacherId);

    @Query(nativeQuery = true, value = "select t1.*\n" +
            "from scon_homework_picture_correct t1\n" +
            "where t1.homework_id = ?1\n" +
            "  and t1.type = 13\n" +
            "  and exists(\n" +
            "        select 1 from scon_homework_picture t2 where t1.homework_picture_id = t2.id and t2.know_teacher_id = ?2\n" +
            "    )")
    List<HomeworkPictureCorrect> findByHomeworkIdAndKnowTeacherIdType(String homeworkId, String teacherId);
}
