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

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.ssj.bean.weixin.libmy.domain.TbLibJoinConsume;

@Repository
public interface ILibJoinConsumeDao extends JpaRepository<TbLibJoinConsume, String> {

	@Query(nativeQuery = true, value = " SELECT IFNULL(SUM(t.lib_service),0) as count from tb_lib_join_consume t where t.from_lib_id in ?1 AND t.type in (2) AND TO_DAYS(t.create_time)>=TO_DAYS(?2) AND TO_DAYS(t.create_time)<TO_DAYS(?3) ORDER BY t.create_time ASC ")
	Integer countTbLibJoinConsumeByConsumeTime(String[] libIds,Date startTime,Date endTime);
	
	
	@Query(nativeQuery = true, value = " SELECT IFNULL(SUM(t.lib_service),0) as count from tb_lib_join_consume t where t.from_lib_id in ?1 AND t.type in (5) AND TO_DAYS(t.create_time)>=TO_DAYS(?2) AND TO_DAYS(t.create_time)<TO_DAYS(?3) ORDER BY t.create_time ASC ")
	Integer countTbLibJoinDistributionByConsumeTime(String[] libIds,Date startTime,Date endTime);
	
	@Query(nativeQuery = true, value = " SELECT IFNULL(SUM(t.lib_service),0) as count from tb_lib_join_consume t where t.from_merchant_id in ?1 AND t.type in (2,3) AND TO_DAYS(t.create_time)>=TO_DAYS(?2) AND TO_DAYS(t.create_time)<=TO_DAYS(?3) ORDER BY t.create_time ASC ")
	Integer countMerchantConsumeByConsumeTime(String[] merchantIds,Date startTime,Date endTime);
	
	@Query(nativeQuery = true, value = " SELECT IFNULL(COUNT(1),0) from tb_lib_join_consume t WHERE t.from_merchant_id=?1 AND t.type=2 AND t.vip_id is NOT NULL AND TO_DAYS(DATE_SUB(NOW(),INTERVAL 1 DAY))=TO_DAYS(t.create_time) ")
	Integer findTbLibJoinConsumeByFrontDay(String merchantId);
	
	@Query(nativeQuery = true, value = " SELECT IFNULL(COUNT(1),0) from tb_lib_join_consume t WHERE t.from_merchant_id=?1 AND t.type=2 AND t.vip_id is NOT NULL AND TO_DAYS(DATE_SUB(NOW(),INTERVAL 1 DAY))>TO_DAYS(t.create_time) ")
	Integer findTbLibJoinConsumeByBeforeDays(String merchantId);
	
	@Query(nativeQuery = true, value = " SELECT * from tb_lib_join_consume t WHERE t.type=7 AND t.from_merchant_id=?1 AND TO_DAYS(t.create_time)>=TO_DAYS(?2) ORDER BY t.create_time ASC	")
	public List<TbLibJoinConsume> findTbLibJoinConsumeByStartActiveTime(String merchantId, Date startActiveTime);

	
}
