<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.its.vms.dao.mapper.VmsManageMapper">

    <insert id="initVmsDsplPrst" parameterType="java.util.Map">
    <![CDATA[
        INSERT INTO TB_VMS_DSPL_PRST(VMS_CTLR_NMBR, PHASE, DSPL_DT, DNLD_YN)
        SELECT VMS_CTLR_NMBR, PHASE, DSPL_DT, DNLD_YN
        FROM (SELECT A.VMS_CTLR_NMBR, B.ORD AS PHASE, '19700101000000' AS DSPL_DT, 'N' AS DNLD_YN
              FROM TB_VMS_CTLR A,
                   (SELECT LEVEL AS ORD FROM DUAL CONNECT BY LEVEL < #{maxPhase}) B
             ) X
        WHERE (VMS_CTLR_NMBR, PHASE) NOT IN (SELECT VMS_CTLR_NMBR, PHASE FROM TB_VMS_DSPL_PRST)
        ORDER BY VMS_CTLR_NMBR, PHASE
        ]]>
    </insert>

    <select id="selectVmsOnOffTime" resultType="com.its.vms.entity.TbVmsOnOffTime" fetchSize="100">
    <![CDATA[
        select t1.vms_ctlr_nmbr AS vmsCtlrNmbr,
               t1.panl_on_time  AS panlOnTime,
               t1.panl_off_time AS panlOffTime
        from tb_vms_ctlr t1
        ]]>
    </select>

    <select id="selectVmsOperMode" resultType="com.its.vms.entity.TbVmsOperMode" fetchSize="100">
    <![CDATA[
        select vms_ctlr_nmbr             AS vmsCtlrNmbr,
               oper_mode                 AS operMode,
               nvl(vms_max_phse_num, 16) AS vmsMaxPhaseNum
        from tb_vms_ctlr
        ]]>
    </select>

    <select id="selectVmsIncd" resultType="com.its.vms.entity.TbVmsIncd" fetchSize="50">
    <![CDATA[
        select v.vms_ctlr_nmbr               AS vmsCtlrNmbr,
               v.link_id                     AS linkId,
               v.ifsc_id                     AS ifscId,
               nvl(v.vms_ifsc_id, 0)         AS vmsIfscId,
               nvl(v.vms_ifsc_nm, ' ')       AS vmsIfscNm,
               nvl(v.dspl_strt_node_nm, ' ') AS dsplStrtNodeNm,
               nvl(v.dspl_end_node_nm, ' ')  AS dsplEndNodeNm,
               nvl(v.detr_id, 0)             AS detrId,
               v.dspl_prrt                   AS dsplPrrt,
               i.incd_ocrr_id                AS incdOcrrId,
               nvl(i.strt_lctn_nm, ' ')      AS strtLctnNm,
               nvl(v.road_nm, ' ')           AS roadNm,
               nvl(v.spot_nm, ' ')           AS spotNm,
               nvl(i.end_lctn_nm, ' ')       AS endLctnNm,
               nvl(i.ocrr_lctn_nm, ' ')      AS ocrrLctnNm,
               nvl(i.incd_titl, ' ')         AS incdTitl,
               (select cmmn_cd_kor_nm
                from tb_cmmn_cd
                where cmmn_cd = i.vms_incd_type_cd
                  and cmmn_clsf_cd = 'VIT')  AS vmsIncdTypeNm,
               i.vms_incd_type_cd            AS vmsIncdTypeCd,
               case when i.vms_incd_type_cd = 'VIT1' then
                        (select cmmn_cd_kor_nm
                         from tb_cmmn_cd
                         where cmmn_cd      = i.vms_incd_detl_type_cd
                           and cmmn_clsf_cd = 'IDT')
                    else
                        (select cmmn_cd_kor_nm
                         from tb_cmmn_cd
                         where cmmn_cd      = i.vms_incd_detl_type_cd
                           and cmmn_clsf_cd = 'EDT')
                   end                            AS vmsIncdDetlNm,
               i.vms_incd_detl_type_cd        AS vmsIncdDetlTypeCd,
               (select cmmn_cd_kor_nm
                from tb_cmmn_cd
                where cmmn_cd      = i.cmtr_grad_cd
                  and cmmn_clsf_cd = 'LTCU') AS cmtrGradNm,
               i.cmtr_grad_cd                 AS cmtrGradCd,
               i.incd_clsr_lane               AS incdClsrLane,
               i.incd_strt_dt                 AS incdStrtDt,
               i.incd_end_prar_dt             AS incdEndPrarDt
        from (select d.vms_ctlr_nmbr, a.link_id,
                     a.ifsc_id, b.vms_ifsc_id,
                     c.vms_ifsc_nm,
                     c.dspl_strt_node_nm,
                     c.dspl_end_node_nm,
                     nvl(c.detr_id, 0) as detr_id,
                     c.road_nm, c.spot_nm,
                     d.dspl_prrt
              from tb_ifsc_rltn_link      a,
                   tb_vms_ifsc_rltn_ifsc  b,
                   tb_vms_ifsc            c,
                   tb_vms_rltn_ifsc       d
              where a.ifsc_id     = b.ifsc_id
                and b.vms_ifsc_id = c.vms_ifsc_id
                and c.vms_ifsc_id = d.vms_ifsc_id ) v,
             (select a.*, b.incd_titl
              from tb_incd_ocrr_vms a,
                   tb_incd_ocrr     b
              where a.incd_prgr_step_cd = 'ISS2'
                and a.vms_dspl_yn       = 'Y'
                and a.incd_ocrr_id      = b.incd_ocrr_id
                and a.incd_end_prar_dt  > to_char(sysdate, 'YYYYMMDDHH24MISS')
                and a.vms_incd_type_cd in ('VIT1', 'VIT2') ) i
        where v.link_id = i.link_id
        order by v.vms_ctlr_nmbr
        ]]>
    </select>

    <select id="selectVmsDsplSchedule" resultType="com.its.vms.entity.TbVmsSchedule" fetchSize="500">
    <![CDATA[
        select vms_ctlr_nmbr            AS vmsCtlrNmbr,
               vms_sch_type             AS vmsSchType,
               vms_sch_form_type        AS vmsSchFormType,
               phase                    AS phase,
               vms_form_id              AS vmsFormId,
               dspl_strt_hh             AS dsplStrtHh,
               dspl_end_hh              AS dsplEndHh,
               dspl_hh                  AS dsplHh,
               nvl(frst_vms_ifsc_id, 0) AS frstVmsIfscId,
               nvl(secd_vms_ifsc_id, 0) AS secdVmsIfscId,
               nvl(thir_vms_ifsc_id, 0) AS thirVmsIfscId,
               nvl(four_vms_ifsc_id, 0) AS fourVmsIfscId,
               nvl(frst_img_ifsc_id, 0) AS frstImgIfscId,
               nvl(secd_img_ifsc_id, 0) AS secdImgIfscId,
               nvl(thir_img_ifsc_id, 0) AS thirImgIfscId,
               nvl(four_img_ifsc_id, 0) AS fourImgIfscId,
               nvl(symb_lib_nmbr, 0)    AS symbLibNmbr,
               strm_addr                AS strmAddr,
               use_yn                   AS useYn,
               dspl_cngs_yn             AS dsplCngsYn,
               vms_form_type_cd         AS vmsFormTypeCd,
               vms_form_sub_type_cd     AS vmsFormSubTypeCd
        from (
                 select b.*, c.vms_form_type_cd,
                        c.vms_form_dspl_drct_cd as vms_form_sub_type_cd,
                        substr(decode(to_char(sysdate, 'd'),
                                      '1', nvl(b.sch_week1, '111111111111111111111111'),
                                      '2', nvl(b.sch_week2, '111111111111111111111111'),
                                      '3', nvl(b.sch_week3, '111111111111111111111111'),
                                      '4', nvl(b.sch_week4, '111111111111111111111111'),
                                      '5', nvl(b.sch_week5, '111111111111111111111111'),
                                      '6', nvl(b.sch_week6, '111111111111111111111111'),
                                           nvl(b.sch_week7, '111111111111111111111111')),
                               to_number(to_char(sysdate, 'HH24'))+1, 1) as sch_yn
                 from tb_vms_ctlr      a,
                      tb_vms_dspl_sch  b,
                      tb_vms_form      c
                 where a.del_yn        = 'N'
                   and b.use_yn        = 'Y'
                   and c.valid_yn      = 'Y'
                   and a.vms_ctlr_nmbr = b.vms_ctlr_nmbr
                   and a.oper_mode     = b.vms_sch_type
                   and b.vms_form_id   = c.vms_form_id
                   and to_char(sysdate, 'YYYYMMDDHH24MISS') between
                     b.dspl_strt_hh and b.dspl_end_hh
             )
        where sch_yn = '1'
        order by vms_ctlr_nmbr asc, phase asc
        ]]>
    </select>

    <update id="batchMergeVmsDsplPrst" parameterType="java.util.Map">
    <![CDATA[
        merge into tb_vms_dspl_prst L
            using (select
                       #{vmsCtlrNmbr}       AS vms_ctlr_nmbr,
                       #{phase}             AS phase,
                       #{dsplDt}            AS dspl_dt,
                       #{vmsSchType}        AS vms_sch_type,
                       #{vmsSchFormType}    AS vms_sch_form_type,
                       #{vmsFormId}         AS vms_form_id,
                       #{dsplHh}            AS dspl_hh,
                       #{vmsDsplMsgData}    AS vms_dspl_msg_data,
                       #{vmsDsplMsgTxt}     AS vms_dspl_msg_txt,
                       #{vmsFormDsplDrctCd} AS vms_form_dspl_drct_cd,
                       #{vmsFormDsplMthdCd} AS vms_form_dspl_mthd_cd,
                       #{vmsDsplMsgImag}    AS vms_dspl_msg_imag,
                       #{dnldYn}            AS dnld_yn
                   from dual) M
            on (L.vms_ctlr_nmbr = M.vms_ctlr_nmbr and L.phase = M.phase)
            when matched then
                update set
                    L.dspl_dt               = M.dspl_dt,
                    L.vms_sch_type          = M.vms_sch_type,
                    L.vms_sch_form_type     = M.vms_sch_form_type,
                    L.vms_form_id           = M.vms_form_id,
                    L.dspl_hh               = M.dspl_hh,
                    L.vms_dspl_msg_data     = M.vms_dspl_msg_data,
                    L.vms_dspl_msg_txt      = M.vms_dspl_msg_txt,
                    L.vms_form_dspl_drct_cd = M.vms_form_dspl_drct_cd,
                    L.vms_form_dspl_mthd_cd = M.vms_form_dspl_mthd_cd,
                    L.vms_dspl_msg_imag     = M.vms_dspl_msg_imag,
                    L.dnld_yn               = M.dnld_yn
            when not matched then
                insert (
                        vms_ctlr_nmbr,
                        phase,
                        dspl_dt,
                        vms_sch_type,
                        vms_sch_form_type,
                        vms_form_id,
                        dspl_hh,
                        vms_dspl_msg_data,
                        vms_dspl_msg_txt,
                        vms_form_dspl_drct_cd,
                        vms_form_dspl_mthd_cd,
                        vms_dspl_msg_imag,
                        dnld_yn )
                    values (
                               M.vms_ctlr_nmbr,
                               M.phase,
                               M.dspl_dt,
                               M.vms_sch_type,
                               M.vms_sch_form_type,
                               M.vms_form_id,
                               M.dspl_hh,
                               M.vms_dspl_msg_data,
                               M.vms_dspl_msg_txt,
                               M.vms_form_dspl_drct_cd,
                               M.vms_form_dspl_mthd_cd,
                               M.vms_dspl_msg_imag,
                               M.dnld_yn )
        ]]>
    </update>


    <update id="mergeVmsDsplPrst" parameterType="com.its.vms.entity.TbVmsDsplPrst">
    <![CDATA[
        merge into tb_vms_dspl_prst L
            using (select
                       #{obj.vmsCtlrNmbr}       AS vms_ctlr_nmbr,
                       #{obj.phase}             AS phase,
                       #{obj.dsplDt}            AS dspl_dt,
                       #{obj.vmsSchType}        AS vms_sch_type,
                       #{obj.vmsSchFormType}    AS vms_sch_form_type,
                       #{obj.vmsFormId}         AS vms_form_id,
                       #{obj.dsplHh}            AS dspl_hh,
                       #{obj.vmsDsplMsgData}    AS vms_dspl_msg_data,
                       #{obj.vmsDsplMsgTxt}     AS vms_dspl_msg_txt,
                       #{obj.vmsFormDsplDrctCd} AS vms_form_dspl_drct_cd,
                       #{obj.vmsFormDsplMthdCd} AS vms_form_dspl_mthd_cd,
                       #{obj.vmsDsplMsgImag}    AS vms_dspl_msg_imag,
                       #{obj.dnldYn}            AS dnld_yn
                   from dual) M
            on (L.vms_ctlr_nmbr = M.vms_ctlr_nmbr and L.phase = M.phase)
            when matched then
                update set
                    L.dspl_dt               = M.dspl_dt,
                    L.vms_sch_type          = M.vms_sch_type,
                    L.vms_sch_form_type     = M.vms_sch_form_type,
                    L.vms_form_id           = M.vms_form_id,
                    L.dspl_hh               = M.dspl_hh,
                    L.vms_dspl_msg_data     = M.vms_dspl_msg_data,
                    L.vms_dspl_msg_txt      = M.vms_dspl_msg_txt,
                    L.vms_form_dspl_drct_cd = M.vms_form_dspl_drct_cd,
                    L.vms_form_dspl_mthd_cd = M.vms_form_dspl_mthd_cd,
                    L.vms_dspl_msg_imag     = M.vms_dspl_msg_imag,
                    L.dnld_yn               = M.dnld_yn
            when not matched then
                insert (
                        vms_ctlr_nmbr,
                        phase,
                        dspl_dt,
                        vms_sch_type,
                        vms_sch_form_type,
                        vms_form_id,
                        dspl_hh,
                        vms_dspl_msg_data,
                        vms_dspl_msg_txt,
                        vms_form_dspl_drct_cd,
                        vms_form_dspl_mthd_cd,
                        vms_dspl_msg_imag,
                        dnld_yn )
                    values (
                               M.vms_ctlr_nmbr,
                               M.phase,
                               M.dspl_dt,
                               M.vms_sch_type,
                               M.vms_sch_form_type,
                               M.vms_form_id,
                               M.dspl_hh,
                               M.vms_dspl_msg_data,
                               M.vms_dspl_msg_txt,
                               M.vms_form_dspl_drct_cd,
                               M.vms_form_dspl_mthd_cd,
                               M.vms_dspl_msg_imag,
                               M.dnld_yn )
        ]]>
    </update>

    <update id="updateVmsDsplPrst" parameterType="com.its.vms.entity.TbVmsDsplPrst">
    <![CDATA[
        update tb_vms_dspl_prst
        set
            dspl_dt               = #{obj.dsplDt},
            vms_sch_type          = #{obj.vmsSchType},
            vms_sch_form_type     = #{obj.vmsSchFormType},
            vms_form_id           = #{obj.vmsFormId},
            dspl_hh               = #{obj.dsplHh},
            vms_dspl_msg_data     = #{obj.vmsDsplMsgData},
            vms_dspl_msg_txt      = #{obj.vmsDsplMsgTxt},
            vms_form_dspl_drct_cd = #{obj.vmsFormDsplDrctCd},
            vms_form_dspl_mthd_cd = #{obj.vmsFormDsplMthdCd},
            vms_dspl_msg_imag     = #{obj.vmsDsplMsgImag},
            dnld_yn               = #{obj.dnldYn}
        where vms_ctlr_nmbr = #{obj.vmsCtlrNmbr}
          and phase         = #{obj.phase}
        ]]>
    </update>

    <update id="batchUpdateVmsDsplPrst" parameterType="java.util.Map">
    <![CDATA[
        update tb_vms_dspl_prst
        set
            dspl_dt               = #{dsplDt},
            vms_sch_type          = #{vmsSchType},
            vms_sch_form_type     = #{vmsSchFormType},
            vms_form_id           = #{vmsFormId},
            dspl_hh               = #{dsplHh},
            vms_dspl_msg_data     = #{vmsDsplMsgData},
            vms_dspl_msg_txt      = #{vmsDsplMsgTxt},
            vms_form_dspl_drct_cd = #{vmsFormDsplDrctCd},
            vms_form_dspl_mthd_cd = #{vmsFormDsplMthdCd},
            vms_dspl_msg_imag     = #{vmsDsplMsgImag},
            dnld_yn               = #{dnldYn}
        where vms_ctlr_nmbr = #{vmsCtlrNmbr}
          and phase         = #{phase}
        ]]>
    </update>

    <insert id="insertVmsDsplPrstHs" parameterType="com.its.vms.entity.TbVmsDsplPrst">
    <![CDATA[
        insert into tb_vms_dspl_hs (
            dspl_dt,
            vms_ctlr_nmbr,
            phase,
            vms_sch_type,
            vms_sch_form_type,
            vms_form_id,
            dspl_hh,
            vms_dspl_msg_data,
            vms_dspl_msg_txt,
            vms_form_dspl_drct_cd,
            vms_form_dspl_mthd_cd,
            dnld_yn )
        values (
                   #{obj.dsplDt},
                   #{obj.vmsCtlrNmbr},
                   #{obj.phase},
                   #{obj.vmsSchType},
                   #{obj.vmsSchFormType},
                   #{obj.vmsFormId},
                   #{obj.dsplHh},
                   #{obj.vmsDsplMsgData},
                   #{obj.vmsDsplMsgTxt},
                   #{obj.vmsFormDsplDrctCd},
                   #{obj.vmsFormDsplMthdCd},
                   #{obj.dnldYn}
               )
        ]]>
    </insert>

    <insert id="batchInsertVmsDsplPrst" parameterType="java.util.Map">
    <![CDATA[
        insert into tb_vms_dspl_hs (
            dspl_dt,
            vms_ctlr_nmbr,
            phase,
            vms_sch_type,
            vms_sch_form_type,
            vms_form_id,
            dspl_hh,
            vms_dspl_msg_data,
            vms_dspl_msg_txt,
            vms_form_dspl_drct_cd,
            vms_form_dspl_mthd_cd,
            dnld_yn )
        values (
                   #{dsplDt},
                   #{vmsCtlrNmbr},
                   #{phase},
                   #{vmsSchType},
                   #{vmsSchFormType},
                   #{vmsFormId},
                   #{dsplHh},
                   #{vmsDsplMsgData},
                   #{vmsDsplMsgTxt},
                   #{vmsFormDsplDrctCd},
                   #{vmsFormDsplMthdCd},
                   #{dnldYn}
               )
        ]]>
    </insert>

    <insert id="createVmsDsplPrstHs" parameterType="com.its.vms.entity.TbVmsDsplPrstResult">
    <![CDATA[
        insert into tb_vms_dspl_hs (
            dspl_dt,
            vms_ctlr_nmbr,
            phase,
            vms_sch_type,
            vms_sch_form_type,
            vms_form_id,
            dspl_hh,
            vms_dspl_msg_data,
            vms_dspl_msg_txt,
            vms_form_dspl_drct_cd,
            vms_form_dspl_mthd_cd,
            dnld_yn )
        select
            dspl_dt,
            vms_ctlr_nmbr,
            phase,
            vms_sch_type,
            vms_sch_form_type,
            vms_form_id,
            dspl_hh,
            vms_dspl_msg_data,
            vms_dspl_msg_txt,
            vms_form_dspl_drct_cd,
            vms_form_dspl_mthd_cd,
            dnld_yn
        from tb_vms_dspl_prst
        where vms_ctlr_nmbr = #{obj.vmsCtlrNmbr}
          and dspl_dt       = #{obj.dsplDt}
          and phase        <= #{obj.phase}
        ]]>
    </insert>

</mapper>
