<?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="WebYouTube">

    <select id="selectTotalWebYouTubeCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
          FROM MU_WEB_YOUTUBE AS MWY
         WHERE MWY.USE_YN = 'Y'
        <if test="webYouTubeSearchStartDate != null and webYouTubeSearchStartDate != '' and webYouTubeSearchEndDate != null and webYouTubeSearchEndDate != ''">
           AND DATE_FORMAT(MWY.WRITE_DATE, '%Y-%m-%d') &gt;= #{webYouTubeSearchStartDate}
           AND DATE_FORMAT(MWY.WRITE_DATE, '%Y-%m-%d') &lt;= #{webYouTubeSearchEndDate}
        </if>
        <if test="webYouTubeSearchKeywordParam != null and webYouTubeSearchKeywordParam != ''">
           AND MWY.TITLE LIKE CONCAT('%',TRIM(#{webYouTubeSearchKeywordParam}),'%')
        </if>
    </select>

    <select id="selectListWebYouTube" parameterType="hashmap" resultType="hashmap">
        SELECT MWY.*
          FROM (
                SELECT MWY.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                  FROM (
                        SELECT MWY.MU_WEB_YOUTUBE_ID                        AS "muWebYouTubeId"
                              ,MWY.TITLE                                    AS "title"
                              ,MWY.URL                                      AS "url"
                              ,CONCAT(DATE_FORMAT(MWY.WRITE_DATE, '%Y-%m-%d')
                                     ,', '
                                     ,DATE_FORMAT(MWY.WRITE_TIME, '%H:%i'))  AS "writeDate"
                              ,IFNULL((SELECT MM.NAME
                                         FROM MU_MEMBER AS MM
                                        WHERE MM.USE_YN       = 'Y'
                                          AND MM.MU_MEMBER_ID = MWY.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                          FROM MU_WEB_YOUTUBE AS MWY
                         WHERE MWY.USE_YN = 'Y'
                           <if test="webYouTubeSearchStartDate != null and webYouTubeSearchStartDate != '' and webYouTubeSearchEndDate != null and webYouTubeSearchEndDate != ''">
                           AND DATE_FORMAT(MWY.WRITE_DATE, '%Y-%m-%d') &gt;= #{webYouTubeSearchStartDate}
                           AND DATE_FORMAT(MWY.WRITE_DATE, '%Y-%m-%d') &lt;= #{webYouTubeSearchEndDate}
                           </if>
                           <if test="webYouTubeSearchKeywordParam != null and webYouTubeSearchKeywordParam != ''">
                           AND MWY.TITLE LIKE CONCAT('%',TRIM(#{webYouTubeSearchKeywordParam}),'%')
                           </if>
                         <choose>
                         <when test="webYouTubeSort != null and webYouTubeSort != ''">
                      ORDER BY ${webYouTubeSort}
                         </when>
                         <otherwise>
                      ORDER BY MWY.WRITE_DATE DESC
                         </otherwise>
                         </choose>
                         LIMIT 18446744073709551615
                ) AS MWY, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) AS MWY
        WHERE 1 = 1
      <if test="webYouTubeStart != null and webYouTubeStart != '' and webYouTubeLimit != null and webYouTubeLimit != ''">
        LIMIT ${webYouTubeStart}, ${webYouTubeLimit}
      </if>
    </select>

    <select id="selectWebYouTube" parameterType="hashmap" resultType="hashmap">
        SELECT MWY.MU_WEB_YOUTUBE_ID     AS "muWebYouTubeId"
              ,MWY.TITLE                 AS "title"
              ,MWY.CONTENT               AS "content"
              ,MWY.URL                   AS "url"
          FROM MU_WEB_YOUTUBE   AS MWY
         WHERE MWY.USE_YN        = 'Y'
           AND MWY.MU_WEB_YOUTUBE_ID  = #{muWebYouTubeId}
         LIMIT 0, 1
    </select>
    
    <insert id="insertWebYouTube" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muWebYouTubeId},LPAD((SELECT NEXTVAL(MU_WEB_YOUTUBE_SEQ)), 10, 0))
        </selectKey>
        INSERT INTO MU_WEB_YOUTUBE(
                    MU_WEB_YOUTUBE_ID
                    ,TITLE
                    ,URL
                    ,WRITE_DATE
                    ,WRITE_TIME
                    ,CUD_FLAG
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
                    ,T_ID
                    ,T_DATE
        )VALUES(
                    #{id}
                    ,#{title}
                    ,#{url}
                    ,NOW()
                    ,NOW()
                    ,'C'
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
                    ,#{tId}
                    ,#{tDate}
        )
    </insert>

    <update id="updateWebYouTube" parameterType="hashmap">
        UPDATE MU_WEB_YOUTUBE
           SET MOD_ID              = #{modId}
              ,MOD_DATE            = NOW()
              ,CUD_FLAG            = 'U'
              ,TITLE               = #{title}
              ,URL                 = #{url}
         WHERE USE_YN        = 'Y'
           AND MU_WEB_YOUTUBE_ID  = #{muWebYouTubeId}
    </update>

    <update id="deleteWebYouTube" parameterType="hashmap">
        UPDATE MU_WEB_YOUTUBE
           SET MOD_ID            = #{modId}
              ,MOD_DATE	         = NOW()
              ,CUD_FLAG          = 'D'
              ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_WEB_YOUTUBE_ID  = #{muWebYouTubeId}
    </update>
</mapper>