<?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.group.dao.impl.BookAppletSceneDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.BookAppletScene">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="rays_classify_id" property="raysClassifyId" jdbcType="BIGINT"/>
        <result column="applet_id" property="appletId" jdbcType="VARCHAR"/>
        <result column="applet_url" property="appletUrl" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="is_delete" property="isDelete" jdbcType="BIT"/>
        <result column="scene_id" property="sceneId" jdbcType="BIGINT"/>
        <result column="account_setting_id" property="accountSettingId" jdbcType="BIGINT"/>
    </resultMap>

    <resultMap id="BaseResultMap4DTO" type="com.pcloud.book.group.dto.BookAppletSceneDTO">
        <result column="applet_id" property="appletId" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, book_group_id, rays_classify_id, applet_id, applet_url, create_time, update_time, is_delete,scene_id,account_setting_id
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.group.entity.BookAppletScene" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_applet_scene
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_group_id,
            rays_classify_id,
            applet_id,
            applet_url,
            create_time,
            update_time,
            is_delete,
            account_setting_id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookGroupId,jdbcType=BIGINT},
            #{raysClassifyId,jdbcType=BIGINT},
            #{appletId,jdbcType=VARCHAR},
            #{appletUrl,jdbcType=VARCHAR},
            NOW(),
            NOW(),
            0,
            #{accountSettingId}
        </trim>
    </insert>

    <insert id="batchInsert" parameterType="com.pcloud.book.group.entity.BookAppletScene" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_applet_scene(
            rays_classify_id,
            applet_id,
            applet_url,
            create_time,
            update_time,
            is_delete,
            scene_id,
            account_setting_id
        ) values
        <foreach collection="list" item="item" index="index"
                 separator=",">
           ( #{item.raysClassifyId,jdbcType=BIGINT},
            #{item.appletId,jdbcType=VARCHAR},
            #{item.appletUrl,jdbcType=VARCHAR},
            NOW(),
            NOW(),
            0,
            #{item.sceneId,jdbcType=BIGINT},
            #{item.accountSettingId}
            )
        </foreach>
    </insert>


    <select id="getByAppletId" parameterType="string" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM book_applet_scene
        WHERE applet_id=#{appletId}
        limit 1
    </select>

    <select id="getByBookGroupId" parameterType="long" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM book_applet_scene
        WHERE book_group_id=#{bookGroupId}
        AND is_delete = 0
        limit 1
    </select>

    <select id="getBySceneId" parameterType="long" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM book_applet_scene
        WHERE scene_id=#{sceneId}
        AND is_delete = 0
        limit 1
    </select>

    <select id="updateRaysClassifyIdByBookGroupId" parameterType="map">
        UPDATE book_applet_scene
        SET rays_classify_id = #{raysClassifyId},
        update_time = NOW()
        WHERE book_group_id=#{bookGroupId}
    </select>

    <update id="deleteByBookGroupId" parameterType="long">
        UPDATE book_applet_scene
        SET is_delete = 1,
        update_time = NOW()
        WHERE book_group_id=#{bookGroupId}
    </update>

    <select id="getBySceneIds" parameterType="list" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM book_applet_scene
        WHERE scene_id
        in
        <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        AND is_delete = 0
        and applet_id is not null
        limit 1
    </select>

   <select id="getBookAppletByBookId" parameterType="map" resultMap="BaseResultMap4DTO">
       select
       a.applet_id
       FROM
       book_applet_scene a
       LEFT JOIN book_group b ON a.book_group_id = b.id
       where
       book_id = #{bookId}
       and
       channel_id = #{channelId}
       and
       create_user = #{adviserId}
       limit 1
   </select>

</mapper>