package com.viettel.campaign.repository.ccms_full;

import com.viettel.campaign.config.DataSourceQualify;
import com.viettel.campaign.model.ccms_full.CampaignCustomer;
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;

@Repository
@Transactional(DataSourceQualify.CCMS_FULL)
public interface CampaignCustomerRepository extends JpaRepository<CampaignCustomer, Long>, CampaignCustomerRepositoryCustom {

    @Query(value = "SELECT COUNT(*) " +
            " FROM CAMPAIGN_CUSTOMER CC " +
            " JOIN CAMPAIGN C ON CC.CAMPAIGN_ID = C.CAMPAIGN_ID " +
            " LEFT JOIN CAMPAIGN_COMPLETE_CODE CCC ON CC.STATUS = CCC.COMPLETE_VALUE" +
            " WHERE CC.COMPANY_SITE_ID = :companySiteId " +
            " AND CC.AGENT_ID = :agentId" +
            " AND C.STATUS = 2 " +
            " AND CC.RECALL_TIME <= SYSDATE " +
            " AND CCC.IS_RECALL = 1", nativeQuery = true)
    Long countRecallCustomer(@Param("companySiteId") Long pCompanySiteId, @Param("agentId") Long agentId);

    @Query(value = "SELECT COUNT(*) " +
            "FROM CAMPAIGN_CUSTOMER CC JOIN CAMPAIGN_COMPLETE_CODE CCC ON CC.CAMPAIGN_ID = CCC.CAMPAIGN_ID " +
            "WHERE CC.CAMPAIGN_ID = :campaignId " +
            "AND CC.CUSTOMER_ID = :customerId " +
            "AND CC.STATUS = 3 " +
            "AND CCC.IS_RECALL = 1 " +
            "AND CCC.STATUS = 1 ", nativeQuery = true)
    Long getCustomerRecall(@Param("campaignId") Long campaignId, @Param("customerId") Long customerId);

}
