<?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.ruoyi.ss.product.mapper.ProductMapper">

    <resultMap type="ProductVO" id="ProductResult" autoMapping="true" />

    <sql id="selectProductVo">
        select
            p.product_id,
            p.store_id,
            s.name storeName,
            p.merchant_id,
            u.user_name merchantName,
            p.name,
            p.picture,
            p.stock,
            p.price,
            p.status,
            p.create_by,
            p.create_time,
            p.update_by,
            p.update_time,
            p.deleted
        from ss_product p
        left join ss_store s on s.store_id = p.store_id
        left join ss_user u on u.user_id = p.merchant_id
    </sql>

    <sql id="searchCondition">
        <if test="query.storeId != null "> and p.store_id = #{query.storeId}</if>
        <if test="query.merchantId != null "> and p.merchant_id = #{query.merchantId}</if>
        <if test="query.name != null  and query.name != ''"> and p.name like concat('%', #{query.name}, '%')</if>
        <if test="query.status != null  and query.status != ''"> and p.status = #{query.status}</if>
    </sql>

    <select id="selectProductList" parameterType="ProductQuery" resultMap="ProductResult">
        <include refid="selectProductVo"/>
        where p.deleted = false
            <include refid="searchCondition"/>
    </select>

    <select id="selectProductByProductId" parameterType="Long" resultMap="ProductResult">
        <include refid="selectProductVo"/>
        where product_id = #{productId} and p.deleted = false
    </select>

    <insert id="insertProduct" parameterType="Product">
        insert into ss_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productId != null">product_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="name != null">name,</if>
            <if test="picture != null">picture,</if>
            <if test="stock != null">stock,</if>
            <if test="price != null">price,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleted != null">deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productId != null">#{productId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="name != null">#{name},</if>
            <if test="picture != null">#{picture},</if>
            <if test="stock != null">#{stock},</if>
            <if test="price != null">#{price},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleted != null">#{deleted},</if>
         </trim>
    </insert>

    <update id="updateProduct" parameterType="Product">
        update ss_product
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where product_id = #{data.productId}
    </update>

    <update id="updateProductStock">
            update ss_product
            set stock = stock - 1
            where product_id = #{productId} and stock > 0
    </update>

    <sql id="updateColumns">
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.merchantId != null">merchant_id = #{data.merchantId},</if>
        <if test="data.name != null">name = #{data.name},</if>
        <if test="data.picture != null">picture = #{data.picture},</if>
        <if test="data.stock != null">stock = #{data.stock},</if>
        <if test="data.price != null">price = #{data.price},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.createBy != null">create_by = #{data.createBy},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.updateBy != null">update_by = #{data.updateBy},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
        <if test="data.deleted != null">deleted = #{data.deleted},</if>
    </sql>

    <update id="deleteProductByProductId" parameterType="Long">
        update ss_product
        set deleted = 1
        where product_id = #{productId}
    </update>

    <update id="deleteProductByProductIds" parameterType="List">
        update ss_product
        set deleted = 1
        where product_id in
        <foreach item="productId" collection="array" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </update>
</mapper>
