<?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.articleCategory.mapper.ArticleCategoryMapper">

    <resultMap type="ArticleCategoryVO" id="ArticleCategoryResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="categoryName"    column="category_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="categoryStatus"    column="category_status"    />
        <result property="sort"    column="sort"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectArticleCategoryVo">
        select
            bac.id,
            bac.category_name,
            bac.parent_id,
            bac.category_status,
            bac.sort,
            bac.create_time
        from bst_article_category bac
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bac.id = #{query.id}</if>
        <if test="query.categoryName != null  and query.categoryName != ''"> and bac.category_name like concat('%', #{query.categoryName}, '%')</if>
        <if test="query.parentId != null "> and bac.parent_id = #{query.parentId}</if>
        <if test="query.categoryStatus != null "> and bac.category_status = #{query.categoryStatus}</if>
        <if test="query.sort != null "> and bac.sort = #{query.sort}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectArticleCategoryList" parameterType="ArticleCategoryQuery" resultMap="ArticleCategoryResult">
        <include refid="selectArticleCategoryVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectArticleCategoryById" parameterType="Long" resultMap="ArticleCategoryResult">
        <include refid="selectArticleCategoryVo"/>
        where id = #{id}
    </select>

    <select id="checkByNameAndParetId" resultType="java.lang.Boolean">
        select count(1) from bst_article_category
        <where>
            <if test="categoryName != null and categoryName != ''">category_name = #{categoryName}</if>
            <if test="parentId != null">AND parent_id = #{parentId}</if>
        </where>
    </select>

    <select id="selectChildrenCategory" resultType="ArticleCategoryVO">
        <include refid="selectArticleCategoryVo"/>
        <where>
            bac.parent_id = #{id}
        </where>
    </select>

    <insert id="insertArticleCategory" parameterType="ArticleCategory" useGeneratedKeys="true" keyProperty="id">
        insert into bst_article_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="categoryName != null and categoryName != ''">category_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="categoryStatus != null">category_status,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="categoryName != null and categoryName != ''">#{categoryName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="categoryStatus != null">#{categoryStatus},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>



    <insert id="batchInsert" parameterType="ArticleCategory" useGeneratedKeys="true" keyProperty="id">
        insert into bst_article_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            category_name,
            parent_id,
            category_status,
            sort,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.categoryName != null  and i.categoryName != ''">#{i.categoryName},</if>
                <if test="i.categoryName == null  or i.categoryName == ''">default,</if>
                <if test="i.parentId != null ">#{i.parentId},</if>
                <if test="i.parentId == null ">default,</if>
                <if test="i.categoryStatus != null ">#{i.categoryStatus},</if>
                <if test="i.categoryStatus == null ">default,</if>
                <if test="i.sort != null ">#{i.sort},</if>
                <if test="i.sort == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_article_category
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="category_name = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.categoryName != null  and item.categoryName != ''">
                        WHEN #{item.id} THEN #{item.categoryName}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `category_name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="parent_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.parentId != null ">
                        WHEN #{item.id} THEN #{item.parentId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `parent_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="category_status = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.categoryStatus != null ">
                        WHEN #{item.id} THEN #{item.categoryStatus}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `category_status`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="sort = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.sort != null ">
                        WHEN #{item.id} THEN #{item.sort}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `sort`
                    </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>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateArticleCategory" parameterType="ArticleCategory">
        update bst_article_category
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{id}
    </update>

    <!--updateArticleCategorySortOnly-->
    <update id="updateArticleCategorySortAndStatusOnly" parameterType="ArticleCategory">
        update bst_article_category set
        <if test="categoryStatus != null">category_status = #{categoryStatus},</if>
        <if test="sort != null">sort = #{sort}</if>
        where id = #{id}
    </update>

    <sql id="updateColumns">
        <if test="categoryName != null and categoryName != ''">category_name = #{data.categoryName},</if>
        <if test="parentId != null">parent_id = #{data.parentId},</if>
        <if test="categoryStatus != null">category_status = #{data.categoryStatus},</if>
        <if test="sort != null">sort = #{data.sort},</if>
        <if test="createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteArticleCategoryById" parameterType="Long">
        delete from bst_article_category where id = #{id}
    </delete>

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