package com.ssj.dao.weixin.sales.dao;

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;

/** 
 * @author  ZhangCaibao 
 * @version 2017年6月22日 下午3:52:24 
 * 类说明 
 */
@Repository
public interface LibVipServiceDao extends JpaRepository<LibVipService, String> {
	
	 public Page<LibVipService> findAll(Specification<LibVipService> specification, Pageable page);

	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where lib_id = ?1 and vip_id =?2 and species=?3   ")
	public LibVipService findByLibIdAndVipIdAndSpecies(String libId,String vipId,String species);
	/**
	 * 这里只取一条，如果有多个馆的服务再另说
	* @param vipId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年6月26日 上午10:35:13 
	* @LibVipService
	 */
	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1  and species =?2  and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now())  LIMIT 1")
	public LibVipService getVipIdService(String vipId ,String species);
	
	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1  and species =?2  and  lib_id = ?3    and  TO_DAYS( date_add(end_time,interval 5 day))  >=  TO_DAYS(now()) order by stat LIMIT 1 ")
	public LibVipService getVipIdService5days(String vipId ,String species,String libId);
	
	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1      and  TO_DAYS( date_add(end_time,interval 5 day))  >=  TO_DAYS(now()) LIMIT 1 ")
	public LibVipService getVipIdZXService5days(String vipId);
	/**
	 * 判断是否有服务。。S001，S003，S004
	* @param vipId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年12月7日 下午4:54:17 
	* @LibVipService
	 */
	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1    and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now()) order by start_time DESC LIMIT 1")
	public LibVipService getVipIdService(String vipId);
	
	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1  and species =?2    and lib_id =?3  and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now())  LIMIT 1")
	public LibVipService getVipIdService(String vipId ,String species,String libId);
	
	
	@Query(nativeQuery = true, value = " select count(1) from   tb_lib_vip_service  where  vip_id =?1  and species =?2  and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now())  ")
	public Integer getVipIdServiceCount(String vipId ,String species);
	@Query(nativeQuery = true, value = " select count(1) from   tb_lib_vip_service  where  vip_id =?1  and species =?2  and lib_id =?3  and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now())  ")
	public Integer getVipIdServiceCount(String vipId ,String species,String libId);
	
	@Query(nativeQuery = true, value = " select count(1) from   tb_lib_vip_service  where  vip_id =?1  and species =?2  and lib_id =?3   and  TO_DAYS( date_add(end_time,interval 5 day))  >=  TO_DAYS(now())  ")
	public Integer getVipIdExpired5days(String vipId ,String species,String libId);
	
	@Query(nativeQuery = true, value = " select count(1) from   tb_lib_vip_service  where  vip_id =?1  and lib_id =?2  and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now())  ")
	public Integer getVipIdAndLibIdCount(String vipId,String libId);
	

	

	 
	 
	 @Query(nativeQuery = true, value =" INSERT into   tb_lib_vip_service (id,vip_id,lib_id,start_time,end_time,stat,species,type,deposit,borrow_num,borrow_day\n" +
			 "			 ,out_money,order_details_id,create_time) VALUES (\n" +
			 "			  UUID() ,?1 , ?2 ,now() ,date_add(now(), INTERVAL 1 YEAR),\n" +
			 "			 1,'S001','C001',0,3,20,0,2,now())")
	 @Modifying
	 public void insertIntoVipIdServiceAndLibId(String vipId,String libId);
	 
	 
	 @Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = ?2   where id =?1 ")
	 @Modifying
	 public Integer updateStat(String id,Integer stat);
	 
	 @Query(nativeQuery = true, value = "update tb_lib_vip_service set zyb_course_id = null , course_hour =null    where vip_id =?1 and lib_id =?2 and species='S003' and  course_hour = ?3 ")
	 @Modifying
	 void  updateByLibIdAndVipIdAndCourseHour(String vipId,String libId,String courseHour);
	 
	 
	 @Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = 2   where order_details_id =?1 ")
	 @Modifying
	 public void  updateVipServiceStat(String orderDeatalId);
	 
	 @Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = 2   where vip_id =?1 and species='S005'  ")
	 @Modifying
	 public void  updateVipServiceByVipId(String vipId);

	@Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = 2,lib_id=null  where vip_id =?1  ")
	@Modifying
	public void  updateVipServiceStatByVipId(String vipId);
	 
	 @Query(nativeQuery = true, value = "SELECT COUNT(DISTINCT(vip_id)) from tb_lib_vip_service  t LEFT JOIN tb_lib_join j on j.id=t.lib_id where j.id=?1 and stat=1 and  ?2 BETWEEN t.start_time AND t.end_time and t.species='S001' ")
	 public Integer getByLibIdCount(String libId,Date selectDate);
	 
	 @Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where lib_id=?1 and stat=1 and species=?2")
	 public Integer getByLibIdAndSpeciesCount(String libId,String species);
	 @Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where  stat=1 and species=?1 ")
	 public Integer getByLibIdAndSpeciesCount(String species);
	 
	 @Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where lib_id in(?1) and stat=1 and species=?2")
	 public Integer getByLibIdAndSpeciesCount(List<String> libIds,String species);

	 @Query(nativeQuery = true, value = "select count(distinct vip_id) from (select vip_id from tb_lib_vip_service where lib_id in (?1) and species in (?2) and stat=1 " +
			 "union select vip_id from tb_lib_vip_bind t2 where master_id=?3) a")
	 public Integer countStudentByLibIds (List<String> libIds, List<String> species, String masterId);
	 
	 @Query(nativeQuery = true, value ="SELECT  e.* from  tb_lib_vip_service  e  JOIN  tb_lib_vip  v\n" +
			 "on e.vip_id  = v.id \n" +
			 "where  v.user_id=?1 AND \n" +
			 "e.species=?2\n" +
			 "GROUP BY vip_id \n" +
			 "ORDER BY  e.stat asc , e.end_time desc ")
	 public List<LibVipService> getByUserIdAndSpeciesCount(String userId,String species);
	 
	 @Query(nativeQuery = true, value = "SELECT e.* FROM tb_lib_vip_service  e  join tb_lib_vip v\n" +
			 "			         on e.vip_id =v.id\n" +
			 "			 			 where  v.user_id=?1\n" +
			 "			 			   and  e.species=?2")
	 public List<LibVipService> getLibServiceAll(String userId,String species);
	 
	 
	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1  and species =?2  and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now())  ")
	public List<LibVipService> getVipIdServiceAll(String vipId ,String species);

	public List<LibVipService> getByVipIdAndSpecies(String vipId ,String species);

	List<LibVipService> getByVipIdAndSpeciesOrderByCreateTimeDesc(String vipId ,String species);
	
	@Query(nativeQuery = true, value = "select * from   tb_lib_vip_service  where  vip_id =?1  and species =?2    and lib_id =?3  and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now())   ")
	public List<LibVipService> getVipIdServiceAll(String vipId ,String species,String libId);
	
	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1    and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now()) ")
	public List<LibVipService> getVipIdServiceAll(String vipId);

	@Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where vip_id = ?1 and  lib_id=?2 and stat=1 and  TO_DAYS(end_time)  >=  TO_DAYS(now()) and is_jia > 0 ")
	public Integer findIsHaveJiaSer(String vipId, String libId);

	@Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where vip_id = ?1 and  lib_id=?2 and stat=1 and  TO_DAYS(end_time)  >=  TO_DAYS(now()) and species =?3 and type in ('C002','C003') ")
	public Integer getMouthServiceCount(String vipid, String libId, String species);

	@Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where vip_id = ?1 and  lib_id=?2 and stat=1 and  TO_DAYS(end_time)  >=  TO_DAYS(now()) and species =?3 and type =?4 and  start_time<'2017-10-01'")
	public Integer getOcBuyServiceCount(String vipid, String libId, String species, String type);

	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1    and  stat=1  and  TO_DAYS(end_time)  >=  TO_DAYS(now()) and lib_id = ?2")
	public List<LibVipService> getVipIdLibServiceAll(String vipId, String libId);

	@Query(nativeQuery = true, value = " select * from   tb_lib_vip_service  where  vip_id =?1  and species ='S001' and lib_id=?2  order by end_time DESC  LIMIT 1")
	public LibVipService getLibVipService(String vipId, String libId);

	@Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where vip_id = ?1 and  lib_id=?2 and stat=1 and  TO_DAYS(end_time)  >=  TO_DAYS(now()) and (is_jia = 0 or is_jia is null)")
	public Integer findJiaServiceByLib(String id, String libId);
	
	@Query(nativeQuery = true, value = "select count(s.id) from  \n" +
			"tb_lib_vip_service  s   \n" +
			"join  tb_lib_vip v on s.vip_id =v.id  \n" +
			"where v.user_id =?1\n" +
			"and  s.stat=1  and  TO_DAYS(s.end_time)  >=  TO_DAYS(now())")
	public Integer getVipServiceByUserId(String userId);

	@Query(nativeQuery = true, value = "select count(*) from tb_lib_vip_service where lib_id=?1 and stat=1 and species='S003'")
	public int countDistinctByLibId(String libId);

	@Query(nativeQuery = true, value = "select * from tb_lib_vip_service where lib_id=?1 and stat=1 and species='S003'")
	public List<LibVipService> findByLibId (String libId);

	public List<LibVipService> findByVipIdInAndLibIdInAndSpeciesInAndStat(String[] vipIds, String[] libIds, String[] species, int state);

	@Query(nativeQuery = true, value = "select * from tb_lib_vip_service where vip_id=?1 and species='S003' and stat = 1 order by start_time desc limit 1")
	public LibVipService findLastZybService (String vipId);

	/**
	 * 根据馆id、vipId查询有效的一对一服务
	 */
	@Query(nativeQuery = true,
			value = "select * from tb_lib_vip_service where lib_id = ?1 and vip_id = ?2 and species = 'S010' and start_time <= ?3 and end_time >= ?3 ")
	LibVipService findEffectiveOne2One(String libId, String libVipId, String currentDateTime);

	@Query(nativeQuery = true, value = "select * from tb_lib_vip_service where lib_id = ?1 and vip_id = ?2 and stat = 1 and ?3 between start_time and end_time")
    LibVipService findEffectiveOneByLibIdAndVipId(String libId, String vipId, String now);

    /**
     * 老版本使用的方法，新版本作业吧服务只有S003了，此方法展示保留
     * @param vipId
     * @return
     */
	@Query(nativeQuery = true, value = "SELECT count(1) from  tb_lib_vip_service where vip_id = ?1 and (species = 'S003' or species = 'S005') and stat = 1")
	public int checkIsZybServiceByVipId(String vipId);

	List<LibVipService> findByVipIdAndSpeciesAndStat(String vipId, String species, int stat);

	List<LibVipService> findByVipIdAndSpecies(String vipId, String species);

	List<LibVipService> findByVipIdAndSpeciesInOrderByEndTimeDesc(String vipId, List<String> species);

	List<LibVipService> findBySpeciesAndStat(String species, int stat);
	
	/*@Query(nativeQuery = true, value = "select DATE_FORMAT(t.end_time,'%Y-%m-%d') endTime from tb_lib_vip_service t "
			+ "where t.vip_id = ?1 and t.species = 'S003' and to_days(t.end_time) >= to_days(now()) ORDER BY t.end_time desc limit 1")
	String selectZybEndTimeByVipId(String vipId);*/
	
	@Query(nativeQuery = true, value = "select 1 from tb_lib_vip_service vs join tb_lib_vip v on vs.vip_id = v.id "
			+ "WHERE v.child_user_id = ?1 and vs.species = 'S003' and to_days(vs.end_time) >= to_days(now()) limit 1")
	String checkZybExistOrExpiredByChildUserId(String childUserId);

	/**
	 * 查询最大结束时间的有效服务
	 * @param vipId
	 * @param species
	 * @return
	 */
	@Query(nativeQuery = true, value = "select * from tb_lib_vip_service " +
			"where vip_id = ?1 and species = ?2 and to_days(end_time) >= to_days(now()) and stat=1 ORDER BY end_time desc limit 1")
	LibVipService findValidTopService(String vipId, String species);
	/**
	 * 查询最大结束时间的有效服务
	 * @param vipId
	 * @param species
	 * @param correctType
	 * @return
	 */
    @Query(nativeQuery = true, value = "select * from tb_lib_vip_service force index(vip_id_end_time_index) " +
            "where vip_id = ?1 and species = ?2 and correct_type = ?3 and to_days(end_time) >= to_days(now()) and stat=1 ORDER BY end_time desc limit 1")
    LibVipService findValidTopService(String vipId, String species, int correctType);

	/**
	 * 查询最大结束时间的服务(包含有效和无效)
	 * @param vipId
	 * @param species
	 * @return
	 */
	LibVipService findTopByVipIdAndSpeciesOrderByEndTimeDesc(String vipId, String species);

	/**
	 * 获取当前有效批改服务
	 * @param vipId
	 * @return
	 */
	@Query(nativeQuery = true, value = "select * from tb_lib_vip_service " +
			"where vip_id = ?1 " +
			"and stat = 1 " +
			"and species in ('S003','S999') " +
			"and to_days(start_time) <= to_days(now()) " +
			"and to_days(end_time) >= to_days(now()) " +
			"ORDER BY species,service_sort,create_time desc,correct_type,start_time limit 1")
	LibVipService getCurrentCorrectService(String vipId);

	/**
	 * 查询过期的服务
	 * @param species 服务种类
	 */
	@Query(nativeQuery = true, value = "select * from tb_lib_vip_service where TO_DAYS(end_time) < TO_DAYS(now()) and stat = 1 and species = ?1 ")
    List<LibVipService> findExpiredListBySpecies(String species);
	
	
	@Query(nativeQuery = true, value = " select * from  tb_lib_vip_service t where  lib_id =?1 and vip_id =?2 and species =?3   and stat=1  ORDER BY start_time ASC ")
	public List<LibVipService> findVipIdServiceByLibId(String libId,String vipId ,String species);
	
	
	@Query(nativeQuery = true, value = " select * from  tb_lib_vip_service t  where   vip_id =?1 and species =?2  and  stat=1 and ((TO_DAYS(start_time) >= TO_DAYS(?3) AND TO_DAYS(start_time) <= TO_DAYS(?4)) OR (TO_DAYS(start_time) <= TO_DAYS(?3) AND TO_DAYS(end_time) >= TO_DAYS(?4)) OR (TO_DAYS(end_time) >= TO_DAYS(?3) AND TO_DAYS(end_time) <= TO_DAYS(?4))) ORDER BY service_sort ASC ")
	public List<LibVipService> findVipIdServiceByIntersectTime(String vipId ,String species,Date startTime,Date endTime);

	@Query(nativeQuery = true, value = " select * from tb_lib_vip_service " +
			"where vip_id =?1 and species =?2 and stat=1 and TO_DAYS(end_time) >= TO_DAYS(now()) " +
			"ORDER BY create_time desc  ")
	List<LibVipService> findValidService(String vipId, String species);

	@Query(nativeQuery = true, value = " select * from tb_lib_vip_service " +
			"where vip_id =?1 and species in (?2) and stat=1 and TO_DAYS(end_time) >= TO_DAYS(now()) " +
			"ORDER BY species,service_sort,create_time desc  ")
	List<LibVipService> findValidService(String vipId, List<String> species);

	@Query(nativeQuery = true, value = " select * from tb_lib_vip_service " +
			"where vip_id =?1 and species =?2 and lib_id=?3 and stat=1 and TO_DAYS(end_time) >= TO_DAYS(now()) " +
			"ORDER BY service_sort,create_time desc ")
	List<LibVipService> findValidService(String vipId, String species, String libId);

	@Query(nativeQuery = true, value = " select * from tb_lib_vip_service " +
			"where species =?1 and lib_id=?2 and stat=1 and TO_DAYS(end_time) >= TO_DAYS(now()) " +
			"ORDER BY service_sort,create_time desc limit 1000")
	List<LibVipService> findValidServiceOfLib(String species, String libId);

	/**
	 * 查找有效期最晚的有效的服务
	 * @param vipId
	 * @param speciesList
	 * @return
	 */
	@Query(nativeQuery = true, value = " select * from tb_lib_vip_service where vip_id =?1 and species in (?2) and stat=1 and TO_DAYS(end_time) >= TO_DAYS(now()) ORDER BY end_time desc limit 1")
	LibVipService findLastValidService(String vipId, List<String> speciesList);

	/**
	 * 查找有效冲突服务
	 * @param vipId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@Query(nativeQuery = true, value = "select *\n" +
			"from tb_lib_vip_service\n" +
			"where stat = 1\n" +
			"  and ((start_time <= ?1 and end_time >= ?1) or\n" +
			"       (start_time <= ?2 and end_time >= ?2) or\n" +
			"       (start_time >= ?1 and end_time <= ?2))\n" +
			"  and vip_id = ?3\n" +
			"  and species = 'S003'\n" +
			"order by start_time;")
	List<LibVipService> findConflictService(String startTime, String endTime, String vipId);


	@Query(nativeQuery = true,value = " select * from tb_lib_vip_service \n" +
			" where vip_id = ?1 \n" +
			" and stat = 1 \n" +
			" and species in ('S003','S999') \n" +
			" and to_days(start_time) <= to_days(now()) \n" +
			" and to_days(end_time) >= to_days(now()) \n" +
			" ORDER BY species,service_sort,correct_type,start_time limit 1;")
	LibVipService findCorrectTypeByVipId(String vipId);

    @Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = 2 where vip_id =?1 ")
    @Modifying
    public void clearService(String vipId);

    @Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = 2 where vip_id =?1 and libId = ?2  ")
    @Modifying
    public void clearService(String vipId, String libId);

    @Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = 2 where vip_id =?1 and lib_id = ?2 and species=?3  ")
    @Modifying
    public void clearService(String vipId, String libId, String species);

    LibVipService findTopByVipIdAndLibIdAndSpeciesAndTypeOrderByCreateTimeDesc(String vipId,String LibId,String species,String type);

    @Query(nativeQuery = true, value = "select tt1.*\n" +
            "from tb_lib_vip_service tt1,\n" +
            "     (select t1.*\n" +
            "      from (select vip_id, lib_id, COUNT(vip_id) sums\n" +
            "            from tb_lib_vip_service\n" +
            "            where species = 'S003'\n" +
            "              AND type = 'C025'\n" +
            "            GROUP BY vip_id, lib_id) t1\n" +
            "      where t1.sums > 1\n" +
            "      order by t1.sums desc) tt2\n" +
            "where tt1.vip_id = tt2.vip_id\n" +
            "  and tt1.lib_id = tt2.lib_id\n" +
            "  and tt1.species = 'S003'\n" +
            "  AND tt1.type = 'C025'\n" +
            "order by tt1.lib_id,tt1.vip_id,tt1.create_time desc limit 2000")
    List<LibVipService> findRepeatSchoolService();

    @Query(nativeQuery = true, value = "update tb_lib_vip_service set stat = 2 where stat = 1 and (species ='S999' or (species = 'S003' and correct_type in (1,3,4)))")
    @Modifying
    void invalidatePlatService();

}
