<?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.ruoyi.manage.mapper.LibraryDownloadLogMapper">
    <!--
      模块：数字图书馆-下载日志
      说明：
        - 仅统计 result='0' 的成功记录；聚合按天返回 day/count。
    -->

    <insert id="insert" parameterType="com.ruoyi.manage.domain.LibraryDownloadLog" useGeneratedKeys="true"
            keyProperty="id">
        insert into tb_library_book_download_log(book_id, asset_id, user_id, result, ip, ua, referer, create_time)
        values (#{bookId}, #{assetId}, #{userId}, #{result}, #{ip}, #{ua}, #{referer}, #{createTime})
    </insert>

    <!-- moved under mapper/manage for consistency -->

    <!-- 统计：按天分组下载数量（仅统计成功 result='0'）。返回字段 day(yyyy-MM-dd), count -->
    <select id="selectDownloadCountByDay" resultType="com.ruoyi.manage.domain.vo.DayCount">
        select DATE_FORMAT(create_time, '%Y-%m-%d') as day,
               count(*)                             as count
        from tb_library_book_download_log
        where create_time &gt;= #{from}
          and create_time &lt; #{to}
          and result = '0'
        group by DATE_FORMAT(create_time, '%Y-%m-%d')
        order by day asc
    </select>

    <!-- 用户下载TOP榜（仅统计成功下载），返回 TopUserVO(userId, username/nickname, passedCount=downloadCount) -->
    <select id="selectTopDownloadUsers" resultType="com.ruoyi.manage.vo.TopUserVO">
        select l.user_id as userId,
        coalesce(max(u.nick_name), max(u.user_name), concat('用户', l.user_id)) as nickname,
        coalesce(max(u.user_name), max(u.nick_name), concat('user', l.user_id)) as username,
        count(1) as passedCount
        from tb_library_book_download_log l
        left join sys_user u on u.user_id = l.user_id
        where l.result = '0'
        group by l.user_id
        order by passedCount desc, userId asc
        <if test="limit != null">limit #{limit}</if>
        <if test="limit == null">limit 5</if>
    </select>
</mapper>
