package com.dbnt.kcscbackend.admin.logs.repository;

import com.dbnt.kcscbackend.admin.logs.entity.TnDailyUserConnLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserLogsRepository extends JpaRepository<TnDailyUserConnLog, Long> {

    @Query(value = "SELECT COUNT(DISTINCT log_dt) "
            + "FROM tn_daily_user_log "
            + "WHERE log_dt BETWEEN TO_DATE(:startDate, 'YYYY-MM-DD') AND TO_DATE(:endDate, 'YYYY-MM-DD')", nativeQuery = true)
    long countDistinctUserIds(@Param("startDate") String startDate, @Param("endDate") String endDate);

    @Query(value = "SELECT TO_CHAR(log_dt, 'YYYY-MM-DD') as log_dt, sum(log_cnt) as log_cnt "
            + "FROM tn_daily_user_log "
            + "WHERE log_dt BETWEEN TO_DATE(:startDate, 'YYYY-MM-DD') AND TO_DATE(:endDate, 'YYYY-MM-DD') "
            + "GROUP BY TO_CHAR(log_dt, 'YYYY-MM-DD') "
            + "ORDER BY log_dt asc", nativeQuery = true)
    List<Object[]> selectCountUser(@Param("startDate") String startDate, @Param("endDate") String endDate);


}
