<?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.taais.demo.mapper.DemoProductMapper">

    <resultMap type="DemoProduct" id="DemoProductResult">
        <result property="productId" column="product_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="productName" column="product_name"/>
        <result property="orderNum" column="order_num"/>
        <result property="status" column="status"/>
    </resultMap>

    <sql id="selectDemoProductVo">
        select product_id, parent_id, product_name, order_num, status
        from demo_product
    </sql>

    <select id="selectDemoProductList" parameterType="DemoProduct" resultMap="DemoProductResult">
        <include refid="selectDemoProductVo"/>
        <where>
            <if test="productName != null  and productName != ''">and product_name like concat('%', #{productName},
                '%')
            </if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
    </select>

    <select id="selectDemoProductByProductId" parameterType="Long" resultMap="DemoProductResult">
        <include refid="selectDemoProductVo"/>
        where product_id = #{productId}
    </select>

    <insert id="insertDemoProduct" parameterType="DemoProduct" useGeneratedKeys="true" keyProperty="productId">
        insert into demo_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="productName != null">product_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null">status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="productName != null">#{productName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null">#{status},</if>
        </trim>
    </insert>

    <update id="updateDemoProduct" parameterType="DemoProduct">
        update demo_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="productName != null">product_name = #{productName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where product_id = #{productId}
    </update>

    <delete id="deleteDemoProductByProductId" parameterType="Long">
        delete
        from demo_product
        where product_id = #{productId}
    </delete>

    <delete id="deleteDemoProductByProductIds" parameterType="String">
        delete from demo_product where product_id in
        <foreach item="productId" collection="array" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </delete>
</mapper>
