<?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.applet.dao.impl.AppletOuterBooklistDaoImpl">

    <resultMap type="com.pcloud.book.applet.entity.AppletOuterBooklist" id="AppletOuterBooklistMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="booklistName" column="booklist_name" jdbcType="VARCHAR"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="digest" column="digest" jdbcType="VARCHAR"/>
        <result property="displayPic" column="display_pic" jdbcType="VARCHAR"/>
        <result property="publish" column="publish" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="agentId" column="agent_id" jdbcType="BIGINT" />
        <result property="isShowHome" column="is_show_home" jdbcType="TINYINT" />
    </resultMap>

    <sql id="Base_Column_List">
        id, booklist_name, user_name, digest, display_pic, publish, create_time, update_time, agent_id, is_show_home
    </sql>
    <!--查询单个-->
    <select id="getById" resultMap="AppletOuterBooklistMap">
        select
          <include refid="Base_Column_List"/>
        from book.applet_outer_booklist
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_outer_booklist(booklist_name, user_name, digest, display_pic, publish, create_time, update_time, agent_id, is_show_home)
        values (#{booklistName}, #{userName}, #{digest}, #{displayPic}, #{publish}, now(), now(), #{agentId}, #{isShowHome})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.applet_outer_booklist
        <set>
            <if test="booklistName != null and booklistName != ''">
                booklist_name = #{booklistName},
            </if>
            <if test="userName != null and userName != ''">
                user_name = #{userName},
            </if>
                digest = #{digest},
                display_pic = #{displayPic},
            <if test="publish != null and publish != ''">
                publish = #{publish},
            </if>
            <if test="isShowHome != null">
                is_show_home = #{isShowHome},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

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

    <delete id="deleteConfigByOuterBooklistId" parameterType="long">
        delete from applet_outer_booklist_config WHERE booklist_id = #{booklistId}
    </delete>

    <insert id="addOuterBooklistConfig" parameterType="list" useGeneratedKeys="true" keyProperty="id">
        insert into applet_outer_booklist_config (
        booklist_id, outer_book_id,seq, create_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{item.booklistId}, #{item.outerBookId}, #{item.seq}, NOW() )
        </foreach>
    </insert>

    <select id="listPageOuterBooklist" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletOuterBooklistDTO">
        SELECT
        b.id,
        b.booklist_name booklistName,
        b.user_name userName,
        b.display_pic displayPic,
        COUNT(c.id) bookCounts,
        b.digest,
        b.publish,
        b.create_time createTime,
        b.is_show_home isShowHome
        FROM
        applet_outer_booklist b
        LEFT JOIN applet_outer_booklist_config c ON b.id = c.booklist_id
        WHERE 1=1 and b.is_delete = 0
        <if test="publish != null">
            AND b.publish = #{publish}
        </if>
        <if test="name != null">
            AND b.booklist_name like concat("%",#{name},"%") 
        </if>
        <choose>
            <when test="agentId != null">
                and b.agent_id = #{agentId}
            </when>
            <otherwise>
                and b.agent_id = 0
            </otherwise>
        </choose>
        <if test="isShowHome != null">
            and is_show_home = #{isShowHome}
        </if>
        group by b.id
        ORDER BY
        b.create_time DESC
    </select>
    
    <select id="getBookIdsByBooklistId" resultType="long" parameterType="long">
        SELECT outer_book_id from applet_outer_booklist_config WHERE booklist_id=#{booklistId}
    </select>

    <select id="getBooklistCountsByBookId" parameterType="long" resultType="integer">
        SELECT COUNT(1) from applet_outer_booklist_config WHERE outer_book_id=#{outerBookId}
    </select>

    <select id="getOuterBooklistPublish" resultType="string" parameterType="map">
        SELECT DISTINCT publish
        FROM applet_outer_booklist
        WHERE
	      publish IS NOT NULL
	      and is_delete = 0
        <choose>
            <when test="agentId != null">
                and agent_id = #{agentId}
            </when>
            <otherwise>
                and agent_id = 0
            </otherwise>
        </choose>
    </select>

    <select id="getBookListByBooklistId" parameterType="long" resultType="com.pcloud.book.applet.dto.AppletOuterBookDTO">
        SELECT
            b.id,
            b.cover_img coverImg,
            b.book_name bookName,
            b.publish,
            b.author,
            b.isbn,
            b.remark,
            b.price,
            b.link_type linkType,
            b.link_url linkUrl,
            b.create_time createTime
        FROM
            applet_outer_book b
        LEFT JOIN applet_outer_booklist_config c ON b.id = c.outer_book_id
        WHERE
            c.booklist_id = #{booklistId}
    </select>

    <select id="getByIds" parameterType="list" resultType="com.pcloud.book.applet.dto.AppletOuterBooklistDTO">
        SELECT
        b.id id,
        b.booklist_name booklistName,
        b.user_name userName,
        b.display_pic displayPic,
        COUNT(c.id) bookCounts,
        b.digest,
        b.publish,
        b.create_time createTime,
        b.is_show_home isShowHome
        FROM
        applet_outer_booklist b
        LEFT JOIN applet_outer_booklist_config c ON b.id = c.booklist_id
        WHERE
        b.is_delete = 0
        and
            b.id IN
        <foreach collection="list" item="item" separator="," open="(" close=")" >
            ${item}
        </foreach>
        GROUP BY b.id
    </select>

    <delete id="batchDeleteOuterBookFromBooklist" parameterType="list">
        DELETE FROM applet_outer_booklist_config
        WHERE id IN
        <foreach collection="list" item="item" separator="," open="(" close=")" >
            ${item}
        </foreach>
    </delete>

    <update id="updateDeleteState" parameterType="long">
        update
        applet_outer_booklist
        set is_delete = 1
        where
        id = #{id}
    </update>


    <select id="updateOuterBooklist" parameterType="com.pcloud.book.applet.dto.AppletOuterBooklistDTO">
        update
        applet_outer_booklist
        set
        is_show_home = #{isShowHome},
        update_time = now()
        where
        id = #{id}
    </select>

    <select id="listBooklistIdByGradeLabelAndClickCount" parameterType="map" resultType="long">
        SELECT
            a.target_id
        FROM
            applet_resource_apply a
        LEFT JOIN applet_link_click c ON a.target_id = c.booklist_id
        AND c.type_id = 5
        AND c.create_date >= #{startDate}
        LEFT JOIN applet_outer_booklist b ON a.target_id = b.id
        WHERE
            a.crowd_type = #{crowdType}
        AND a.target_type = "BOOKLIST"
        AND a.target_id > 0
        AND b.is_show_home = 1
        AND b.is_delete = 0
        GROUP BY
            a.target_id
        ORDER BY
            COUNT(c.id) DESC,
            c.create_time DESC,
            a.target_id DESC
    </select>

</mapper>