package com.viettel.campaign.repository.ccms_full;

import com.viettel.campaign.config.DataSourceQualify;
import com.viettel.campaign.model.ccms_full.TimeZoneDialMode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author anhvd_itsol
 */

@Repository
@Transactional(DataSourceQualify.CCMS_FULL)
public interface TimeZoneDialModeRepository extends JpaRepository<TimeZoneDialMode, Long> {

    List<TimeZoneDialMode> findTimeZoneDialModeByCampaignIdAndCompanySiteId(Long campaignId, Long companySiteId);

    @Query(value = "SELECT * " +
            "FROM (SELECT R.* " +
            "      FROM TIME_RANGE_DIAL_MODE R " +
            "      WHERE R.CAMPAIGN_ID = 11 " +
            "        AND R.COMPANY_SITE_ID = 662691 " +
            "        AND TO_DATE(R.START_TIME, 'YYYY/MM/DD HH24:MI:SS') <= TO_DATE(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') " +
            "      ORDER BY R.START_TIME DESC " +
            "     ) WHERE ROWNUM = 1", nativeQuery = true)
    TimeZoneDialMode findDialModeAtCurrent(@Param("campaignId") Long campaignId, @Param("companySiteId") Long companySiteId);

    void deleteAllByCampaignIdAndCompanySiteId(Long campaignId, Long companySiteId);
}
