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

import com.ssj.bean.sys.homework.domain.HomeworkPicture;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
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.Date;
import java.util.List;
import java.util.Optional;


/**
 * 类名称：HomeworkPictureDao<br>
 * 类描述 作业数据访问接口<br>
 * 
 * @version v1.0
 */
@Repository
public interface HomeworkPictureDao extends JpaRepository<HomeworkPicture, String> {

    @Modifying
    @Query(value = "update scon_homework_picture son set son.is_delete=1, son.is_feedback=1, son.is_mark=4, son.is_needing_know=0 where son.id = ?1", nativeQuery = true)
    public void updatePictureAsDelete(String homeworkId);

    public Page<HomeworkPicture> findAll(Specification<HomeworkPicture> specification, Pageable page);

    public List<HomeworkPicture> findByisDeleteAndParentIdInAndIsFeedbackOrderByCreateTimeAsc(int isDelete, List<String> parentId,
                                                                          int isFeedback);
    
    public List<HomeworkPicture> findByisDeleteAndParentIdInAndIsFeedbackAndCreateTimeAfterOrderByCreateTimeAsc(int isDelete, List<String> parentId,
            int isFeedback,Date createTime);
    
    @Modifying
    @Query(value ="update scon_homework_picture son set son.is_feedback=1 where son.id = ?1", nativeQuery = true)
    public void updateByIsFeedback(String id);

    @Modifying
    @Query(value ="delete from  scon_homework_picture  where zyb_work_id = ?1 and derver='1' ", nativeQuery = true)
	public void delZybWorkPic(String id);
    
    public long countByTeacherIdAndIsFeedback(String teacherId,int isFeedback);

    @Query(nativeQuery = true, value = " SELECT * FROM `scon_homework_picture` t where t.homework_id=?1 AND t.is_delete=0 ORDER BY t.create_time ASC ")
    public List<HomeworkPicture> queryHomeworkPictureByHomeworkId(String homeworkId);

    @Query(nativeQuery = true, value = " SELECT * FROM `scon_homework_picture` t where t.derver=?1 AND to_days(create_time) = to_days(now()) ")
    public List<HomeworkPicture> findTodayByDerver(String derver);

    @Query(nativeQuery = true, value = " SELECT * FROM `scon_homework_picture` t where t.derver=?1 AND t.subject=?2 AND to_days(create_time) = to_days(now()) ")
    public List<HomeworkPicture> findTodayByDerverAndSubject(String derver, String subject);

    @Query(nativeQuery = true, value = " select t.* from scon_homework_picture t where to_days(create_time) = to_days(now()) " +
            "and t.vip_id=?1 and is_delete=0 and is_mark in (0, 2) ")
    public List<HomeworkPicture> findTodayByVipId(String vipId);

    @Query(nativeQuery = true, value = " select t.* from scon_homework_picture t where to_days(create_time) = to_days(now()) " +
            "and t.vip_id=?1 and t.subject=?2 and t.is_delete = 0 and is_mark in (0, 2)")
    public List<HomeworkPicture> findTodayByVipIdAndSubject(String vipId, String subject);

    @Query(nativeQuery = true, value = "select t.* from scon_homework_picture t where vip_id = ?1 and subject = ?2 and create_time between  ?3 and ?4")
    public List<HomeworkPicture> findByVipIdAndSubject(String vipId, String subject, String beginTime, String endTime);

    @Query(nativeQuery = true, value = "SELECT\n" +
    		"	t3.*\n" +
    		"FROM	\n" +
    		"scon_homework_picture t3		 \n" +
    		"LEFT JOIN tb_lib_vip_service t1 ON t1.vip_id = t3.vip_id\n" +
    		"WHERE\n" +
    		"	t3.is_feedback =?1\n" +
    		"AND t1.lib_id IN (?2)\n" +
    		"AND t1.species ='S003' AND t1.stat =1\n" +
    		"AND TO_DAYS(t3.create_time) = TO_DAYS(NOW())\n" +
    		"ORDER BY\n" +
    		"	t3.create_time")
    public List<HomeworkPicture> findByIsFeedbackAndLibIdInAndCreateTimeAfterOrderByCreateTimeAsc(int isFeedback, List<String> libIds);

