<?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.VmsSymbMapper">

    <select id="selectVmsSymbLib" resultType="com.its.vms.entity.TbVmsSymbLib" fetchSize="200">
    <![CDATA[
        select t1.symb_lib_nmbr  AS symbLibNmbr,
               t1.rgst_dt        AS rgstDt,
               t1.red_data       AS redData,
               t1.green_data     AS greenData,
               t1.symb_expl      AS symbExpl,
               t1.imag_data      AS imagData,
               t1.vms_type_cd    AS vmsTypeCd,
               nvl(t1.symb_imag_type, 'BMP') AS symbImagType,
               t1.symb_file_nm   AS symbFileNm,
               t1.symb_type      AS symbType,
               t1.avi_data       AS aviData,
               t1.imag_size      AS imagSize,
               t1.play_tm        AS playTm,
               t1.del_yn         AS delYn,
               t1.updt_dt        AS updtDt,
               nvl(t2.posx, 0)   AS posX,
               nvl(t2.posy, 0)   AS posY
        from tb_vms_symb_lib t1
        left outer join tb_vms_symb_ifsc t2
        on t1.symb_lib_nmbr = t2.symb_lib_nmbr
        and t2.cell_id = 1
        ]]>
    </select>

    <select id="selectVmsSymbCellInfo" resultType="com.its.vms.entity.TbVmsSymbIfsc" fetchSize="200">
    <![CDATA[
        select t1.symb_lib_nmbr       AS symbLibNmbr,
               t1.cell_id             AS cellId,
               t1.posx                AS posX,
               t1.posy                AS posY,
               nvl(t1.vms_ifsc_id, 0) AS vmsIfscId,
               t2.updt_dt             AS updtDt
        from tb_vms_symb_ifsc t1
        inner join tb_vms_symb_lib t2
        on t1.symb_lib_nmbr = t2.symb_lib_nmbr
        order by t1.symb_lib_nmbr, t1.cell_id
        ]]>
    </select>

    <select id="selectVmsScheSymbLib" resultType="com.its.vms.entity.VmsScheduleSymb" fetchSize="200">
    <![CDATA[
        select a.vms_ctlr_nmbr as vmsCtlrNmbr,
               b.symb_lib_nmbr as symbLibNmbr,
               min(c.updt_dt)  as updtDt
        from (select vms_ctlr_nmbr, vms_form_id
              from tb_vms_dspl_sch
              group by vms_ctlr_nmbr, vms_form_id) a,
             (select vms_form_id, symb_lib_nmbr
              from tb_vms_form_object
              where vms_form_object_type_cd in (1, 2, 17, 27, 37, 47, 406, 407, 408)
              group by vms_form_id, symb_lib_nmbr) b,
             (select symb_lib_nmbr, updt_dt
              from tb_vms_symb_lib
              where symb_type in ('SBT2', 'SBT4')) c
        where a.vms_form_id = b.vms_form_id
          and b.symb_lib_nmbr = c.symb_lib_nmbr
        group by a.vms_ctlr_nmbr, b.symb_lib_nmbr
        order by a.vms_ctlr_nmbr, b.symb_lib_nmbr
    ]]>
    </select>

    <select id="selectVmsSymbLibDnld" resultType="com.its.vms.entity.TbVmsSymbLibDnld" fetchSize="500">
    <![CDATA[
        select t1.vms_ctlr_nmbr      AS vmsCtlrNmbr,
               t1.dnld_symb_lib_nmbr AS dnldSymbLibNmbr,
               t1.dnld_dt            AS dnldDt,
               t1.dnld_cnt           AS dnldCnt,
               t1.symb_lib_nmbr      AS symbLibNmbr,
               t1.updt_dt            AS updtDt
        from tb_vms_symb_lib_dnld t1
        ]]>
    </select>

    <update id="updateVmsSymbLibDnld" parameterType="com.its.vms.entity.TbVmsSymbLibDnld">
    <![CDATA[
        merge into tb_vms_symb_lib_dnld L
            using (select
                       #{obj.vmsCtlrNmbr}     AS vms_ctlr_nmbr,
                       #{obj.dnldSymbLibNmbr} AS dnld_symb_lib_nmbr,
                       #{obj.dnldDt}          AS dnld_dt,
                       #{obj.dnldCnt}         AS dnld_cnt,
                       #{obj.symbLibNmbr}     AS symb_lib_nmbr,
                       #{obj.updtDt}          AS updt_dt
                   from dual) M
                on (L.vms_ctlr_nmbr = M.vms_ctlr_nmbr
                and L.dnld_symb_lib_nmbr = M.dnld_symb_lib_nmbr)
            when matched then
                update set
                    L.dnld_dt       = M.dnld_dt,
                    L.dnld_cnt      = M.dnld_cnt+1,
                    L.symb_lib_nmbr = M.symb_lib_nmbr,
                    L.updt_dt       = M.updt_dt
            when not matched then
                insert (
                        vms_ctlr_nmbr,
                        dnld_symb_lib_nmbr,
                        dnld_dt,
                        dnld_cnt,
                        symb_lib_nmbr,
                        updt_dt )
                    values (
                       M.vms_ctlr_nmbr,
                       M.dnld_symb_lib_nmbr,
                       M.dnld_dt,
                       M.dnld_cnt,
                       M.symb_lib_nmbr,
                       M.updt_dt )
        ]]>
    </update>

</mapper>
