package com.its.op.dao.repository.its.vms;

import com.its.op.entity.its.vms.TbVmsCtrlHs;
import com.its.op.entity.its.vms.TbVmsCtrlHsKey;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface TbVmsCtrlHsRepository extends JpaRepository<TbVmsCtrlHs, TbVmsCtrlHsKey>, JpaSpecificationExecutor<TbVmsCtrlHs> {

    @Query("select p from TbVmsCtrlHs p inner join fetch p.vms where p.ctrlDt between :fromDt and :toDt and p.vmsCtlrNmbr in :ids")
    List<TbVmsCtrlHs> findAllByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt, @Param("ids") List<Long> ids);

    @Transactional
    @Modifying
    @Query(value =
            "INSERT INTO TB_VMS_CTRL_HS(                " +
            "                          CTRL_DT,         " +
            "                          VMS_CTLR_NMBR,   " +
            "                          CTRL_TYPE,       " +
            "                          CTRL_VAL,        " +
            "                          CTRL_RESULT,     " +
            "                          USER_ID          " +
            "                         )                 " +
            "                   VALUES(                 " +
            "                          :CTRL_DT,        " +
            "                          :VMS_CTLR_NMBR,  " +
            "                          :CTRL_TYPE,      " +
            "                          :CTRL_VAL,       " +
            "                          :CTRL_RESULT,    " +
            "                          :USER_ID         " +
            "                         )                 ",
            nativeQuery = true)
    void insertHs(@Param("CTRL_DT") String CTRL_DT,
                  @Param("VMS_CTLR_NMBR") Long VMS_CTLR_NMBR,
                  @Param("CTRL_TYPE") String CTRL_TYPE,
                  @Param("CTRL_VAL") String CTRL_VAL,
                  @Param("CTRL_RESULT") Integer CTRL_RESULT,
                  @Param("USER_ID") String USER_ID);
}
