<?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.dora.mapper.WechatNewsArticleMapper">

    <resultMap id="BaseResultMap" type="com.dora.entity.WechatNewsArticle">
        <id column="id" property="id" />
        <result column="template_id" property="templateId" />
        <result column="title" property="title" />
        <result column="author" property="author" />
        <result column="digest" property="digest" />
        <result column="content" property="content" />
        <result column="content_source_url" property="contentSourceUrl" />
        <result column="thumb_media_id" property="thumbMediaId" />
        <result column="show_cover_pic" property="showCoverPic" />
        <result column="url" property="url" />
        <result column="thumb_url" property="thumbUrl" />
        <result column="need_open_comment" property="needOpenComment" />
        <result column="only_fans_can_comment" property="onlyFansCanComment" />
        <result column="sort_order" property="sortOrder" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <sql id="Base_Column_List">
        id, template_id, title, author, digest, content, content_source_url, thumb_media_id,
        show_cover_pic, url, thumb_url, need_open_comment, only_fans_can_comment, sort_order,
        create_time, update_time
    </sql>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_news_article
        WHERE id = #{id}
    </select>

    <select id="selectByTemplateId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_news_article
        WHERE template_id = #{templateId}
        ORDER BY sort_order ASC
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO wechat_news_article (
            template_id, title, author, digest, content, content_source_url, thumb_media_id,
            show_cover_pic, url, thumb_url, need_open_comment, only_fans_can_comment, sort_order
        ) VALUES (
            #{templateId}, #{title}, #{author}, #{digest}, #{content}, #{contentSourceUrl}, #{thumbMediaId},
            #{showCoverPic}, #{url}, #{thumbUrl}, #{needOpenComment}, #{onlyFansCanComment}, #{sortOrder}
        )
    </insert>

    <update id="updateById">
        UPDATE wechat_news_article
        SET title = #{title},
            author = #{author},
            digest = #{digest},
            content = #{content},
            content_source_url = #{contentSourceUrl},
            thumb_media_id = #{thumbMediaId},
            show_cover_pic = #{showCoverPic},
            url = #{url},
            thumb_url = #{thumbUrl},
            need_open_comment = #{needOpenComment},
            only_fans_can_comment = #{onlyFansCanComment},
            sort_order = #{sortOrder},
            update_time = CURRENT_TIMESTAMP
        WHERE id = #{id}
    </update>

    <delete id="deleteById">
        DELETE FROM wechat_news_article WHERE id = #{id}
    </delete>

    <delete id="deleteByTemplateId">
        DELETE FROM wechat_news_article WHERE template_id = #{templateId}
    </delete>

    <insert id="insertBatch">
        INSERT INTO wechat_news_article (
            template_id, title, author, digest, content, content_source_url, thumb_media_id,
            show_cover_pic, url, thumb_url, need_open_comment, only_fans_can_comment, sort_order
        ) VALUES
        <foreach collection="articles" item="article" separator=",">
            (#{article.templateId}, #{article.title}, #{article.author}, #{article.digest}, #{article.content},
             #{article.contentSourceUrl}, #{article.thumbMediaId}, #{article.showCoverPic}, #{article.url},
             #{article.thumbUrl}, #{article.needOpenComment}, #{article.onlyFansCanComment}, #{article.sortOrder})
        </foreach>
    </insert>

    <select id="getMaxSortOrder" resultType="java.lang.Integer">
        SELECT MAX(sort_order) FROM wechat_news_article WHERE template_id = #{templateId}
    </select>

</mapper>
