<?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.advertising.dao.impl.AdvertisingBrandDataDaoImpl">

    <resultMap type="com.pcloud.book.advertising.entity.AdvertisingBrandData" id="AdvertisingBrandDataMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="fromType" column="from_type" jdbcType="VARCHAR"/>
        <result property="bookId" column="book_id" jdbcType="INTEGER"/>
        <result property="adviserId" column="adviser_id" jdbcType="INTEGER"/>
        <result property="channelId" column="channel_id" jdbcType="INTEGER"/>
        <result property="sceneId" column="scene_id" jdbcType="INTEGER"/>
        <result property="serveId" column="serve_id" jdbcType="INTEGER"/>
        <result property="addType" column="add_type" jdbcType="VARCHAR"/>
        <result property="addCount" column="add_count" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>


    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="AdvertisingBrandDataMap" parameterType="com.pcloud.book.advertising.entity.AdvertisingBrandData">
        select
          id, from_type, book_id, adviser_id, channel_id, scene_id, serve_id, add_type, add_count, create_time, update_time
        from book.advertising_brand_data
        <where>
            <if test="fromType != null and fromType != ''">
                and from_type = #{fromType}
            </if>
            <if test="bookId != null">
                and book_id = #{bookId}
            </if>
            <if test="adviserId != null">
                and adviser_id = #{adviserId}
            </if>
            <if test="channelId != null">
                and channel_id = #{channelId}
            </if>
            <if test="sceneId != null">
                and scene_id = #{sceneId}
            </if>
            <if test="serveId != null">
                and serve_id = #{serveId}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.advertising_brand_data(from_type, book_id, adviser_id, channel_id, scene_id, serve_id, add_type, add_count, create_time, update_time)
        values (#{fromType}, #{bookId}, #{adviserId}, #{channelId}, #{sceneId}, #{serveId}, #{addType}, #{addCount}, NOW(), NOW())
    </insert>

    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
        insert into advertising_brand_data (
        from_type, book_id, adviser_id, channel_id,
        scene_id, serve_id, add_type, add_count, create_time, update_time
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.fromType}, #{item.bookId}, #{item.adviserId}, #{item.channelId},
            #{item.sceneId}, #{item.serveId}, #{item.addType}, #{item.addCount}, NOW(), NOW()
            )
        </foreach>
    </insert>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.advertising_brand_data where id = #{id}
    </delete>

    <select id="getQrServeData4Channel" parameterType="map" resultMap="AdvertisingBrandDataMap">
        select
          id, from_type, book_id, adviser_id, channel_id, scene_id, serve_id, add_type, add_count, create_time, update_time
        from advertising_brand_data
        WHERE from_type = #{fromType}
        and book_id = #{bookId}
        and scene_id = #{sceneId}
        <if test="adviserId != null">
            and adviser_id = #{adviserId}
        </if>
        <if test="channelId != null">
            and channel_id = #{channelId}
        </if>
        and serve_id in
        <foreach collection="serveIdList" item="item" open="(" close=")" separator="," index="index">
            #{item}
        </foreach>
    </select>

    <select id="getQrData4Channel" parameterType="map" resultMap="AdvertisingBrandDataMap">
        select
        id, from_type, book_id, adviser_id, channel_id, scene_id, serve_id, add_type, add_count, create_time, update_time
        from advertising_brand_data
        WHERE from_type = #{fromType}
        and book_id = #{bookId}
        and adviser_id = #{adviserId}
        and channel_id = #{channelId}
        and scene_id in
        <foreach collection="sceneIdList" item="item" open="(" close=")" separator="," index="index">
            #{item}
        </foreach>
    </select>

    <select id="getBookAddByType" parameterType="map" resultMap="AdvertisingBrandDataMap">
        select
        book_id, add_count
        from advertising_brand_data
        WHERE from_type = 'BOOK'
        and book_id in
        <foreach collection="bookIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
        and add_type=#{addType}
        group by book_id
    </select>

</mapper>