<?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.articlecontent.IArticleContentDao">

    <resultMap id="articleContentDTO" type="com.cm.common.article.pojo.dtos.articlecontent.ArticleContentDTO">
        <id column="article_content_id" property="articleContentId"/>
        <result column="title" property="title"/>
        <result column="sub_title" property="subTitle"/>
        <result column="summary" property="summary"/>
        <result column="link" property="link"/>
        <result column="source" property="source"/>
        <result column="author" property="author"/>
        <result column="publish_date" property="publishDate"/>
        <result column="is_publish" property="isPublish"/>
        <result column="content" property="content"/>
        <result column="sort" property="sort"/>
        <result column="article_category_id" property="articleCategoryId"/>
        <result column="title_join_by_article_category_id" property="titleJoinByArticleCategoryId"/>
        <result column="summary_join_by_article_category_id" property="summaryJoinByArticleCategoryId"/>
    </resultMap>

    <!-- 文章内容列表 -->
    <select id="listArticleContentAll" parameterType="map" resultMap="articleContentDTO">
        SELECT
            t1.title,
            t1.sub_title,
            t1.summary,
            t1.source,
            t1.author,
            t1.publish_date,
            t1.is_publish,
            t1.content,
            t1.article_category_id,
            jt1.title title_join_by_article_category_id,
            jt1.summary summary_join_by_article_category_id,
            t1.sort,
            t1.article_content_id
        FROM
            article_content t1
        INNER JOIN
            article_category jt1
        ON
            t1.article_category_id = jt1.article_category_id
            AND
            jt1.is_delete = 0
        WHERE
        t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND
            t1.title LIKE CONCAT('%s', #{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="articleCategoryId != null and articleCategoryId != ''">
            AND
            t1.article_category_id = #{articleCategoryId}
        </if>
        <if test="articleContentIds != null and articleContentIds.size > 0">
            AND
            t1.article_content_id IN
            <foreach collection="articleContentIds" index="index" open="(" separator="," close=")">
                #{articleContentIds[${index}]}
            </foreach>
        </if>
        <if test="creator != null and creator != ''">
            AND
            t1.creator = #{creator}
        </if>
        <if test="creators != null and creators.size > 0">
            AND
            t1.creator IN
            <foreach collection="creators" index="index" open="(" separator="," close=")">
                #{creators[${index}]}
            </foreach>
        </if>
        ORDER BY t1.publish_date DESC, t1.gmt_create DESC
    </select>

    <!-- 新增文章内容 -->
    <insert id="saveArticleContent" parameterType="map">
        INSERT INTO article_content(
            article_content_id,
            title,
            sub_title,
            summary,
            link,
            source,
            author,
            publish_date,
            is_publish,
            content,
            sort,
            article_category_id,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{articleContentId},
            #{title},
            #{subTitle},
            #{summary},
            #{link},
            #{source},
            #{author},
            #{publishDate},
            #{isPublish},
            #{content},
            #{sort},
            #{articleCategoryId},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除文章内容 -->
    <update id="removeArticleContent" parameterType="map">
        UPDATE
            article_content
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            article_content_id IN
            <foreach collection="articleContentIds" index="index" open="(" separator="," close=")">
                #{articleContentIds[${index}]}
            </foreach>
    </update>

    <!-- 修改文章内容 -->
    <update id="updateArticleContent" parameterType="map">
        UPDATE
            article_content
        SET
        <if test="title != null and title != ''">
            title = #{title},
        </if>
        <if test="subTitle != null and subTitle != ''">
            sub_title = #{subTitle},
        </if>
        <if test="summary != null and summary != ''">
            summary = #{summary},
        </if>
        <if test="link != null and link != ''">
            link = #{link},
        </if>
        <if test="source != null and source != ''">
            source = #{source},
        </if>
        <if test="author != null and author != ''">
            author = #{author},
        </if>
        <if test="publishDate != null and publishDate != ''">
            publish_date = #{publishDate},
        </if>
        <if test="isPublish != null">
            is_publish = #{isPublish},
        </if>
        <if test="content != null and content != ''">
            content = #{content},
        </if>
        <if test="articleCategoryId != null and articleCategoryId != ''">
            article_category_id = #{articleCategoryId},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            article_content_id = #{articleContentId}
    </update>

    <!-- 文章内容详情 -->
    <select id="getArticleContent" parameterType="map" resultMap="articleContentDTO">
        SELECT
            t1.title,
            t1.sub_title,
            t1.summary,
            t1.link,
            t1.source,
            t1.author,
            t1.publish_date,
            t1.is_publish,
            t1.content,
            t1.article_category_id,
            t1.sort,
            t1.article_content_id
        FROM
            article_content t1
        WHERE
            t1.is_delete = 0
        <if test="articleContentId != null and articleContentId != ''">
        AND
            t1.article_content_id = #{articleContentId}
        </if>
    </select>

    <!-- 文章内容列表 -->
    <select id="listArticleContent" parameterType="map" resultMap="articleContentDTO">
        SELECT
            t1.title,
            t1.sub_title,
            t1.summary,
            t1.source,
            t1.author,
            t1.publish_date,
            t1.is_publish,
            t1.article_category_id,
            jt1.title title_join_by_article_category_id,
            jt1.summary summary_join_by_article_category_id,
            t1.sort,
            t1.article_content_id
        FROM
            article_content t1
        INNER JOIN
            article_category jt1
        ON
            t1.article_category_id = jt1.article_category_id
        AND
            jt1.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND
            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="articleCategoryId != null and articleCategoryId != ''">
        AND
            t1.article_category_id = #{articleCategoryId}
        </if>
        <if test="articleContentIds != null and articleContentIds.size > 0">
        AND
            t1.article_content_id IN
            <foreach collection="articleContentIds" index="index" open="(" separator="," close=")">
                #{articleContentIds[${index}]}
            </foreach>
        </if>
        <if test="creator != null and creator != ''">
        AND
            t1.creator = #{creator}
        </if>
        <if test="creators != null and creators.size > 0">
        AND
            t1.creator IN
            <foreach collection="creators" index="index" open="(" separator="," close=")">
                #{creators[${index}]}
            </foreach>
        </if>
        ORDER BY t1.publish_date DESC, t1.gmt_create DESC
    </select>

    <!-- 统计文章数量 -->
    <select id="countArticleCount" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            article_content
        WHERE
            is_delete = 0
        <if test="articleCategoryId != null and articleCategoryId != ''">
        AND
            article_category_id = #{articleCategoryId}
        </if>
        <if test="isPublish != null">
        AND
            is_publish = #{isPublish}
        </if>
        <if test="articleCategoryId != null and articleCategoryId != ''">
        AND
            article_category_id = #{articleCategoryId}
        </if>
        <if test="articleContentIds != null and articleContentIds.size > 0">
        AND
            article_content_id IN
            <foreach collection="articleContentIds" index="index" open="(" separator="," close=")">
                #{articleContentIds[${index}]}
            </foreach>
        </if>
        <if test="creator != null and creator != ''">
        AND
            creator = #{creator}
        </if>
        <if test="creators != null and creators.size > 0">
        AND
            creator IN
            <foreach collection="creators" index="index" open="(" separator="," close=")">
                #{creators[${index}]}
            </foreach>
        </if>
    </select>

</mapper>