package com.ssj.dao.weixin.zuoyb.dao;

import com.ssj.bean.weixin.zyb.domain.ZuoybStudentPush;
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;

/** 
 * @author  ZhangCaibao 
 * @version 2017年7月21日 上午11:28:45 
 * 类说明 
 */
@Repository
public interface ZuoybStudentPushDao  extends JpaRepository<ZuoybStudentPush, String> {

	public Page<ZuoybStudentPush> findAll(Specification<ZuoybStudentPush> specification, Pageable page);
	
	
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_push  where lib_id = ?1 and  vip_id = ?2 and course_hour =?3 and TO_DAYS(push_time)  =  TO_DAYS(?4) ")
	public ZuoybStudentPush  findByLibIdAndVipIdAndCourseHour(String libId,String vipId,String courseHour,String date);
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_push  where push_time between ?1 and ?2 and vip_id = ?3 LIMIT 1 ")
	public ZuoybStudentPush  findByVipIdAndDate(String beginTime, String endTime, String vipId);

	@Query(nativeQuery = true, value = "select  *  from  zyb_student_push  where vip_id=?1 ")
	public List<ZuoybStudentPush>  findByVipId(String vipId);

	@Query(nativeQuery = true, value = "select  *  from  zyb_student_push  where push_time between ?1 and ?2 and vip_id = ?3")
	public List<ZuoybStudentPush>  findRangDatePushByVipId(String beginTime, String endTime, String vipId);

	@Query(nativeQuery = true, value = "select  *  from  zyb_student_push s where  TO_DAYS(s.push_time) = TO_DAYS(now())  and s.vip_id =?1  LIMIT 1 ")
	public ZuoybStudentPush  findByVipIdToDay(String vipId);

	@Query(nativeQuery = true, value = "select  count(1)  from  zyb_student_push  where vip_id = ?1 and TO_DAYS(push_time)  =  TO_DAYS(now()) and type =1 ")
	public Integer getToDayAndVipId(String vipId);
	
	
	/**
	 *  计算时间内这天，该馆有多少打分人数，
	* @param libId
	* @param date
	* @return
	* 创建人：ZhangCaibao 
	* 2017年10月16日 下午2:12:04 
	* @Integer
	 */
	@Query(nativeQuery = true, value ="SELECT  *  from  zyb_student_push\n" +
			"where lib_id=?1 and TO_DAYS(?2)= TO_DAYS(push_time) and jrxx is not null order by jrxx desc  ")
	public List<ZuoybStudentPush> yesterdayLibIdpush(String libId,String date);
	// 获取最近三十天的签到签离时长，没有签离默认给2小时，统计中时长
	@Query(nativeQuery = true, value ="SELECT   \n" +
			"ROUND(IFNULL(sum(TIMESTAMPDIFF(MINUTE,ryke_time,IFNULL(leave_time,date_sub(ryke_time, interval -2 hour))))/60,0),0)\n" +
			"from  tb_lib_subscribe where\n" +
			"vip_id=?1 and \n" +
			"ryke_time is not null  and \n" +
			" flow_stat in (3,4)\n" +
			"  and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(leave_time)")
	public long getXqLearningTime(String vipId);
	/**
	 * 获取该用户当天签到的时间，
	* @param vipId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年11月7日 上午11:09:58 
	* @String
	 */
	@Query(nativeQuery = true, value =" SELECT  DATE_FORMAT(e.ryke_time,'%k:%i')  from  tb_lib_subscribe  e\n" +
			"where e.vip_id=?1 and  e.lib_id=?2 and \n" +
			"TO_DAYS(e.ryke_time) = TO_DAYS(now())")
	public String getToDayVipSubscribe(String vipId,String libId);

	public int countDistinctByTeacherIdAndType(String teacherId, int type);

	@Query(nativeQuery = true, value ="select * from zyb_student_push where type=?1 and lib_id in (?2) and TO_DAYS(push_time)  = TO_DAYS(NOW())")
	public List<ZuoybStudentPush> findByTypeAndLibIdInOrderByPushTimeAsc(int type, List<String> libId);
	
	@Query(nativeQuery = true, value ="select zp.* from scon_homework_picture s LEFT JOIN zyb_student_work zs ON s.zyb_work_id = zs.id " + 
			"LEFT JOIN zyb_student_push zp ON zs.student_push_id = zp.id where s.zyb_work_id = ?1 limit 1")
	public ZuoybStudentPush findSuggestByStudentWorkId(String studentWorkId);

	public ZuoybStudentPush findFirstByTeacherIdAndVipIdOrderByPushTimeDesc (String teacherId, String vipId);

	public ZuoybStudentPush findFirstByVipIdOrderByPushTimeDesc(String vipId);

	@Query(nativeQuery = true, value ="select count(1) from zyb_student_push where  push_time between ?2 and ?3 and vip_id=?1 and view_time is not null")
	public Integer countParentViewReport (String vipId, String beginTime, String endTime);

	@Query(nativeQuery = true, value ="select avg(level) from zyb_student_push where vip_id=?1")
	public Float avgParentPraise (String vipId);

	@Query(nativeQuery = true, value ="select * from zyb_student_push where vip_id=?1 and level < 3  and suggest is not null order by push_time desc LIMIT 1")
	public ZuoybStudentPush findLastLess3Suggest (String vipId);

	@Query(nativeQuery = true, value ="select * from zyb_student_push where vip_id=?1 and suggest is not null order by push_time desc LIMIT 1")
	public ZuoybStudentPush findOneSuggestNotNull (String vipId);

	/**
	 * 查询一段时间内已反馈的报告数
	 * @param type
	 * @param vipId
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public int countByTypeAndVipIdAndPushTimeBetween (int type, String vipId, Date beginTime, Date endTime);

	/**
	 * 根据作业id查询
	 * @param homeworkId
	 * @return
	 */
	@Query(nativeQuery = true, value = "select zyb_student_push.* from scon_homework, zyb_student_push" +
			" where scon_homework.vip_id = zyb_student_push.vip_id" +
			" and to_days(scon_homework.create_time) = to_days(zyb_student_push.push_time) and scon_homework.id = ?1")
	ZuoybStudentPush findByHomeworkId(String homeworkId);

	@Query(nativeQuery = true,value = "update zyb_student_push set teacher_id = ?1 where vip_id =?2 and TO_DAYS(push_time) = TO_DAYS(now())")
	@Modifying
	public void updateByTeacherIdAndVipId(String teacherId,String vipId);

	/**
	 * 查找老师某一天数据
	 *
	 * @param teacherId
	 * @param date
	 * @param type
	 * @return
	 */
	@Query(nativeQuery = true, value = "select * from zyb_student_push where teacher_id = ?1 and push_time REGEXP ?2 and type = ?3 order by push_time")
	List<ZuoybStudentPush> findByTeacherIdAndPushTimeAndType(String teacherId, String date, int type);

	/**
	 * 查找老师某一天数据
	 *
	 * @param teacherId
	 * @param date
	 * @return
	 */
	@Query(nativeQuery = true, value = "select * from zyb_student_push where teacher_id = ?1 and push_time REGEXP ?2 order by push_time")
	List<ZuoybStudentPush> findByTeacherIdAndPushTime(String teacherId, String date);

	@Query(nativeQuery = true, value ="select count(1) from zyb_student_push where type=2 and vip_id=?1 ")
	int findCountByVipId(String vipId);
}
