<?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.mapper.ResourcePageFolderItemMapper">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.ResourcePageFolderItem">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="folder_id" jdbcType="BIGINT" property="folderId"/>
        <result column="resource_page_id" jdbcType="BIGINT" property="resourcePageId"/>
        <result column="resource_page_column_id" jdbcType="BIGINT" property="resourcePageColumnId"/>
        <result column="serve_id" jdbcType="BIGINT" property="serveId"/>
        <result column="serve_type" jdbcType="VARCHAR" property="serveType"/>
        <result column="link_url" jdbcType="VARCHAR" property="linkUrl"/>
        <result column="type_code" jdbcType="VARCHAR" property="typeCode"/>
        <result column="seq_num" jdbcType="INTEGER" property="seqNum"/>
        <result column="is_show" jdbcType="INTEGER" property="isShow"/>
        <result column="guide_content" jdbcType="VARCHAR" property="guideContent"/>
        <result column="logo_url" jdbcType="VARCHAR" property="logoUrl"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="create_user" jdbcType="BIGINT" property="createUser"/>
        <result column="qrcode_source" jdbcType="INTEGER" property="qrcodeSource"/>
        <result column="qrcode_show_type" jdbcType="INTEGER" property="qrcodeShowType"/>
        <result column="account_setting_id" jdbcType="BIGINT" property="accountSettingId"/>
        <result column="self_big_pic" jdbcType="VARCHAR" property="selfBigPic"/>
        <result column="self_small_pic" jdbcType="VARCHAR" property="selfSmallPic"/>
        <result column="self_serve_name" jdbcType="VARCHAR" property="selfServeName"/>
    </resultMap>

    <resultMap id="resourcePageFolderItemVO" type="com.pcloud.book.group.vo.ResourcePageFolderItemVO" extends="BaseResultMap">
    </resultMap>

    <sql id="Base_Column_List">
        id,
        folder_id,
        resource_page_column_id,
        resource_page_id,
        serve_id,
        serve_type,
        link_url,
        type_code,
        seq_num,
        is_show,
        guide_content,
        logo_url,
        create_time,
        update_time,
        create_user,
        qrcode_source,
        qrcode_show_type,
        account_setting_id,
        self_big_pic,
        self_small_pic,
        self_serve_name
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from resource_page_folder_item
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from resource_page_folder_item
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.pcloud.book.group.entity.ResourcePageFolderItem"
            useGeneratedKeys="true">
        insert into resource_page_folder_item (folder_id, resource_page_id, resource_page_column_id, serve_id,
                                               serve_type, link_url, type_code,
                                               seq_num, is_show, guide_content,
                                               logo_url, create_time, update_time,
                                               create_user, qrcode_source, qrcode_show_type,
                                               account_setting_id, self_big_pic, self_small_pic,
                                               self_serve_name)
        values (#{folderId,jdbcType=BIGINT}, #{resourcePageId,jdbcType=BIGINT}, #{resourcePageColumnId,jdbcType=BIGINT},
                #{serveId,jdbcType=BIGINT},
                #{serveType,jdbcType=VARCHAR}, #{linkUrl,jdbcType=VARCHAR}, #{typeCode,jdbcType=VARCHAR},
                #{seqNum,jdbcType=INTEGER}, #{isShow,jdbcType=INTEGER}, #{guideContent,jdbcType=VARCHAR},
                #{logoUrl,jdbcType=VARCHAR}, now(), now(),
                #{createUser,jdbcType=BIGINT}, #{qrcodeSource,jdbcType=INTEGER}, #{qrcodeShowType,jdbcType=INTEGER},
                #{accountSettingId,jdbcType=BIGINT}, #{selfBigPic,jdbcType=VARCHAR}, #{selfSmallPic,jdbcType=VARCHAR},
                #{selfServeName,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.pcloud.book.group.entity.ResourcePageFolderItem"
            useGeneratedKeys="true">
        insert into resource_page_folder_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="folderId != null">
                folder_id,
            </if>
            <if test="resourcePageId != null">
                resource_page_id,
            </if>
            <if test="resourcePageColumnId != null">
                resource_page_column_id,
            </if>
            <if test="serveId != null">
                serve_id,
            </if>
            <if test="serveType != null and serveType != ''">
                serve_type,
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url,
            </if>
            <if test="typeCode != null and typeCode != ''">
                type_code,
            </if>
            <if test="seqNum != null">
                seq_num,
            </if>
            <if test="isShow != null">
                is_show,
            </if>
            <if test="guideContent != null and guideContent != ''">
                guide_content,
            </if>
            <if test="logoUrl != null and logoUrl != ''">
                logo_url,
            </if>
            create_time,
            update_time,
            <if test="createUser != null">
                create_user,
            </if>
            <if test="qrcodeSource != null">
                qrcode_source,
            </if>
            <if test="qrcodeShowType != null">
                qrcode_show_type,
            </if>
            <if test="accountSettingId != null">
                account_setting_id,
            </if>
            <if test="selfBigPic != null and selfBigPic != ''">
                self_big_pic,
            </if>
            <if test="selfSmallPic != null and selfSmallPic != ''">
                self_small_pic,
            </if>
            <if test="selfServeName != null and selfServeName != ''">
                self_serve_name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="folderId != null">
                #{folderId,jdbcType=BIGINT},
            </if>
            <if test="resourcePageId != null">
                #{resourcePageId,jdbcType=BIGINT},
            </if>
            <if test="resourcePageColumnId != null">
                #{resourcePageColumnId,jdbcType=BIGINT},
            </if>
            <if test="serveId != null">
                #{serveId,jdbcType=BIGINT},
            </if>
            <if test="serveType != null and serveType != ''">
                #{serveType,jdbcType=VARCHAR},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                #{linkUrl,jdbcType=VARCHAR},
            </if>
            <if test="typeCode != null and typeCode != ''">
                #{typeCode,jdbcType=VARCHAR},
            </if>
            <if test="seqNum != null">
                #{seqNum,jdbcType=INTEGER},
            </if>
            <if test="isShow != null">
                #{isShow,jdbcType=INTEGER},
            </if>
            <if test="guideContent != null and guideContent != ''">
                #{guideContent,jdbcType=VARCHAR},
            </if>
            <if test="logoUrl != null and logoUrl != ''">
                #{logoUrl,jdbcType=VARCHAR},
            </if>
            now(),
            now(),
            <if test="createUser != null">
                #{createUser,jdbcType=BIGINT},
            </if>
            <if test="qrcodeSource != null">
                #{qrcodeSource,jdbcType=INTEGER},
            </if>
            <if test="qrcodeShowType != null">
                #{qrcodeShowType,jdbcType=INTEGER},
            </if>
            <if test="accountSettingId != null">
                #{accountSettingId,jdbcType=BIGINT},
            </if>
            <if test="selfBigPic != null and selfBigPic != ''">
                #{selfBigPic,jdbcType=VARCHAR},
            </if>
            <if test="selfSmallPic != null and selfSmallPic != ''">
                #{selfSmallPic,jdbcType=VARCHAR},
            </if>
            <if test="selfServeName != null and selfServeName != ''">
                #{selfServeName,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.group.entity.ResourcePageFolderItem">
        update resource_page_folder_item
        <set>
            <if test="folderId != null">
                folder_id = #{folderId,jdbcType=BIGINT},
            </if>
            <if test="resourcePageId != null">
                resource_page_id = #{resourcePageId,jdbcType=BIGINT},
            </if>
            <if test="resourcePageColumnId != null">
                resource_page_column_id = #{resourcePageColumnId,jdbcType=BIGINT},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId,jdbcType=BIGINT},
            </if>
            <if test="serveType != null and serveType != ''">
                serve_type = #{serveType,jdbcType=VARCHAR},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl,jdbcType=VARCHAR},
            </if>
            <if test="typeCode != null and typeCode != ''">
                type_code = #{typeCode,jdbcType=VARCHAR},
            </if>
            <if test="seqNum != null">
                seq_num = #{seqNum,jdbcType=INTEGER},
            </if>
            <if test="isShow != null">
                is_show = #{isShow,jdbcType=INTEGER},
            </if>
            <if test="guideContent != null and guideContent != ''">
                guide_content = #{guideContent,jdbcType=VARCHAR},
            </if>
            <if test="logoUrl != null and logoUrl != ''">
                logo_url = #{logoUrl,jdbcType=VARCHAR},
            </if>
            update_time = now(),
            <if test="createUser != null">
                create_user = #{createUser,jdbcType=BIGINT},
            </if>
            <if test="qrcodeSource != null">
                qrcode_source = #{qrcodeSource,jdbcType=INTEGER},
            </if>
            <if test="qrcodeShowType != null">
                qrcode_show_type = #{qrcodeShowType,jdbcType=INTEGER},
            </if>
            <if test="accountSettingId != null">
                account_setting_id = #{accountSettingId,jdbcType=BIGINT},
            </if>
            <if test="selfBigPic != null and selfBigPic != ''">
                self_big_pic = #{selfBigPic,jdbcType=VARCHAR},
            </if>
            <if test="selfSmallPic != null and selfSmallPic != ''">
                self_small_pic = #{selfSmallPic,jdbcType=VARCHAR},
            </if>
            <if test="selfServeName != null and selfServeName != ''">
                self_serve_name = #{selfServeName,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.pcloud.book.group.entity.ResourcePageFolderItem">
        update resource_page_folder_item
        set folder_id               = #{folderId,jdbcType=BIGINT},
            resource_page_id        = #{resourcePageId,jdbcType=BIGINT},
            resource_page_column_id = #{resourcePageColumnId,jdbcType=BIGINT},
            serve_id                = #{serveId,jdbcType=BIGINT},
            serve_type              = #{serveType,jdbcType=VARCHAR},
            link_url                = #{linkUrl,jdbcType=VARCHAR},
            type_code               = #{typeCode,jdbcType=VARCHAR},
            seq_num                 = #{seqNum,jdbcType=INTEGER},
            is_show                 = #{isShow,jdbcType=INTEGER},
            guide_content           = #{guideContent,jdbcType=VARCHAR},
            logo_url                = #{logoUrl,jdbcType=VARCHAR},
            update_time             = now(),
            create_user             = #{createUser,jdbcType=BIGINT},
            qrcode_source           = #{qrcodeSource,jdbcType=INTEGER},
            qrcode_show_type        = #{qrcodeShowType,jdbcType=INTEGER},
            account_setting_id      = #{accountSettingId,jdbcType=BIGINT},
            self_big_pic            = #{selfBigPic,jdbcType=VARCHAR},
            self_small_pic          = #{selfSmallPic,jdbcType=VARCHAR},
            self_serve_name         = #{selfServeName,jdbcType=VARCHAR}
        where id = #{id,jdbcType=BIGINT}
    </update>

    <delete id="deleteByColumnIds">
        delete
        from resource_page_folder_item
        where resource_page_column_id in
        <foreach collection="columnIds" open="(" item="it" separator="," close=")">
            #{it,jdbcType=BIGINT}
        </foreach>
    </delete>


    <insert id="batchInsert">
        insert into resource_page_folder_item (folder_id, resource_page_id, resource_page_column_id, serve_id,
                                               serve_type, link_url, type_code,
                                               seq_num, is_show, guide_content,
                                               logo_url, create_time, update_time,
                                               create_user, qrcode_source, qrcode_show_type,
                                               account_setting_id, self_big_pic, self_small_pic,
                                               self_serve_name)
        values
        <foreach collection="itemList" item="it" separator=",">
            (#{it.folderId,jdbcType=BIGINT}, #{it.resourcePageId,jdbcType=BIGINT}, #{it.resourcePageColumnId,jdbcType=BIGINT},
             #{it.serveId,jdbcType=BIGINT},
             #{it.serveType,jdbcType=VARCHAR}, #{it.linkUrl,jdbcType=VARCHAR}, #{it.typeCode,jdbcType=VARCHAR},
             #{it.seqNum,jdbcType=INTEGER}, #{it.isShow,jdbcType=INTEGER}, #{it.guideContent,jdbcType=VARCHAR},
             #{it.logoUrl,jdbcType=VARCHAR}, now(), now(),
             #{it.createUser,jdbcType=BIGINT}, #{it.qrcodeSource,jdbcType=INTEGER}, #{it.qrcodeShowType,jdbcType=INTEGER},
             #{it.accountSettingId,jdbcType=BIGINT}, #{it.selfBigPic,jdbcType=VARCHAR}, #{it.selfSmallPic,jdbcType=VARCHAR},
             #{it.selfServeName,jdbcType=VARCHAR})
        </foreach>
    </insert>


    <select id="listByFolderIds" resultMap="resourcePageFolderItemVO">
        select
        <include refid="Base_Column_List"/>
        from resource_page_folder_item
        where folder_id in
        <foreach collection="folderIds" open="(" item="it" separator="," close=")">
            #{it,jdbcType=BIGINT}
        </foreach>
    </select>

    <select id="getServeListBySceneId4QrUpdate" resultType="com.pcloud.book.group.vo.ResourcePageItemVO">
        select rpfi.serve_id   serveId,
               rpfi.serve_type serveType,
               rpfi.link_url   linkUrl,
               rp.create_user  createUser,
               rpfi.type_code  typeCode,
               rp.scene_id     sceneId,
               rp.id           resourcePageId,
               rpc.column_seq  columnSeq,
               rpf.seq_num     folderNum,
               rpfi.seq_num    serveNum
        from resource_page as rp
                 join resource_page_column rpc on rp.id = rpc.resource_page_id
                 join resource_page_folder rpf on rp.id = rpf.resource_page_id
                 join resource_page_folder_item rpfi on rp.id = rpfi.resource_page_id
        where rp.scene_id = #{sceneId,jdbcType=BIGINT}
          AND rpfi.serve_type IN ('APP', 'PRODUCT', 'MEMBER_ACTIVITY', 'RESOURCE')
        GROUP BY rpfi.serve_id
        ORDER BY columnSeq,
                 folderNum,
                 serveId
    </select>

    <select id="checkOneServeItemByPageId" resultType="com.pcloud.book.group.vo.ResourcePageItemVO">
        SELECT
            i.serve_id serveId,
            i.serve_type serveType,
            i.link_url linkUrl,
            i.type_code typeCode,
            i.create_user createUser,
            i.resource_page_id resourcePageId
        FROM
            resource_page p
                LEFT JOIN resource_page_folder_item i ON i.resource_page_id = p.id
        WHERE
            resource_page_id = #{resourcePageId,jdbcType=BIGINT}
          <if test="ignoreRays">
              AND p.open_rays = 0
          </if>
        HAVING
            COUNT( i.id ) = 1
           AND i.serve_type IN ( "APP", "PRODUCT", "RESOURCE" )
    </select>
</mapper>