<?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="cn.iocoder.mall.product.dao.ProductSpuMapper">

    <sql id="FIELDS">
        id, name, sell_point, description, cid,
        pic_urls, visible, sort, price, quantity,
        create_time
    </sql>

    <select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
        SELECT
            <include refid="FIELDS" />
        FROM product_spu
        WHERE id = #{id}
        AND deleted = 0
    </select>

    <insert id="insert" parameterType="ProductSpuDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO product_spu (
            name, sell_point, description, cid, pic_urls,
            visible, sort, price, quantity,
            deleted, create_time
        ) VALUES (
            #{name}, #{sellPoint}, #{description}, #{cid}, #{picUrls},
            #{visible}, #{sort}, #{price}, #{quantity},
             #{deleted}, #{createTime}
        )
    </insert>

    <update id="update" parameterType="ProductSpuDO">
        UPDATE product_spu
        <set>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="sellPoint != null">
                sell_point = #{sellPoint},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="cid != null">
                cid = #{cid},
            </if>
            <if test="picUrls != null">
                pic_urls = #{picUrls},
            </if>
            <if test="visible != null">
                visible = #{visible},
            </if>
            <if test="price != null">
                price = #{price},
            </if>
            <if test="quantity != null">
                quantity = #{quantity},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="deleted != null">
                deleted = #{deleted}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="selectListByNameLikeOrderBySortAsc" resultType="ProductSpuDO">
        SELECT
            <include refid="FIELDS" />
        FROM product_spu
        <where>
            <if test="name != null">
                name LIKE "%"#{name}"%"
            </if>
            <if test="cid != null">
                AND cid = #{cid}
            </if>
            <if test="visible != null">
                AND visible = #{visible}
            </if>
            AND deleted = 0
        </where>
        ORDER BY sort ASC
        LIMIT #{offset}, #{limit}
    </select>

    <select id="selectCountByNameLike" resultType="Integer">
        SELECT
            COUNT(1)
        FROM product_spu
        <where>
            <if test="name != null">
                name LIKE "%"#{name}"%"
            </if>
            <if test="cid != null">
                AND cid = #{cid}
            </if>
            <if test="visible != null">
                AND visible = #{visible}
            </if>
            AND deleted = 0
        </where>
    </select>

</mapper>