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

import com.ssj.bean.weixin.zyb.domain.ZuoybStudentBg;
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.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


/** 
 * @author  ZhangCaibao 
 * @version 2017年11月2日 上午11:12:01 
 * 类说明 
 */
@Repository
public interface ZuoybStudentBgDao extends JpaRepository<ZuoybStudentBg, String>{

	public Page<ZuoybStudentBg> findAll(Specification<ZuoybStudentBg> specification, Pageable page);
	
	@Query(nativeQuery = true, value = "SELECT *  from " +
			" zyb_student_bg  " +
			" where  " +
			" TO_DAYS(start_date) = TO_DAYS(?1) and TO_DAYS(end_date) =TO_DAYS(?2) and \n" +
			" vip_id=?3")
	public ZuoybStudentBg findByStartDateAndEndDateAndVipId(String startDate,String endDate,String vipId);
	/**
	 * 获取时间段的总体学习建议，合并为一条
	* @param startDate
	* @param endDate
	* @param vipId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年11月2日 下午6:11:56 
	* @String
	 */
	@Query(nativeQuery = true, value = "SELECT \n" +
			" h.child_name \n" +
			"  from  \n" +
			"zyb_student_work  k  JOIN \n" +
			"zyb_student_work_details  h  on k.id = h.student_work_id\n" +
			"where \n" +
			" TO_DAYS(k.scoring_time) >= TO_DAYS(?1) and TO_DAYS(k.scoring_time) <=TO_DAYS(?2) and \n" +
			"k.vip_id=?3 and h.num =111 ORDER BY CHAR_LENGTH(h.child_name) desc  LIMIT 1 ")
	public String findStartDateAndEndDateAndVipIdAndSuggest(String startDate,String endDate,String vipId);
	/**
	 * 计算某个改知识点易错，只要出现上传图片，就算错题/一个人算一次，累加，
	* @param startDate
	* @param endDate
	* @param howDetailsId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年11月3日 下午2:37:32 
	* @long
	 */
	@Query(nativeQuery = true, value = "SELECT  IFNULL(ROUND(z1.d/z2.c*100,0),0)  from  (\n" +
			"SELECT count(1) d from (\n" +
			"SELECT g.id  from  zyb_student_work_how_details  s  INNER JOIN \n" +
			"  zyb_student_work  k  on k.id = s.student_work_id\n" +
			" INNER JOIN   zyb_student_work_img g\n" +
			" on s.id  =g.how_details_id \n" +
			"where s.how_details_id =?3 AND\n" +
			"k.scoring_time > ?1 and k.scoring_time <=?2 \n" +
			"GROUP BY k.vip_id ) a ) z1 ,\n" +
			"(\n" +
			"SELECT count(1) c  from (\n" +
			"SELECT k.id  from  zyb_student_work_how_details  s  INNER JOIN \n" +
			"  zyb_student_work  k  on k.id = s.student_work_id\n" +
			"where s.how_details_id =?3 \n" +
			"AND TO_DAYS(k.scoring_time) >= TO_DAYS(?1) and TO_DAYS(k.scoring_time) <TO_DAYS(?2) \n" +
			"GROUP BY k.vip_id ) b ) z2")
	public String getYiCuoTi(String startDate,String endDate,String howDetailsId);
	/**
	 * 获取有效时间内有推送记录的vip
	* @param startDate
	* @param endDate
	* @param vipId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年11月4日 下午7:25:53 
	* @List<String>
	 */
	@Query(nativeQuery = true, value = "SELECT  vip_id  from  zyb_student_push  \n" +
			"where  TO_DAYS(push_time)>=TO_DAYS(?1) and  TO_DAYS(push_time)<TO_DAYS(?2) and type =2 \n" +
			"GROUP BY vip_id ")
	public List<String> getStartDateAndEndDateValidVipId(String startDate,String endDate);
}
