<?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="com.csnf.mapper.ProductMapper">
    <resultMap id="BaseResultMap" type="com.csnf.entity.Product">
        <id column="product_id" jdbcType="INTEGER" property="product_id"/>
        <result column="product_name" jdbcType="VARCHAR" property="product_name"/>
        <result column="product_title" jdbcType="VARCHAR" property="product_title"/>
        <result column="product_price" jdbcType="DECIMAL" property="product_price"/>
        <result column="product_sale_price" jdbcType="DECIMAL" property="product_sale_price"/>
        <result column="product_create_date" jdbcType="TIMESTAMP" property="product_create_date"/>
        <result column="product_category_id" jdbcType="INTEGER" property="product_category_id"/>
        <result column="product_isEnabled" jdbcType="BIT" property="product_isEnabled"/>
        <result column="product_sale_count" jdbcType="INTEGER" property="product_sale_count"/>
        <result column="product_review_count" jdbcType="INTEGER" property="product_review_count"/>
    </resultMap>
    <delete id="deleteByPrimaryKey" parameterType="Integer">
        delete
        from product
        where product_id = #{product_id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.csnf.entity.Product">
        <selectKey keyProperty="product_id" order="AFTER" resultType="Integer">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into product (product_name, product_title, product_price,
        product_sale_price, product_create_date,
        product_category_id, product_isEnabled, product_sale_count,
        product_review_count)
        values (#{product_name,jdbcType=VARCHAR}, #{product_title,jdbcType=VARCHAR}, #{product_price,jdbcType=DECIMAL},
        #{product_sale_price,jdbcType=DECIMAL}, #{product_create_date,jdbcType=TIMESTAMP},
        #{product_category_id,jdbcType=INTEGER}, #{product_isEnabled,jdbcType=BIT},
        #{product_sale_count,jdbcType=INTEGER},
        #{product_review_count,jdbcType=INTEGER})
    </insert>
    <update id="updateByPrimaryKey" parameterType="com.csnf.entity.Product">
        update product
        set product_name         = #{product_name,jdbcType=VARCHAR},
            product_title        = #{product_title,jdbcType=VARCHAR},
            product_price        = #{product_price,jdbcType=DECIMAL},
            product_sale_price   = #{product_sale_price,jdbcType=DECIMAL},
            product_create_date  = #{product_create_date,jdbcType=TIMESTAMP},
            product_category_id  = #{product_category_id,jdbcType=INTEGER},
            product_isEnabled    = #{product_isEnabled,jdbcType=BIT},
            product_sale_count   = #{product_sale_count,jdbcType=INTEGER},
            product_review_count = #{product_review_count,jdbcType=INTEGER}
        where product_id = #{product_id,jdbcType=INTEGER}
    </update>
    <select id="selectByPrimaryKey" parameterType="Integer" resultMap="BaseResultMap">
        select product_id,
               product_name,
               product_title,
               product_price,
               product_sale_price,
               product_create_date,
               product_category_id,
               product_isEnabled,
               product_sale_count,
               product_review_count
        from product
        where product_id = #{product_id,jdbcType=INTEGER}
    </select>
    <select id="selectAll" resultMap="BaseResultMap">
        select product_id,
               product_name,
               product_title,
               product_price,
               product_sale_price,
               product_create_date,
               product_category_id,
               product_isEnabled,
               product_sale_count,
               product_review_count
        from product
    </select>
    <select id="selectByConditional" resultMap="BaseResultMap">
        select product_id, product_name, product_title, product_price, product_sale_price,
        product_create_date, product_category_id, product_isEnabled, product_sale_count,
        product_review_count
        from product
        <where>
            <if test="productName != null ">
                product_name like concat('%',#{productName},'%')
            </if>
            <if test="categoryId!=null">
                and product_category_id=#{categoryId}
            </if>
            <if test="isEnabled!=null">
                and product_isEnabled=#{isEnabled}
            </if>
            <if test="minPrice!=null and maxPrice!=null">
                and product_price between #{minPrice} and #{maxPrice}
            </if>
            <if test="minPrice!=null and maxPrice ==null">
                and product_price &gt;= #{minPrice}
            </if>
            <if test="minPrice==null and maxPrice!=null">
                and product_price &lt;= #{maxPrice}
            </if>

        </where>
        <if test="pageUtil!=null">
            limit #{pageUtil.startIndex},#{pageUtil.pageSize}
        </if>
    </select>
</mapper>