<?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.fs.live.mapper.LiveCartMapper">

    <resultMap type="LiveCart" id="LiveCartResult">
        <result property="cartId"    column="cart_id"    />
        <result property="liveId"    column="live_id"    />
        <result property="userId"    column="user_id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="productId"    column="product_id"    />
        <result property="productAttrValueId"    column="product_attr_value_id"    />
        <result property="cartNum"    column="cart_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isPay"    column="is_pay"    />
        <result property="isDel"    column="is_del"    />
        <result property="isBuy"    column="is_buy"    />
        <result property="checked"    column="checked"    />
    </resultMap>

    <sql id="selectLiveCartVo">
        select cart_id, live_id, user_id, goods_id, product_id, product_attr_value_id, cart_num, create_time, update_time, is_pay, is_del, is_buy, checked from live_cart
    </sql>

    <select id="selectLiveCartList" parameterType="LiveCart" resultMap="LiveCartResult">
        <include refid="selectLiveCartVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="goodsId != null "> and goods_id = #{goodsId}</if>
            <if test="productId != null  and productId != ''"> and product_id = #{productId}</if>
            <if test="productAttrValueId != null "> and product_attr_value_id = #{productAttrValueId}</if>
            <if test="cartNum != null  and cartNum != ''"> and cart_num = #{cartNum}</if>
            <if test="isPay != null "> and is_pay = #{isPay}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="isBuy != null "> and is_buy = #{isBuy}</if>
            <if test="checked != null "> and checked = #{checked}</if>
        </where>
    </select>

    <select id="selectLiveCartListVo" parameterType="LiveCart" resultType="com.fs.live.vo.LiveCartVo">
        select a.cart_id, a.live_id, a.user_id, a.goods_id, a.product_id, a.product_attr_value_id, a.cart_num, a.create_time, a.update_time, a.is_pay, a.is_del, a.is_buy,a.checked,
        b.product_name, b.img_url,b.price, b.ot_price, b.stock,b.sales
        from live_cart a
        left join fs_store_product b on a.product_id = b.product_id
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="goodsId != null "> and goods_id = #{goodsId}</if>
            <if test="productId != null  and productId != ''"> and product_id = #{productId}</if>
            <if test="productAttrValueId != null "> and product_attr_value_id = #{productAttrValueId}</if>
            <if test="cartNum != null  and cartNum != ''"> and cart_num = #{cartNum}</if>
            <if test="isPay != null "> and is_pay = #{isPay}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="isBuy != null "> and is_buy = #{isBuy}</if>
            <if test="checked != null "> and checked = #{checked}</if>
        </where>
    </select>


    <select id="selectLiveCartByCartId" parameterType="Long" resultMap="LiveCartResult">
        <include refid="selectLiveCartVo"/>
        where cart_id = #{cartId}
    </select>

    <insert id="insertLiveCart" parameterType="LiveCart" useGeneratedKeys="true" keyProperty="cartId">
        insert into live_cart
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="productId != null and productId != ''">product_id,</if>
            <if test="productAttrValueId != null">product_attr_value_id,</if>
            <if test="cartNum != null and cartNum != ''">cart_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isPay != null">is_pay,</if>
            <if test="isDel != null">is_del,</if>
            <if test="isBuy != null">is_buy,</if>
            <if test="checked != null">checked,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="productId != null and productId != ''">#{productId},</if>
            <if test="productAttrValueId != null">#{productAttrValueId},</if>
            <if test="cartNum != null and cartNum != ''">#{cartNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isPay != null">#{isPay},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="isBuy != null">#{isBuy},</if>
            <if test="checked != null">#{checked},</if>
         </trim>
    </insert>

    <update id="updateLiveCart" parameterType="LiveCart">
        update live_cart
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="userId != null and userId != ''">user_id = #{userId},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="productId != null and productId != ''">product_id = #{productId},</if>
            <if test="productAttrValueId != null">product_attr_value_id = #{productAttrValueId},</if>
            <if test="cartNum != null and cartNum != ''">cart_num = #{cartNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isPay != null">is_pay = #{isPay},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="isBuy != null">is_buy = #{isBuy},</if>
            <if test="checked != null">checked = #{checked},</if>
        </trim>
        where cart_id = #{cartId}
    </update>

    <delete id="deleteLiveCartByCartId" parameterType="Long">
        delete from live_cart where cart_id = #{cartId}
    </delete>

    <delete id="deleteLiveCartByCartIds" parameterType="String">
        delete from live_cart where cart_id in
        <foreach item="cartId" collection="array" open="(" separator="," close=")">
            #{cartId}
        </foreach>
    </delete>

    <delete id="deleteByCartIdsAndUserId" parameterType="String">
        delete from live_cart where user_id = #{userId} and cart_id in
        <foreach item="cartId" collection="cartIds" open="(" separator="," close=")">
            #{cartId}
        </foreach>
    </delete>
</mapper>
