<?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.fs.store.mapper.FsHomeArticleMapper">

    <resultMap type="FsHomeArticle" id="FsHomeArticleResult">
        <result property="articleId"    column="article_id"    />
        <result property="categoryId"    column="category_id"    />
        <result property="title"    column="title"    />
        <result property="imageUrl"    column="image_url"    />
        <result property="isTui"    column="is_tui"    />
        <result property="videoUrl"    column="video_url"    />
        <result property="content"    column="content"    />
        <result property="views"    column="views"    />
        <result property="sort"    column="sort"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="publishTime"    column="publish_time"    />
        <result property="publicStatus"    column="public_status"    />
        <result property="description"    column="description"    />
    </resultMap>

    <sql id="selectFsHomeArticleVo">
        select article_id, category_id, title, image_url, is_tui, video_url, content, views, sort, create_time, update_time, publish_time, public_status, description from fs_home_article
    </sql>

    <select id="selectFsHomeArticleList" resultType="FsHomeArticle">
        SELECT
        a.article_id,
        a.category_id,
        ac.category_name,
        a.title,
        a.image_url,
        a.is_tui,
        a.video_url,
        a.content,
        a.views,
        a.sort,
        a.create_time,
        a.update_time,
        a.publish_time,
        a.public_status,
        a.description
        FROM
        fs_home_article a
        left join fs_home_article_category ac on ac.category_id = a.category_id
        <where>
            <if test="categoryId != null "> and a.category_id = #{categoryId}</if>
            <if test="title != null  and title != ''"> and a.title like concat('%', #{title}, '%')</if>
            <if test="imageUrl != null  and imageUrl != ''"> and a.image_url = #{imageUrl}</if>
            <if test="isTui != null "> and a.is_tui = #{isTui}</if>
            <if test="videoUrl != null  and videoUrl != ''"> and a.video_url = #{videoUrl}</if>
            <if test="content != null  and content != ''"> and a.content = #{content}</if>
            <if test="views != null "> and a.views = #{views}</if>
            <if test="sort != null "> and a.sort = #{sort}</if>
            <if test="publishTime != null "> and a.publish_time = #{publishTime}</if>
            <if test="publicStatus != null "> and a.public_status = #{publicStatus}</if>
        </where>
        order by a.create_time desc
    </select>

    <select id="selectFsHomeArticleById" parameterType="Long" resultMap="FsHomeArticleResult">
        <include refid="selectFsHomeArticleVo"/>
        where article_id = #{articleId}
    </select>

    <insert id="insertFsHomeArticle" parameterType="FsHomeArticle" useGeneratedKeys="true" keyProperty="articleId">
        insert into fs_home_article
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">category_id,</if>
            <if test="title != null">title,</if>
            <if test="imageUrl != null">image_url,</if>
            <if test="isTui != null">is_tui,</if>
            <if test="videoUrl != null">video_url,</if>
            <if test="content != null">content,</if>
            <if test="views != null">views,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="publishTime != null">publish_time,</if>
            <if test="publicStatus != null">public_status,</if>
            <if test="description != null">description,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">#{categoryId},</if>
            <if test="title != null">#{title},</if>
            <if test="imageUrl != null">#{imageUrl},</if>
            <if test="isTui != null">#{isTui},</if>
            <if test="videoUrl != null">#{videoUrl},</if>
            <if test="content != null">#{content},</if>
            <if test="views != null">#{views},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="publishTime != null">#{publishTime},</if>
            <if test="publicStatus != null">#{publicStatus},</if>
            <if test="description != null">#{description},</if>
         </trim>
    </insert>

    <update id="updateFsHomeArticle" parameterType="FsHomeArticle">
        update fs_home_article
        <trim prefix="SET" suffixOverrides=",">
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="title != null">title = #{title},</if>
            <if test="imageUrl != null">image_url = #{imageUrl},</if>
            <if test="isTui != null">is_tui = #{isTui},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="content != null">content = #{content},</if>
            <if test="views != null">views = #{views},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="publishTime != null">publish_time = #{publishTime},</if>
            <if test="publicStatus != null">public_status = #{publicStatus},</if>
            <if test="description != null">description = #{description},</if>
        </trim>
        where article_id = #{articleId}
    </update>

    <delete id="deleteFsHomeArticleById" parameterType="Long">
        delete from fs_home_article where article_id = #{articleId}
    </delete>

    <delete id="deleteFsHomeArticleByIds" parameterType="String">
        delete from fs_home_article where article_id in
        <foreach item="articleId" collection="array" open="(" separator="," close=")">
            #{articleId}
        </foreach>
    </delete>

    <select id="selectHomeArticlePageList" resultType="HomeArticleListVO">
        SELECT
        article_id,
        title,
        content,
        image_url,
        views,
        publish_time,
        public_status,
        description
        FROM
        fs_home_article
        <where>
            public_status = 1
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="keywords != null  and keywords != ''"> and title like concat('%', #{keywords}, '%')</if>
        </where>
        order by sort asc
    </select>

    <select id="getHomeArticleDetailsById" resultType="HomeArticleDetailsVO">
        SELECT
            article_id,
            title,
            image_url,
            video_url,
            content,
            publish_time,
            description,
            views
        FROM
            fs_home_article
        WHERE
            article_id = #{articleId}
    </select>

</mapper>
