<?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.ResourcePageNavigationDaoImpl">

    <resultMap type="com.pcloud.book.group.entity.ResourcePageNavigation" id="ResourcePageNavigationMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="resourcePageId" column="resource_page_id" jdbcType="INTEGER"/>
        <result property="navigationType" column="navigation_type" jdbcType="INTEGER"/>
        <result property="navigationName" column="navigation_name" jdbcType="VARCHAR"/>
        <result property="navigationIcon" column="navigation_icon" jdbcType="VARCHAR"/>
        <result property="navigationSeq" column="navigation_seq" jdbcType="INTEGER"/>
        <result property="serveId" column="serve_id" jdbcType="INTEGER"/>
        <result property="serveType" column="serve_type" jdbcType="INTEGER"/>
        <result property="typeCode" column="type_code" jdbcType="INTEGER"/>
        <result property="linkUrl" column="link_url" jdbcType="INTEGER"/>
        <result property="isHide" column="is_hide" jdbcType="INTEGER"/>
        <result property="createUser" column="create_user" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
         id, resource_page_id, navigation_type, navigation_name, navigation_icon, navigation_seq, serve_id, serve_type, type_code, link_url, is_hide
    </sql>
    <!--查询单个-->
    <select id="getById" resultMap="ResourcePageNavigationMap">
        select
         <include refid="Base_Column_List"/>
        from book.resource_page_navigation
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_navigation(resource_page_id, navigation_type, navigation_name, navigation_icon, navigation_seq, create_user, serve_id, serve_type, type_code, link_url, is_hide, create_time, update_time)
        values (#{resourcePageId}, #{navigationType}, #{navigationName}, #{navigationIcon}, #{navigationSeq}, #{createUser}, #{serveId}, #{serveType}, #{typeCode}, #{linkUrl}, IFNULL(#{isHide},0), now(), now())
    </insert>

    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_navigation(resource_page_id, navigation_type, navigation_name, navigation_icon, navigation_seq, create_user, serve_id, serve_type, type_code, link_url, is_hide, create_time, update_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.resourcePageId}, #{item.navigationType}, #{item.navigationName}, #{item.navigationIcon}, #{item.navigationSeq}, #{item.createUser}, #{item.serveId}, #{item.serveType}, #{item.typeCode}, #{item.linkUrl}, IFNULL(#{item.isHide}, 0), now(), now())
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.resource_page_navigation
        <set>
            <if test="navigationName != null and navigationName != ''">
                navigation_name = #{navigationName},
            </if>
                navigation_icon = #{navigationIcon},
            <if test="navigationSeq != null">
                navigation_seq = #{navigationSeq},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId},
            </if>
            <if test="serveType != null">
                serve_type = #{serveType},
            </if>
            <if test="typeCode != null">
                type_code = #{typeCode},
            </if>
            <if test="isHide != null">
                is_hide = #{isHide},
            </if>
            <if test="linkUrl != null">
                link_url = #{linkUrl},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

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

    <select id="getByPageId" parameterType="long" resultMap="ResourcePageNavigationMap">
        select
        <include refid="Base_Column_List"/>
        from book.resource_page_navigation
        where resource_page_id = #{resourcePageId}
        order by navigation_seq asc, id asc
    </select>

    <select id="getNoNavigationPages" resultType="long" parameterType="map">
        SELECT
            r.id
        FROM
            resource_page r
        LEFT JOIN resource_page_navigation n ON r.id = n.resource_page_id
        <if test="openRays != null and openRays == 1">
            AND n.navigation_type = 2
        </if>
        WHERE
            n.id IS NULL
          <if test="openRays != null and openRays == 1">
              AND r.open_rays = #{openRays}
          </if>
    </select>

    <select id="getByType" parameterType="map" resultMap="ResourcePageNavigationMap">
        select
        <include refid="Base_Column_List"/>
        from book.resource_page_navigation
        where resource_page_id = #{resourcePageId}
        and navigation_type = #{navigationType}
        limit 1
    </select>

    <select id="getMaxSeq" parameterType="long" resultType="integer">
        SELECT
        IFNULL(MAX(navigation_seq), 0)
        FROM
        book.resource_page_navigation
        where resource_page_id = #{resourcePageId}
    </select>

    <delete id="deleteNavigation" parameterType="map">
        DELETE
        from book.resource_page_navigation
        where resource_page_id = #{resourcePageId}
        and navigation_type = #{navigationType}
    </delete>

    <select id="getByName" parameterType="map" resultMap="ResourcePageNavigationMap">
        select
        <include refid="Base_Column_List"/>
        from book.resource_page_navigation
        where resource_page_id = #{resourcePageId}
        and navigation_name = #{navigationName}
        limit 1
    </select>

    <update id="updateSeq4Batch" parameterType="list">
        update book.resource_page_navigation set navigation_seq =
        <foreach collection="list" open="CASE" separator=" " close="END" item="item">
            WHEN id = #{item.id} THEN #{item.navigationSeq}
        </foreach>
        where id IN
        <foreach collection="list" item="item" close=")" separator="," open="(">
            ${item.id}
        </foreach>
    </update>

    <select id="getNavigationByMoveType" parameterType="map" resultMap="ResourcePageNavigationMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        book.resource_page_navigation
        WHERE
        resource_page_id = #{resourcePageId}
        <choose>
            <when test="moveType == 'UP'">
                AND navigation_seq &lt; #{navigationSeq}
                ORDER BY
                navigation_seq DESC
            </when>
            <otherwise>
                AND navigation_seq > #{navigationSeq}
                ORDER BY
                navigation_seq ASC
            </otherwise>
        </choose>
        LIMIT 1
    </select>

    <update id="updateNavigationSeqNum" parameterType="map">
        update resource_page_navigation
        set navigation_seq = #{navigationSeq},
        update_time = now()
        where id = #{id}
    </update>

    <select id="countByPageId" parameterType="map" resultType="Integer">
        select
        ifnull(count(1),0)
        from book.resource_page_navigation
        where resource_page_id = #{resourcePageId}
    </select>
</mapper>
