<?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.mapper.clickhouse.BookMapper">

    <select id="listBookGroup4Channel" resultType="com.pcloud.book.group.vo.ListBook4ChannelVO" parameterType="map">
        SELECT b.BOOK_ID                              bookId,
               any(b.COVER_IMG)                       coverImg,
               any(bg.id)                             bookGroupId,
               any(b.ISBN)                            isbn,
               any(b.BOOK_NAME)                       bookName,
               any(concat('BK', toString(b.BOOK_ID))) bookNumber
        FROM (SELECT BOOK_ID, COVER_IMG, ISBN, BOOK_NAME
              FROM book
              WHERE IS_DELETE = 0
                AND BOOK_NAME NOT LIKE concat('%', '红榜', '%')
                AND BOOK_ID IN (SELECT toInt64(BOOK_ID) FROM book_adviser WHERE IS_PRINT = 1)
                AND CHAR_LENGTH(BOOK_NAME) >= 8
                 ) b
                 JOIN ( SELECT id, book_id, create_time, group_qrcode_name
                        FROM book_group
                        WHERE id IN (SELECT toInt64(book_group_id) FROM book_group_classify WHERE is_delete = 0)
                          AND is_delete = 0
                        <if test="channelId != null"> AND channel_id = ${channelId} </if>
                        <if test="startTime != null "> AND create_time &gt;= #{startTime} </if>
                        <if test="endTime != null "> AND create_time &lt;=  #{endTime} </if>
                        <if test="isFundBook != null and isFundBook == 1">
                            AND book_id IN (SELECT BOOK_ID FROM book_fund WHERE END_TIME &lt; now() AND START_TIME > now())
                        </if>
            ) bg ON b.BOOK_ID = bg.book_id
        <if test="keywords != null">
            WHERE b.ISBN like concat('%',#{keywords},'%') OR b.BOOK_NAME like concat('%',#{keywords},'%') OR bg.group_qrcode_name like concat('%',#{keywords},'%')
        </if>
        GROUP BY b.BOOK_ID
        ORDER BY b.BOOK_ID DESC
        LIMIT ${offset}, ${limit}
    </select>

    <select id="getBookScanCounts" resultType="com.pcloud.analysisengine.qrcode.dto.BookScanCountDto">

        select book_id bookId, scans scanCount
        from collect.total_scan_gmv
        where toDate(create_time) = today()
        and toInt64(scans) > 0
  and bookId in
  <foreach collection="list" item="item" separator="," open="(" close=")">
      #{item}
  </foreach>


    </select>

    <select id="countBookGroup4Channel" resultType="java.lang.Integer">
        SELECT uniqExact(b.BOOK_ID)
        FROM (SELECT BOOK_ID, COVER_IMG, ISBN, BOOK_NAME
              FROM book
              WHERE IS_DELETE = 0
                AND BOOK_NAME NOT LIKE concat('%', '红榜', '%')
                AND BOOK_ID IN (SELECT toInt64(BOOK_ID) FROM book_adviser WHERE IS_PRINT = 1)
                AND CHAR_LENGTH(BOOK_NAME) >= 8
                 ) b
                 JOIN ( SELECT id, book_id, create_time, group_qrcode_name
                        FROM book_group
                        WHERE id IN (SELECT toInt64(book_group_id) FROM book_group_classify WHERE is_delete = 0)
                          AND is_delete = 0
                        <if test="channelId != null"> AND channel_id = ${channelId} </if>
                        <if test="startTime != null "> AND create_time &gt;= #{startTime} </if>
                        <if test="endTime != null "> AND create_time &lt;=  #{endTime} </if>
                        <if test="isFundBook != null and isFundBook == 1">
                            AND book_id IN (SELECT BOOK_ID FROM book_fund WHERE END_TIME &lt; now() AND START_TIME > now())
                        </if>
            ) bg ON b.BOOK_ID = bg.book_id
        <if test="keywords != null">
            WHERE b.ISBN like concat('%',#{keywords},'%') OR b.BOOK_NAME like concat('%',#{keywords},'%') OR bg.group_qrcode_name like concat('%',#{keywords},'%')
        </if>
    </select>

</mapper>
