package com.its.api.its.repository.vms;

import com.its.api.its.model.entity.vms.TbVmsMsgDsplSch;
import com.its.api.its.model.entity.vms.TbVmsMsgDsplSchKey;
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 org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TbVmsMsgDsplSchRepository extends JpaRepository<TbVmsMsgDsplSch, TbVmsMsgDsplSchKey>, JpaSpecificationExecutor<TbVmsMsgDsplSch> {

    @Modifying
    @Query("delete from TbVmsMsgDsplSch p where p.vmsCtlrNmbr = :vmsCtlrNmbr and p.trfcSituTypeCd in :trfcSituTypeCd")
    void deleteDataByVmsCtrlTrfcSituTypeCd(@Param("vmsCtlrNmbr") Long vmsCtlrNmbr, @Param("trfcSituTypeCd") List<String> trfcSituTypeCd);

    @Query("select p from TbVmsMsgDsplSch p inner join fetch p.form where p.vmsCtlrNmbr = :vmsCtlrNmbr and p.trfcSituTypeCd in :trfcSituTypeCd")
    List<TbVmsMsgDsplSch> findAllByVmsCtrlTrfcSituTypeCd(@Param("vmsCtlrNmbr") Long vmsCtlrNmbr, @Param("trfcSituTypeCd") List<String> trfcSituTypeCd);

    @Query("select nvl(max(p.phase), 0) + 1 as maxPhase from TbVmsMsgDsplSch p where p.vmsCtlrNmbr = :vmsCtlrNmbr and p.trfcSituTypeCd = :trfcSituTypeCd")
    Integer findMaxPhaseByVmsCtrlTrfcSituTypeCd(@Param("vmsCtlrNmbr") Long vmsCtlrNmbr, @Param("trfcSituTypeCd") String trfcSituTypeCd);

    @Query("select p from TbVmsMsgDsplSch p inner join fetch p.form where p.trfcSituTypeCd = :trfcSituTypeCd and p.vmsFormId = :vmsFormId")
    List<TbVmsMsgDsplSch> findAllByAllEditVmsList(@Param("trfcSituTypeCd") String trfcSituTypeCd, @Param("vmsFormId") Integer vmsFormId);

}