    @Query(nativeQuery = true, value = "select t3.* from scon_homework_picture t3 left join tb_lib_vip_service t1 " +
            "on t1.vip_id=t3.vip_id and t1.lib_id in (?2)  where t3.is_feedback=?1 AND t1.species ='S003' AND t1.stat =1  and TO_DAYS(t3.create_time)  = TO_DAYS(NOW())")
    public List<HomeworkPicture> findTodayNotFeebackHomeworkByMaster(int isFeedback, List<String> libIds);

    public HomeworkPicture findTopByHomeworkIdAndSubject(String homeworkId,String subject);

    @Query(nativeQuery = true, value = "select sp.* from scon_homework_picture sp left join tb_lib_manager tm on sp.teacher_id = tm.user_id and tm.type=8 \n" +
            "where sp.vip_id=?1 and sp.subject=?2 and  to_days(sp.create_time) = to_days(now()) and tm.is_online=1 and tm.is_banned=0 order by sp.allot_time desc limit 1")
    public HomeworkPicture findTop(String vipId, String subject);

    public HomeworkPicture findFirstByTeacherIdAndVipIdOrderByCreateTimeDesc(String teacherId, String vipId);

    @Modifying
    @Query(value ="update scon_homework_picture set zyb_work_stat = 2  where zyb_work_id = ?1", nativeQuery = true)
	public void updateStat(String zybworkId);

    @Modifying
    @Query(value ="update scon_homework_picture set teacher_id =null  where is_delete=0 and is_feedback=0 and service_type=1 and is_mark in(0,2) and create_time>=? and create_time<?",nativeQuery = true)
    public void updateTeacherId(String beginTime,String endTime);

    @Query(nativeQuery = true,value ="select zyb_work_id from scon_homework_picture where derver = ?1 and is_delete = 0 and is_feedback = 1 and `subject` = ?2 " + 
    		"and create_time >= date_format(?3,'%Y-%m-%d 15:00:00') and create_time <= date_format(?4,'%Y-%m-%d 15:00:00') GROUP BY zyb_work_id")
    public List<String> findZybWorkIdByHomeworkRecord(String derver, String subject, String today, String tomorrow);

    @Query(nativeQuery = true,value ="select * from scon_homework_picture  where subject=?1 and create_time > ?2 and create_time < ?3" +
            " and vip_id = ?4 order by create_time")
    public List<HomeworkPicture> findSortById (String subject, String beginTime, String endTime, String vipId);

    public long countByFileKey(String fileKey);

    public long countByDerverFileKey(String derverFileKey);

    public HomeworkPicture findByDerverFileKeyOrderByCreateTimeDesc(String derverFileKey);

    @Query(nativeQuery = true, value = "select count(1) from  scon_homework_picture  where zyb_work_id = ?1 and zyb_work_stat = 2")
	public int isSubHomeWorkPic(String zybWorkId);

    @Query(nativeQuery = true, value = "select count(1) from  scon_homework_picture  where vip_id = ?1 and TO_DAYS(CREATE_TIME) = TO_DAYS(NOW())")
    public int countByVipIdOfToday(String vipId);

    @Query(nativeQuery = true, value = "select count(*) from scon_homework_picture where teacher_id=?1 and create_time between ?2 and ?3 and is_feedback=0 and is_mark not in (1, 3)")
    public int countByTeacherIdNotFeedback(String teacherId, String beginDateTimeStr, String endDateTimeStr);

    @Query(value ="select * from  scon_homework_picture where file_key=?1 and vip_id=?2 and `subject`=?3 and TO_DAYS(create_time)=TO_DAYS(NOW())", nativeQuery = true)
	public HomeworkPicture findByFileKeyAndVipAndSubject(String fileKey, String vipId, String courseName);

    @Modifying
    @Query(value ="delete from  scon_homework_picture  where  file_key=?2 and vip_id=?1 and TO_DAYS(create_time)=TO_DAYS(NOW())", nativeQuery = true)
	public void delZybWorkPic(String vipId, String fileKey);

