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

import com.ssj.bean.weixin.zyb.domain.ZuoybStudentWork;
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月6日 下午2:41:23 
 * 类说明 
 */
@Repository
public interface ZuoybStudentWorkDao extends JpaRepository<ZuoybStudentWork, String>{

	public Page<ZuoybStudentWork> findAll(Specification<ZuoybStudentWork> specification, Pageable page);
	
	
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where lib_id = ?1 and  vip_id = ?2 and zyb_course_id =?3 and TO_DAYS(scoring_time)  =  TO_DAYS(now()) ")
	public ZuoybStudentWork getByLibIdAndVipIdAndCourseId(String libId,String vipId,String courseId);

	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where lib_id = ?1 and  vip_id = ?2 and course_name =?3 and TO_DAYS(scoring_time)  =  TO_DAYS(now()) ")
	public ZuoybStudentWork getByLibIdAndVipIdAndAndCourseName(String libId, String vipId, String courseName);

	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where lib_id = ?1 and  vip_id = ?2  and TO_DAYS(scoring_time)  =  TO_DAYS(now()) ")
	public List<ZuoybStudentWork> getByLibIdAndVipId(String libId,String vipId);
	
	
	@Query(nativeQuery = true, value = "delete from  zyb_student_work_details  where student_work_id = ?1  ")
	@Modifying
	public void deleteWorkDetalByStudentWorkId(String studentWorkId);
	
	@Query(nativeQuery = true, value = "select count(1) from  zyb_student_work  where vip_id = ?1 and lib_id = ?2  and zyb_course_id != ?3 and TO_DAYS(scoring_time)  =  TO_DAYS(now()) ")
	public Integer getByVipIdAndLibIdCount(String vipId,String libId,String zybCourseId);
	
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where lib_id = ?1 and  vip_id = ?2  and  course_hour = ?3 and TO_DAYS(scoring_time)  =  TO_DAYS(now()) order by num asc , scoring_time asc  ")
	public List<ZuoybStudentWork> getByLibIdAndVipIdAndHour(String libId,String vipId,String courseHour);
	
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where lib_id = ?1 and  vip_id = ?2  and  course_hour = ?3 and type = ?4 and TO_DAYS(scoring_time)  =  TO_DAYS(now()) order by num asc , scoring_time asc  ")
	public List<ZuoybStudentWork> getByLibIdAndVipIdAndHourAndType(String libId,String vipId,String courseHour,Integer type);
	
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where lib_id = ?1 and  vip_id = ?2   and type = ?3 and TO_DAYS(scoring_time)  =  TO_DAYS(now())   order by num asc , scoring_time asc  ")
	public List<ZuoybStudentWork> getByLibIdAndVipIdAndType(String libId,String vipId,Integer type);
	
	@Query(nativeQuery = true, value = "SELECT  ROUND(AVG(s.child_stat))  from     zyb_student_work k   JOIN  zyb_student_work_details  s\n" +
			"on s.student_work_id = k.id \n" +
			"where   \n" +
			"k.vip_id = ?1 and k.lib_id = ?2  and  \n" +
			"TO_DAYS(s.scoring_time)  =  TO_DAYS(?3) and  k.type = 1 and s.num =101")
	public Integer getByAvgCount(String vipId,String libId,String date);
	
	@Query(nativeQuery = true, value = "SELECT  child_stat  from     zyb_student_work k   JOIN  zyb_student_work_details  s\n" +
			"			on s.student_work_id = k.id\n" +
			"			where   \n" +
			"			k.vip_id = ?1 and k.lib_id = ?2  and   k.course_hour = ?4 and  k.course_name='总体' and \n" +
			"			TO_DAYS(s.scoring_time)  =  TO_DAYS(?3) and s.num =101")
	public Integer getByZTCount(String vipId,String libId,String date,String courseHour);
	
