package com.viettel.campaign.repository.ccms_full;

import com.viettel.campaign.model.ccms_full.CampaignCfg;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CampaignCfgRepository extends JpaRepository<CampaignCfg, Long>{

    @Query(" select u FROM CampaignCfg u WHERE u.status = 1")
    Page<CampaignCfg> findAll(Pageable pageable);


    @Query("FROM CampaignCfg WHERE completeName LIKE concat('%', :name, '%') ")
    List<CampaignCfg> findByName(@Param("name") String name, Pageable pageable);

    List<CampaignCfg> findByCompleteNameContains(String name, Pageable pageable);

    @Modifying
    @Query("update CampaignCfg c set c.status = 0  where c.campaignCompleteCodeId in (:p_ids) and c.companySiteId=:p_company_site_id" )
    int deletedList(@Param("p_ids") List<Long> p_ids, @Param("p_company_site_id") Long p_company_site_id);


    List<CampaignCfg> findCampaignCompleteCodesByCompanySiteId(Long companySiteId);

    @Query(value="SELECT max(completeValue) FROM CampaignCfg WHERE companySiteId = :companySiteId GROUP BY companySiteId")
    Short findByMaxCompanySiteId(Long companySiteId);
    @Modifying
    @Query("update CampaignCfg c set c.status = 0 where c.campaignCompleteCodeId=:p_campaignCompleteCode_list_id and c.companySiteId=:p_company_site_id")
    int deleteCampaignCompleteCodeBy(@Param("p_campaignCompleteCode_list_id") Long p_campaignCompleteCode_list_id, @Param("p_company_site_id") Long p_company_site_id);

    @Query(value = "FROM CampaignCfg WHERE status = 1 AND completeValue = :completeValue AND completeType = :completeType AND companySiteId = :companySiteId")
    List<CampaignCfg> getCustomerStatus(@Param("completeValue") String completeValue, @Param("completeType") Short completeType, @Param("companySiteId") Long companySiteId);

    @Query(value = "FROM CampaignCfg WHERE status = 1 AND completeValue <> :completeValue AND completeType = :completeType AND companySiteId = :companySiteId")
    List<CampaignCfg> getCustomerStatusWithoutValue(@Param("completeValue") String completeValue, @Param("completeType") Short completeType, @Param("companySiteId") Long companySiteId);

    @Query("select c from CampaignCfg c where c.companySiteId =:p_company_site_id")
    List<CampaignCfg> findAllCampaignCompleteCode();
}
