<?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.AdvertisingAdviserPermissionDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingAdviserPermission">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="adviser_id" property="adviserId" jdbcType="INTEGER"/>
        <result column="book_id" property="bookId" jdbcType="INTEGER"/>
        <result column="channel_id" property="channelId" jdbcType="INTEGER"/>
        <result column="is_open" property="isOpen" jdbcType="BIT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, adviser_id, book_id, channel_id, is_open, create_time, update_time
    </sql>

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

    <select id="getByAdviserId" resultMap="BaseResultMap" parameterType="map">
        select
        <include refid="Base_Column_List"/>
        from advertising_adviser_permission
        where adviser_id = #{adviserId}
        and book_id = #{bookId}
        and channel_id = #{channelId}
    </select>

    <select id="checkBookOpen" parameterType="map" resultType="Boolean">
        SELECT
        IF (
            adp.is_open = 0
            OR (
                adp.is_open IS NULL
                AND ap.is_open = 0
            ),
            0,
            1
        ) isOpen
        FROM
            book_adviser a
        LEFT JOIN advertising_agent_permission ap ON ap.agent_id = #{agentId}
        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
        WHERE
            a.BOOK_ID = #{bookId}
        AND a.ADVISER_ID = #{adviserId}
        AND a.CHANNEL_ID = #{channelId}
        AND a.IS_MAIN_EDITOR = 1
    </select>

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

    <delete id="deleteByAdviserId" parameterType="Long">
        delete from advertising_adviser_permission
        where adviser_id = #{adviserId}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingAdviserPermission"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_adviser_permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            adviser_id,
            book_id,
            channel_id,
            is_open,
            create_time,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{adviserId,jdbcType=INTEGER},
            #{bookId,jdbcType=INTEGER},
            #{channelId,jdbcType=INTEGER},
            #{isOpen,jdbcType=BIT},
            NOW(),
            NOW(),
        </trim>
    </insert>

    <insert id="batchInsert"  parameterType="com.pcloud.book.advertising.entity.AdvertisingAdviserPermission"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_adviser_permission (
            adviser_id,
            book_id,
            channel_id,
            is_open,
            create_time,
            update_time
        ) values
        <foreach collection="list" item="item" index="index"
                 separator=",">
            (#{item.adviserId,jdbcType=INTEGER},
            #{item.bookId,jdbcType=INTEGER},
            #{item.channelId,jdbcType=INTEGER},
            #{item.isOpen,jdbcType=BIT},
            NOW(),
            NOW())
        </foreach>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingAdviserPermission">
        update advertising_adviser_permission
        <set>
            <if test="adviserId != null">
                adviser_id = #{adviserId,jdbcType=INTEGER},
            </if>
            <if test="bookId != null">
                book_id = #{bookId,jdbcType=INTEGER},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId,jdbcType=INTEGER},
            </if>
            <if test="isOpen != null">
                is_open = #{isOpen,jdbcType=BIT},
            </if>
                update_time = NOW(),
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
</mapper>