    /**
     * 统计学生作业数量
     * @param vipId
     * @return
     */
    int countByVipId(String vipId);

    /**
     * 查询多个科目的所有作业
     * @param homeworkIds
     * @return
     */
    @Query(nativeQuery = true,
            value = "select * from scon_homework_picture where homework_id in (?1) and is_delete = 0 order by create_time")
    List<HomeworkPicture> findByHomeworkIdIn(List<String> homeworkIds);

    /**
     * 查询作业，derver_file_key不为空是小塾的作业
     * @param homeworkId
     * @return
     */
    @Query(nativeQuery = true,
            value = "select * from scon_homework_picture where homework_id = ?1 and derver_file_key is not null order by create_time")
    List<HomeworkPicture> findByHomeworkIdAndDerverFileKeyIsNotNull(String homeworkId);

    @Query(nativeQuery = true,
            value = "select * from scon_homework_picture where vip_id = ?1 and subject = ?2 ")
    List<HomeworkPicture> findByVipIdAndSubject(String vipId, String subject);
    @Query(nativeQuery = true,
            value = "select * from scon_homework_picture where is_delete= ?1 and is_feedback = ?2 and teacher_id=?3 and to_days(create_time) = to_days(now())")
    List<HomeworkPicture> findByIsDeleteAndIsFeedbackAndTeacherId(int isDelete, int isFeedback, String teacherId);
    
    @Query(nativeQuery = true, value="select * from scon_homework_picture " +
            "where is_feedback=0 and to_days(create_time) = to_days(now())")
    List<HomeworkPicture> findTodayNotFeedbackHomeworkPicture();

    @Query(nativeQuery = true, value="select * from scon_homework_picture " +
            "where is_feedback=0 and date(create_time) = ?1")
    List<HomeworkPicture> findDateNotFeedbackHomeworkPicture(String date);

    Integer countByVipIdAndIsMark(String vipId, int isMark);

    @Query(nativeQuery = true,value = "select count(id) from scon_homework_picture where vip_id=?1 and is_mark !=4 and TO_DAYS(CREATE_TIME) = TO_DAYS(NOW()) ")
    Integer countTodayByVipId(String vipId);

    /**
     * 根据作业id和图片路径查询作业图片
     * @param homeworkId 作业id
     * @param fileKey 图片路径
     */
    HomeworkPicture findByHomeworkIdAndFileKey(String homeworkId, String fileKey);

    @Query(nativeQuery = true, value = "select t1.* from scon_homework_picture t1 left join  scon_homework t2 " +
            " on t1.homework_id=t2.id where t1.vip_id=?1 and date(t1.create_time) = ?3 and t1.is_delete=0 and t1.is_feedback=0 and t1.subject=?2 " +
            " and t1.is_mark in (0, 2) and t2.correct_time is null")
    List<HomeworkPicture> findDateNotFeedbackAndNotPushHomework (String vipId, String subject, String date);

    @Query(nativeQuery = true, value = "select t1.* from scon_homework_picture t1 " +
            " where t1.vip_id=?1 and date(t1.create_time) = ?2 and t1.is_delete=0 and t1.is_feedback=0  " +
            " and t1.is_mark in (0, 2)")
    List<HomeworkPicture> findDateNotFeedback (String vipId, String date);

    @Query(nativeQuery = true, value = "select * from scon_homework_picture where id = ?1 ")
    Optional<HomeworkPicture> findById(String homeworkPictureId);
    
    @Query(nativeQuery = true, value = "select count(*) from scon_homework_picture s where s.vip_id = ?1 and s.homework_id = ?2 and s.id != ?3"
    		+ " and s.is_delete = 0 and s.is_feedback = 0 and s.is_mark in (0, 2)")
    public int findByNoFinishHomework(String vipId,String homeworkId,String homeworkPictureId);
    
