<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.push.dao.impl.NewsDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.News">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="news_from" property="newsFrom" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="publisher" property="publisher" jdbcType="VARCHAR"/>
        <result column="url" property="url" jdbcType="VARCHAR"/>
        <result column="short_url" property="shortUrl" jdbcType="VARCHAR"/>
        <result column="news_date" property="newsDate" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    <resultMap id="BaseResultESMap" type="com.pcloud.book.es.entity.ESNews">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="news_from" property="newsFrom" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="publisher" property="publisher" jdbcType="VARCHAR"/>
        <result column="url" property="url" jdbcType="VARCHAR"/>
        <result column="short_url" property="shortUrl" jdbcType="VARCHAR"/>
        <result column="news_date" property="newsDate" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, type, news_from, title, publisher, url, short_url, news_date, create_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from news
        where id = #{id,jdbcType=BIGINT}
    </select>

    <insert id="insert" parameterType="com.pcloud.book.push.entity.News" useGeneratedKeys="true" keyProperty="id">
        insert into news
        <trim prefix="(" suffix=")" suffixOverrides=",">
            type,
            news_from,
            title,
            publisher,
            url,
            short_url,
            news_date,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{type,jdbcType=VARCHAR},
            #{newsFrom,jdbcType=VARCHAR},
            #{title,jdbcType=VARCHAR},
            #{publisher,jdbcType=VARCHAR},
            #{url,jdbcType=VARCHAR},
            #{shortUrl,jdbcType=VARCHAR},
            #{newsDate,jdbcType=VARCHAR},
            NOW()
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.push.entity.News">
        update news
        <set>
            <if test="type != null">
                type = #{type,jdbcType=VARCHAR},
            </if>
            <if test="newsFrom != null">
                news_from = #{newsFrom,jdbcType=VARCHAR},
            </if>
            <if test="title != null">
                title = #{title,jdbcType=VARCHAR},
            </if>
            <if test="publisher != null">
                publisher = #{publisher,jdbcType=VARCHAR},
            </if>
            <if test="url != null">
                url = #{url,jdbcType=VARCHAR},
            </if>
            <if test="shortUrl != null">
                short_url = #{shortUrl,jdbcType=VARCHAR},
            </if>
            <if test="newsDate != null">
                news_date = #{newsDate,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--查询所有-->
    <select id="findAll" parameterType="map" resultMap="BaseResultESMap">
        select <include refid="Base_Column_List"/>
        from news
        where id>#{maxId}
        limit #{offset}
    </select>

    <!--获取全表数量-->
    <select id="count" resultType="Integer">
        select count(1) from news
    </select>

    <!--批量新增-->
    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
        insert into news (
            type,
            news_from,
            title,
            publisher,
            url,
            short_url,
            news_date,
            create_time
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
         (
            #{item.type,jdbcType=VARCHAR},
            #{item.newsFrom,jdbcType=VARCHAR},
            #{item.title,jdbcType=VARCHAR},
            #{item.publisher,jdbcType=VARCHAR},
            #{item.url,jdbcType=VARCHAR},
            #{item.shortUrl,jdbcType=VARCHAR},
            #{item.newsDate,jdbcType=VARCHAR},
            NOW()
         )
        </foreach>
    </insert>

    <!--根据链接查询-->
    <select id="getByUrl" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from news
        where url = #{url} limit 1
    </select>
</mapper>