<?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="kr.co.jinwoosi.clfd.conts.prod.mapper.ProdMapper">

    <sql id="where">
        <if test="!empty(prodTyList)">
            AND (
                <foreach collection="prodTyList" item="prodTy" index="index" separator="OR" open="(" close=")">
                    PROD_TY = #{prodTy}
                </foreach>
                )
        </if>
        <if test="!empty(skTy)"> AND SK_TY LIKE '%'||#{skTy}||'%' </if>
        <if test="!empty(prodNmLike)" > AND LOWER(PROD_NM) LIKE LOWER('%'||#{prodNmLike}||'%') </if>
        <if test="!empty(prodTyLike)" > AND PROD_TY LIKE '%'||#{prodTyLike}||'%' </if>
        <if test="!empty(skTyLike)" > AND SK_TY LIKE '%'||#{skTyLike}||'%' </if>
        <if test="!empty(prodIrdntLike)" > AND LOWER(PROD_IRDNT) LIKE LOWER('%'||#{prodIrdntLike}||'%') </if>
        <if test="!empty(recSt)"> AND REC_ST = #{recSt}</if>
        <if test="!empty(showSt)"> AND SHOW_ST = #{showSt}</if>
    </sql>

    <select id="select" parameterType="prodSearch" resultType="prod">
        SELECT PROD_ID, PROD_CD, PROD_NM, PROD_TY, FN_CD_DTL_NM('prodTy', PROD_TY) AS PROD_TY_NM,
            PROD_CPCTY, PROD_PRC, PROD_ST, FN_CD_DTL_NM('prodSt', PROD_ST) AS PROD_ST_NM, SHOW_ST, SK_TY, PROD_IRDNT, PROD_DTL, PROD_HASHTAG, IMAGE_DIR, PROD_IMAGE, RCMD_NO,
            REG_ID, (SELECT USER_NM FROM JCMS_USER WHERE USER_ID = CP.REG_ID) AS REG_NM, REG_DT, UPD_ID, UPD_DT, DEL_ID, DEL_DT, REC_ST
        FROM CF_PROD CP
        WHERE PROD_ID = #{prodId}
    </select>

    <select id="selectList" parameterType="prodSearch" resultType="prod">
        SELECT *
        FROM (
            SELECT ROWNUM RNUM , LIST.*
            FROM (
                SELECT PROD_ID, PROD_CD, PROD_NM, PROD_TY, FN_CD_DTL_NM('prodTy', PROD_TY) AS PROD_TY_NM,
                    PROD_CPCTY, PROD_PRC, PROD_ST, FN_CD_DTL_NM('prodSt', PROD_ST) AS PROD_ST_NM, SHOW_ST, SK_TY, PROD_IRDNT, PROD_DTL, PROD_HASHTAG, IMAGE_DIR, PROD_IMAGE, RCMD_NO,
                    REG_ID, (SELECT USER_NM FROM JCMS_USER WHERE USER_ID = CP.REG_ID) AS REG_NM, REG_DT, UPD_ID, UPD_DT, DEL_ID, DEL_DT, REC_ST
                FROM CF_PROD CP
                WHERE DEL_DT IS NULL
                    <include refid="where"/>
                <if test="!empty(sort) and !empty(sortOrd)">
                ORDER BY ${sort} ${sortOrd}
                </if>
            ) LIST
        )
        <if test="pagingYn == true" >
            <![CDATA[
				WHERE RNUM > #{start} AND RNUM <= #{start} + #{length}
			]]>
        </if>
    </select>

    <select id="count" parameterType="prodSearch" resultType="java.lang.Integer">
        SELECT COUNT(PROD_ID)
        FROM CF_PROD CP
        WHERE DEL_DT IS NULL
            <include refid="where"/>
    </select>

    <select id="skTyList" parameterType="skTySearch" resultType="skTy">
        SELECT SK_TY_CD FROM CF_SK_TY
    </select>

    <insert id="insert" parameterType="prod">
        <selectKey keyProperty="prodId" resultType="java.lang.Long" order="BEFORE">
            SELECT PROD_ID_SEQ.NEXTVAL AS prodId FROM DUAL
        </selectKey>
        INSERT INTO CF_PROD(
            PROD_ID, PROD_CD, PROD_NM, PROD_TY, PROD_CPCTY, PROD_PRC, PROD_ST, SHOW_ST, SK_TY,
            PROD_IRDNT, PROD_DTL, PROD_HASHTAG, IMAGE_DIR, PROD_IMAGE, RCMD_NO, REG_ID, REG_DT, REC_ST
        ) VALUES (
            #{prodId}, #{prodCd}, #{prodNm}, #{prodTy}, #{prodCpcty}, #{prodPrc}, #{prodSt}, #{showSt}, #{skTy},
            #{prodIrdnt}, #{prodDtl}, #{prodHashtag}, #{imageDir}, #{prodImage}, 0, #{regId}, SYSDATE, #{recSt}
        )
    </insert>

    <update id="update" parameterType="prod">
        UPDATE CF_PROD
        SET UPD_ID = #{updId}
            ,UPD_DT = SYSDATE
            <if test="!empty(prodCd)"> , PROD_CD = #{prodCd} </if>
            <if test="!empty(prodNm)"> , PROD_NM = #{prodNm} </if>
            <if test="!empty(prodTy)"> , PROD_TY = #{prodTy} </if>
            <if test="!empty(prodCpcty)"> , PROD_CPCTY = #{prodCpcty} </if>
            <if test="!empty(prodPrc)"> , PROD_PRC = #{prodPrc} </if>
            <if test="!empty(prodSt)"> , PROD_ST = #{prodSt} </if>
            <if test="!empty(showSt)"> , SHOW_ST = #{showSt} </if>
            <if test="!empty(skTy)"> , SK_TY = #{skTy} </if>
            <if test="!empty(prodIrdnt)"> , PROD_IRDNT = #{prodIrdnt} </if>
            <if test="!empty(prodDtl)"> , PROD_DTL = #{prodDtl} </if>
            <if test="!empty(prodHashtag)"> , PROD_HASHTAG = #{prodHashtag} </if>
            <if test="!empty(imageDir)"> , IMAGE_DIR = #{imageDir} </if>
            <if test="!empty(prodImage)"> , PROD_IMAGE = #{prodImage} </if>
            <if test="!empty(rcmdNo)"> , RCMD_NO = #{rcmdNo} </if>
            <if test="!empty(recSt)"> , REC_ST = #{recSt} </if>
        WHERE PROD_ID = #{prodId}
    </update>

    <update id="delete" parameterType="prod">
        UPDATE CF_PROD
        SET DEL_ID = #{delId}
            ,DEL_DT = SYSDATE
        WHERE PROD_ID = #{prodId}
    </update>

</mapper>