    @Query(nativeQuery = true, value = "select count(*) from scon_homework_picture s " +
            " where s.vip_id = ?1 and s.id != ?2 and date(s.create_time) = ?3 and s.is_delete=0 and s.is_feedback=0 and s.is_mark in (0, 2) ")
    public int findTodaySubjectNotFeedback(String vipId, String homeworkPictureId, String date);

    /**
     * 根据id列表查询
     */
    List<HomeworkPicture> findByIdInAndIsDelete(List<String> homeworkPictureIdList, Integer isDelete);
    
    public long countByHomeworkId(String homeworkId);
    
    @Query(nativeQuery = true, value = "select count(*) from scon_homework_picture s " +
            " where s.vip_id = ?1 and s.homework_id != ?2 and date(s.create_time) = ?3 and s.is_delete=0 and s.is_feedback=0 and s.is_mark in (0, 2) ")
    public int findTodayOtherSubjectNotFeedback(String vipId, String homeworkId, String date);
    
	@Query(nativeQuery = true, value = "SELECT * FROM `scon_homework_picture` where homework_id=?1 AND is_feedback = 0")
    public List<HomeworkPicture> findNoFeedbackByHomeworkId(String homeworkId);
	
	@Query(nativeQuery = true, value = "SELECT * FROM `scon_homework_picture` where teacher_id=?1 AND subject = ?2 and vip_id = ?3 AND to_days(create_time) = to_days(now())")
	public List<HomeworkPicture> findByTodayTeacherHomeworkPicture(String teacherId, String subject, String vipId);
	
	@Query(nativeQuery = true, value = "SELECT * FROM `scon_homework_picture` where homework_id=?1 AND subject = ?2 and vip_id = ?3 AND to_days(create_time) = to_days(now())")
	public List<HomeworkPicture> findByTodayHomeworkPicture(String homeworkId, String subject, String vipId);
	
	@Query(nativeQuery = true, value = "SELECT * FROM `scon_homework_picture` where know_teacher_id=?1 AND subject = ?2 and vip_id = ?3 ")
	public List<HomeworkPicture> findByTodayKnowTeacherHomeworkPicture(String teacherId, String subject, String vipId);

	@Query(nativeQuery = true, value = "select count(1) from  scon_homework_picture  where vip_id = ?1 and subject = ?2 and TO_DAYS(CREATE_TIME) = TO_DAYS(NOW())")
    public int countByVipIdAndSubjectOfToday(String vipId,String subject);

	@Query(nativeQuery = true, value = "select * from scon_homework_picture where homework_id = ?1 and is_delete = 0 order by create_time desc limit 1 ")
    HomeworkPicture findLastByHomeworkId(String homeworkId);

    int countByHomeworkIdAndIsMarkNotInAndIdNot(String homeworkId, List<Integer> isMarkList, String id);

    HomeworkPicture findTopByIsFeedbackAndIsDeleteAndHomeworkIdOrderByCreateTimeAsc(int isFeedback, int isDelete, String homeworkId);

    @Query(nativeQuery = true, value = "SELECT hp.id, hp.file_key, hp.is_delete, hp.is_feedback, hp.is_delete, " +
            " hp.teacher_id, hp.allot_time,  hp.create_time, hp.corrected_picture_url, hpc.check_date, hpc.checker, hpc.is_check_error," +
            " hpc.error_reason" +
            " FROM scon_homework_picture hp left join scon_homework_picture_correct hpc on hp.id=hpc.homework_picture_id and hpc.type = ?1" +
            " where hp.homework_id=?2")
    List<HomeworkPicture> findCheckPictureByHomeworkId (int type, String homeworkId);

    @Query(nativeQuery = true, value = "SELECT hp.id, hp.file_key, hp.is_delete, hp.is_know_feedback, hp.is_delete " +
            " hp.know_teacher_id, hp.create_time, hp.corrected_picture_url, hpc.check_date, hpc.checker, hpc.is_check_error," +
            " hpc.error_reason" +
            " FROM scon_homework_picture hp left join scon_homework_picture_correct hpc on hp.id=hpc.homework_picture_id and hpc.type = 9" +
            " where hp.homework_id=?2 and is_know_feedback=1 and is_needing_know=1")
    List<HomeworkPicture> findKnowCheckPictureByHomeworkId(String homeworkId);

