<?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.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"/>
        <result column="content_from" property="contentFrom" jdbcType="INTEGER"/>
    </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>

    <!--查找要发送的新闻（推荐公众号）-->
    <!--
        1、ad_news_ban 排除掉禁止发送的
        2、ad_news_group_record 排除掉已发送的
    -->
    <select id="getNewsToSendBySetIdAndAdviser" parameterType="map" resultMap="BaseResultMap">
        SELECT
            a.id, a.type, a.news_from, a.title, a.publisher, a.url, a.short_url, a.news_date, a.create_time,0 content_from
        FROM
            ad_news a
            INNER JOIN ad_news_wechat b ON a.news_from = b.news_from AND b.is_delete = 0
            INNER JOIN ad_news_wechat_choose c ON c.ad_news_wechat_id = b.id AND c.is_delete = 0 AND c.adviser_id = #{adviserId}
        WHERE
            NOT EXISTS ( SELECT 1 FROM ad_news_ban e WHERE a.id = e.ad_news_id AND e.adviser_id = c.adviser_id )
            AND NOT EXISTS (SELECT 1 from ad_news_group_record f WHERE f.ad_news_id=a.id and f.ad_news_set_id=#{adNewsSetId})
        ORDER BY
            a.create_time DESC
            LIMIT #{top}
    </select>

    <!--查找要发送的新闻（自有公众号）-->
    <!--
        1、ad_news_ban 排除掉禁止发送的
        2、ad_news_group_record 排除掉已发送的
    -->
    <select id="getNewsToSendBySetIdAndAdviser4Own" parameterType="map" resultMap="BaseResultMap">
        SELECT
        a.id, a.type, a.news_from, a.title, a.publisher, a.url, a.short_url, a.news_date, a.create_time,2 content_from
        FROM
        ad_news a
        INNER JOIN ad_news_wechat b ON a.news_from = b.news_from AND b.is_delete = 0 AND b.adviser_id=#{adviserId}
        WHERE
        NOT EXISTS ( SELECT 1 FROM ad_news_ban e WHERE a.id = e.ad_news_id AND e.adviser_id = #{adviserId} )
        AND NOT EXISTS (SELECT 1 from ad_news_group_record f WHERE f.ad_news_id=a.id and f.ad_news_set_id=#{adNewsSetId})
        ORDER BY
        a.create_time DESC
        LIMIT #{top}
    </select>

    <!--查找要发送的新闻（新闻标题匹配标签）-->
    <!--
        1、ad_news_ban 排除掉禁止发送的
        2、ad_news_group_record 排除掉已发送的
    -->
    <select id="getNewsByLabelName" parameterType="map" resultMap="BaseResultMap">
        SELECT
            a.id, a.type, a.news_from, a.title, a.publisher, a.url, a.short_url, a.news_date, a.create_time,0 content_from
        FROM
            ad_news a
            INNER JOIN ad_news_wechat b ON a.news_from = b.news_from AND b.is_delete = 0
            INNER JOIN ad_news_wechat_choose c ON c.ad_news_wechat_id = b.id AND c.is_delete = 0 AND c.adviser_id = #{adviserId}
        WHERE
            NOT EXISTS ( SELECT 1 FROM ad_news_ban e WHERE a.id = e.ad_news_id AND e.adviser_id = c.adviser_id )
            AND NOT EXISTS (SELECT 1 from ad_news_group_record f WHERE f.ad_news_id=a.id and f.ad_news_set_id=#{adNewsSetId})
            AND a.create_time >= #{beginTime}
            AND (a.title LIKE CONCAT('%', #{proLabelName},'%')
              OR a.title LIKE CONCAT('%', #{depLabelName},'%')
              OR a.title LIKE CONCAT('%', #{purLabelName},'%'))
        ORDER BY
            a.create_time DESC
            LIMIT #{top}
    </select>

    <!--查找要发送的新闻（公众号名称匹配标签）-->
    <!--
        1、ad_news_ban 排除掉禁止发送的
        2、ad_news_group_record 排除掉已发送的
    -->
    <select id="getNews4WechatByLabelName" parameterType="map" resultMap="BaseResultMap">
        SELECT
            a.id, a.type, a.news_from, a.title, a.publisher, a.url, a.short_url, a.news_date, a.create_time,0 content_from
        FROM
            ad_news a
            INNER JOIN ad_news_wechat b ON a.news_from = b.news_from AND b.is_delete = 0
            INNER JOIN ad_news_wechat_choose c ON c.ad_news_wechat_id = b.id AND c.is_delete = 0 AND c.adviser_id = #{adviserId}
        WHERE
            NOT EXISTS ( SELECT 1 FROM ad_news_ban e WHERE a.id = e.ad_news_id AND e.adviser_id = c.adviser_id )
            AND NOT EXISTS (SELECT 1 from ad_news_group_record f WHERE f.ad_news_id=a.id and f.ad_news_set_id=#{adNewsSetId})
            AND a.create_time >= #{beginTime}
            AND (b.wechat_name LIKE CONCAT('%', #{proLabelName},'%')
              OR b.wechat_name LIKE CONCAT('%', #{depLabelName},'%')
              OR b.wechat_name LIKE CONCAT('%', #{purLabelName},'%'))
        ORDER BY
            a.create_time DESC
            LIMIT #{top}
    </select>

    <!--获取素材库列表-->
    <select id="getAdNewsList" parameterType="map" resultType="com.pcloud.book.adnews.entity.AdNews">
        SELECT
            a.id id
            ,a.type type
            ,a.news_from newsFrom
            ,b.wechat_name wechatName
            ,a.title title
            ,a.publisher publisher
            ,a.url url
            ,a.short_url shortUrl
            ,a.news_date newsDate
            ,a.create_time createTime
            ,IFNULL((SELECT 1 FROM ad_news_group_record WHERE ad_news_id=a.id and create_user=#{partyId} LIMIT 1),0) hasUsed
            ,IFNULL((SELECT 1 FROM ad_news_ban WHERE ad_news_id=a.id and adviser_id=#{partyId} LIMIT 1),0) hasBan
        FROM
            ad_news a
            INNER JOIN ad_news_wechat b on a.news_from=b.news_from and b.is_delete=0
            <choose>
                <when test="contentFrom != null and contentFrom == 2">
                  AND b.adviser_id=#{partyId}
                </when>
                <otherwise>
                    <if test="adNewsWechatIds != null and adNewsWechatIds.size()>0">
                        AND b.id IN
                        <foreach collection="adNewsWechatIds" item="item" index="i" separator=","  open="(" close=")">
                            ${item}
                        </foreach>
                    </if>
                    INNER JOIN ad_news_wechat_choose c ON b.id=c.ad_news_wechat_id and c.create_user=#{partyId} and c.is_delete=0
                </otherwise>
            </choose>
        where 1=1
        <if test="title!=null">
            AND a.title like concat('%', #{title},'%')
        </if>
        <if test="beginTime!=null">
            AND a.create_time <![CDATA[>=]]> #{beginTime}
        </if>
        <if test="endTime!=null">
            AND a.create_time<![CDATA[<=]]> #{endTime}
        </if>
        <if test="hasBan==0">
            AND NOT EXISTS (SELECT 1 FROM ad_news_ban WHERE ad_news_id=a.id and adviser_id=#{partyId} LIMIT 1)
        </if>
        <if test="hasBan==1">
            AND EXISTS (SELECT 1 FROM ad_news_ban WHERE ad_news_id=a.id and adviser_id=#{partyId} LIMIT 1)
        </if>
        <if test="hasUsed==0">
            AND NOT EXISTS (SELECT 1 FROM ad_news_group_record WHERE ad_news_id=a.id and create_user=#{partyId} LIMIT 1)
        </if>
        <if test="hasUsed==1">
            AND EXISTS (SELECT 1 FROM ad_news_group_record WHERE ad_news_id=a.id and create_user=#{partyId} LIMIT 1)
        </if>
        ORDER BY a.create_time DESC,a.id 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>