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

    <resultMap type="com.kmall.admin.entity.mk.store.MkStorePromEntity" id="mkStorePromMap">
        <result property="promId" column="prom_id"/>
        <result property="promName" column="prom_name"/>
		<result property="thirdMerchSn" column="third_merch_sn"/>
        <result property="storeId" column="store_id"/>
        <result property="promBegTime" column="prom_beg_time"/>
        <result property="promEndTime" column="prom_end_time"/>
        <result property="promType" column="prom_type"/>
        <result property="promTypeId" column="prom_type_id"/>
        <result property="storeTopicId" column="store_topic_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="promUrl" column="prom_url"/>
        <result property="promChnlId" column="prom_chnl_id"/>
        <result property="isPut" column="is_put"/>
        <result property="isFinished" column="is_finished"/>
        <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"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.mk.store.MkStorePromEntity">
		select
			`prom_id`,
			`prom_name`,
			`store_id`,
			`prom_beg_time`,
			`prom_end_time`,
			`prom_type`,
			`prom_type_id`,
			`store_topic_id`,
			`goods_id`,
			`prom_url`,
			`prom_chnl_id`,
			`is_put`,
			`is_finished`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`,third_merch_sn
		from mk_store_prom
		where prom_id = #{id}
	</select>

	<select id="queryObjectByName" resultType="com.kmall.admin.entity.mk.store.MkStorePromEntity">
		select
		`prom_id`,
		`prom_name`,
		`store_id`,
		`prom_beg_time`,
		`prom_end_time`,
		`prom_type`,
		`prom_type_id`,
		`store_topic_id`,
		`goods_id`,
		`prom_url`,
		`prom_chnl_id`,
		`is_put`,
		`is_finished`,
		`is_valid`,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,third_merch_sn
		from mk_store_prom
		where prom_name = #{promName}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.store.MkStorePromEntity">
		SELECT
		p.`prom_id`,
		p.`prom_name`,
		p.`store_id`,
		p.`prom_beg_time`,
		p.`prom_end_time`,
		p.`prom_type`,
		p.`store_topic_id`,
		p.`goods_id`,
		p.`prom_url`,
		p.`prom_chnl_id`,
		ps.receiv_money,
		p.`prom_type_id`,
		p.`is_put`,
		p.`is_finished`,
		p.`is_valid`,
		p.`note`,
		p.`creater_sn`,
		p.`create_time`,
		p.`moder_sn`,
		p.`mod_time`,
		p.`tstm`,
		p.third_merch_sn,
		c.prom_chn_name 'promChnName',
		s.store_name 'storeName',
		g.NAME 'goodsName',
		ps.browse_num 'browseNum',
		ps.visit_num 'visitNum',
		ps.pay_order_num 'payOrderNum',
		ps.receiv_money 'receivMoney',
		ps.act_money 'actMoney',
		t.title 'topicTitle',
		t.item_pic_url 'itemPicUrl',
		g.list_pic_url 'listPicUrl'
		FROM
		mk_store_prom p
		LEFT JOIN mk_store_prom_chnl c ON p.prom_chnl_id = c.prom_chnl_id
		LEFT JOIN mall_store s ON p.store_id = s.id
		LEFT JOIN mall_goods g ON p.goods_id = g.id
		LEFT JOIN mk_store_prom_stat ps ON p.prom_id = ps.prom_id
		LEFT JOIN mall_store_topic t on p.store_topic_id = t.id
		left join third_merchant_biz tm on p.third_merch_sn = tm.third_merch_sn
		WHERE 1=1 and p.`is_valid`=0
		<if test="promName != null and promName.trim() != ''">
			AND p.prom_name LIKE concat('%',#{promName},'%')
		</if>
		<if test="promType != null and promType != ''">
			AND p.prom_type = #{promType}
		</if>
		<if test="isFinished != null and isFinished != ''">
			AND p.is_finished = #{isFinished}
		</if>
		<if test="goodsId != null and goodsId != ''">
			AND p.goods_id = #{goodsId}
		</if>
		<if test="storeId != null and storeId != ''">
			AND s.id = #{storeId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND p.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND tm.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="merchSn != null and merchSn != ''">
			and tm.merch_sn = #{merchSn}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by p.prom_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 p
		LEFT JOIN mk_store_prom_chnl c ON p.prom_chnl_id = c.prom_chnl_id
		LEFT JOIN mall_store s ON p.store_id = s.id
		LEFT JOIN mall_goods g ON p.goods_id = g.id
		LEFT JOIN mk_store_prom_stat ps ON p.prom_id = ps.prom_id
		LEFT JOIN mall_store_topic t on p.store_topic_id = t.id
		left join third_merchant_biz tm on p.third_merch_sn = tm.third_merch_sn
		WHERE 1=1 and p.`is_valid`=0
		<if test="promName != null and promName.trim() != ''">
			AND p.prom_name LIKE concat('%',#{promName},'%')
		</if>
		<if test="promType != null and promType != ''">
			AND p.prom_type = #{promType}
		</if>
		<if test="isFinished != null and isFinished != ''">
			AND p.is_finished = #{isFinished}
		</if>
		<if test="goodsId != null and goodsId != ''">
			AND p.goods_id = #{goodsId}
		</if>
		<if test="storeId != null and storeId != ''">
			AND s.id = #{storeId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			AND p.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND tm.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="merchSn != null and merchSn != ''">
			and tm.merch_sn = #{merchSn}
		</if>
	</select>

	<insert id="save" parameterType="com.kmall.admin.entity.mk.store.MkStorePromEntity">
		insert into mk_store_prom(
			`prom_id`,
			`prom_name`,
			`store_id`,
			third_merch_sn,
			`prom_beg_time`,
			`prom_end_time`,
			`prom_type`,
			`prom_type_id`,
			`store_topic_id`,
			`goods_id`,
			`prom_url`,
			`prom_chnl_id`,
			`is_put`,
			`is_finished`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{promId},
			#{promName},
			#{storeId},
			#{thirdMerchSn},
			#{promBegTime},
			#{promEndTime},
			#{promType},
			#{promTypeId},
			#{storeTopicId},
			#{goodsId},
			#{promUrl},
			#{promChnlId},
			#{isPut},
			#{isFinished},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.store.MkStorePromEntity">
		update mk_store_prom 
		<set>
			<if test="promName != null">`prom_name` = #{promName}, </if>
			<if test="thirdMerchSn != null">`third_merch_sn` = #{thirdMerchSn}, </if>
			<if test="storeId != null">`store_id` = #{storeId}, </if>
			<if test="promBegTime != null">`prom_beg_time` = #{promBegTime}, </if>
			<if test="promEndTime != null">`prom_end_time` = #{promEndTime}, </if>
			<if test="promType != null">`prom_type` = #{promType}, </if>
			<if test="promTypeId != null">`prom_type_id` = #{promTypeId}, </if>
			<if test="storeTopicId != null">`store_topic_id` = #{storeTopicId}, </if>
			<if test="goodsId != null">`goods_id` = #{goodsId}, </if>
			<if test="promUrl != null">`prom_url` = #{promUrl}, </if>
			<if test="promChnlId != null">`prom_chnl_id` = #{promChnlId}, </if>
			<if test="isPut != null">`is_put` = #{isPut}, </if>
			<if test="isFinished != null">`is_finished` = #{isFinished}, </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_id = #{promId}
	</update>
	
	<delete id="delete">
		delete from mk_store_prom where prom_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mk_store_prom where prom_id in 
		<foreach item="promId" collection="array" open="(" separator="," close=")">
			#{promId}
		</foreach>
	</delete>

</mapper>