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

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

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

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

    @Query(nativeQuery = true, value = "select * from scon_homework where vip_id=?1 and subject=?2 and start_time between ?3 and ?4")
    public Homework findByVipIdAndSubject(String vipId, String subject, Date beiginTime, Date endTime);

    @Query(nativeQuery = true, value = "select * from scon_homework where vip_id=?1 and subject=?2 and create_time between ?3 and ?4 limit 1")
    public Homework findByVipIdAndSubjectAndRangTime(String vipId, String subject, String beiginTime, String endTime);
    
    @Query(nativeQuery = true, value = "select * from scon_homework where to_days(create_time) = to_days(now()) and vip_id = ?1 and subject = ?2")
    public Homework findByTodaySubjectHomework(String vipId,String subject);

    /**
     * 查询学生当天的作业记录
     * @param vipId
     * @param beginTime
     * @param endTime
     * @return
     */
    public List<Homework> findByVipIdAndCreateTimeBetween(String vipId, Date beginTime, Date endTime);


    /**
     * 查询学生当天的作业记录
     * @param vipId
     * @param beginTime
     * @param endTime
     * @return
     */
    @Query(nativeQuery = true, value = "select * from scon_homework where vip_id=?1 and create_time between ?2 and ?3")
    public List<Homework> findByVipIdAndCreateTimeBetween(String vipId, String beginTime, String endTime);

    /**
     * 根据多个id查询未完成内容
     * @param Ids 作业id集合
     * @return
     */
    @Query(nativeQuery = true, value = "select unfinished_content from scon_homework where id in (?1) and unfinished_content is not null ")
    List<String> findUnfinishedContentByIdList(List<String> Ids);
    
    @Query(nativeQuery = true, value = "delete scon_homework from scon_homework LEFT JOIN scon_homework_picture ON scon_homework.id = scon_homework_picture.homework_id "
    		+ "where scon_homework_picture.homework_id is null and date_add(scon_homework.create_time, interval 2 hour) < NOW()")
	@Modifying
    public void deleteNoPictureHomework();

    /**
     * 查询是否有存在的作业记录
     * @param vipId 学生id
     * @param startTime 开始时间
     * @param subject 科目
     */
    @Query(nativeQuery = true, value = "select * from scon_homework where vip_id = ?1 and date(start_time) = ?2 and subject = ?3 ")
    Homework findByVipIdStartTimeSubject(String vipId, String startTime, String subject);

    /**
     * 根据作业Id查询是否有批改记录
     * @return
     */
    @Query(nativeQuery = true,value = "select * from scon_homework where id=?1 ")
    Optional<Homework> findById(String homeworkId);

    /**
     * 查询近7天使用过作业吧服务的人
     */
    @Query(nativeQuery = true,value = "select count(distinct vip_id) from scon_homework_picture where create_time<now() and create_time>=date_add(now(),interval -7 day)")
    int findUserCountByCreateTime();

    @Query(nativeQuery = true,value = "select count(id) from tb_lib_vip where id in(select distinct vip_id from scon_homework_picture where create_time<now() and create_time>=date_add(now(),interval -7 day)) and " +
          " activate_time>=date_add(now(), interval -7 day)")
    int findRepetiveCount();

    @Query(nativeQuery = true,value = "update scon_homework set start_time=null, end_time=null, work_type=3 where id=?1 ")
    @Modifying
    public void updateHomework2NoHomework(String id);

    @Query(nativeQuery = true,value = "delete from tb_lib_vip where vip_id=?1")
    @Modifying
    public void deleteByVipId(String vipId);
}