    List<HomeworkPicture> findByHomeworkIdAndTypeInAndIsFeedbackAndIsDelete(String homeworkId, int[] type, int isFeedback, int isDelete);

    @Query(nativeQuery = true, value = "SELECT hp.* FROM scon_homework_picture hp left join scon_homework_picture_correct hpc on hp.id=hpc.homework_picture_id and hpc.type = 8\n" +
            "where hp.homework_id=?1 and hp.is_feedback=?2 and hp.is_delete=?3 and hpc.start_time is null and hpc.end_time is null ")
    List<HomeworkPicture> findByHomeworkIdAndIsFeedbackAndIsDelete(String homeworkId, int isFeedback, int isDelete);

    /**
     * 查询老师下的作业图片
     * @param homeworkId
     * @param teacherId
     * @param isDelete
     * @return
     */
    List<HomeworkPicture> findByHomeworkIdAndTeacherIdAndIsDelete(String homeworkId, String teacherId, int isDelete);

    /**
     * 查询匹配知识点老师下所有作业(显示的匹配知识点作业应该是批改老师已经批改完,这里回收站需要用到)
     *
     * @param homeworkId    作业
     * @param knowTeacherId 匹配知识点老师
     * @param isFeedback    是否批改完
     * @param isNeedingKnow 是否需要分配知识点老师
     * @return
     */
    List<HomeworkPicture> findByHomeworkIdAndKnowTeacherIdAndIsFeedbackAndIsNeedingKnowAndTeacherIdIsNotNull(String homeworkId, String knowTeacherId, int isFeedback, int isNeedingKnow);
    /**
     * 查询老师下所有作业，包括非学科作业（is_delete=0）
     * @param homeworkId
     * @param teacherId
     * @return
     */
    List<HomeworkPicture> findByHomeworkIdAndTeacherId(String homeworkId, String teacherId);

    /**
     * 统计老师未读报错数
     * @param teacherId
     * @param isCheckError
     * @param isErrorRead
     * @return
     */
    long countByTeacherIdAndIsCheckErrorAndIsErrorRead(String teacherId, int isCheckError, int isErrorRead);

    @Modifying
    @Query(value = "UPDATE scon_homework_picture_correct sc\n" +
            "SET sc.is_error_read = 1\n" +
            "WHERE\n" +
            " sc.type = 8\n" +
            "AND sc.is_check_error = 1\n" +
            "and sc.homework_picture_id in (SELECT id from scon_homework_picture where teacher_id = ?1 )", nativeQuery = true)
    void updateIsErrorRead(String teacherId);

    @Modifying
    @Query(value = "UPDATE scon_homework_picture_correct sc\n" +
            "SET sc.is_error_read = 1\n" +
            "WHERE\n" +
            "	 sc.type = 13\n" +
            "AND sc.is_check_error = 1\n" +
            "and sc.homework_picture_id in (SELECT id from scon_homework_picture where know_teacher_id =?1 )", nativeQuery = true)
    void updateKonwIsErrorRead(String teacherId);

    /**
     *查询模糊和不全的列表
     */
    @Query(nativeQuery = true, value = "SELECT * FROM scon_homework_picture where vip_id=?1 and subject=?2 and is_mark=?3 and TO_DAYS(create_time) = TO_DAYS(NOW())")
    List<HomeworkPicture> findByVipIdAndSubjectAndIsMark (String vipId,String subject,Integer isMark );

    /**
     * 查询老师未批改数量
     * @param beginTime
     * @param teacherId
     * @return
     */
    @Query(nativeQuery = true, value = "select count(1) from scon_homework_picture where create_time > ?1 and teacher_id=?2 and is_feedback = 0  and is_mark in (0, 2) ")
    int countByCreateTimeAndTeacherId (String beginTime, String teacherId);

