<?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.AdNewsWechatDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.adnews.entity.AdNewsWechat">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="news_from" property="newsFrom" jdbcType="VARCHAR"/>
        <result column="wechat_name" property="wechatName" jdbcType="VARCHAR"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="is_delete" property="isDelete" jdbcType="BOOLEAN"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, news_from, wechat_name, adviser_id, is_delete, create_time, update_time
    </sql>

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

    <select id="getListByAdviserId" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from ad_news_wechat
        where adviser_id = #{adviserId,jdbcType=BIGINT}
        AND is_delete=0
    </select>

    <!--获取公众号列表-->
    <select id="getAdNewsWechatList" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ad_news_wechat
        where
        news_from IS NOT NULL
        AND adviser_id IS NULL
        AND is_delete=0
        order by id desc
    </select>

    <!--获取编辑自有公众号-->
    <select id="getAdNewsWechatListForAdviser" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ad_news_wechat
        where
        adviser_id=#{adviserId}
        AND is_delete=0
        order by id desc
    </select>

    <!--获取编辑已选中的公众号列表-->
    <select id="getAdNewsWechatChooseList" parameterType="map" resultMap="BaseResultMap">
        SELECT
            a.id,
            a.news_from,
            a.wechat_name,
            a.adviser_id,
            a.is_delete,
            a.create_time,
            a.update_time
        FROM
            ad_news_wechat a
            INNER JOIN ad_news_wechat_choose b ON b.ad_news_wechat_id = a.id
            AND b.is_delete = 0
            AND b.adviser_id = #{partyId}
        WHERE
            a.`news_from` IS NOT NULL
            AND a.adviser_id IS NULL
            AND a.is_delete=0
    </select>

    <select id="checkRepeat" parameterType="map" resultType="boolean">
        SELECT count(1) FROM ad_news_wechat
        WHERE (wechat_name=#{wechatName} AND adviser_id IS NULL)
            OR (wechat_name=#{wechatName} AND adviser_id=#{adviserId})
    </select>

    <select id="existsAdNewsWechat" parameterType="map" resultType="boolean">
        SELECT count(1) FROM ad_news_wechat
        WHERE adviser_id=#{adviserId}
    </select>

    <insert id="insert" parameterType="com.pcloud.book.adnews.entity.AdNewsWechat" useGeneratedKeys="true"
            keyProperty="id">
        insert into ad_news_wechat
        <trim prefix="(" suffix=")" suffixOverrides=",">
            wechat_name, adviser_id, is_delete, create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{wechatName,jdbcType=VARCHAR},
            #{adviserId,jdbcType=BIGINT},
            0,
            NOW()
        </trim>
    </insert>

    <insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyProperty="id">
        insert into ad_news_wechat(
            wechat_name,
            adviser_id,
            is_delete,
            create_time
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.wechatName,jdbcType=VARCHAR},
            #{item.adviserId,jdbcType=BIGINT},
            0,
            NOW()
            )
        </foreach>
    </insert>


</mapper>