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

    <resultMap type="com.kmall.admin.entity.TopicEntity" id="topicMap">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <result property="avatar" column="avatar"/>
        <result property="itemPicUrl" column="item_pic_url"/>
        <result property="subtitle" column="subtitle"/>
        <result property="topicCategoryId" column="topic_category_id"/>
        <result property="priceInfo" column="price_info"/>
        <result property="readCount" column="read_count"/>
        <result property="scenePicUrl" column="scene_pic_url"/>
        <result property="topicTemplateId" column="topic_template_id"/>
        <result property="topicTagId" column="topic_tag_id"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.TopicEntity">
		select
			`id`,
			`title`,
			`content`,
			`avatar`,
			`item_pic_url`,
			`subtitle`,
			`topic_category_id`,
			`price_info`,
			`read_count`,
			`scene_pic_url`,
			`topic_template_id`,
			`topic_tag_id`
		from mall_topic
		where id = #{id}
	</select>

    <select id="queryList" resultType="com.kmall.admin.entity.TopicEntity">
        select
        `id`,
        `title`,
        `content`,
        `avatar`,
        `item_pic_url`,
        `subtitle`,
        `topic_category_id`,
        `price_info`,
        `read_count`,
        `scene_pic_url`,
        `topic_template_id`,
        `topic_tag_id`
        from mall_topic
        WHERE 1=1
        <if test="title != null and title.trim() != ''">
            AND title LIKE concat('%',#{title},'%')
        </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_topic
        WHERE 1=1
        <if test="title != null and title.trim() != ''">
            AND title LIKE concat('%',#{title},'%')
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.TopicEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_topic(
			`title`,
			`content`,
			`avatar`,
			`item_pic_url`,
			`subtitle`,
			`topic_category_id`,
			`price_info`,
			`read_count`,
			`scene_pic_url`,
			`topic_template_id`,
			`topic_tag_id`)
		values(
			#{title},
			#{content},
			#{avatar},
			#{itemPicUrl},
			#{subtitle},
			#{topicCategoryId},
			#{priceInfo},
			#{readCount},
			#{scenePicUrl},
			#{topicTemplateId},
			#{topicTagId})
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.TopicEntity">
        update mall_topic
        <set>
            <if test="title != null">`title` = #{title},</if>
            <if test="content != null">`content` = #{content},</if>
            <if test="avatar != null">`avatar` = #{avatar},</if>
            <if test="itemPicUrl != null">`item_pic_url` = #{itemPicUrl},</if>
            <if test="subtitle != null">`subtitle` = #{subtitle},</if>
            <if test="topicCategoryId != null">`topic_category_id` = #{topicCategoryId},</if>
            <if test="priceInfo != null">`price_info` = #{priceInfo},</if>
            <if test="readCount != null">`read_count` = #{readCount},</if>
            <if test="scenePicUrl != null">`scene_pic_url` = #{scenePicUrl},</if>
            <if test="topicTemplateId != null">`topic_template_id` = #{topicTemplateId},</if>
            <if test="topicTagId != null">`topic_tag_id` = #{topicTagId}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from mall_topic where id = #{value}
	</delete>

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

</mapper>