<?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.ruoyi.bst.article.mapper.ArticleMapper">

    <resultMap type="ArticleVO" id="ArticleResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="categoryId"    column="category_id"    />
        <result property="title"    column="title"    />
        <result property="brief"    column="brief"    />
        <result property="content"    column="content"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="articleType"    column="article_type"    />
    </resultMap>

    <sql id="selectArticleVo">
        select
            ba.id,
            ba.category_id,
            ba.title,
            ba.brief,
            ba.content,
            ba.create_time,
            ba.update_time,
            ba.article_type
        from bst_article ba
    </sql>

    <sql id="searchCondition">
        <if test="query.categoryId != null "> and category_id = #{query.categoryId}</if>
        <if test="query.title != null  and query.title != ''"> and title like concat('%', #{query.title}, '%')</if>
        <if test="query.brief != null  and query.brief != ''"> and brief like concat('%', #{query.brief}, '%')</if>
        <if test="query.content != null  and query.content != ''"> and content like concat('%', #{query.content}, '%')</if>
        <if test="query.articleType != null  and query.articleType != ''"> and article_type = #{query.articleType}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectArticleList" parameterType="ArticleQuery" resultMap="ArticleResult">
        <include refid="selectArticleVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectArticleById" parameterType="Long" resultMap="ArticleResult">
        <include refid="selectArticleVo"/>
        where id = #{id}
    </select>
    <select id="selectChildrenById" resultType="com.ruoyi.bst.article.domain.ArticleVO">
            <include refid="selectArticleVo"/>
            where category_id = #{id}
    </select>

    <insert id="insertArticle" parameterType="Article" useGeneratedKeys="true" keyProperty="id">
        insert into bst_article
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">category_id,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="brief != null">brief,</if>
            <if test="content != null">content,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="articleType != null and articleType != ''">article_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">#{categoryId},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="brief != null">#{brief},</if>
            <if test="content != null">#{content},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="articleType != null and articleType != ''">#{articleType},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Article" useGeneratedKeys="true" keyProperty="id">
        insert into bst_article
        <trim prefix="(" suffix=")" suffixOverrides=",">
            category_id,
            title,
            brief,
            content,
            create_time,
            update_time,
            article_type,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.categoryId != null ">#{i.categoryId},</if>
                <if test="i.categoryId == null ">default,</if>
                <if test="i.title != null  and i.title != ''">#{i.title},</if>
                <if test="i.title == null  or i.title == ''">default,</if>
                <if test="i.brief != null ">#{i.brief},</if>
                <if test="i.brief == null ">default,</if>
                <if test="i.content != null ">#{i.content},</if>
                <if test="i.content == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.updateTime != null ">#{i.updateTime},</if>
                <if test="i.updateTime == null ">default,</if>
                <if test="i.articleType != null  and i.articleType != ''">#{i.articleType},</if>
                <if test="i.articleType == null  or i.articleType == ''">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_article
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="category_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.categoryId != null ">
                        WHEN #{item.id} THEN #{item.categoryId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `category_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="title = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.title != null  and item.title != ''">
                        WHEN #{item.id} THEN #{item.title}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `title`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="brief = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.brief != null ">
                        WHEN #{item.id} THEN #{item.brief}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `brief`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="content = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.content != null ">
                        WHEN #{item.id} THEN #{item.content}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `content`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="update_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.updateTime != null ">
                        WHEN #{item.id} THEN #{item.updateTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `update_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="article_type = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.articleType != null  and item.articleType != ''">
                        WHEN #{item.id} THEN #{item.articleType}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `article_type`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateArticle" parameterType="Article">
        update bst_article
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.categoryId != null">category_id = #{data.categoryId},</if>
        <if test="data.title != null and data.title != ''">title = #{data.title},</if>
        <if test="data.brief != null">brief = #{data.brief},</if>
        <if test="data.content != null">content = #{data.content},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
        <if test="data.articleType != null and data.articleType != ''">article_type = #{data.articleType},</if>
    </sql>

    <delete id="deleteArticleById" parameterType="Long">
        delete from bst_article where id = #{id}
    </delete>

    <delete id="deleteArticleByIds" parameterType="String">
        delete from bst_article where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
