<?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="ink.wgink.module.article.dao.content.IContentDao">

    <cache flushInterval="3600000"/>

    <resultMap id="contentDTO" type="ink.wgink.module.article.pojo.dtos.content.ContentDTO">
        <id column="content_id" property="contentId"/>
        <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="category_id" property="categoryId"/>
        <result column="category_title" property="categoryTitle"/>
        <result column="category_summary" property="categorySummary"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="cover_photos" property="coverPhotos"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `article_content` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
            `content_id` char(36) NOT NULL COMMENT '主键',
            `title` varchar(255) DEFAULT NULL COMMENT '标题',
            `sub_title` varchar(255) DEFAULT NULL COMMENT '子标题',
            `summary` varchar(255) DEFAULT NULL COMMENT '概述',
            `link` varchar(255) DEFAULT NULL COMMENT '连接',
            `source` varchar(255) DEFAULT NULL COMMENT '来源',
            `author` varchar(255) DEFAULT NULL COMMENT '作者',
            `publish_date` varchar(40) DEFAULT NULL COMMENT '发布时间',
            `is_publish` int(11) DEFAULT NULL COMMENT '是否发布',
            `cover_photos` varchar(255) DEFAULT NULL COMMENT '封面图片',
            `content` longtext COMMENT '正文',
            `category_id` char(36) DEFAULT NULL COMMENT '文章类别',
            `sort` varchar(255) DEFAULT NULL COMMENT '文章排序',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`id`,`content_id`),
            KEY `content_id` (`content_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增文章内容 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO article_content(
            content_id,
            title,
            sub_title,
            summary,
            link,
            source,
            author,
            publish_date,
            is_publish,
            cover_photos,
            content,
            sort,
            category_id,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{contentId},
            #{title},
            #{subTitle},
            #{summary},
            #{link},
            #{source},
            #{author},
            #{publishDate},
            #{isPublish},
            #{coverPhotos},
            #{content},
            #{sort},
            #{categoryId},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <!-- 修改文章内容 -->
    <update id="update" parameterType="map" flushCache="true">
        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="coverPhotos != null and coverPhotos != ''">
            cover_photos = #{coverPhotos},
        </if>
        <if test="content != null and content != ''">
            content = #{content},
        </if>
        <if test="categoryId != null and categoryId != ''">
            category_id = #{categoryId},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            content_id = #{contentId}
    </update>

    <!-- 文章内容详情 -->
    <select id="get" parameterType="map" resultMap="contentDTO" useCache="true">
        SELECT
            t1.title,
            t1.sub_title,
            t1.summary,
            t1.link,
            t1.source,
            t1.author,
            t1.publish_date,
            t1.is_publish,
            t1.cover_photos,
            t1.content,
            t1.category_id,
            t1.sort,
            t1.content_id
        FROM
            article_content t1
        WHERE
            t1.is_delete = 0
        <if test="contentId != null and contentId != ''">
        AND
            t1.content_id = #{contentId}
        </if>
    </select>

    <!-- 文章内容列表 -->
    <select id="list" parameterType="map" resultMap="contentDTO" useCache="true">
        SELECT
            t1.title,
            t1.sub_title,
            t1.summary,
            t1.source,
            t1.author,
            t1.publish_date,
            t1.is_publish,
            t1.cover_photos,
            t1.content,
            t1.category_id,
            jt1.title category_title,
            jt1.summary category_summary,
            t1.sort,
            LEFT(t1.gmt_create, 19) gmt_create,
            t1.content_id
        FROM
            article_content t1
        INNER JOIN
            article_category jt1
        ON
            t1.category_id = jt1.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="isPublish != null">
        AND
            t1.is_publish = #{isPublish}
        </if>
        <if test="categoryId != null and categoryId != ''">
        AND
            t1.category_id = #{categoryId}
        </if>
        <if test="contentIds != null and contentIds.size > 0">
        AND
            t1.content_id IN
            <foreach collection="contentIds" index="index" open="(" separator="," close=")">
                #{contentIds[${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>
        <if test="data_authority != null">
            <choose>
                <when test="data_authority == 'self'">
        AND
            t1.creator = #{data_creator}
                </when>
                <otherwise>
        AND
            t1.creator IN
                    <foreach collection="data_creators" index="index" open="(" separator="," close=")">
                #{data_creators[${index}]}
                    </foreach>
                </otherwise>
            </choose>
        </if>
        ORDER BY t1.publish_date DESC
    </select>

    <!-- 统计文章数量 -->
    <select id="count" parameterType="map" resultType="Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            article_content
        WHERE
            is_delete = 0
        <if test="categoryId != null and categoryId != ''">
        AND
            category_id = #{categoryId}
        </if>
        <if test="isPublish != null">
        AND
            is_publish = #{isPublish}
        </if>
        <if test="categoryId != null and categoryId != ''">
        AND
            category_id = #{categoryId}
        </if>
        <if test="contentIds != null and contentIds.size > 0">
        AND
            content_id IN
            <foreach collection="contentIds" index="index" open="(" separator="," close=")">
                #{contentIds[${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>