<?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.clfd.prod.mapper.ProdLikeMapper">
    <sql id="where">
        <if test="!empty(prodLikeId)"> AND PROD_LIKE_ID = #{prodLikeId}</if>
        <if test="!empty(prodId)"> AND PROD_ID = #{prodId}</if>
        <if test="!empty(userId)"> AND USER_ID = #{userId}</if>
    </sql>
    <sql id="uniqueWhere">
        <choose>
            <when test="!empty(prodLikeId)">PROD_LIKE_ID = #{prodLikeId}</when>
            <otherwise>
                <if test="!empty(prodId) and !empty(userId)">(PROD_ID = #{prodId} AND USER_ID = #{userId}) </if>
            </otherwise>
        </choose>
    </sql>

    <insert id="insert" parameterType="prodLike">
        <selectKey keyProperty="prodLikeId" resultType="java.lang.Long" order="BEFORE" >
            select PROD_LIKE_ID_SEQ.NEXTVAL from DUAL
        </selectKey>
        INSERT INTO CF_PROD_LIKE (PROD_LIKE_ID, PROD_ID, USER_ID, REG_DT) VALUES (#{prodLikeId}, #{prodId}, #{userId}, SYSDATE)
    </insert>

    <delete id="deleteAndReturnCount" parameterType="prodLike">
        DELETE
        FROM CF_PROD_LIKE
        WHERE
            <include refid="uniqueWhere"/>
    </delete>

    <select id="select" parameterType="prodLikeSearch" resultType="prodLike">
        SELECT *
        FROM CF_PROD_LIKE
        WHERE
            <include refid="uniqueWhere"/>
    </select>
    
    <select id="selectList" parameterType="prodLikeSearch" resultType="prodLike">
        SELECT *
        FROM (
            SELECT ROWNUM RNUM , LIST.*
            FROM (
                SELECT *
                FROM CF_PROD_LIKE
                WHERE 1=1
                    <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>
</mapper>
