<?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.OfflineCartDao">

    <resultMap type="com.kmall.admin.entity.OfflineCartEntity" id="offlineCartMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="sku" column="sku"/>
        <result property="goodsSn" column="goods_sn"/>
        <result property="productId" column="product_id"/>
		<result property="prodBarcode" column="prod_barcode"/>
        <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="stockNum" column="stock_num"/>
        <result property="listPicUrl" column="list_pic_url"/>
        <result property="storeId" column="store_id"/>
        <result property="createrSn" column="creater_sn"/>
        <result property="createTime" column="create_time"/>
        <result property="moderSn" column="moder_sn"/>
        <result property="modTime" column="mod_time"/>
        <result property="tstm" column="tstm"/>
    </resultMap>
	<select id="queryObjectByGoodsIdAndType" resultType="com.kmall.admin.entity.OfflineCartEntity">
		select * from offline_cart where goods_id = #{goodsId} and user_id = #{userId}
	</select>

	<select id="queryOfflineCartByBizType" resultMap="offlineCartMap">
		select a.* ,s.store_name
		from offline_cart a
		inner join mall_store s on a.store_id=s.id
		where 1=1
		<if test="userId != null">
			AND a.user_id = #{userId}
		</if>
		<if test="storeId != null">
			AND a.store_id = #{storeId}
		</if>
	</select>

	<select id="queryObject" resultType="com.kmall.admin.entity.OfflineCartEntity">
		select
			`id`,
			`user_id`,
			`goods_id`,
			`sku`,
			`goods_sn`,
			`product_id`,
			`goods_name`,
			`market_price`,
			`retail_price`,
			`number`,
			`stock_num`,
			`list_pic_url`,
			`store_id`,
			prod_barcode,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from offline_cart
		where id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.OfflineCartEntity">
		select
    		`id`,
    		`user_id`,
    		`goods_id`,
    		`sku`,
    		`goods_sn`,
    		`product_id`,
    		`goods_name`,
    		`market_price`,
    		`retail_price`,
    		`number`,
    		`stock_num`,
    		`list_pic_url`,
    		`store_id`,
			prod_barcode,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`
		from offline_cart
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</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 offline_cart
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.OfflineCartEntity" useGeneratedKeys="true" keyProperty="id">
		insert into offline_cart(
			`user_id`,
			`goods_id`,
			`sku`,
			`goods_sn`,
			`product_id`,
			`prod_barcode`,
			`goods_name`,
			`market_price`,
			`retail_price`,
			`number`,
			`stock_num`,
			`list_pic_url`,
			`store_id`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{userId},
			#{goodsId},
			#{sku},
			#{goodsSn},
			#{productId},
			#{prodBarcode},
			#{goodsName},
			#{marketPrice},
			#{retailPrice},
			#{number},
			#{stockNum},
			#{listPicUrl},
			#{storeId},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.OfflineCartEntity">
		update offline_cart 
		<set>
			<if test="userId != null">`user_id` = #{userId}, </if>
			<if test="goodsId != null">`goods_id` = #{goodsId}, </if>
			<if test="sku != null">`sku` = #{sku}, </if>
			<if test="goodsSn != null">`goods_sn` = #{goodsSn}, </if>
			<if test="productId != null">`product_id` = #{productId}, </if>
			<if test="prodBarcode != null">`prod_barcode` = #{prodBarcode}, </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="stockNum != null">`stock_num` = #{stockNum}, </if>
			<if test="listPicUrl != null">`list_pic_url` = #{listPicUrl}, </if>
			<if test="storeId != null">`store_id` = #{storeId}, </if>
			<if test="createrSn != null">`creater_sn` = #{createrSn}, </if>
			<if test="createTime != null">`create_time` = #{createTime}, </if>
			<if test="moderSn != null">`moder_sn` = #{moderSn}, </if>
			<if test="modTime != null">`mod_time` = #{modTime}, </if>
			<if test="tstm != null">`tstm` = #{tstm}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from offline_cart where id = #{value}
	</delete>

	<delete id="deleteByUserId">
		delete from offline_cart where user_id = #{userId}
	</delete>

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

</mapper>