<?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.admin.dao.CartDao">
    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.admin.entity.CartEntity" id="cartMap">
        <result property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="userId" column="user_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsSn" column="goods_sn"/>
        <result property="productId" column="product_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="marketPrice" column="market_price"/>
        <result property="retailPrice" column="retail_price"/>
        <result property="number" column="number"/>
        <result property="goodsSpecificationNameValue" column="goods_specification_name_value"/>
        <result property="goodsSpecificationIds" column="goods_specification_ids"/>
        <result property="checked" column="checked"/>
        <result property="listPicUrl" 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"/>
        <result property="storeName" column="store_name"/>

    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.CartEntity">
		select * from mall_cart where id = #{value}
	</select>

    <select id="queryList" resultMap="cartMap">
        select a.* ,b.nickname as user_name
        from mall_cart a
        left join mall_user b on a.user_id = b.id
        where 1=1
        <if test="userId != null">
            AND a.user_id = #{userId}
        </if>
        <if test="goodsBizType != null">
            AND a.goods_biz_type = #{goodsBizType}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by 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 where 1=1
        <if test="userId != null">
            AND mall_cart.user_id = #{userId}
        </if>
	</select>

    <insert id="save" parameterType="com.kmall.admin.entity.CartEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_cart
		(
			`user_id`,
			`goods_id`, 
			`goods_sn`, 
			`product_id`, 
			`goods_name`, 
			`market_price`, 
			`retail_price`, 
			`number`,
			`goods_specification_name_value`,
			`goods_specification_ids`,
			`checked`, 
			`list_pic_url`,
			`stock_num`,
			`store_id`,
			`sku`,
			`goods_biz_type`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`
		)
		values
		(
			#{userId},
			#{goodsId}, 
			#{goodsSn}, 
			#{productId}, 
			#{goodsName}, 
			#{marketPrice}, 
			#{retailPrice}, 
			#{number}, 
			#{goodsSpecificationNameValue},
			#{goodsSpecificationIds},
			#{checked}, 
			#{listPicUrl},
			#{stockNum},
			#{storeId},
			#{sku},
			#{goodsBizType},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime}
		)
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.CartEntity">
        update mall_cart
        <set>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="goodsId != null">`goods_id` = #{goodsId},</if>
            <if test="goodsSn != null">`goods_sn` = #{goodsSn},</if>
            <if test="productId != null">`product_id` = #{productId},</if>
            <if test="goodsName != null">`goods_name` = #{goodsName},</if>
            <if test="marketPrice != null">`market_price` = #{marketPrice},</if>
            <if test="retailPrice != null">`retail_price` = #{retailPrice},</if>
            <if test="number != null">`number` = #{number},</if>
            <if test="goodsSpecificationNameValue != null">`goods_specification_name_value` =
                #{goodsSpecificationNameValue},
            </if>
            <if test="goodsSpecificationIds != null">`goods_specification_ids` = #{goodsSpecificationIds},</if>
            <if test="checked != null">`checked` = #{checked},</if>
            <if test="listPicUrl != null">`list_pic_url` = #{listPicUrl},</if>
            <if test="stockNum != null">`stock_num` = #{stockNum},</if>
            <if test="storeId != null">`store_id` = #{storeId},</if>
            <if test="goodsBizType != null">`goods_biz_type` = #{goodsBizType},</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>
        </set>
        where id = #{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>

</mapper>