<?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">
<!-- UIWS system 이식: 메뉴(tb_uiws_menu). -->
<mapper namespace="com.zioinfo.cms.uiws.system.mapper.MenuMapper">

    <select id="searchAll" resultType="com.zioinfo.cms.uiws.system.model.SysMenu">
        SELECT * FROM tb_uiws_menu ORDER BY sort_ord ASC, menu_id ASC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countAll" resultType="long">
        SELECT COUNT(*) FROM tb_uiws_menu
    </select>

    <select id="search" resultType="com.zioinfo.cms.uiws.system.model.SysMenu">
        SELECT * FROM tb_uiws_menu
        WHERE use_yn = 'Y'
        <if test="keyword != null and keyword != ''">
            AND (menu_id ILIKE '%' || #{keyword} || '%' OR menu_nm ILIKE '%' || #{keyword} || '%')
        </if>
        ORDER BY sort_ord ASC, menu_id ASC
    </select>

    <select id="findAllOrdered" resultType="com.zioinfo.cms.uiws.system.model.SysMenu">
        SELECT * FROM tb_uiws_menu ORDER BY sort_ord ASC, menu_id ASC
    </select>

    <select id="findById" resultType="com.zioinfo.cms.uiws.system.model.SysMenu">
        SELECT * FROM tb_uiws_menu WHERE menu_id = #{menuId}
    </select>

    <select id="existsById" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_menu WHERE menu_id = #{menuId})
    </select>

    <select id="existsByParentMenuId" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_menu WHERE parent_menu_id = #{parentMenuId})
    </select>

    <select id="existsByProgramId" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_menu WHERE program_id = #{programId})
    </select>

    <insert id="insert" parameterType="com.zioinfo.cms.uiws.system.model.SysMenu">
        INSERT INTO tb_uiws_menu
            (menu_id, menu_nm, parent_menu_id, program_id, menu_url, sort_ord, use_yn, created_by, created_at)
        VALUES
            (#{menuId}, #{menuNm}, #{parentMenuId}, #{programId}, #{menuUrl}, #{sortOrd}, #{useYn}, #{createdBy}, now())
    </insert>

    <update id="update" parameterType="com.zioinfo.cms.uiws.system.model.SysMenu">
        UPDATE tb_uiws_menu SET
            menu_nm = #{menuNm}, parent_menu_id = #{parentMenuId}, program_id = #{programId},
            menu_url = #{menuUrl}, sort_ord = #{sortOrd}, use_yn = #{useYn},
            updated_by = #{updatedBy}, updated_at = now()
        WHERE menu_id = #{menuId}
    </update>

    <delete id="deleteById">
        DELETE FROM tb_uiws_menu WHERE menu_id = #{menuId}
    </delete>
</mapper>
