<?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_program). -->
<mapper namespace="com.zioinfo.cms.uiws.system.mapper.ProgramMapper">

    <sql id="searchWhere">
        WHERE 1=1
        <if test="keyword != null and keyword != ''">
            AND (program_id ILIKE '%' || #{keyword} || '%' OR program_nm ILIKE '%' || #{keyword} || '%')
        </if>
        <if test="programType != null and programType != ''">AND program_type = #{programType}</if>
    </sql>

    <select id="search" resultType="com.zioinfo.cms.uiws.system.model.SysProgram">
        SELECT * FROM tb_uiws_program <include refid="searchWhere"/>
        ORDER BY program_id ASC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countSearch" resultType="long">
        SELECT COUNT(*) FROM tb_uiws_program <include refid="searchWhere"/>
    </select>

    <select id="searchActive" resultType="com.zioinfo.cms.uiws.system.model.SysProgram">
        SELECT * FROM tb_uiws_program
        WHERE use_yn = 'Y'
        <if test="keyword != null and keyword != ''">
            AND (program_id ILIKE '%' || #{keyword} || '%' OR program_nm ILIKE '%' || #{keyword} || '%')
        </if>
        ORDER BY program_id ASC
    </select>

    <select id="findById" resultType="com.zioinfo.cms.uiws.system.model.SysProgram">
        SELECT * FROM tb_uiws_program WHERE program_id = #{programId}
    </select>

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

    <insert id="insert" parameterType="com.zioinfo.cms.uiws.system.model.SysProgram">
        INSERT INTO tb_uiws_program
            (program_id, program_nm, program_type, program_url, category, use_yn, created_by, created_at)
        VALUES
            (#{programId}, #{programNm}, #{programType}, #{programUrl}, #{category}, #{useYn}, #{createdBy}, now())
    </insert>

    <update id="update" parameterType="com.zioinfo.cms.uiws.system.model.SysProgram">
        UPDATE tb_uiws_program SET
            program_nm = #{programNm}, program_type = #{programType}, program_url = #{programUrl},
            category = #{category}, use_yn = #{useYn}, updated_by = #{updatedBy}, updated_at = now()
        WHERE program_id = #{programId}
    </update>

    <delete id="deleteById">
        DELETE FROM tb_uiws_program WHERE program_id = #{programId}
    </delete>
</mapper>
