<?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.product.mapper.SpuExtensionMapper">
    <resultMap id="spuExtensionMap" type="com.tmerclub.cloud.product.model.SpuExtension">
        <id column="spu_extend_id" property="spuExtendId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="spu_id" property="spuId"/>
        <result column="comment_num" property="commentNum"/>
        <result column="water_sold_num" property="waterSoldNum"/>
    </resultMap>
    <sql id="Vo_Column_List">
        `spu_extend_id`,
        `create_time`,
        `update_time`,
        `spu_id`,
        `comment_num`
    </sql>
    <select id="list" resultType="com.tmerclub.cloud.product.vo.SpuExtensionVO">
        select
        <include refid="Vo_Column_List"/>
        from spu_extension
        order by spu_extend_id desc
    </select>
    <select id="getBySpuExtendId" resultType="com.tmerclub.cloud.product.vo.SpuExtensionVO">
        select
        <include refid="Vo_Column_List"/>
        from spu_extension
        where spu_extend_id = #{spuExtendId}
    </select>
    <insert id="save">
        insert into spu_extension (`spu_id`, `comment_num`)
        values (#{spuExtension.spuId}, 0);
    </insert>
    <delete id="deleteById">
        delete
        from spu_extension
        where spu_id = #{spuId}
    </delete>

    <select id="getBySpuId" resultMap="spuExtensionMap">
        select `spu_extend_id`, `spu_id`, `comment_num`,`water_sold_num`
        from spu_extension
        where spu_id = #{spuId}
    </select>

    <update id="update">
        update spu_extension
        <set>
            <if test="spuExtension.stock != null">
                stock = #{spuExtension.stock},
            </if>
            <if test="spuExtension.actualStock != null">
                actual_stock = #{spuExtension.actualStock},
            </if>
            <if test="spuExtension.lockStock != null">
                lock_stock = #{spuExtension.lockStock}
            </if>
        </set>
        where spu_id = #{spuExtension.spuId}
    </update>
    <update id="updateWaterSoldNumBySpuId">
        update spu_extension
        set water_sold_num = #{waterSoldNum}
        where spu_id = #{spuId}
    </update>
    <update id="changeCommentNum">
        update spu_extension
        set comment_num = comment_num + #{changeCount}
        where spu_id = #{spuId}
    </update>
    <update id="updateSaleNumBySeckillOrder">
        update spu_extension
        set  `sale_num` = `sale_num` + #{prodCount}
        where spu_id = #{spuId}
    </update>
</mapper>
