<?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.AdvertisingAgentPermissionDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingAgentPermission">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="agent_id" property="agentId" 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, agent_id, is_open, create_time, update_time
    </sql>

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

    <select id="getByAgentId" resultMap="BaseResultMap" parameterType="Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_agent_permission
        where agent_id = #{agentId}
    </select>

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

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingAgentPermission"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_agent_permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            agent_id,
            is_open,
            create_time,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{agentId,jdbcType=INTEGER},
            #{isOpen,jdbcType=BIT},
            NOW(),
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingAgentPermission">
        update advertising_agent_permission
        <set>
            <if test="agentId != null">
                agent_id = #{agentId,jdbcType=INTEGER},
            </if>
            <if test="isOpen != null">
                is_open = #{isOpen,jdbcType=BIT},
            </if>
                update_time = NOW(),
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <select id="getCloseAgentId" resultType="Long">
        SELECT DISTINCT
        agent_id
        FROM
        advertising_agent_permission
        WHERE
        is_open = 0
    </select>
</mapper>