<?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.kmall.api.dao.ApiCartMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.CartVo" id="cartMap">
        <result property="id" column="id"/>
        <result property="user_id" column="user_id"/>
        <result property="store_id" column="store_id"/>
        <result property="goods_id" column="goods_id"/>
        <result property="goods_sn" column="goods_sn"/>
        <result property="product_id" column="product_id"/>
        <result property="goods_name" column="goods_name"/>
        <result property="market_price" column="market_price"/>
        <result property="retail_price" column="retail_price"/>
        <result property="number" column="number"/>
        <result property="goods_specification_name_value" column="goods_specification_name_value"/>
        <result property="goods_specification_ids" column="goods_specification_ids"/>
        <result property="checked" column="checked"/>
        <result property="list_pic_url" column="list_pic_url"/>
        <result column="sku" property="sku" jdbcType="VARCHAR" />
        <result column="goods_biz_type" property="goodsBizType" jdbcType="CHAR" />
        <result column="creater_sn" property="createrSn" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="moder_sn" property="moderSn" jdbcType="VARCHAR" />
        <result column="mod_time" property="modTime" jdbcType="TIMESTAMP" />
        <result column="tstm" property="tstm" jdbcType="TIMESTAMP" />
        <result property="stockNum" column="productStockNum"/>
    </resultMap>

    <sql id="Base_Column_List" >
        id, user_id, goods_id, sku, goods_sn, product_id, goods_name, market_price, retail_price,
        number, goods_specification_ids, checked, list_pic_url, store_id, goods_biz_type,
        creater_sn, create_time, moder_sn, mod_time, tstm,goods_specification_name_value,stock_num
    </sql>
    <select id="queryObject" resultMap="cartMap">
		select
        <include refid="Base_Column_List" /> from mall_cart where id = #{value}
	</select>
    <select id="queryCartByGoodsBizType" resultMap="cartMap">
        select
        <include refid="Base_Column_List" /> from mall_cart where goods_biz_type = #{goodsBizType}
    </select>
    <select id="queryObjectByGoodsIdAndUserId" resultMap="cartMap">
        SELECT * FROM `mall_cart` where goods_id =#{goods_id} and user_id =#{user_id}
    </select>

    <select id="queryList" resultMap="cartMap">
        select a.*,
        b.list_pic_url as list_pic_url,
        psr.retail_price as retail_product_price,psr.stock_num 'productStockNum'
        from mall_cart a
        left join mall_goods b on a.goods_id = b.id
        left join mall_product c on c.goods_id = a.goods_id and c.id = a.product_id
        left join mall_product_store_rela psr on psr.product_id = c.id and psr.store_id = #{store_id}
        where 1 = 1
        and psr.stock_num > 0
        <if test="user_id != null">
            AND a.user_id = #{user_id}
        </if>
        <if test="goods_id != null">
            AND a.goods_id = #{goods_id}
        </if>
        <if test="product_id != null">
            AND a.product_id = #{product_id}
        </if>
        <if test="store_id != null">
            AND psr.store_id = #{store_id}
        </if>
        <if test="checked != null">
            AND a.checked = #{checked}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>


    <select id="queryValidCartList" resultMap="cartMap">
        select a.*,
        b.list_pic_url as list_pic_url,
        psr.retail_price as retail_product_price,psr.stock_num 'productStockNum'
        from mall_cart a
        left join mall_goods b on a.goods_id = b.id
        left join mall_product c on c.goods_id = a.goods_id and c.id = a.product_id
        left join mall_product_store_rela psr on psr.product_id = c.id and psr.store_id = #{store_id}
        where 1 = 1
        and psr.stock_num = 0
        <if test="user_id != null">
            AND a.user_id = #{user_id}
        </if>
        <if test="goods_id != null">
            AND a.goods_id = #{goods_id}
        </if>
        <if test="product_id != null">
            AND a.product_id = #{product_id}
        </if>
        <if test="store_id != null">
            AND psr.store_id = #{store_id}
        </if>
        <if test="checked != null">
            AND a.checked = #{checked}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_cart a
        where 1 = 1
        <if test="user_id != null">
            AND a.user_id = #{user_id}
        </if>
        <if test="goods_id != null">
            AND a.goods_id = #{goods_id}
        </if>
        <if test="product_id != product_id">
            AND a.product_id = #{product_id}
        </if>
        <if test="checked != null">
            AND a.checked = #{checked}
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.api.entity.CartVo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO mall_cart
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="user_id != null" >
                user_id,
            </if>
            <if test="stockNum != null" >
                stock_num,
            </if>
            <if test="goods_id != null" >
                goods_id,
            </if>
            <if test="sku != null" >
                sku,
            </if>
            <if test="goods_sn != null" >
                goods_sn,
            </if>
            <if test="product_id != null" >
                product_id,
            </if>
            <if test="goods_name != null" >
                goods_name,
            </if>
            <if test="market_price != null" >
                market_price,
            </if>
            <if test="retail_price != null" >
                retail_price,
            </if>
            <if test="number != null" >
                number,
            </if>
            <if test="goods_specification_ids != null" >
                goods_specification_ids,
            </if>
            <if test="checked != null" >
                checked,
            </if>
            <if test="list_pic_url != null" >
                list_pic_url,
            </if>
            <if test="store_id != null" >
                store_id,
            </if>
            <if test="goodsBizType != null" >
                goods_biz_type,
            </if>
            <if test="createrSn != null" >
                creater_sn,
            </if>
            <if test="createTime != null" >
                create_time,
            </if>
            <if test="moderSn != null" >
                moder_sn,
            </if>
            <if test="modTime != null" >
                mod_time,
            </if>
            <if test="tstm != null" >
                tstm,
            </if>
            <if test="goods_specification_name_value != null" >
                goods_specification_name_value,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="user_id != null" >
                #{user_id},
            </if>
            <if test="stockNum != null" >
                #{stockNum},
            </if>
            <if test="goods_id != null" >
                #{goods_id,jdbcType=INTEGER},
            </if>
            <if test="sku != null" >
                #{sku,jdbcType=VARCHAR},
            </if>
            <if test="goods_sn != null" >
                #{goods_sn,jdbcType=VARCHAR},
            </if>
            <if test="product_id != null" >
                #{product_id,jdbcType=INTEGER},
            </if>
            <if test="goods_name != null" >
                #{goods_name,jdbcType=VARCHAR},
            </if>
            <if test="market_price != null" >
                #{market_price,jdbcType=DECIMAL},
            </if>
            <if test="retail_price != null" >
                #{retail_price,jdbcType=DECIMAL},
            </if>
            <if test="number != null" >
                #{number,jdbcType=SMALLINT},
            </if>
            <if test="goods_specification_ids != null" >
                #{goods_specification_ids,jdbcType=VARCHAR},
            </if>
            <if test="checked != null" >
                #{checked,jdbcType=BIT},
            </if>
            <if test="list_pic_url != null" >
                #{list_pic_url,jdbcType=VARCHAR},
            </if>
            <if test="store_id != null" >
                #{store_id,jdbcType=INTEGER},
            </if>
            <if test="goodsBizType != null" >
                #{goodsBizType,jdbcType=CHAR},
            </if>
            <if test="createrSn != null" >
                #{createrSn,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null" >
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="moderSn != null" >
                #{moderSn,jdbcType=VARCHAR},
            </if>
            <if test="modTime != null" >
                #{modTime,jdbcType=TIMESTAMP},
            </if>
            <if test="tstm != null" >
                #{tstm,jdbcType=TIMESTAMP},
            </if>
            <if test="goods_specification_name_value != null" >
                #{goods_specification_name_value,jdbcType=LONGVARCHAR},
            </if>
        </trim>
	</insert>

    <update id="update" parameterType="com.kmall.api.entity.CartVo">
        update mall_cart
        <set >
            <if test="user_id != null" >
                user_id = #{user_id},
            </if>
            <if test="stockNum != null" >
                stock_num = #{stockNum},
            </if>
            <if test="goods_id != null" >
                goods_id = #{goods_id,jdbcType=INTEGER},
            </if>
            <if test="sku != null" >
                sku = #{sku,jdbcType=VARCHAR},
            </if>
            <if test="goods_sn != null" >
                goods_sn = #{goods_sn,jdbcType=VARCHAR},
            </if>
            <if test="product_id != null" >
                product_id = #{product_id,jdbcType=INTEGER},
            </if>
            <if test="goods_name != null" >
                goods_name = #{goods_name,jdbcType=VARCHAR},
            </if>
            <if test="market_price != null" >
                market_price = #{market_price,jdbcType=DECIMAL},
            </if>
            <if test="retail_price != null" >
                retail_price = #{retail_price,jdbcType=DECIMAL},
            </if>
            <if test="number != null" >
                number = #{number,jdbcType=SMALLINT},
            </if>
            <if test="goods_specification_ids != null" >
                goods_specification_ids = #{goods_specification_ids,jdbcType=VARCHAR},
            </if>
            <if test="checked != null" >
                checked = #{checked,jdbcType=BIT},
            </if>
            <if test="list_pic_url != null" >
                list_pic_url = #{list_pic_url,jdbcType=VARCHAR},
            </if>
            <if test="store_id != null" >
                store_id = #{store_id,jdbcType=INTEGER},
            </if>
            <if test="goodsBizType != null" >
                goods_biz_type = #{goodsBizType,jdbcType=CHAR},
            </if>
            <if test="createrSn != null" >
                creater_sn = #{createrSn,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null" >
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="moderSn != null" >
                moder_sn = #{moderSn,jdbcType=VARCHAR},
            </if>
            <if test="modTime != null" >
                mod_time = #{modTime,jdbcType=TIMESTAMP},
            </if>
            <if test="tstm != null" >
                tstm = #{tstm,jdbcType=TIMESTAMP},
            </if>
            <if test="goods_specification_name_value != null" >
                goods_specification_name_value = #{goods_specification_name_value,jdbcType=LONGVARCHAR},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="updateCheck">
        update mall_cart
        set `checked` = #{isChecked}
        where product_id in
        <foreach item="product_id" collection="productIds" open="(" separator="," close=")">
            #{product_id}
        </foreach>
        and user_id = #{user_id} and store_id = #{store_id}
    </update>

    <delete id="delete">
		delete from mall_cart where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from mall_cart where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteByProductIds">
        delete from mall_cart where product_id in
        <foreach item="id" collection="productIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        and store_id = #{store_id}
    </delete>

    <delete id="deleteByCart">
        delete from mall_cart
        where  user_id = #{user_id} and store_id = #{store_id} and  checked = #{checked}
    </delete>
</mapper>