<?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.pcloud.book.applet.dao.impl.AppletNewsServeDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletNewsServe">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="applet_news_id" property="appletNewsId" jdbcType="BIGINT"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="pic_url" property="picUrl" jdbcType="VARCHAR"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="INTEGER"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="BaseDTOResultMap" type="com.pcloud.book.applet.dto.AppletNewsServeDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="applet_news_id" property="appletNewsId" jdbcType="BIGINT"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="pic_url" property="picUrl" jdbcType="VARCHAR"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="INTEGER"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, applet_news_id, title, pic_url, link_url, serve_id, serve_type, update_time, create_time        
    </sql>

    <sql id="BaseDto_Column_List">
        id, applet_news_id, title, pic_url, link_url, serve_id, serve_type, update_time, create_time
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_news_serve
        WHERE id = #{id}
    </select>
    
    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_news_serve
    </select>

    <select id="getByAppletNewsId" resultMap="BaseDTOResultMap">
        SELECT
        <include refid="BaseDto_Column_List"/>
        FROM applet_news_serve
        WHERE applet_news_id = #{appletNewsId}
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_news_serve(
            applet_news_id, 
            title, 
            pic_url, 
            link_url, 
            serve_id, 
            serve_type, 
            update_time, 
            create_time
        ) VALUES (
            #{appletNewsId, jdbcType=BIGINT}, 
            #{title, jdbcType=VARCHAR}, 
            #{picUrl, jdbcType=VARCHAR}, 
            #{linkUrl, jdbcType=VARCHAR}, 
            #{serveId, jdbcType=INTEGER}, 
            #{serveType, jdbcType=INTEGER},
            NOW(),
            NOW()
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_news_serve (
            applet_news_id, 
            title, 
            pic_url, 
            link_url, 
            serve_id, 
            serve_type, 
            update_time, 
            create_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.appletNewsId, jdbcType=BIGINT}, 
            #{item.title, jdbcType=VARCHAR}, 
            #{item.picUrl, jdbcType=VARCHAR}, 
            #{item.linkUrl, jdbcType=VARCHAR}, 
            #{item.serveId, jdbcType=BIGINT},
            #{item.serveType, jdbcType=INTEGER},
            NOW(),
            NOW()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_news_serve
        <set>
            <if test="appletNewsId != null">
                applet_news_id = #{appletNewsId},
            </if>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="picUrl != null and picUrl != ''">
                pic_url = #{picUrl},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId},
            </if>
            <if test="serveType != null">
                serve_type = #{serveType},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM applet_news_serve where id = #{id}
    </delete>

    <!--通过主键删除-->
    <delete id="deleteByAppletNewsId">
        DELETE FROM applet_news_serve where applet_news_id = #{appletNewsId}
    </delete>

</mapper>
