<?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.mall.mapper.MallMemberCartMapper">
    
    <resultMap type="MallMemberCart" id="MallMemberCartResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="productId"    column="product_id"    />
        <result property="total"    column="total"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectMallMemberCartVo">
        select id, user_id, goods_id, product_id, total, update_at, del_flag from mall_member_cart
    </sql>

    <select id="selectMallMemberCartList" parameterType="MallMemberCart" resultMap="MallMemberCartResult">
        select mmc.id, mmc.user_id, mmc.goods_id, mmc.product_id, mmc.total
        from mall_member_cart mmc
        left join mall_goods_goods mgg on mmc.goods_id = mgg.id
        <where>
            <if test="userId != null  and userId != ''"> and mmc.user_id = #{userId}</if>
            <if test="id != null  and id != ''"> and mmc.id = #{id}</if>
            and mmc.del_flag = '0'
            and mgg.del_flag = '0'
            and mgg.status = '0'
        </where>
    </select>
    
    <select id="selectMallMemberCartById" parameterType="Long" resultMap="MallMemberCartResult">
        <include refid="selectMallMemberCartVo"/>
        where id = #{id}
    </select>
    <select id="selectMallMemberCartByCart" parameterType="MallMemberCart" resultMap="MallMemberCartResult">
        select mmc.id, mmc.user_id, mmc.goods_id, mmc.product_id, mmc.total
        from mall_member_cart mmc
        left join mall_goods_goods mgg on mmc.goods_id = mgg.id
        <where>
            <if test="userId != null  and userId != ''"> and mmc.user_id = #{userId}</if>
            <if test="productId != null  and productId != ''"> and mmc.product_id = #{productId}</if>
            <if test="goodsId != null  and goodsId != ''"> and mmc.goods_id = #{goodsId}</if>
            and mmc.del_flag = '0'
            and mgg.del_flag = '0'
            and mgg.status = '0'
        </where>
    </select>

    <insert id="insertMallMemberCart" parameterType="MallMemberCart" useGeneratedKeys="true" keyProperty="id">
        insert into mall_member_cart
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="productId != null">product_id,</if>
            <if test="total != null">total,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="productId != null">#{productId},</if>
            <if test="total != null">#{total},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateMallMemberCart" parameterType="MallMemberCart">
        update mall_member_cart
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="total != null">total = #{total},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallMemberCartById" parameterType="Long">
        delete from mall_member_cart where id = #{id}
    </delete>

    <delete id="deleteMallMemberCartByIds" parameterType="String">
        delete from mall_member_cart where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteMallMemberCartByCart" parameterType="MallMemberCart">
        delete from mall_member_cart where id = #{id} and user_id = #{userId}
    </delete>
</mapper>