<?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.backendsys.mapper.B2c.B2cShoppingCartMapper">

    <sql id="includeShoppingCart">
        id id,
        id shopping_cart_id,
        member_id,
        good_id,
        sku_id,
        quantity,
        COALESCE(source, '') source,
        create_time,
        update_time
    </sql>
    <resultMap id="resultMapShoppingCart" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="shopping_cart_id" column="shopping_cart_id" javaType="java.lang.Long"/>
        <result property="member_id" column="member_id" javaType="java.lang.Long"/>
        <result property="good_id" column="good_id" javaType="java.lang.Long"/>
        <result property="sku_id" column="sku_id" javaType="java.lang.Long"/>
        <result property="quantity" column="quantity" javaType="java.lang.Integer" />
        <result property="source" column="source" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <!--LEFT JOIN b2c_goods_brand gbp ON gb.parent_id = gbp.id-->
    <select id="queryShoppingCartList" resultMap="resultMapShoppingCart">
        SELECT
        <include refid="includeShoppingCart" />
        FROM b2c_shopping_cart
        <where>
            <if test="member_id != null and member_id != ''">
                AND member_id = #{member_id}
            </if>
        </where>
    </select>

    <select id="queryShoppingCartDetail" resultMap="resultMapShoppingCart">
        SELECT
        <include refid="includeShoppingCart" />
        FROM b2c_shopping_cart
        <where>
            <if test="member_id != null and member_id != ''">
                AND member_id = #{member_id}
            </if>
            <if test="good_id != null and good_id != ''">
                AND good_id = #{good_id}
            </if>
            <if test="sku_id != null and sku_id != ''">
                AND sku_id = #{sku_id}
            </if>
        </where>
    </select>
    
    <insert id="insertShoppingCart" parameterType="com.backendsys.entity.B2c.B2cShoppingCartDTO"
        useGeneratedKeys="true" keyProperty="shopping_cart_id">
        INSERT INTO b2c_shopping_cart (member_id, good_id, sku_id, quantity
        <if test="source != null and source != ''">, source</if>
        )
        VALUES (#{member_id}, #{good_id}, #{sku_id}, #{quantity}
        <if test="source != null and source != ''">, #{source}</if>
        )
    </insert>

    <update id="updateShoppingCart" parameterType="com.backendsys.entity.B2c.B2cShoppingCartDTO"
        useGeneratedKeys="true" keyProperty="shopping_cart_id">
        UPDATE b2c_shopping_cart SET quantity = #{quantity}
        <if test="sku_id != null and sku_id != ''">, sku_id = #{sku_id}</if>
        <if test="source != null and source != ''">, source = #{source}</if>
        WHERE id = #{shopping_cart_id}
    </update>

    <update id="updateShoppingCartBySku" parameterType="com.backendsys.entity.B2c.B2cShoppingCartDTO"
            useGeneratedKeys="true" keyProperty="shopping_cart_id">
        UPDATE b2c_shopping_cart SET quantity = #{quantity}
        <if test="sku_id != null and sku_id != ''">, sku_id = #{sku_id}</if>
        <if test="source != null and source != ''">, source = #{source}</if>
        WHERE member_id = #{member_id} AND good_id = #{good_id} AND sku_id = #{sku_id}
    </update>

    <delete id="cleanShoppingCart">
        DELETE FROM b2c_shopping_cart WHERE member_id = #{member_id}
    </delete>

</mapper>