	@Query(nativeQuery = true, value = " update zyb_student_work  set push_time =now() ,stat=3 ,student_push_id = ?4"
			+ " where vip_id = ?1  and lib_id=?2 and  TO_DAYS(scoring_time)  =  TO_DAYS(?3) and course_hour =?5 ")
	@Modifying
	public  void  updatePushTimeAndStat(String vipId,String libId,String date, String pushId,String courseHour);
	
	
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where student_push_id = ?1 order by num asc , scoring_time asc  ")
	public List<ZuoybStudentWork> findByStudentPushId(String studentPushId);	
	
	@Query(nativeQuery = true, value ="SELECT count(s.id)\n" +
			"			FROM\n" +
			"				zyb_student_work k  JOIN  zyb_student_work_how_details s \n" +
			"       on k.id = s.student_work_id\n" +
			"			WHERE\n" +
			"				k.vip_id = ?1\n" +
			"			AND k.lib_id = ?2\n" +
			"			AND TO_DAYS(k.scoring_time) = TO_DAYS(?3)\n" +
			"			AND k.course_hour =?4")
	public Integer getHowDetailsId(String vipId,String libId,String date,String courseHour);
	
	@Query(nativeQuery = true, value = "SELECT  count(1)  from  zyb_student_work_img  where object_id in (\n" +
			"SELECT\n" +
			"   k.id  \n" +
			"FROM\n" +
			"	zyb_student_work k\n" +
			"WHERE\n" +
			"	k.vip_id = ?1\n" +
			"AND k.lib_id = ?2\n" +
			"AND TO_DAYS(k.scoring_time) = TO_DAYS(?3)\n" +
			"AND k.course_hour =?4\n" +
			")\n" +
			"and wrong_title is  not null  and coordinate is not NULL ")
	public Integer workImgCount(String vipId,String libId,String date,String courseHour);
	
	
	@Query(nativeQuery = true, value = "select  *  from  zyb_student_work  where lib_id = ?1 and  vip_id = ?2  and TO_DAYS(scoring_time)  =  TO_DAYS(now()) order by  scoring_time asc  ")
	public List<ZuoybStudentWork> getByLibIdAndVipIdAndTimer(String libId,String vipId);
	
	@Query(nativeQuery = true, value ="SELECT  k.*  from \n" +
			"zyb_student_work k\n" +
			"LEFT JOIN  zyb_student_work_how  w\n" +
			"on k.id =w.student_work_id\n" +
			" where how_ids is not null  and w.id is null \n" +
			"ORDER BY push_time desc ")
	public List<ZuoybStudentWork> oldDataWorkHow();
	
	
	@Query(nativeQuery = true, value =" select SUM(time_cost) as timecount  from  zyb_student_work \n" +
			"		 	where   TO_DAYS(NOW()) -TO_DAYS(scoring_time) BETWEEN 1 and  7      \n" +
			"		    and vip_id=?1 and type =1 and stat=3  ")
	public Long learningbyVipId(String vipId);
	
	@Query(nativeQuery = true, value =" select count(1) from  zyb_student_push where vip_id=?1 and type=2 ")
	 public Long studyReportbyVipId(String vipId);
	
	@Query(nativeQuery = true, value ="SELECT GROUP_CONCAT(CONCAT(s.parent_name,s.child_name)) from  zyb_student_work_details s  LEFT join zyb_student_work k \n" +
			"on s.student_work_id = k.id \n" +
			"where k.num =4  and TO_DAYS(k.scoring_time) = TO_DAYS(NOW())  \n" +
			"and TO_DAYS(s.scoring_time) = TO_DAYS(NOW())  \n" +
			"and k.lib_id=?1  and k.vip_id =?2\n" +
			"and s.num>=101 and s.num<=104  and s.child_stat >1 \n" +
			"ORDER BY s.num asc ")
	public String  getGzd(String libId,String vipId);

	@Query(nativeQuery = true, value ="select sum(time_cost) from zyb_student_work where student_id =?1 and scoring_time between ?2 and ?3")
	public Integer getTheMonthTimeCost(String vipId, String beginTime, String endTime);

	@Query(nativeQuery = true, value ="select * from zyb_student_work where vip_id=?1 and course_name= ?2 and scoring_time between ?3 and ?4")
	public ZuoybStudentWork findByVipIdAndcAndCourseName (String vipId, String subject, String beginTime, String endTime);

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

}


