<?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.adnews.dao.impl.AdNewsDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.adnews.entity.AdNews">
        <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 ad_news
        where id = #{id,jdbcType=BIGINT}
    </select>

    <insert id="insert" parameterType="com.pcloud.book.adnews.entity.AdNews" useGeneratedKeys="true" keyProperty="id">
        insert into ad_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.adnews.entity.AdNews">
        update ad_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>

    <!--批量新增-->
    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
        insert into ad_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="getNewsToSendBySetIdAndAdviser" parameterType="map" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        ad_news t
        WHERE
        t.id IN (
            SELECT
            t1.ad_news_id
            FROM
            ad_news_choose t1
            WHERE
            t1.is_delete=0
            and t1.adviser_id =#{adviserId}
        )
        AND t.id NOT IN (
            SELECT
            t2.ad_news_id
            FROM
            ad_news_group_record t2
            WHERE
            t2.ad_news_set_id =#{adNewsSetId}
        )
        ORDER BY
        t.create_time DESC
        LIMIT #{top}
    </select>

    <!--获取素材库列表-->
    <select id="getAdNewsList" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from ad_news
        where id not in (
        select distinct ad_news_id from ad_news_choose
        where adviser_id = #{partyId}
        )
        <if test="title!=null">
            and title like concat('%', #{title},'%')
        </if>
        order by create_time desc
    </select>

    <!--获取编辑选择的素材-->
    <select id="getAdNewsChooseList" parameterType="map" resultType="com.pcloud.book.adnews.entity.AdNews">
        SELECT
        t1.id id,
        t1.type type,
        t1.news_from newsFrom,
        t1.title title,
        t1.publisher publisher,
        t1.url url,
        t1.short_url shortUrl,
        t1.news_date newsDate,
        t1.create_time createTime,
        CASE COUNT(t3.id) WHEN 0 THEN 0 ELSE 1 END hasUsed
        FROM
        ad_news t1
        INNER JOIN ad_news_choose t2 ON t1.id = t2.ad_news_id
        LEFT JOIN ad_news_group_record t3 ON t1.id=t3.ad_news_id
        WHERE
        t2.is_delete = 0
        AND t2.adviser_id = #{partyId}
        <if test="title!=null">
            AND t1.title like concat('%', #{title},'%')
        </if>
        GROUP BY
        t1.id
        <if test="hasUsed!=null">
            having hasUsed=#{hasUsed}
        </if>
    </select>
</mapper>