<?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.advertising.dao.impl.AdvertisingDistributionBookDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingDistributionBook">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="ad_id" property="adId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="agent_id" property="agentId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="is_book_group" property="isBookGroup" jdbcType="BIT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="distribution_time" property="distributionTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, ad_id, book_id, adviser_id, agent_id, channel_id, is_book_group, classify_id, qrcode_id, distribution_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_distribution_book
        where id = #{id}
    </select>

    <select id="getByAdId" resultMap="BaseResultMap" parameterType="Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_distribution_book
        where ad_id = #{adId}
    </select>

    <select id="countByAdId" resultType="Integer" parameterType="map">
        select
        count(1)
        from advertising_distribution_book
        where ad_id = #{adId}
    </select>
    
    <select id="getDistributionBookNum" resultType="Long">
        SELECT
            count(
                DISTINCT (
                    CONCAT(a.BOOK_ID, '-', a.CHANNEL_ID)
                )
            )
        FROM
            BOOK_ADVISER a
        INNER JOIN BOOK b ON a.BOOK_ID = b.BOOK_ID
        AND a.IS_DELETE = 0
        AND b.IS_DELETE = 0
        INNER JOIN advertising_distribution_book db ON db.book_id = a.BOOK_ID
        AND db.channel_id = a.CHANNEL_ID
        AND db.adviser_id = a.ADVISER_ID
        AND db.qrcode_id IS NULL
        LEFT JOIN advertising_agent_permission ap ON ap.agent_id = db.agent_id
        LEFT JOIN advertising_adviser_permission adp ON adp.adviser_id = a.ADVISER_ID
        AND adp.channel_id = a.CHANNEL_ID
        AND adp.book_id = a.BOOK_ID
    </select>

    <select id="getDistributionWechatGroupNum" resultType="Long">
        select
            count(distinct qrcode_id)
        from
            advertising_distribution_book
        where
            qrcode_id is not null
    </select>

    <select id="distributionWechatGroupList" parameterType="map" resultType="AdGroupQrcodeDTO">
        SELECT
            c.book_id bookId,
            c.channel_id channelId,
            c.create_user adviserId,
            q.id groupQrcodeId,
            q.group_name groupQrcodeName,
            db.agent_id agentId,
            b.ISBN isbn,
            b.BOOK_NAME bookName,
            db.distribution_time distributionTime,
            IF (
                adp.is_open = 0
                OR (
                    adp.is_open IS NULL
                    AND ap.is_open = 0
                ),
                0,
                1
            ) isOpen
        FROM
            book_group_classify c
        INNER JOIN book b ON b.BOOK_ID = c.book_id
        INNER JOIN book_group_qrcode q ON c.id = q.classify_id
        INNER JOIN advertising_distribution_book db ON db.qrcode_id = q.id
        AND db.ad_id = #{adId}
        AND db.classify_id = c.id
        AND db.book_id = c.book_id
        LEFT JOIN advertising_agent_permission ap ON ap.agent_id = db.agent_id
        LEFT JOIN advertising_adviser_permission adp ON adp.adviser_id = c.create_user
        AND adp.channel_id = c.channel_id
        AND adp.book_id = c.book_id
        WHERE
            1 = 1
        <if test="name != null">
            AND (b.BOOK_NAME LIKE CONCAT('%', #{name}, '%') OR q.group_name LIKE CONCAT('%', #{name}, '%'))
        </if>
        GROUP BY
            db.qrcode_id
        ORDER BY
            db.distribution_time DESC
    </select>
    
    <select id="checkBookGroupAdExist" parameterType="map" resultType="Integer">
        SELECT
            count(1)
        FROM
            advertising_distribution_book b,
            advertising_space s
        WHERE
            b.ad_id = s.id
        AND b.book_id = #{bookId}
        AND b.channel_id = #{channelId}
        AND b.is_book_group = 1
        AND s.is_open = 1
        AND s.ad_position = 'BOOK_GROUP_PAGE'
        AND s.ad_position_detail = #{adPositionDetail}
    </select>

    <select id="checkBookAdExist" parameterType="map" resultType="Integer">
        SELECT
            count(1)
        FROM
            advertising_distribution_book b,
            advertising_space s
        WHERE
            b.ad_id = s.id
        AND b.book_id = #{bookId}
        AND b.channel_id = #{channelId}
        AND b.is_book_group = 0
        AND s.is_open = 1
        AND s.ad_position = 'APP_OR_PRODUCT'
        AND s.ad_position_detail = #{adPositionDetail}
        AND s.ad_position_mode = #{adPositionMode}
    </select>

    <select id="checkQrcodeIdExist" parameterType="map" resultType="Long">
        select
            b.qrcode_id
        from
            advertising_distribution_book b,
            advertising_space s
        where
            b.ad_id = s.id
        and b.ad_id = #{adId}
        and s.is_open = 1
        and b.qrcode_id in
        <foreach collection="qrcodeIds" index="i" item="item" open="("
                 separator="," close=")">
            ${item}
        </foreach>
        group by
            b.qrcode_id
    </select>

    <select id="checkAdSpaceHasBook" parameterType="map" resultType="Boolean">
        select
            exists (
                select
                    id
                from
                    advertising_distribution_book
                where
                    ad_id = #{adId}
            )
    </select>

    <delete id="deleteById" parameterType="Long">
        delete from advertising_distribution_book
        where id = #{id}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingDistributionBook"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_distribution_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ad_id,
            book_id,
            adviser_id,
            agent_id,
            channel_id,
            is_book_group,
            classify_id,
            qrcode_id,
            distribution_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{adId,jdbcType=BIGINT},
            #{bookId,jdbcType=BIGINT},
            #{adviserId,jdbcType=BIGINT},
            #{agentId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{isBookGroup,jdbcType=BIT},
            #{classifyId,jdbcType=BIGINT},
            #{qrcodeId,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <insert id="batchInsert"  parameterType="com.pcloud.book.advertising.entity.AdvertisingDistributionBook"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_distribution_book (
            ad_id,
            book_id,
            adviser_id,
            agent_id,
            channel_id,
            is_book_group,
            classify_id,
            qrcode_id,
            distribution_time
        ) values
        <foreach collection="list" item="item" index="index"
                 separator=",">
            (#{item.adId,jdbcType=BIGINT},
            #{item.bookId,jdbcType=BIGINT},
            #{item.adviserId,jdbcType=BIGINT},
            #{item.agentId,jdbcType=BIGINT},
            #{item.channelId,jdbcType=BIGINT},
            #{item.isBookGroup,jdbcType=BIT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.qrcodeId,jdbcType=BIGINT},
            NOW())
        </foreach>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingDistributionBook">
        update advertising_distribution_book
        <set>
            <if test="adId != null">
                ad_id = #{adId,jdbcType=BIGINT},
            </if>
            <if test="bookId != null">
                book_id = #{bookId,jdbcType=BIGINT},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="agentId != null">
                agent_id = #{agentId,jdbcType=BIGINT},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId,jdbcType=BIGINT},
            </if>
            <if test="isBookGroup != null">
                is_book_group = #{isBookGroup,jdbcType=BIT},
            </if>
            <if test="classifyId != null">
                classify_id = #{classifyId,jdbcType=BIGINT},
            </if>
            <if test="qrcodeId != null">
                qrcode_id = #{qrcodeId,jdbcType=BIGINT},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <resultMap id="QrcodeAdvertisingSpaceMap" type="QrcodeAdvertisingSpaceCountDTO">
        <id column="qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="count" property="advertisingSpaceNum" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="mapAdvertisingSpaceNum" resultMap="QrcodeAdvertisingSpaceMap" parameterType="list">
        SELECT
        qrcode_id,
        count(DISTINCT id) count
        FROM
        advertising_distribution_book
        WHERE
        qrcode_id IN
        <foreach collection = "list" index="index" item = "item" open = "("  separator= "," close = ")">
            #{item}
        </foreach>
        GROUP BY
        qrcode_id
    </select>

    <select id="advertisingDistributionDetail4WechatGroup" parameterType="map" resultType="com.pcloud.book.advertising.dto.AdvertisingDistributionDTO">
        SELECT
        distribution_time distributionTime
        FROM
        advertising_distribution_book
        WHERE
        qrcode_id = #{qrcodeId}
        AND ad_id = #{adId}
        ORDER BY
        distribution_time DESC
    </select>

    <select id="getChannelIdByAdIdBookId" resultMap="BaseResultMap" parameterType="map">
        select
        <include refid="Base_Column_List"/>
        from advertising_distribution_book
        where ad_id = #{adId}
        and  book_id = #{bookId,jdbcType=BIGINT}
        limit 1
    </select>


</mapper>