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

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

import java.util.List;

/** 
 * @author  ZhangCaibao 
 * @version 2017年7月4日 上午11:29:05 
 * 类说明 
 */
@Repository
public interface ZuoybStudentWorkImgDao extends JpaRepository<ZuoybStudentWorkImg, String>{
	
	@Query(nativeQuery = true, value = "delete from  zyb_student_work_img  where object_id = ?1 ")
    @Modifying
	public void deleteByObjectId(String objectId);
	/**
	 * 根据推送ID 查询所有的图片
	* @param pushId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年11月6日 下午5:08:22 
	* @List<ZuoybStudentWorkImg>
	 */
	@Query(nativeQuery = true, value = "SELECT  g.* from  zyb_student_work  k  JOIN zyb_student_work_img g  on\n" +
			"k.id = g.object_id\n" +
			"where k.student_push_id=?1  and g.type =1  LIMIT 2 ")
	public List<ZuoybStudentWorkImg> getObjectList(String pushId);
	
	
	public Long countByObjectIdIn(List<String> objectIds);

	@Query(nativeQuery = true, value = "select COUNT(distinct img) from zyb_student_work_img where object_id in (?1)")
	public int countDistinctByObjectIdIn(List<String> objectIds);


	/**
	 * 根据object_id查询错题和知识点
	 * @param objectId
	 * @return
	 */
	public List<ZuoybStudentWorkImg> findByObjectId(String objectId);

	/**
	 * 统计学生错题数量（type=1为错题）
	 * @param vipId
	 * @return
	 */
	@Query(nativeQuery = true, value = "select COUNT(1) from zyb_student_work_img t1 " +
			" where t1.type=1 and exists (select 1 from zyb_student_work t2 where t1.object_id = t2.id and t2.vip_id = ?1)")
	long countByVipId(String vipId);
}
