package com.ssj.dao.sys.problem.dao;

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




/**
 * 类名称：ConfigDao<br>
 * 类描述：系统参数数据访问接口<br>
 * 
 * @version v1.0
 */
@Repository
public interface ProblemPictureDao extends JpaRepository<ProblemPicture, String> {

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

    @Modifying
    @Query(value = "update scon_problem_picture son set son.is_delete=1 where son.id = ?1", nativeQuery = true)
    public void deleteById(String problemId);

    public List<ProblemPicture> findByisDeleteAndParentIdInAndIsFeedbackOrderByCreateTimeAsc(int isDelete, List<String> parentId,
                                                                         int isFeedback);
    
    public List<ProblemPicture> findByisDeleteAndTeacherIdAndIsFeedbackAndCreateTimeAfterOrderByCreateTimeAsc(int isDelete, String teacherId,int isFeedback,Date createTime);

    @Modifying
    @Query("update ProblemPicture son set son.isFeedback=1 where son.id = ?1")
    public void updateByIsFeedback(String id);
    
    @Query(value = "select * from  scon_problem_picture s  where s.is_delete = 0 and s.parent_id = ?1 and s.derver = ?2 and "
    		+ "s.create_time >= ?3 and s.create_time <= date_format(?3,'%Y-%m-%d 23:59:59') ", nativeQuery = true)
    public List<ProblemPicture> findProblemReviewByTime(String parentId,String derver,String time);
    
    public ProblemPicture findTopByisDeleteAndParentIdAndVipIdOrderByCreateTimeDesc(int isDelete, String parentId,String derver);
    
    public long count();
    
    public long countByTeacherIdAndIsFeedback(String teacherId,int isFeedback);

    public long countByTeacherIdAndIsFeedbackAndCreateTimeBefore(String teacherId, int isFeedback, Date endTime);

    @Query(nativeQuery = true, value = "SELECT\n" +
    		"	t3.*\n" +
    		"FROM	\n" +
    		"scon_problem_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<ProblemPicture> findByIsFeedbackAndLibIdInAndCreateTimeAfterOrderByCreateTimeAsc(int isFeedback, List<String> libIds);

    @Query(nativeQuery = true, value = "select t3.* from scon_problem_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<ProblemPicture> findNotFeedbackList(int isFeedback, List<String> libIds);

    public ProblemPicture findFirstByTeacherIdAndVipIdOrderByCreateTimeDesc(String teacherId, String vipId);

    public long countByFileKey(String fileKey);

    @Query(nativeQuery = true, value = "select count(distinct vip_id) from scon_problem_picture where is_feedback != 1 and teacher_id = ?1 and TO_DAYS(create_time) = TO_DAYS(now())")
    public int countNotFeedbackVip (String teacherId);

	public List<ProblemPicture> findByisDeleteAndTeacherIdAndIsFeedbackAndCreateTimeAfterAndVipIdOrderByCreateTimeAsc(
			int i, String teacherUserId, int isFeedback, Date todatDate, String vipId);

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

    @Query(nativeQuery = true, value = "select count(distinct vip_id) from scon_problem_picture where teacher_id=?1 and TO_DAYS(CREATE_TIME) = TO_DAYS(NOW()) and is_feedback=0")
    public int countByTeacherIdNotFeedback(String teacherId);

    @Query(nativeQuery = true, value = "select * from scon_problem_picture where vip_id=?1 and subject=?2 and  to_days(create_time) = to_days(now()) limit 1")
    public ProblemPicture findTop(String vipId, String subject);


    /**
     * 查询做历史的未解答难题的数量
     * @param isFeedback
     * @param endTime
     * @param teacherId
     * @return
     */
    public int countByIsFeedbackAndCreateTimeBeforeAndTeacherId(int isFeedback, Date endTime, String teacherId);
    /**
     * 统计学生难题数量
     * @param vipId
     * @return
     */
    long countByVipId(String vipId);

    /**
     * 查询小孩账户下未绑定老师的未解答难题
     * @param vipId
     * @param subject
     * @return
     */
    @Query(nativeQuery = true, value = "select * from scon_problem_picture where vip_id=?1 and subject=?2 and is_feedback=0 and is_delete = 0 " +
            " and (teacher_id is null or teacher_id = '')")
    List<ProblemPicture> findUnansweredAndNoTeacherByVipIdAndSubject(String vipId, String subject);

    /**
     * 查询小孩账户下已绑定老师的未解答难题
     * @param vipId
     * @param subject
     * @return
     */
    @Query(nativeQuery = true, value = "select * from scon_problem_picture where vip_id=?1 and is_feedback=0 and is_delete = 0 " +
            " and  teacher_id = ?2 and subject=?3 ")
    List<ProblemPicture> findUnansweredByVipIdAndTeacherIdAndSubject(String vipId, String teacherId, String subject);

    /**
     * 查询难题总数量
     * @param
     * @return
     */
    @Query(nativeQuery = true,value = "select count(id) from scon_problem_picture where vip_id=?1 and is_delete=0 and TO_DAYS(CREATE_TIME) = TO_DAYS(NOW()) ")
    public int countProblemSumByVipId(String vipId);

    ProblemPicture findByIdAndIsDelete(String id, int isDelete);
}
