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

    <resultMap type="com.kmall.admin.entity.mk.store.StoreTopicEntity" id="storeTopicMap">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <result property="itemPicUrl" column="item_pic_url"/>
        <result property="subtitle" column="subtitle"/>
        <result property="promTypeId" column="prom_type_id"/>
        <result property="topicPrice" column="topic_price"/>
        <result property="storeId" column="store_id"/>
        <result property="thirdMerchSn" column="third_merch_sn"/>
        <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.StoreTopicEntity">
		select
			`id`,
			`title`,
			`content`,
			`item_pic_url`,
			`subtitle`,
			`prom_type_id`,
			`topic_price`,
			`store_id`,
			`third_merch_sn`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mall_store_topic
		where id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.mk.store.StoreTopicEntity">
		select
		d.`id`,
		d.`title`,
		d.`content`,
		d.`item_pic_url`,
		d.`subtitle`,
		d.`prom_type_id`,
		d.`topic_price`,
		d.`store_id`,
		d.`third_merch_sn`,
		d.`is_valid`,
		d.`note`,
		d.`creater_sn`,
		d.`create_time`,
		d.`moder_sn`,
		d.`mod_time`,
		d.`tstm`
		from mall_store_topic d left join third_merchant_biz t on d.third_merch_sn = t.third_merch_sn
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND title LIKE concat('%',#{name},'%')
		</if>
		<if test="storeId != null and storeId != ''">
			and d.store_id = #{storeId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			and d.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="merchSn != null and merchSn != ''">
			and t.merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND t.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by d.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_store_topic d left join third_merchant_biz t on d.third_merch_sn = t.third_merch_sn
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND title LIKE concat('%',#{name},'%')
		</if>
		<if test="storeId != null and storeId != ''">
			and d.store_id = #{storeId}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn != ''">
			and d.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="merchSn != null and merchSn != ''">
			and t.merch_sn = #{merchSn}
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode != ''">
			AND t.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.mk.store.StoreTopicEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_store_topic(
			`title`,
			`content`,
			`item_pic_url`,
			`subtitle`,
			`prom_type_id`,
			`topic_price`,
			`store_id`,
			`third_merch_sn`,
			`is_valid`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{title},
			#{content},
			#{itemPicUrl},
			#{subtitle},
			#{promTypeId},
			#{topicPrice},
			#{storeId},
			#{thirdMerchSn},
			#{isValid},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.mk.store.StoreTopicEntity">
		update mall_store_topic 
		<set>
			<if test="title != null">`title` = #{title}, </if>
			<if test="content != null">`content` = #{content}, </if>
			<if test="itemPicUrl != null">`item_pic_url` = #{itemPicUrl}, </if>
			<if test="subtitle != null">`subtitle` = #{subtitle}, </if>
			<if test="promTypeId != null">`prom_type_id` = #{promTypeId}, </if>
			<if test="topicPrice != null">`topic_price` = #{topicPrice}, </if>
			<if test="storeId != null">`store_id` = #{storeId}, </if>
			<if test="thirdMerchSn != null">`third_merch_sn` = #{thirdMerchSn}, </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 id = #{id}
	</update>
	
	<delete id="delete">
		delete from mall_store_topic where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_store_topic where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>