<?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.cm.common.article.dao.articlecategory.IArticleCategoryDao">

    <resultMap id="articleCategoryDTO" type="com.cm.common.article.pojo.dtos.articlecategory.ArticleCategoryDTO">
        <id column="article_category_id" property="articleCategoryId"/>
        <result column="title" property="title"/>
        <result column="summary" property="summary"/>
    </resultMap>

    <!-- 新增文章类别 -->
    <insert id="saveArticleCategory" parameterType="map">
        INSERT INTO article_category (
            article_category_id,
            title,
            summary,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{articleCategoryId},
            #{title},
            #{summary},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除文章类别 -->
    <update id="removeArticleCategory" parameterType="map">
        UPDATE
            article_category
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            article_category_id IN
        <foreach collection="articleCategoryIds" index="index" open="(" separator="," close=")">
            #{articleCategoryIds[${index}]}
        </foreach>
    </update>

    <!-- 修改文章类别 -->
    <update id="updateArticleCategory" parameterType="map">
        UPDATE
            article_category
        SET
        <if test="title != null and title != ''">
            title = #{title},
        </if>
        <if test="summary != null and summary != ''">
            summary = #{summary},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            article_category_id = #{articleCategoryId}
    </update>

    <!-- 文章类别详情 -->
    <select id="getArticleCategory" parameterType="map" resultMap="articleCategoryDTO">
        SELECT
            t1.title,
            t1.summary,
            t1.article_category_id
        FROM
            article_category t1
        WHERE
            t1.is_delete = 0
        <if test="articleCategoryId != null and articleCategoryId != ''">
        AND
            t1.article_category_id = #{articleCategoryId}
        </if>
    </select>

    <!-- 文章类别列表 -->
    <select id="listArticleCategory" parameterType="map" resultMap="articleCategoryDTO">
        SELECT
            t1.title,
            t1.summary,
            t1.article_category_id
        FROM
            article_category t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            t1.title LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="articleCategoryIds != null and articleCategoryIds.size > 0">
        AND
            t1.article_category_id IN
            <foreach collection="articleCategoryIds" index="index" open="(" separator="," close=")">
            #{articleCategoryIds[${index}]}
            </foreach>
        </if>
        ORDER BY t1.gmt_create
    </select>

</mapper>