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

    <resultMap type="com.pcloud.book.group.entity.ResourcePageItem" id="ResourcePageItemMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="resourcePageId" column="resource_page_id" jdbcType="INTEGER"/>
        <result property="resourcePageColumnId" column="resource_page_column_id" jdbcType="INTEGER"/>
        <result property="serveId" column="serve_id" jdbcType="INTEGER"/>
        <result property="serveType" column="serve_type" jdbcType="VARCHAR"/>
        <result property="linkUrl" column="link_url" jdbcType="VARCHAR"/>
        <result property="typeCode" column="type_code" jdbcType="VARCHAR"/>
        <result property="seqNum" column="seq_num" jdbcType="INTEGER"/>
        <result property="isShow" column="is_show" jdbcType="BOOLEAN"/>
        <result property="guideContent" column="guide_content" jdbcType="VARCHAR"/>
        <result property="logoUrl" column="logo_url" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createUser" column="create_user" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="ResourcePageItemMap">
        select
          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
        from book.resource_page_item
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_item(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)
        values (#{resourcePageId}, #{resourcePageColumnId}, #{serveId}, #{serveType}, #{linkUrl}, #{typeCode},
         #{seqNum}, #{isShow}, #{guideContent}, #{logoUrl}, now(), now(), #{createUser})
    </insert>

    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        insert into book.resource_page_item(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)
        values
        <foreach collection="list" separator="," index="index" item="item">
            (#{item.resourcePageId}, #{item.resourcePageColumnId}, #{item.serveId}, #{item.serveType},
            #{item.linkUrl}, #{item.typeCode}, #{item.seqNum}, #{item.isShow}, #{item.guideContent}, #{item.logoUrl},
            now(), now(), #{item.createUser})
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.resource_page_item
        <set>
            <if test="resourcePageId != null">
                resource_page_id = #{resourcePageId},
            </if>
            <if test="resourcePageColumnId != null">
                resource_page_column_id = #{resourcePageColumnId},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId},
            </if>
            <if test="serveType != null and serveType != ''">
                serve_type = #{serveType},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

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


    <delete id="deleteByPageColumnId">
        delete from book.resource_page_item where resource_page_column_id = #{resourcePageColumnId}
    </delete>

    <select id="getResourcePageItemByColumnId" resultType="com.pcloud.book.group.vo.ResourcePageItemVO" parameterType="map">
        SELECT
            serve_id serveId,
            serve_type serveType,
            link_url linkUrl,
            type_code typeCode,
            logo_url logoUrl,
            guide_content guideContent,
            create_user createUser
        FROM
            resource_page_item
        WHERE
            resource_page_column_id = #{resourcePageColumnId}
        ORDER BY seq_num asc, id asc
    </select>

    <select id="getResourcePageItemByPageId" resultType="com.pcloud.book.group.vo.ResourcePageItemVO" parameterType="map">
         SELECT
            serve_id serveId,
            serve_type serveType,
            link_url linkUrl,
            resource_page_column_id resourcePageColumnId,
            type_code typeCode,
            logo_url logoUrl,
            guide_content guideContent,
            create_user createUser
        FROM
            resource_page_item
        WHERE
            resource_page_id = #{resourcePageId}
        <if test="isShow != null">
            and is_show = #{isShow}
        </if>
        ORDER BY seq_num asc, id asc
    </select>

    <select id="getResourceCounts" parameterType="long">
        SELECT  COUNT(1)
        FROM
            resource_page_item
        WHERE
            resource_page_id = #{resourcePageId}
    </select>

    <select id="getServeListByBookGroupId" parameterType="long" resultType="com.pcloud.book.group.entity.BookGroupServe">
       SELECT
            i.serve_id serveId,
            i.serve_type serveType,
            i.link_url serveUrl,
            p.book_group_id bookGroupId,
            p.create_user createUser,
            i.type_code typeCode
        FROM
            resource_page_item i
        LEFT JOIN resource_page p ON i.resource_page_id = p.id
        LEFT JOIN resource_page_column c ON i.resource_page_column_id = c.id
        WHERE
            p.book_group_id = #{bookGroupId}
        AND i.serve_type IN ('APP', 'PRODUCT')
        ORDER BY
            c.column_seq ASC
    </select>

</mapper>