package org.jk.rag.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jk.common.mybatis.core.mapper.BaseMapperPlus;
import org.jk.rag.domain.EvaluationTask;
import org.jk.rag.domain.SysUserOnlineLog;
import org.jk.rag.domain.dto.ConversationReqDTO;
import org.jk.rag.domain.dto.OverviewKnowledgeDTO;
import org.jk.rag.domain.entity.BmKnowledge;
import org.jk.rag.domain.vo.SysUserOnlineLogVo;

import java.util.List;

/**
 * 在线人数记录Mapper接口
 *
 * @author Lion Li
 * @date 2026-01-22
 */
public interface SysUserOnlineLogMapper extends BaseMapperPlus<SysUserOnlineLog, SysUserOnlineLogVo> {

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT " +
        "        t.id," +
        "        t.online_num," +
        "        t.create_time" +
        "    FROM (" +
        "        SELECT " +
        "            id," +
        "            online_num," +
        "            create_time," +
        "            ROW_NUMBER() OVER (" +
        "                PARTITION BY DATE(create_time) " +
        "                ORDER BY CAST(online_num AS UNSIGNED) DESC" +
        "            ) AS rn" +
        "        FROM sys_user_online_log" +
        "        WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)" +
        "    ) t" +
        "    WHERE t.rn = 1" +
        "    ORDER BY t.create_time;" +
        "</script>")
    List<SysUserOnlineLogVo> queryThirtyDayData();
    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT " +
        "    DATE_FORMAT(create_time, '%Y-%m-01') AS createTime,  " +
        "    SUM(CAST(online_num AS UNSIGNED)) AS onlineNum " +
        " FROM sys_user_online_log" +
        " WHERE create_time >= DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01 00:00:00'), INTERVAL 12 MONTH)" +
        " GROUP BY DATE_FORMAT(create_time, '%Y-%m-01')  " + // 核心修改：和SELECT的日期表达式完全一致
        " ORDER BY createTime ASC;  " +
        "</script>")
    List<SysUserOnlineLogVo> queryTwelveMonthData();

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  DATE(create_time) AS createTime," +
        "  count(id) AS ConversationNum " +
        "FROM dialog " +
        "WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)" +
        "GROUP BY createTime " +
        "ORDER BY createTime " +
        "</script>")
    List<ConversationReqDTO> queryThirtyDayConversationData();
    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  DATE_FORMAT(create_time, '%Y-%m') AS createTime," +
        "  count(id) AS ConversationNum " +
        "FROM dialog " +
        "WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)" +
        "GROUP BY createTime " +
        "ORDER BY createTime " +
        "</script>")
    List<ConversationReqDTO> queryTwelveMonthConversationData();

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  DATE(create_time) AS createTime," +
        "  (1.0 * count(id)) / IFNULL(count(DISTINCT dialog_id), 1) AS ConversationNum " +
        "FROM dialog_detail " +
        "WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) and type = 'user' " +
        "GROUP BY createTime " +
        "ORDER BY createTime " +
        "</script>")
    List<ConversationReqDTO> queryThirtyDayConversationDetailData();
    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  DATE_FORMAT(create_time, '%Y-%m') AS createTime," +
        "  (1.0 * count(id)) / IFNULL(count(DISTINCT dialog_id), 1) AS ConversationNum " +
        "FROM dialog_detail " +
        "WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) and type = 'user'" +
        "GROUP BY createTime " +
        "ORDER BY createTime " +
        "</script>")
    List<ConversationReqDTO> queryTwelveMonthConversationDetailData();

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  count(*) " +
        "FROM dialog_log " +
        "WHERE app_id = #{appId}" +
        "</script>")
    int queryAppCountData(@Param("appId") String appId);

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  sum(cost_time)/count(id) " +
        "FROM dialog_log " +
        "WHERE app_id = #{appId}" +
        "</script>")
    Long queryAppCostTime(@Param("appId") String appId);

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  * " +
        "FROM evaluation_task " +
        "WHERE review_rag = #{appId} and status = '2' " +
        "ORDER BY create_time DESC" +
        " limit 1" +
        "</script>")
    EvaluationTask queryAppTaskTime(@Param("appId") String appId);

    @Select("<script>" +
        " SELECT" +
        "   knowledge_id knowledgeId" +
        "   ,name knowledgeName" +
        "   ,length total" +
        " FROM bm_knowledge " +
        "</script>")
    Page<OverviewKnowledgeDTO> queryKnowledgeData(@Param("page") Page page);

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  sum(dd.input_token) tokenSize " +
        "FROM dialog d " +
        "left join dialog_detail dd on d.id = dd.dialog_id " +
        "WHERE d.app_id = #{appId}" +
        "</script>")
    Integer queryAppOutputTokenData(@Param("appId") String appId);

    @InterceptorIgnore(dataPermission = "true", tenantLine = "true")
    @Select("<script>" +
        " SELECT" +
        "  sum(dd.output_token) tokenSize " +
        "FROM dialog d " +
        "left join dialog_detail dd on d.id = dd.dialog_id " +
        "WHERE d.app_id = #{appId}" +
        "</script>")
    Integer queryAppInputTokenData(@Param("appId") String appId);
}