    /**
     * 查询匹配知识点老师未匹配数量
     * @param beginTime
     * @param teacherId
     * @return
     */
    @Query(nativeQuery = true, value = "select count(1) from scon_homework_picture hp" +
            " where hp.create_time > ?1 and hp.know_teacher_id=?2 " +
            " and hp.is_know_feedback = 0 ")
    int countByCreateTimeAndKnowTeacherId (String beginTime, String teacherId);


    @Query(nativeQuery = true, value = "select * from scon_homework_picture where homework_id = ?1 and is_delete = 0 order by create_time desc limit 2 ")
    List<HomeworkPicture> findLastTowByHomeworkId(String homeworkId);

    @Modifying
    @Query(nativeQuery = true, value = "update scon_homework_picture_correct set is_check_error = 0,checker = null,check_date = null,error_reason = null,is_error_read = 0 where homework_picture_id = ?1 and type = ?2 ")
    void updateResetCheckHomework(String homeworkPictureId, int type);

    @Query(nativeQuery = true,value = "select count(sc.id) from scon_homework_picture s LEFT JOIN scon_homework_picture_correct sc ON s.id = sc.homework_picture_id  " +
            "where s.teacher_id = ?1 and sc.type = ?2 and sc.is_check_error = ?3 and sc.is_error_read = ?4")
    long findTeacherErrorCount(String teacherId, int type, int isCheckError, int isErrorRead);

    @Query(nativeQuery = true,value = "select count(sc.id) from scon_homework_picture s LEFT JOIN scon_homework_picture_correct sc ON s.id = sc.homework_picture_id  " +
            "where s.know_teacher_id = ?1 and sc.type = ?2 and sc.is_check_error = ?3 and sc.is_error_read = ?4")
    long findKnowTeacherErrorCount(String teacherId, int type, int isCheckError, int isErrorRead);

    @Query(nativeQuery =true,value = "select * from scon_homework_picture where id=?1 and type in ?2 ")
    HomeworkPicture findByIdAndTypeIn(String id,int [] type);

    @Query(nativeQuery =true,value = "select * from scon_homework_picture where id=?1 and type =?2")
    HomeworkPicture findByIdAndType(String id,int  type);
    
    @Modifying
    @Query(value ="update scon_homework_picture son set son.know_teacher_id=?2 where son.id = ?1", nativeQuery = true)
    public void updateHomeworkPictureByKnowTeacherId(String id,String knowTeacherId);

    /**
     * 查询该学生所有科目不是非学科的数量
     *
     * @param createTime 作业日期
     * @param vipId 学生id
     * @param isDelete 是否删除（0否， 1是）
     */
    @Query(value ="select count(1) from scon_homework_picture where to_days(create_time)=to_days(?1) and vip_id = ?2 and is_delete=?3 and id != ?4", nativeQuery = true)
    public long countByVipIdAndCreateTimeAndIsDelete(Date createTime, String vipId, int isDelete, String homeworkPictureId);

    @Modifying
    @Query(value = "update scon_homework_picture set know_teacher_id=null where id in ?1",nativeQuery = true)
    public void updateHomeworkPictureByIdIn(Object [] id);

    @Query(nativeQuery = true,value = "select * from scon_homework_picture where vip_id=?1 and create_time regexp ?2 order by create_time desc")
    List<HomeworkPicture> findByVipIdAndCreateTime(String vipId,String createTime);

    @Query(nativeQuery = true,value = "select s.* from scon_homework_picture s LEFT JOIN scon_homework_picture_correct sc ON s.id = sc.homework_picture_id and sc.type=8 " +
            "where to_days(s.create_time)=to_days(now()) and sc.start_time is null and sc.end_time is null and floor((UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(s.allot_time))/60)>=15 and s.is_feedback=0 and s.is_delete=0 and s.teacher_id is not null and s.service_type = 1")
    List<HomeworkPicture> queryOvertimeList();

    @Query(nativeQuery = true, value = "select sp.* from scon_homework_picture sp left join tb_lib_manager tm on sp.teacher_id = tm.user_id and tm.type=8 \n" +
            "where sp.vip_id=?1 and sp.subject=?2 and  to_days(sp.create_time) = to_days(now()) and tm.is_online=1 and tm.is_banned=0 and sp.teacher_id<>?3 order by sp.allot_time desc limit 1")
    HomeworkPicture findStudentSubjectHomeworkNotThis(String vipId, String subject, String teacherId);

