package com.cirle.scientific.dao;

import java.util.List;


import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.cirle.scientific.po.QkjLive;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface QkjLiveDao extends PagingAndSortingRepository<QkjLive, Long>, JpaSpecificationExecutor<QkjLive> {

	QkjLive findByLiveId(String liveId);

	@Query(value = "(SELECT a.* FROM (SELECT * FROM qkj_live WHERE live_status=1 and status=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?3) ORDER BY live_begin_time ASC) a) "
					+"	UNION all "
					+"	(SELECT b.* FROM (SELECT * FROM qkj_live WHERE live_status=0 AND STATUS=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?3) ORDER BY live_begin_time ASC) b) "
					+"	UNION all "
					+"	(SELECT b.* FROM (SELECT * FROM qkj_live WHERE STATUS=1 and live_type=2 and m_or_pc in (?3) ORDER BY create_time desc) b) "
					+"	UNION all "
					+"	(SELECT d.* FROM (SELECT * FROM qkj_live WHERE live_status=3 AND STATUS=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?3) ORDER BY live_begin_time desc) d) "
					+"	UNION all "
					+"	(SELECT d.* FROM (SELECT * FROM qkj_live WHERE live_status=2 AND STATUS=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?3) ORDER BY live_begin_time desc) d) "
					+" order by istop  desc , create_time  desc  limit ?1,?2",nativeQuery = true)
    List<QkjLive> findList(int start, int size,Integer[] mOrPc);

	@Query(value = "(SELECT a.* FROM (SELECT * FROM qkj_live WHERE id in(SELECT d.live_id from qkj_live_series d where d.series_id = ?1) and live_status=1 and status=1 and is_visiable =1 ORDER BY live_begin_time ASC) a) "
			+"	UNION all "
			+"	(SELECT b.* FROM (SELECT * FROM qkj_live WHERE id in(SELECT d.live_id from qkj_live_series d where d.series_id = ?1) and live_status=0 AND STATUS=1 and is_visiable =1  ORDER BY live_begin_time ASC) b) "
			+"	UNION all "
			+"	(SELECT d.* FROM (SELECT * FROM qkj_live WHERE id in(SELECT d.live_id from qkj_live_series d where d.series_id = ?1) and live_status>=2 AND STATUS=1 and is_visiable =1  ORDER BY live_begin_time desc) d) "
			+"  order by istop desc limit ?2,?3",nativeQuery = true)
	List<QkjLive> findSonList(int seriesId, int start, int size);

	/**
	 * 更具系列直播id 查出子系列的直播的数据（过滤掉已隐藏的直播）
	 * @param seriesId  系列直播的id
	 * @param start  分页的页数
	 * @param size   每页的行数
	 * @return
	 */
	@Query(value = "SELECT * FROM qkj_live WHERE id = ?1 and  status=1 and is_visiable=1  limit ?2,?3",nativeQuery = true)
	QkjLive selectSonList(int seriesId, int start, int size);

	@Query(value = "SELECT d.live_id from qkj_live_series d where d.series_id = ?1 order by sqn asc ",nativeQuery = true)
	List<Integer> selectLiveIdBySqn(int seriesId);

	@Query(value = "select count(*) from ("
			+"(SELECT a.* FROM (SELECT * FROM qkj_live WHERE live_status=1 and status=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?1) ORDER BY live_begin_time ASC) a) "
			+"	UNION all "
			+"	(SELECT b.* FROM (SELECT * FROM qkj_live WHERE live_status=0 AND STATUS=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?1) ORDER BY live_begin_time ASC) b) "
			+"	UNION all "
			+"	(SELECT b.* FROM (SELECT * FROM qkj_live WHERE STATUS=1 and live_type=2 and m_or_pc in (?1) ORDER BY create_time desc) b) "
			+"	UNION all "
			+"	(SELECT d.* FROM (SELECT * FROM qkj_live WHERE live_status=3 AND STATUS=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?1) ORDER BY live_begin_time desc) d) "
			+"	UNION all "
			+"	(SELECT d.* FROM (SELECT * FROM qkj_live WHERE live_status=2 AND STATUS=1 and live_type!=2 and is_visiable=1 and m_or_pc in (?1) ORDER BY live_begin_time desc) d)" +
			")m ",nativeQuery = true)
	Integer selectAllCounts(Integer[] mOrPc);


	@Query(value = "select a.* from qkj_live a where live_status=1  and  is_visiable=1 ",nativeQuery = true)
	List<QkjLive> findMy();

	@Transactional(propagation= Propagation.REQUIRES_NEW)
	@Modifying
	@Query(value = "update qkj_live set  purchased_members  =  purchased_members + :num where id = :liveId and live_status=1",nativeQuery = true)
	void updatePurchasedMembers(@Param(value = "num")long num, @Param(value = "liveId")long liveId);

	@Transactional(propagation= Propagation.REQUIRES_NEW)
	@Modifying
	@Query(value = "update qkj_live set  join_members  =  join_members + :num where id = :liveId and live_status=1",nativeQuery = true)
	void updateJoinMembers(@Param(value = "num")long num, @Param(value = "liveId")long liveId);

	@Transactional(propagation= Propagation.REQUIRES_NEW)
	@Modifying
	@Query(value = "update qkj_live set  join_members  =  join_members - :num where id = :liveId and live_status=1",nativeQuery = true)
	void reduceJoinMembers(@Param(value = "num")long num, @Param(value = "liveId")long liveId);

	@Query(value = "SELECT * FROM qkj_live WHERE id = ?1 ",nativeQuery = true)
	QkjLive findById(String liveId);
//
//	@Transactional(propagation= Propagation.REQUIRES_NEW)
//	@Modifying
//	@Query("update QkjLive lv set lv.purchasedMembers=lv.purchasedMembers+: num where lv.id=:liveId and lv.liveStatus=1")
//	void updatePurchasedMembers(@Param(value = "num") long num, @Param(value = "liveId")long liveId);


}
