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

    <select id="getCategoryManagementCnt" resultType="Integer">
        /** CategoryManagementSql.getCategoryManagementCnt **/
        SELECT COUNT(*) as TOTAL_COUNT
          FROM (SELECT CATEGORY_NO
                             , CATEGORY_DIV_CD
                             , CATEGORY_NM
                             , REG_DATE
                          FROM madeu.HP_CATEGORY
                         <where>
                         	<if test="@org.springframework.util.StringUtils@hasLength(searchCategoryDivCd)">
                         		AND CATEGORY_DIV_CD = #{searchCategoryDivCd}
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(searchCategoryNm)">
                         		AND CATEGORY_NM LIKE CONCAT('%', #{searchCategoryNm}, '%')
                         	</if>
                         	AND USE_YN = 'Y'
                         </where>
                         
                         
			    ) total	
    </select>

    <select id="getCategoryManagementList" parameterType="hashmap" resultType="hashmap">
        /** CategoryManagementSql.getCategoryManagementList **/
        SELECT HC.*
          FROM (
                SELECT HC.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) as ROW_NUM
                  FROM (
                        SELECT CATEGORY_NO
                             , CATEGORY_DIV_CD
                             , CATEGORY_NM
                             , ORDER_NO
                             , DATE_FORMAT(REG_DATE, '%Y-%m-%d %H:%i') AS REG_DATE
                          FROM madeu.HP_CATEGORY
                         <where>
                         	<if test="@org.springframework.util.StringUtils@hasLength(searchCategoryDivCd)">
                         		AND CATEGORY_DIV_CD = #{searchCategoryDivCd}
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(searchCategoryNm)">
                         		AND CATEGORY_NM LIKE CONCAT('%', #{searchCategoryNm}, '%')
                         	</if>
                         	AND USE_YN = 'Y'
                         </where>
                         
                           <choose>
	                           <when test="@org.springframework.util.StringUtils@hasLength(gridSort)">
	                      			ORDER BY ${gridSort}
	                           </when>
	                           <otherwise>
	                      			ORDER BY CATEGORY_DIV_CD, CATEGORY_NO
	                           </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                ) HC, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) HC
        WHERE 1 = 1
        ORDER BY HC.CATEGORY_DIV_CD, HC.REG_DATE ASC
        LIMIT ${gridLimitStart}, ${gridLimitEnd}
    </select>
    
    <insert id="putCategoryManagement" parameterType="hashmap">
        <selectKey resultType="hashmap" keyProperty="categoryNo,orderNo" order="BEFORE">
            SELECT IFNULL(MAX(CATEGORY_NO),0) + 1 as categoryNo
                 , CASE WHEN #{orderNo} IS NULL OR #{orderNo} = '' OR #{orderNo} = 0 THEN IFNULL(MAX(ORDER_NO),0) + 1 ELSE #{orderNo} END as orderNo
              FROM HP_CATEGORY
             WHERE CATEGORY_DIV_CD = #{categoryDivCd}
        </selectKey>
        /** CategoryManagementSql.putCategoryManagement **/
	    INSERT INTO HP_CATEGORY(
	        CATEGORY_NO
            , CATEGORY_DIV_CD
            , CATEGORY_NM
            , ORDER_NO
            , USE_YN
            , REG_ID
	        , REG_DATE
	        , MOD_ID
	        , MOD_DATE
	    ) VALUES (
	        #{categoryNo},
	        #{categoryDivCd},
	        #{categoryNm},
	        #{orderNo},
	        'Y',
	        #{regId},
	        NOW(),
	        #{regId},
	        NOW()
        )
    </insert>
    
    <update id="modCategoryManagement" parameterType="hashmap">
        /** CategoryManagementSql.modCategoryManagement **/
	    UPDATE HP_CATEGORY
	       SET CATEGORY_NM = #{categoryNm}
	          ,ORDER_NO = #{orderNo}
	          ,MOD_ID = #{modId}
	          ,MOD_DATE = NOW()
	     WHERE CATEGORY_NO = #{categoryNo}
	     AND CATEGORY_DIV_CD = #{categoryDivCd}
	     AND USE_YN = 'Y'
    </update>
    
    <update id="delCategoryManagement" parameterType="hashmap">
        /** CategoryManagementSql.delCategoryManagement **/
	    UPDATE HP_CATEGORY
	       SET USE_YN = 'N'
	          ,MOD_ID = #{modId}
	          ,MOD_DATE = NOW()
	     WHERE CATEGORY_NO = #{categoryNo}
	     AND CATEGORY_DIV_CD = #{categoryDivCd}
	     AND USE_YN = 'Y'
    </update>
    
</mapper>