<?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.heketai.website.mapper.NewsArticleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.heketai.website.entity.NewsArticle">
        <id column="id" property="id"/>
        <result column="language" property="language"/>
        <result column="title" property="title"/>
        <result column="cid" property="cid"/>
        <result column="tag_id" property="tagId"/>
        <result column="img" property="img"/>
        <result column="content" property="content"/>
        <result column="description" property="description"/>
        <result column="user_id" property="userId"/>
        <result column="hit" property="hit"/>
        <result column="is_hot" property="isHot"/>
        <result column="is_top" property="isTop"/>
        <result column="is_audit" property="isAudit"/>
        <result column="publish_time" property="publishTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="BaseColumnList">
        id
        ,
        language,
        title,
        tag_id,
        img,
        content,
        description,
        user_id,
        hit,
        is_hot,
        is_top,
        is_audit,
        publish_time,
        update_time
    </sql>
    <select id="preArticle" resultType="com.heketai.website.entity.NewsArticle">
        SELECT
        <include refid="BaseColumnList"/>
        FROM news_article
        WHERE id <![CDATA[ < ]]> #{id}
        AND language = #{language}
        AND type = #{type}
        AND cid = #{cid}
        ORDER BY id DESC
        LIMIT 1
    </select>

    <select id="nextArticle" resultType="com.heketai.website.entity.NewsArticle">
        SELECT
        <include refid="BaseColumnList"/>
        FROM news_article
        WHERE id <![CDATA[ > ]]> #{id}
        AND language = #{language}
        AND type = #{type}
        AND cid = #{cid}
        LIMIT 1
    </select>

    <insert id="insertId" useGeneratedKeys="true" keyColumn="id" keyProperty="id"
            parameterType="com.heketai.website.entity.NewsArticle">

        insert into news_article
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="language != null and language !=''">
                `language`,
            </if>
            <if test="id != null and language !=''" >
               , id
            </if>
            <if test="title != null and title !=''">
                title,
            </if>
            <if test="cid != null and cid != ''">
                cid,
            </if>
            <if test="tagId != null and tagId != ''">
                tag_id,
            </if>
            <if test="img != null and img != ''">
                img,
            </if>
            <if test="content != null and content != ''">
                content,
            </if>
            <if test="description != null and description != ''">
                description,
            </if>
            <if test="userId != null and userId != ''">
                user_id,
            </if>
            <if test="hit != null and hit != ''">
                hit,
            </if>
            <if test="isHot != null and isHot != ''">
                is_hot,
            </if>
            <if test="isTop != null and isTop != ''">
                is_top,
            </if>
            <if test="isAudit != null and isAudit != ''">
                is_audit,
            </if>
            <if test="publishTime != null">
                publish_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="type != null and type != ''">
                `type`,
            </if>
        </trim>

        <trim prefix="values (" suffix=")" suffixOverrides=",">

            <if test="language != null and language !=''">
                #{language}
            </if>
            <if test="id != null and language !=''" >
                , #{id}
            </if>
            <if test="title != null and title !=''">
                , #{title}
            </if>
            <if test="cid != null and cid !=''">
                , #{cid}
            </if>
            <if test="tagId != null and tagId !=''">
                , #{tagId}
            </if>
            <if test="img != null and img !=''">
                , #{img}
            </if>
            <if test="content != null and content !=''">
                , #{content}
            </if>
            <if test="description != null and description !=''">
                , #{description}
            </if>
            <if test="userId != null and userId !=''">
                , #{userId}
            </if>
            <if test="hit != null and hit !=''">
                , #{hit}
            </if>
            <if test="isHot != null and isHot !=''">
                , #{isHot}
            </if>
            <if test="isTop != null and isTop !=''">
                , #{isTop}
            </if>
            <if test="isAudit != null and isAudit !=''">
                , #{isAudit}
            </if>
            <if test="publishTime != null">
                ,#{publishTime}
            </if>
            <if test="updateTime != null ">
                , #{updateTime}
            </if>
            <if test="type != null and type !=''">
                , #{type}
            </if>
        </trim>
    </insert>

</mapper>
