package com.dora.mapper;

import com.dora.entity.ToolUsageDailyStats;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 工具使用每日统计Mapper
 */
@Mapper
public interface ToolUsageDailyStatsMapper {

    /**
     * 插入或更新每日统计
     */
    @Insert("INSERT INTO tool_usage_daily_stats (stats_date, tool_id, tool_code, tool_name, " +
            "total_calls, success_calls, failed_calls, total_points_cost, unique_users, create_time, update_time) " +
            "VALUES (#{statsDate}, #{toolId}, #{toolCode}, #{toolName}, " +
            "#{totalCalls}, #{successCalls}, #{failedCalls}, #{totalPointsCost}, #{uniqueUsers}, NOW(), NOW()) " +
            "ON DUPLICATE KEY UPDATE " +
            "total_calls = #{totalCalls}, success_calls = #{successCalls}, failed_calls = #{failedCalls}, " +
            "total_points_cost = #{totalPointsCost}, unique_users = #{uniqueUsers}, update_time = NOW()")
    int insertOrUpdate(ToolUsageDailyStats stats);

    /**
     * 查询某天的统计数据
     */
    @Select("SELECT * FROM tool_usage_daily_stats WHERE stats_date = #{statsDate} ORDER BY total_calls DESC")
    List<ToolUsageDailyStats> selectByDate(@Param("statsDate") LocalDate statsDate);

    /**
     * 查询日期范围内的统计数据
     */
    @Select("SELECT * FROM tool_usage_daily_stats WHERE stats_date >= #{startDate} AND stats_date <= #{endDate} " +
            "ORDER BY stats_date DESC, total_calls DESC")
    List<ToolUsageDailyStats> selectByDateRange(@Param("startDate") LocalDate startDate,
                                                 @Param("endDate") LocalDate endDate);

    /**
     * 查询某工具的日期范围统计
     */
    @Select("SELECT * FROM tool_usage_daily_stats WHERE tool_code = #{toolCode} " +
            "AND stats_date >= #{startDate} AND stats_date <= #{endDate} ORDER BY stats_date DESC")
    List<ToolUsageDailyStats> selectByToolCodeAndDateRange(@Param("toolCode") String toolCode,
                                                            @Param("startDate") LocalDate startDate,
                                                            @Param("endDate") LocalDate endDate);

    /**
     * 汇总日期范围内的统计数据
     */
    @Select("SELECT SUM(total_calls) as total_calls, SUM(success_calls) as success_calls, " +
            "SUM(failed_calls) as failed_calls, SUM(total_points_cost) as total_points_cost " +
            "FROM tool_usage_daily_stats WHERE stats_date >= #{startDate} AND stats_date <= #{endDate}")
    @Results({
            @Result(property = "totalCalls", column = "total_calls"),
            @Result(property = "successCalls", column = "success_calls"),
            @Result(property = "failedCalls", column = "failed_calls"),
            @Result(property = "totalPointsCost", column = "total_points_cost")
    })
    StatsSummary selectSummaryByDateRange(@Param("startDate") LocalDate startDate,
                                           @Param("endDate") LocalDate endDate);

    /**
     * 按工具汇总日期范围内的统计数据
     */
    @Select("SELECT tool_code, tool_name, SUM(total_calls) as total_calls, " +
            "SUM(success_calls) as success_calls, SUM(total_points_cost) as total_points_cost " +
            "FROM tool_usage_daily_stats WHERE stats_date >= #{startDate} AND stats_date <= #{endDate} " +
            "GROUP BY tool_code, tool_name ORDER BY total_calls DESC")
    @Results({
            @Result(property = "toolCode", column = "tool_code"),
            @Result(property = "toolName", column = "tool_name"),
            @Result(property = "totalCalls", column = "total_calls"),
            @Result(property = "successCalls", column = "success_calls"),
            @Result(property = "totalPointsCost", column = "total_points_cost")
    })
    List<ToolStatsSummary> selectToolSummaryByDateRange(@Param("startDate") LocalDate startDate,
                                                         @Param("endDate") LocalDate endDate);

    /**
     * 统计汇总内部类
     */
    class StatsSummary {
        private Long totalCalls;
        private Long successCalls;
        private Long failedCalls;
        private Long totalPointsCost;

        public Long getTotalCalls() { return totalCalls; }
        public void setTotalCalls(Long totalCalls) { this.totalCalls = totalCalls; }
        public Long getSuccessCalls() { return successCalls; }
        public void setSuccessCalls(Long successCalls) { this.successCalls = successCalls; }
        public Long getFailedCalls() { return failedCalls; }
        public void setFailedCalls(Long failedCalls) { this.failedCalls = failedCalls; }
        public Long getTotalPointsCost() { return totalPointsCost; }
        public void setTotalPointsCost(Long totalPointsCost) { this.totalPointsCost = totalPointsCost; }
    }

    /**
     * 工具统计汇总内部类
     */
    class ToolStatsSummary {
        private String toolCode;
        private String toolName;
        private Long totalCalls;
        private Long successCalls;
        private Long totalPointsCost;

        public String getToolCode() { return toolCode; }
        public void setToolCode(String toolCode) { this.toolCode = toolCode; }
        public String getToolName() { return toolName; }
        public void setToolName(String toolName) { this.toolName = toolName; }
        public Long getTotalCalls() { return totalCalls; }
        public void setTotalCalls(Long totalCalls) { this.totalCalls = totalCalls; }
        public Long getSuccessCalls() { return successCalls; }
        public void setSuccessCalls(Long successCalls) { this.successCalls = successCalls; }
        public Long getTotalPointsCost() { return totalPointsCost; }
        public void setTotalPointsCost(Long totalPointsCost) { this.totalPointsCost = totalPointsCost; }
    }
}
