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

import com.its.op.entity.its.vms.TbVmsFormObject;
import com.its.op.entity.its.vms.TbVmsFormObjectKey;
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 TbVmsFormObjectRepository extends JpaRepository<TbVmsFormObject, TbVmsFormObjectKey>, JpaSpecificationExecutor<TbVmsFormObject> {

    @Query("select p from TbVmsFormObject p")
    List<TbVmsFormObject> findAll();

    @Query("select count(p.symbLibNmbr) from TbVmsFormObject p where p.symbLibNmbr = :id")
    Integer findUsageCountBySymbolId(@Param("id") Integer id);

    @Query("select count(p.vmsIfscId) from TbVmsFormObject p where p.vmsIfscId = :vmsIfscId")
    Integer findUsageCountByVmsIfscId(@Param("vmsIfscId") Long vmsIfscId);

    @Modifying
    @Query("delete from TbVmsFormObject p where p.vmsFormId = :vmsFormId")
    void deleteDataById(@Param("vmsFormId") Integer vmsFormId);

    @Transactional
    @Modifying
    @Query(value = 
            "INSERT INTO TB_VMS_FORM_OBJECT (            " +
            "                VMS_FORM_ID,                " +
            "                VMS_FORM_OBJECT_ID,         " +
            "                VMS_FORM_DSPL_ROW,          " +
            "                VMS_FORM_DSPL_CLMN,         " +
            "                VMS_FORM_OBJECT_TYPE_CD,    " +
            "                VMS_FONT_NAME_CD,           " +
            "                VMS_FONT_COLR_CD,           " +
            "                VMS_FONT_BOLD,              " +
            "                VMS_FONT_SIZE,              " +
            "                VMS_FONT_ALIGN,             " +
            "                VMS_DSPL_TXT,               " +
            "                VMS_DSPL_FIGR,              " +
            "                VMS_DSPL_XCRDN,             " +
            "                VMS_DSPL_YCRDN,             " +
            "                VMS_DSPL_WIDTH,             " +
            "                VMS_DSPL_HEIGHT,            " +
            "                VMS_DSPL_BLINKING,          " +
            "                VMS_DSPL_BKCOLOR,           " +
            "                VMS_DSPL_SIZE,              " +
            "                TRFC_FILL_CD,               " +
            "                SYMB_LIB_NMBR,              " +
            "                VMS_IFSC_ID                 " +
            "                )                           " +
            "  SELECT                                    " +
            "                :newFormId,                 " +
            "                VMS_FORM_OBJECT_ID,         " +
            "                VMS_FORM_DSPL_ROW,          " +
            "                VMS_FORM_DSPL_CLMN,         " +
            "                VMS_FORM_OBJECT_TYPE_CD,    " +
            "                VMS_FONT_NAME_CD,           " +
            "                VMS_FONT_COLR_CD,           " +
            "                VMS_FONT_BOLD,              " +
            "                VMS_FONT_SIZE,              " +
            "                VMS_FONT_ALIGN,             " +
            "                VMS_DSPL_TXT,               " +
            "                VMS_DSPL_FIGR,              " +
            "                VMS_DSPL_XCRDN,             " +
            "                VMS_DSPL_YCRDN,             " +
            "                VMS_DSPL_WIDTH,             " +
            "                VMS_DSPL_HEIGHT,            " +
            "                VMS_DSPL_BLINKING,          " +
            "                VMS_DSPL_BKCOLOR,           " +
            "                VMS_DSPL_SIZE,              " +
            "                TRFC_FILL_CD,               " +
            "                SYMB_LIB_NMBR,              " +
            "                VMS_IFSC_ID                 " +
            "  FROM TB_VMS_FORM_OBJECT                   " +
            " WHERE VMS_FORM_ID = :orgFormId             ", 
            nativeQuery = true)
    void copyFormObjects(@Param("newFormId") Integer newFormId, @Param("orgFormId") Integer orgFormId);

}