    /**
     * 查询作业的所有图片
     * @param homeworkId
     * @return
     */
    List<HomeworkPicture> findByHomeworkId(String homeworkId);

    /**
     * 查询某天有作业的批改老师
     * @param date
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT teacher_id from scon_homework_picture where teacher_id is not null and create_time REGEXP ? GROUP BY teacher_id")
    List<String> findOnedayTeacherOfHomework(String date);
    /**
     * 查询某天有已完成作业的批改老师
     * @param date
     * @return
     */
    @Query(nativeQuery = true,value = "select t.teacher_id from (\n" +
            "            select s.teacher_id,\n" +
            "            sum(case when s.is_delete = 0 and s.is_mark in (0, 2) and s.is_feedback = 0 then 1 else 0 end) no_feedback_count\n" +
            "    from scon_homework_picture s\n" +
            "    where s.create_time regexp ?\n" +
            "    group by s.teacher_id, s.homework_id) t\n" +
            "    where t.no_feedback_count = 0")
    List<String> findOnedayTeacherOfFeedbackHomework(String date);

    /**
     * 查询某天之后有作业的注销批改老师
     * @param date
     * @return
     */
    @Query(nativeQuery = true,value = "select teacher_id from scon_homework_picture\n" +
            "            where teacher_id in ?2\n" +
            "            and create_time >= ?1\n" +
            "            group by teacher_id")
    List<String> findOnedayAfterTeacherOfHomework(String date,List<String> teacherIds);
    /**
     * 查询某天有作业的匹配老师
     * @param date
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT know_teacher_id from scon_homework_picture where know_teacher_id is not null and create_time REGEXP ? GROUP BY know_teacher_id")
    List<String> findOnedayKnowTeacherOfHomework(String date);
    /**
     * 查询某天有已完成作业的匹配老师
     * @param date
     * @return
     */
    @Query(nativeQuery = true,value = "select t.know_teacher_id from (\n" +
            "                        select s.know_teacher_id,\n" +
            "                        sum(case when s.is_delete = 0 and s.is_mark in (0, 2) and s.is_feedback = 0 then 1 else 0 end) no_feedback_count\n" +
            "                        from scon_homework_picture s\n" +
            "                        where s.create_time regexp ?\n" +
            "                        group by s.know_teacher_id, s.homework_id) t\n" +
            "                    where t.no_feedback_count = 0")
    List<String> findOnedayKnowTeacherOfFeedbackHomework(String date);
    /**
     * 查询某天之后有作业的注销匹配老师
     * @param date
     * @param teacherIds
     * @return
     */
    @Query(nativeQuery = true,value = "select know_teacher_id from scon_homework_picture\n" +
            "where know_teacher_id in ?2\n" +
            "  and create_time >= ?1\n" +
            "group by know_teacher_id")
    List<String> findOnedayAfterKnowTeacherOfHomework(String date,List<String> teacherIds);

    HomeworkPicture findTopByHomeworkIdAndVipIdAndServiceTypeAndTeacherIdIsNotNull(String homeworkId, String vipId, int serviceType);

    @Query(nativeQuery = true, value = "select * from scon_homework_picture where file_key = ?1 and is_delete = 0 limit 1 ")
    HomeworkPicture findTopByFileKey(String oldFileKey);

    int countByHomeworkIdAndIsDelete(String homeworkId, int isDelete);

    @Modifying
    @Query(nativeQuery = true,value = "delete from scon_homework_picture set is_delete=1 where vip_id=?1")
    void deleteByVipId(String vipId);

