<?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.group.dao.impl.ResourcePageDaoImpl">

    <resultMap type="com.pcloud.book.group.entity.ResourcePage" id="ResourcePageMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="bookGroupId" column="book_group_id" jdbcType="INTEGER"/>
        <result property="style" column="style" jdbcType="INTEGER"/>
        <result property="showBook" column="show_book" jdbcType="INTEGER"/>
        <result property="openRays" column="open_rays" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createUser" column="create_user" jdbcType="INTEGER"/>
        <result property="qrcodeUrl" column="qrcode_url" jdbcType="VARCHAR"/>
        <result property="sceneId" column="scene_id" jdbcType="INTEGER"/>
        <result property="officialSceneId" column="official_scene_id" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="ResourcePageMap">
        select
          id, book_group_id, style, show_book, open_rays, create_time, update_time, create_user, qrcode_url, scene_id, official_scene_id
        from book.resource_page
        where id = #{id}
    </select>


    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page(book_group_id, style, show_book, open_rays, create_time, update_time, create_user,
         qrcode_url, scene_id)
        values (#{bookGroupId}, #{style}, #{showBook}, #{openRays}, now(), now(), #{createUser}, #{qrcodeUrl}, #{sceneId})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.resource_page
        <set>
            <if test="style != null">
                style = #{style},
            </if>
            <if test="showBook != null">
                show_book = #{showBook},
            </if>
            <if test="openRays != null">
                open_rays = #{openRays},
            </if>
            <if test="officialSceneId != null">
                official_scene_id = #{officialSceneId},
            </if>
            <if test="qrcodeUrl != null">
                qrcode_url = #{qrcodeUrl},
            </if>
                update_time = NOW(),
        </set>
        where id = #{id}
    </update>

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

    <select id="getByBookGroupId" parameterType="long" resultMap="ResourcePageMap">
         select
          id, book_group_id, style, show_book, open_rays, qrcode_url, official_scene_id
        from book.resource_page
        where book_group_id = #{bookGroupId}
    </select>

    <select id="getByBookId" parameterType="map" resultMap="ResourcePageMap">
        SELECT
            p.id,
            p.book_group_id,
            p.style,
            p.show_book,
            p.open_rays
        FROM
            resource_page p
        INNER JOIN book_group g ON p.book_group_id = g.id and g.is_delete = 0
        AND g.join_group_type = 4
        WHERE
            g.book_id = #{bookId}
            <if test="channelId != null">
                AND g.channel_id = #{channelId}
            </if>
            <if test="adviserId != null">
                AND g.create_user = #{adviserId}
            </if>
        ORDER BY
            p.open_rays DESC, p.book_group_id desc
        LIMIT 1
    </select>

    <update id="setRaysCloseByBook" parameterType="map">
        UPDATE resource_page
        SET open_rays = 0
        WHERE
        book_group_id IN (
        SELECT
        id
        FROM
        book_group
        WHERE
        book_id = #{bookId}
        AND create_user = #{adviserId}
        AND channel_id = #{channelId}
        AND join_group_type = 4
        AND id != #{bookGroupId}
        )
    </update>

    <update id="updatePageUpdateTime" parameterType="long">
         update book.resource_page
          SET update_time = NOW()
          where id = #{id}
    </update>

    <select id="getBySceneId" parameterType="long" resultMap="ResourcePageMap">
        select
        id, book_group_id, style, show_book, open_rays, qrcode_url, scene_id, official_scene_id
        from book.resource_page
        where scene_id = #{sceneId}
    </select>

</mapper>