File size: 4,282 Bytes
5fc700d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using ToolHub.Services;
using ToolHub.Models;

namespace ToolHub.Controllers;

[Authorize(Roles = "Admin")]
public class ToolStatisticsController : Controller
{
    private readonly IToolService _toolService;

    public ToolStatisticsController(IToolService toolService)
    {
        _toolService = toolService;
    }

    /// <summary>
    /// 获取工具统计数据页面
    /// </summary>
    public async Task<IActionResult> Index(int toolId = 0)
    {
        ViewData["Title"] = "工具统计";
        
        if (toolId > 0)
        {
            var tool = await _toolService.GetToolByIdAsync(toolId);
            if (tool != null)
            {
                ViewData["ToolName"] = tool.Name;
                ViewData["ToolId"] = toolId;
            }
        }
        
        return View();
    }

    /// <summary>
    /// 获取工具统计数据
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetToolStatistics(int toolId, DateTime? date = null)
    {
        try
        {
            var targetDate = date ?? DateTime.Today;
            var stats = await _toolService.GetToolStatisticsAsync(toolId, targetDate);
            
            return Json(new { success = true, data = stats });
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message });
        }
    }

    /// <summary>
    /// 获取工具访问记录
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetToolAccessRecords(int toolId, DateTime? startDate = null, DateTime? endDate = null, int limit = 100)
    {
        try
        {
            var records = await _toolService.GetToolAccessRecordsAsync(toolId, startDate, endDate, limit);
            
            return Json(new { success = true, data = records });
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message });
        }
    }

    /// <summary>
    /// 获取用户访问记录
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetUserAccessRecords(int userId, DateTime? startDate = null, DateTime? endDate = null, int limit = 100)
    {
        try
        {
            var records = await _toolService.GetUserToolAccessRecordsAsync(userId, startDate, endDate, limit);
            
            return Json(new { success = true, data = records });
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message });
        }
    }

    /// <summary>
    /// 获取工具使用趋势
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetToolUsageTrend(int toolId, int days = 30)
    {
        try
        {
            var trend = await _toolService.GetToolUsageTrendAsync(toolId, days);
            
            return Json(new { success = true, data = trend });
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message });
        }
    }

    /// <summary>
    /// 获取热门工具
    /// </summary>
    [HttpGet]
    public async Task<IActionResult> GetPopularTools(int count = 10, DateTime? startDate = null)
    {
        try
        {
            var tools = await _toolService.GetPopularToolsAsync(count, startDate);
            
            return Json(new { success = true, data = tools });
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message });
        }
    }

    /// <summary>
    /// 记录工具操作
    /// </summary>
    [HttpPost]
    public async Task<IActionResult> RecordToolAction([FromBody] RecordToolActionRequest request)
    {
        try
        {
            await _toolService.RecordToolActionAsync(request.ToolId, request.ActionType, request.Duration);
            
            return Json(new { success = true });
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message });
        }
    }
}

public class RecordToolActionRequest
{
    public int ToolId { get; set; }
    public string ActionType { get; set; } = "view";
    public int Duration { get; set; } = 0;
}