    /**
     * 查找指定班级下的指定科目的未分配校区批改老师的作业列表
     *
     * @param subjects
     * @param classId
     * @return
     */
    @Query(nativeQuery = true, value = "select *\n" +
            "from scon_homework_picture\n" +
            "where teacher_id is null\n" +
            "  and is_delete = 0\n" +
            "  and service_type in (2, 5)\n" +
            "  and subject in ?1\n" +
            "  and vip_id in (\n" +
            "    select vip_id\n" +
            "    from tb_class_student\n" +
            "    where status = 1\n" +
            "      and class_id = ?2\n" +
            ")\n" +
            " and create_time >= ?3")
    List<HomeworkPicture> findUnallocatedHomeworkList(List<String> subjects, String classId, String beginTime);

    /**
     * 查找小孩的所有未分配校区批改老师的作业列表
     *
     * @param vipId
     * @return
     */
    @Query(nativeQuery = true, value = "select *\n" +
            "from scon_homework_picture\n" +
            "where teacher_id is null\n" +
            "  and is_delete = 0\n" +
            "  and service_type in (2, 5)\n" +
            "  and vip_id = ?1\n" +
            "  and create_time >= ?2")
    List<HomeworkPicture> findUnallocatedHomeworkList(String vipId, String beginTime);

    /**
     * 查找老师未批改作业
     * @param teacherId
     * @param beginTime
     * @return
     */
    @Query(nativeQuery = true, value = "select *\n" +
            "from scon_homework_picture\n" +
            "where teacher_id =?1\n" +
            "  and is_delete = 0\n" +
            "  and service_type in (2, 5)\n" +
            "  and is_feedback = 0\n" +
            "  and create_time >= ?2")
    List<HomeworkPicture> findUncorrectedHomeworkList(String teacherId, String beginTime);

    /**
     * 查找学生未批改作业
     * @param vipId
     * @param beginTime
     * @return
     */
    @Query(nativeQuery = true, value = "select *\n" +
            "from scon_homework_picture\n" +
            "where vip_id =?1\n" +
            "  and is_delete = 0\n" +
            "  and service_type in (2, 5)\n" +
            "  and is_feedback = 0\n" +
            "  and create_time >= ?2")
    List<HomeworkPicture> findUncorrectedHomeworkListByVipId(String vipId, String beginTime);


    /**
     * 查找老师某个校区未批改作业
     * @param teacherId
     * @param beginTime
     * @return
     */
    @Query(nativeQuery = true, value = "select t1.*\n" +
            "from scon_homework_picture t1 join tb_lib_vip t2\n" +
            " on t1.vip_id=t2.id\n" +
            "where t1.teacher_id =?1\n" +
            "  and t1.is_delete = 0\n" +
            "  and t1.service_type in (2, 5)\n" +
            "  and t1.is_feedback = 0\n" +
            "  and t1.create_time >= ?2\n"+
            "  and t2.lib_id=?3 ")
    List<HomeworkPicture> findUncorrectedLibHomeworkList(String teacherId, String beginTime,String libId);

    /**
     * 查找老师未批改作业
     * @param teacherId
     * @param beginTime
     * @param subjects
     * @return
     */
    @Query(nativeQuery = true, value = "select *\n" +
            "from scon_homework_picture\n" +
            "where teacher_id =?1\n" +
            "  and is_delete = 0\n" +
            "  and service_type in (2, 5)\n" +
            "  and is_feedback = 0\n" +
            "  and create_time >= ?2\n" +
            "  and subject in ?3")
    List<HomeworkPicture> findUncorrectedHomeworkList(String teacherId, String beginTime, List<String> subjects);

    /**
     * 查找该校区老师未批改作业
     * @param teacherId
     * @param beginTime
     * @param subjects
     * @return
     */
    @Query(nativeQuery = true, value = "select t1.*\n" +
            "from scon_homework_picture t1 left join tb_lib_vip t2\n" +
            " on t1.vip_id=t2.id\n" +
            "where t1.teacher_id =?1\n" +
            "  and t1.is_delete = 0\n" +
            "  and t1.service_type in (2, 5)\n" +
            "  and t1.is_feedback = 0\n" +
            "  and t1.create_time >= ?2\n"+
            "  and t1.subject in ?3\n" +
            "  and t2.lib_id=?3")
    List<HomeworkPicture> findUncorrectedLibHomeworkList(String teacherId, String beginTime, List<String> subjects,String libId);
}
