package com.izouma.nineth.repo;

import com.izouma.nineth.domain.InviteDate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

public interface InviteDateRepo  extends JpaRepository<InviteDate, Long>, JpaSpecificationExecutor<InviteDate> {
    @Query("update InviteDate t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);


    /**
     * 根据活动ID查询邀请数据
     * @param activityId 1
     * @return
     */
    @Query( nativeQuery = true ,value ="select * from invite_date where activity_id = ?1 and status = ?2 and del = false")
    List<InviteDate> findActivityIdDate(Long activityId,int status);


    /**
     * 查询邀请者成功邀请数量
     * @param activityId 1
     * @return
     */
    @Query( nativeQuery = true ,value ="select count(*) from invite_date where activity_id = ?1 and inviter_user_id = ?2 and status = ?3 and del = false")
    Integer findinviterUserIdCount(Long activityId,Long inviterUserId,int status);

    /**
     * 查询邀请者获得空投数量
     * @param activityId 1
     * @return
     */
    @Query( nativeQuery = true ,value ="select sum(air_drop_award) from invite_date where activity_id = ?1 and inviter_user_id = ?2 and del = false")
    Integer findinviterAirDropAward(Long activityId,Long inviterUserId);


    /**
     * 根据活动ID查询邀请数据
     * @param activityId 1
     * @return
     */
    @Query( nativeQuery = true ,value ="select inviter_user_id, count(*) num from invite_date where activity_id = ?1 and status = ?2 and del = false " +
            " group by  inviter_user_id  order by count(*) DESC limit 30 ")
    List<Map<String,Object>> findActivityIdDateGroup(Long activityId, int status);



    /**
     * 查询需要发放奖励的记录
     * @return
     */
    @Query( nativeQuery = true ,value ="select * from invite_date where automatic_rewards = true  and del = false  limit ?1")
    List<InviteDate> findActivitydata(int count);


}
