package com.ssj.dao.weixin.library.dao;

import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.sales.domain.LibVipService;
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;
import java.util.Optional;

@Repository
public interface ILibVipDao extends JpaRepository<LibVip, String> {

	/**
	 * 分页查询列表
	 * @param specification
	 * @param page
	 * @return
	 */
	Page<LibVip> findAll(Specification<LibVip> specification, Pageable page);

	LibVip findByUserId(String userId);
	
	@Query(nativeQuery = true, value = "select *  from tb_lib_vip  where qrcode_url is  null   order by flow_id ASC ")
	List<LibVip>  getLibVipIsNullQrCodeAll();

	LibVip findByUserIdAndStat(String userId, String stat);
	
	@Query(nativeQuery = true, value = "select t.*  from tb_lib_vip t left join tb_lib_vip_service t11 on t.id = t11.vip_id  where t.user_id = ?1 and t11.species = ?2 and t11.stat=1 and  TO_DAYS(end_time)  >=  TO_DAYS(now()) ")
	List<LibVip> findListByUserId(String id,String species);
		
	
	@Query(nativeQuery = true, value = "select *  from tb_lib_vip  where user_id = ?1")
	List<LibVip> getByUserId(String userId);
	
	@Query(nativeQuery = true, value = "select *  from tb_lib_vip  where user_id = ?1  and  derver is null ")
	List<LibVip> getByUserIdDerverIsNull(String userId);
	
	@Query(nativeQuery = true, value = "select *  from tb_lib_vip  where flow_id = ?1 LIMIT 1 ")
	LibVip findByCode(String code);
	
	@Query(nativeQuery = true, value = "SELECT  v.*   FROM  tb_lib_vip v   JOIN tb_lib_vip_service  e \n" +
			"  on e.vip_id =v.id\n" +
			"where   v.user_id=?1 and e.species=?2 \n" +
			"GROUP BY v.id  order by e.stat ASC  ")
	List<LibVip> findByUserIdAndSpeciseService(String userId,String species);
	@Query(nativeQuery = true, value = "SELECT  v.*   FROM  tb_lib_vip v   JOIN tb_lib_vip_service  e \n" +
			"  on e.vip_id =v.id\n" +
			"where   v.user_id=?1 and e.species=?2 and e.lib_id =?3 and e.stat=1 \n" +
			"GROUP BY v.id  order by e.stat ASC  ")
	List<LibVip> findByUserIdAndSpeciseService(String userId,String species,String libId);

	@Query(nativeQuery = true, value = "select *  from tb_lib_vip  where user_id = ?1 LIMIT 1 ")
	LibVip findOneByUserId(String userId);
	
	@Query(nativeQuery = true, value = "SELECT   v.*  from  zyb_student_push s join  tb_lib_vip  v  on s.vip_id  =v.id\n" +
			"			where  TO_DAYS(s.push_time)>=TO_DAYS(?1) and  TO_DAYS(s.push_time)<TO_DAYS(?2) and s.type =2 \n" +
			"			GROUP BY s.vip_id ")
	public List<LibVip> getStartDateAndEndDateValidVip(String startDate,String endDate);
	
	@Query(nativeQuery = true, value = "select *  from tb_lib_vip  where card_code = ?1 LIMIT 1 ")
	LibVip findByCardCode(String cardCode);

	public LibVip findByDerver(String derver);

	@Query(value = "select count(distinct t.derver) from tb_lib_vip t left join scon_problem_picture t1 on t.derver = t1.derver  where teacher_id = ?1" , nativeQuery = true)
	public int findDitinctVipByTearcherIdTotal(String teacherId);
	
	 @Query(nativeQuery = true, value = "update tb_lib_vip  set derver =null  where id = ?1 ")
	 @Modifying
	 public void updateVipByVipId(String vipId);
	 
	 @Query(value = "select distinct user_id from tb_lib_vip t where t.derver != '' group by t.derver" , nativeQuery = true)
	 public List<String> findDistinctUserIdList();

	 public List<LibVip> findByDerverIn(List<String> dervers);

	 public List<LibVip> findByIdIn(List<String> ids);

	@Query(nativeQuery = true, value = "select * from tb_lib_vip t where t.child_name REGEXP ?1")
	public List<LibVip> findByChildNameLike(String childName);
	
	public LibVip findByQrcodeUrl(String qrcodeUrl);

	public LibVip findByChildUserId(String childUserId);

	@Query(nativeQuery = true, value = "select * from tb_lib_vip t where t.user_id=?1 and t.child_name = ?2 limit 1")
	public LibVip findByUserIdAndChildName (String userId, String childName);
	
	@Query(nativeQuery = true, value = "select * from tb_lib_vip t where t.user_id=?1 and t.child_name = ?2 ")
	public List<LibVip> findByUserIdAndChildNames (String userId, String childName);

	@Query(nativeQuery = true, value = "select * from tb_lib_vip where (user_id = ?1 and child_user_id = ?2) or (user_id = ?2 and child_user_id = ?1)")
	public LibVip validateIsParent(String userId, String childUserId);
	
	@Query(nativeQuery = true, value = "select t.* from tb_lib_vip t LEFT JOIN ssj_parent_role p ON t.id = p.vip_id where p.role_user_id = ?1 ")
	public List<LibVip> getOtherChildInfo(String userId);
	
	@Query(nativeQuery = true, value = "select p.role_name from tb_lib_vip t LEFT JOIN ssj_parent_role p ON t.id = p.vip_id "
			+ "where (t.child_user_id = ?1 or t.child_user_id = ?2) and (p.role_user_id = ?1 or p.role_user_id = ?2)")
	public String validateIsSecondParent(String userId, String childUserId);
	
	@Query(nativeQuery = true, value = "update tb_lib_vip set grade = ?1  where grade = ?2")
	@Modifying
	public void updateGradeByGrade(String newGrade,String oldGrade);

	@Query(nativeQuery = true,value = "select * from tb_lib_vip where id=?1")
	public Optional<LibVip> findById(String id);

	@Query(nativeQuery = true,value = "select count(id) from tb_lib_vip where activate_time<now() and activate_time>=date_add(now(), interval -7 day)")
	public int findActiveNumByActivateTime();

	/**
	 * 查询指定小孩
	 * @param childName
	 * @param grade
	 * @param school
	 * @param parentName
	 * @return
	 */
	List<LibVip> findByChildNameAndGradeAndSchoolAndParentName(String childName, String grade, String school, String parentName);

	@Query(nativeQuery = true,value = "select * from tb_lib_vip_service where vip_id=?1 and stat=1 and species='S999' ")
	LibVipService findInnerStudentByVipId(String vipId);
	
	@Query(nativeQuery = true,value = "SELECT v.* from  tb_lib_vip v  where v.stat=1  AND v.lib_id=?1 ORDER BY v.create_time DESC LIMIT 10000")
	public List<LibVip> findByLibId(String libId);

	@Query(nativeQuery = true,value = "select * from tb_lib_vip where user_id=?1 ")
	public List<LibVip> findByUseer(String parentId);
	
	@Query(nativeQuery = true,value = "SELECT v.* from  tb_lib_vip v  where v.activate_time>=?2  AND v.lib_id=?1 AND v.stat=1 ORDER BY v.create_time DESC LIMIT 10000")
	public List<LibVip> findByLibIdActivateTime(String libId,Date signTime);
	
	int countByLibId(String libId);
	
	 @Query(nativeQuery = true, value = "update tb_lib_vip set lib_id = null where lib_id = ?1 ")
	 @Modifying
	 public void updateLibId(String libId);
	 
	 
		        
	
	
}
