<?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.tmerclub.cloud.cuerecycle.mapper.CueProductMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap id="cueProductVOMap" type="com.tmerclub.cloud.cuerecycle.model.vo.CueProductVO">
        <result property="productId" column="product_id"/>
        <result property="brandId" column="brand_id"/>
        <result property="seriesId" column="series_id"/>
        <result property="typeId" column="type_id"/>
        <result property="productName" column="product_name"/>
        <result property="productDescription" column="product_description"/>
        <result property="productImages" column="product_images"/>
        <result property="productPrice" column="product_price"/>
        <result property="productSeq" column="product_seq"/>
        <result property="productStatus" column="product_status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="Vo_Column_List">
        product_id,brand_id,series_id,type_id,product_name,product_description,product_images,product_price,product_seq,product_status,deleted,create_time,update_time
    </sql>

    <select id="list" resultMap="cueProductVOMap">
        SELECT
            <include refid="Vo_Column_List" />
        FROM
            cue_product
        <where>
            deleted = 0
            <if test="dto.productName != null and dto.productName != ''">
                AND product_name LIKE CONCAT('%', #{dto.productName}, '%')
            </if>
            <if test="dto.productStatus != null">
                AND product_status = #{dto.productStatus}
            </if>
            <if test="dto.brandId != null">
                AND brand_id = #{dto.brandId}
            </if>
            <if test="dto.typeId != null">
                AND type_id = #{dto.typeId}
            </if>
            <if test="dto.seriesId != null">
                AND series_id = #{dto.seriesId}
            </if>
        </where>
        ORDER BY
            product_seq ASC,
            product_id DESC
    </select>

    <select id="getById" resultMap="cueProductVOMap">
        SELECT
            <include refid="Vo_Column_List" />
        FROM
            cue_product
        WHERE
            product_id = #{productId}
    </select>

    <insert id="save" useGeneratedKeys="true" keyProperty="productId">
        INSERT INTO cue_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="brandId != null">brand_id,</if>
            <if test="seriesId != null">series_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="productDescription != null and productDescription != ''">product_description,</if>
            <if test="productImages != null and productImages != ''">product_images,</if>
            <if test="productPrice != null">product_price,</if>
            <if test="productSeq != null">product_seq,</if>
            <if test="productStatus != null">product_status,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="brandId != null">#{brandId},</if>
            <if test="seriesId != null">#{seriesId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="productDescription != null and productDescription != ''">#{productDescription},</if>
            <if test="productImages != null and productImages != ''">#{productImages},</if>
            <if test="productPrice != null">#{productPrice},</if>
            <if test="productSeq != null">#{productSeq},</if>
            <if test="productStatus != null">#{productStatus},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="update">
        UPDATE cue_product
        <set>
            <if test="brandId != null">
                brand_id = #{brandId},
            </if>
            <if test="seriesId != null">
                series_id = #{seriesId},
            </if>
            <if test="typeId != null">
                type_id = #{typeId},
            </if>
            <if test="productName != null and productName != ''">
                product_name = #{productName},
            </if>
            <if test="productDescription != null and productDescription != ''">
                product_description = #{productDescription},
            </if>
            <if test="productImages != null and productImages != ''">
                product_images = #{productImages},
            </if>
            <if test="productPrice != null">
                product_price = #{productPrice},
            </if>
            <if test="productSeq != null">
                product_seq = #{productSeq},
            </if>
            <if test="productStatus != null">
                product_status = #{productStatus},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where product_id = #{productId}
    </update>
</mapper>