<?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.mk.store.MkStorePromOrderRealDao">

    <resultMap type="com.kmall.admin.entity.mk.store.MkStorePromOrderRealEntity" id="mkStorePromOrderRealMap">
        <result property="promOrderRealId" column="prom_order_real_id"/>
        <result property="promId" column="prom_id"/>
        <result property="cartId" column="cart_id"/>
        <result property="orderId" column="order_id"/>
		<result property="orderGoodsId" column="order_goods_id"/>
        <result property="isValid" column="is_valid"/>
        <result property="note" column="note"/>
        <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"/>
		<result property="userId" column="user_id"/>
		<result property="isPromStatScan" column="is_prom_stat_scan"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.store.MkStorePromOrderRealEntity">
		select
			`prom_order_real_id`,
			`prom_id`,
			`cart_id`,
			`order_id`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`,order_goods_id,user_id,is_prom_stat_scan
		from mk_store_prom_order_real
		where prom_order_real_id = #{id}
	</select>

	<select id="queryPromRealListByMap" resultType="com.kmall.admin.entity.mk.store.MkStorePromOrderRealEntity">
		select
		`prom_order_real_id`,
		`prom_id`,
		`cart_id`,
		`order_id`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,order_goods_id,user_id,is_prom_stat_scan
		from mk_store_prom_order_real
		where 1=1
		<if test="promId != null and promId != ''">
			AND prom_id = #{promId}
		</if>
		<if test="cartId != null and cartId != ''">
			AND cart_id = #{cartId}
		</if>
		<if test="orderId != null and orderId != ''">
			AND order_id = #{orderId}
		</if>
	</select>

	<select id="querySubmitNumByPromId" resultType="com.kmall.admin.entity.mk.store.MkStorePromOrderRealEntity">
		SELECT
			s.user_id
		FROM
			mk_store_prom_visitors s
			LEFT JOIN mk_store_prom_order_real r ON s.prom_id = r.prom_id
			AND s.user_id = r.user_id
		WHERE
			r.prom_id = #{promId}
			GROUP BY r.user_id
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.store.MkStorePromOrderRealEntity">
		select
    		`prom_order_real_id`,
    		`prom_id`,
    		`cart_id`,
    		`order_id`,
    		`is_valid`,
    		`note`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`,order_goods_id,user_id,is_prom_stat_scan
		from mk_store_prom_order_real
		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 prom_order_real_id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mk_store_prom_order_real
		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.mk.store.MkStorePromOrderRealEntity">
		insert into mk_store_prom_order_real(
			`prom_order_real_id`,
			`prom_id`,
			`cart_id`,
			`order_id`,
			order_goods_id,
			user_id,
			is_prom_stat_scan,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{promOrderRealId},
			#{promId},
			#{cartId},
			#{orderId},
			#{orderGoodsId},
			#{userId},
			#{isPromStatScan},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.store.MkStorePromOrderRealEntity">
		update mk_store_prom_order_real 
		<set>
			<if test="promId != null">`prom_id` = #{promId}, </if>
			<if test="cartId != null">`cart_id` = #{cartId}, </if>
			<if test="orderId != null">`order_id` = #{orderId}, </if>
			<if test="orderGoodsId != null">`order_goods_id` = #{orderGoodsId}, </if>
			<if test="userId != null">`user_id` = #{userId}, </if>
			<if test="isPromStatScan != null">`is_prom_stat_scan` = #{isPromStatScan}, </if>
			<if test="isValid != null">`is_valid` = #{isValid}, </if>
			<if test="note != null">`note` = #{note}, </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 prom_order_real_id = #{promOrderRealId}
	</update>
	
	<delete id="delete">
		delete from mk_store_prom_order_real where prom_order_real_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_store_prom_order_real where prom_order_real_id in 
		<foreach item="promOrderRealId" collection="array" open="(" separator="," close=")">
			#{promOrderRealId}
		</foreach>
	</delete>

</mapper>