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

    <resultMap type="com.pcloud.book.group.entity.ResourcePageColumn" id="ResourcePageColumnMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="resourcePageId" column="resource_page_id" jdbcType="INTEGER"/>
        <result property="columnType" column="column_type" jdbcType="INTEGER"/>
        <result property="columnName" column="column_name" jdbcType="VARCHAR"/>
        <result property="columnFormat" column="column_format" jdbcType="INTEGER"/>
        <result property="columnSeq" column="column_seq" jdbcType="INTEGER"/>
        <result property="showMore" column="show_more" jdbcType="BOOLEAN"/>
        <result property="showCount" column="show_count" 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="labelIcon" column="label_icon" jdbcType="VARCHAR"/>
        <result property="navigationId" column="navigation_id" jdbcType="INTEGER"/>
        <result property="backgroundType" column="background_type" jdbcType="INTEGER"/>
        <result property="backgroundUrl" column="background_url" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="ResourceColumnAndServe" type="com.pcloud.book.group.vo.ResourceColumnAndServeVO">
        <result property="resourcePageColumnId" column="id" jdbcType="INTEGER"/>
        <result property="resourcePageId" column="resource_page_id" jdbcType="INTEGER"/>
        <result property="columnType" column="column_type" jdbcType="INTEGER"/>
        <result property="columnName" column="column_name" jdbcType="VARCHAR"/>
        <result property="columnFormat" column="column_format" jdbcType="INTEGER"/>
        <result property="columnSeq" column="column_seq" jdbcType="INTEGER"/>
        <result property="showCount" column="show_count" jdbcType="INTEGER"/>
        <result property="showMore" column="show_more" jdbcType="BOOLEAN"/>
        <result property="labelIcon" column="label_icon" jdbcType="VARCHAR"/>
        <result property="navigationId" column="navigation_id" jdbcType="INTEGER"/>
        <result property="backgroundType" column="background_type" jdbcType="INTEGER"/>
        <result property="backgroundUrl" column="background_url" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="ResourcePageColumnMap">
        select
          id, resource_page_id, column_type, column_name, column_format, column_seq, show_more,show_count, create_time, label_icon, navigation_id,background_type,background_url
        from book.resource_page_column
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_column(resource_page_id, column_type, column_name, column_format, column_seq,
         show_more, show_count, create_time, update_time, create_user, label_icon, navigation_id,background_type,background_url)
        values (#{resourcePageId}, #{columnType}, #{columnName}, #{columnFormat}, #{columnSeq},
        #{showMore}, #{showCount}, now(), now(), #{createUser}, #{labelIcon}, #{navigationId},#{backgroundType}, #{backgroundUrl})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.resource_page_column
        <set>
            <if test="columnType != null">
                column_type = #{columnType},
            </if>
            <if test="columnName != null and columnName != ''">
                column_name = #{columnName},
            </if>
            <if test="columnFormat != null">
                column_format = #{columnFormat},
            </if>
            <if test="columnSeq != null">
                column_seq = #{columnSeq},
            </if>
            <if test="showCount != null">
                show_count = #{showCount},
            </if>
            <if test="showMore != null">
                show_more = #{showMore},
            </if>
                label_icon = #{labelIcon},
                update_time = now(),
            <if test="navigationId != null">
                navigation_id = #{navigationId},
            </if>
            <if test="backgroundType != null">
                background_type = #{backgroundType},
            </if>
            <if test="backgroundUrl != null">
                background_url = #{backgroundUrl},
            </if>
        </set>
        where id = #{id}
    </update>

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

    <select id="getMaxSeq" parameterType="map" resultType="integer">
        SELECT
        IFNULL(MAX(column_seq), 0)
        FROM
        book.resource_page_column
        where resource_page_id = #{resourcePageId}
        <if test="navigationId != null">
            and navigation_id = #{navigationId}
        </if>
    </select>

    <select id="getColumnListByPageId" resultMap="ResourceColumnAndServe" parameterType="map">
        SELECT
        resource_page_id,
        id,
        column_type,
        column_name,
        column_format,
        column_seq,
        show_count,
        show_more,
        label_icon,
        navigation_id,
        background_type,
        background_url
        FROM
            resource_page_column
        WHERE
            resource_page_id = #{resourcePageId}
        <if test="navigationId != null">
            and navigation_id = #{navigationId}
        </if>
        <if test="columnType!=null">
            and column_type = #{columnType}
        </if>
        ORDER BY
            column_seq ASC,
            id ASC
    </select>

    <select id="getColumnIdListByPageId" resultType="Integer" parameterType="map">
        SELECT
        DISTINCT column_type
        FROM
            resource_page_column
        WHERE
            resource_page_id = #{resourcePageId}
        <if test="navigationId != null">
            and navigation_id = #{navigationId}
        </if>
    </select>

    <select id="getByColumnTypeAndPage" parameterType="map" resultMap="ResourcePageColumnMap">
         select
          id, resource_page_id, column_type, column_name, column_format, column_seq, show_more, show_count, create_time, navigation_id, background_type, background_url
        from book.resource_page_column
        where resource_page_id = #{resourcePageId}
        and column_type = #{columnType}
    </select>

    <update id="updateColumnSeqNum" parameterType="map">
        update resource_page_column
        set column_seq = #{columnSeq},
        update_time = now()
        where id = #{id}
    </update>

    <select id="getColumByMoveType" parameterType="map" resultMap="ResourcePageColumnMap">
        SELECT
        id, resource_page_id, column_type, column_name, column_format, column_seq, show_more, show_count, create_time, navigation_id,background_type, background_url
        FROM
        book.resource_page_column
        WHERE
        resource_page_id = #{resourcePageId}
        AND navigation_id = #{navigationId}
        <choose>
            <when test="moveType == 'UP'">
                AND column_seq &lt; #{columnSeq}
                ORDER BY
                column_seq DESC
            </when>
            <otherwise>
                AND column_seq > #{columnSeq}
                ORDER BY
                column_seq ASC
            </otherwise>
        </choose>
        LIMIT 1
    </select>

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

    <delete id="deleteByPageId" parameterType="long">
         delete from book.resource_page_column where resource_page_id = #{resourcePageId}
    </delete>

    <update id="updateNavigationId" parameterType="map">
         update book.resource_page_column
         set navigation_id = #{navigationId}
         where resource_page_id = #{resourcePageId}
    </update>

    <select id="getByNavigation" resultMap="ResourcePageColumnMap" parameterType="long">
         select
          id, resource_page_id, column_type, column_name, column_format, column_seq, show_more,show_count, create_time, label_icon, navigation_id, background_type, background_url
        from book.resource_page_column
        where navigation_id = #{navigationId}
    </select>

    <select id="getColumnIdsByPageId" resultType="long" parameterType="long">
        SELECT
            id
        FROM
            resource_page_column
        WHERE
            resource_page_id = #{resourcePageId}
    </select>

    <delete id="deleteByIds" parameterType="list">
        delete from book.resource_page_column
        where id IN
        <foreach collection="list" index="index" close=")" open="(" item="item" separator=",">
            #{item}
        </foreach>
    </delete>

</mapper>