package com.ssj.dao.weixin.story.dao;

import java.util.List;

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 com.ssj.bean.weixin.story.bean.StoryCard;

@Repository
public interface StoryCardDao  extends JpaRepository<StoryCard, String>{
	
	 public Page<StoryCard> findAll(Specification<StoryCard> specification, Pageable page);
	 

	 @Query(nativeQuery = true, value =" SELECT  count(1) from  tb_story_card  where  user_id = ?1  and  story_id = ?2 " )
	 public Integer  getUserCount(String userId,String storyId);
	 
	 /**
	  * 打卡的天数
	  * @param userId
	  * @return
	  */
	 @Query(nativeQuery = true, value ="SELECT  count(1) from (\n" +
			 "			 SELECT user_id from   tb_story_card where user_id =?1 \n" +
			 "			 GROUP BY  story_id ) t" )
	 public Integer  punchCardDays(String userId);
	 @Query(nativeQuery = true, value ="SELECT  count(1) from (\n" +
			 "			 SELECT story_task_id from   tb_story_card where user_id =?1 and story_id = ?2 \n" +
			 "			 GROUP BY  story_task_id ) t" )
	 public Integer  punchCardDays(String userId,String storyId);
	 /**
	  * 获取打卡的总人数
	  * @param userId
	  * @return
	  */
	 @Query(nativeQuery = true, value ="SELECT  count(1) from  (\n" +
			 "SELECT user_id from   tb_story_card \n" +
			 "GROUP BY  user_id ) t")
	 public Integer  getUserCard();
	 /**
	  * 查询有多少人打卡
	  * @param storyId
	  * @return
	  */
	 @Query(nativeQuery = true, value ="SELECT  count(1) from (\n" +
			 "			 SELECT user_id from   tb_story_card where story_id =?1 \n" +
			 "			 GROUP BY  user_id ) t")
	 public Integer  getStoryIdCount(String storyId);
	 
	 
	 @Query(nativeQuery = true, value ="SELECT  c.days_num  from  tb_story_card c\n" +
			 "where  c.story_id =?1\n" +
			 "and c.user_id =?2\n" +
			 "GROUP BY c.days_num ")
	 public List<Integer> getStoryCardDayNum(String storyId,String userId);
	 
	 @Query(nativeQuery = true, value =" SELECT  days_num  from   tb_story_card  where story_id = ?1 and user_id = ?2 ORDER BY   days_num asc  ")
	 public List<Integer>  getStoryIdUserDayNum(String storyId,String userId);
	 @Query(nativeQuery = true, value =" SELECT  *  from   tb_story_card  where user_id = ?1 and story_task_id = ?2    ORDER BY   create_time asc limit 1 ")
	 public StoryCard  findByUserIdAndStoryTaskId(String userId,String storyTaskId);
	 
	 
}
