<?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.example.mapper.ProductMapper">

    <sql id="Base_Column_List">
        id,product_state,product_name,product_icons,product_ids,product_bid_price,product_sell_price
    </sql>

    <select id="selectAll" resultType="com.example.entity.Product">
        select
        <include refid="Base_Column_List" />
        from product
        <where>
            <if test="id != null"> and id = #{id}</if>
            <if test="productState != null"> and product_state like concat('%', #{productState}, '%')</if>
            <if test="productName != null"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="productIcons != null"> and product_icons like concat('%', #{productIcons}, '%')</if>
            <if test="productTypeIds != null"> and product_type_ids like concat('%', #{productIds}, '%')</if>
            <if test="productBidPrice != null"> and product_bid_price = #{productBidPrice}</if>
            <if test="productSellPrice != null"> and product_sell_price = #{productSellPrice}</if>
        </where>
        order by id desc
    </select>

    <select id="selectById" resultType="com.example.entity.Product">
        select
        <include refid="Base_Column_List" />
        from product
        where id = #{id}
    </select>

    <delete id="deleteById">
        delete from product
        where  id = #{id}
    </delete>

    <insert id="insert" parameterType="com.example.entity.Product" useGeneratedKeys="true">
        insert into product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="productState != null">product_state,</if>
            <if test="productName != null">product_name,</if>
            <if test="productIcons != null">product_icons,</if>
            <if test="productTypeIds != null">product_type_ids,</if>
            <if test="productBidPrice != null">product_bid_price,</if>
            <if test="productSellPrice != null">product_sell_price,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="productState != null">#{productState},</if>
            <if test="productName != null">#{productName},</if>
            <if test="productIcons != null">#{productIcons},</if>
            <if test="productTypeIds != null">#{productTypeIds},</if>
            <if test="productBidPrice != null">#{productBidPrice},</if>
            <if test="productSellPrice != null">#{productSellPrice},</if>
        </trim>
    </insert>

    <update id="updateById" parameterType="com.example.entity.Product">
        update product
        <set>
            <if test="productState != null">
                product_state = #{productState},
            </if>
            <if test="productName != null">
                product_name = #{productName},
            </if>
            <if test="productIcons != null">
                product_icons = #{productIcons},
            </if>
            <if test="productIds != null">
                product_type_ids = #{productTypeIds},
            </if>
            <if test="productBidPrice != null">
                product_bid_price = #{productBidPrice},
            </if>
            <if test="productSellPrice != null">
                product_sell_price = #{productSellPrice},
            </if>
        </set>
        where id = #{id} 
    </update>

</mapper>