feat: 添加访问统计模块
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Furion.Application.Bbs.Services
|
||||
{
|
||||
public interface IAccessLogService
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Mapster;
|
||||
using Yi.Framework.Infrastructure.Ddd.Repositories;
|
||||
using Yi.Furion.Core.Bbs.Dtos.AccessLog;
|
||||
using Yi.Furion.Core.Bbs.Entities;
|
||||
|
||||
namespace Yi.Furion.Application.Bbs.Services.Impl
|
||||
{
|
||||
public class AccessLogService : IAccessLogService,IDynamicApiController
|
||||
{
|
||||
private readonly IRepository<AccessLogEntity> _repository;
|
||||
public AccessLogService(IRepository<AccessLogEntity> repository) { _repository = repository; }
|
||||
|
||||
/// <summary>
|
||||
/// 触发
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost("")]
|
||||
public async Task AccessAsync()
|
||||
{
|
||||
//可判断http重复,防止同一ip多次访问
|
||||
var last = await _repository._DbQueryable.OrderByDescending(x => x.CreationTime).FirstAsync();
|
||||
|
||||
if (last is null || last.CreationTime.Date != DateTime.Today)
|
||||
{
|
||||
await _repository.InsertAsync(new AccessLogEntity());
|
||||
}
|
||||
else
|
||||
{
|
||||
await _repository._Db.Updateable<AccessLogEntity>().SetColumns(it => it.Number == it.Number + 1).Where(it => it.Id == last.Id).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前周数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<AccessLogDto[]> GetWeekAsync()
|
||||
{
|
||||
var lastSeven = await _repository._DbQueryable.OrderByDescending(x => x.CreationTime).ToPageListAsync(1, 7);
|
||||
|
||||
return WeekTimeHandler(lastSeven.ToArray());
|
||||
}
|
||||
|
||||
private AccessLogDto[] WeekTimeHandler(AccessLogEntity[] data)
|
||||
{
|
||||
data = data.Where(x => x.CreationTime.DayOfWeek == DateTime.Now.DayOfWeek).ToArray();
|
||||
Dictionary<DayOfWeek, AccessLogDto> processedData = new Dictionary<DayOfWeek, AccessLogDto>();
|
||||
|
||||
// 初始化字典,将每天的数据都设为0
|
||||
foreach (DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek)))
|
||||
{
|
||||
processedData.Add(dayOfWeek, new AccessLogDto());
|
||||
}
|
||||
|
||||
// 处理原始数据
|
||||
foreach (var item in data)
|
||||
{
|
||||
DayOfWeek dayOfWeek = item.CreationTime.DayOfWeek;
|
||||
// 如果当天有数据,则更新字典中的值为对应的Number
|
||||
var sss= data.Adapt<AccessLogDto>();
|
||||
processedData[dayOfWeek] = item.Adapt<AccessLogDto>();
|
||||
|
||||
}
|
||||
return processedData.Values.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,18 @@
|
||||
Label服务抽象
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Yi.Furion.Application.Bbs.Services.Impl.AccessLogService.AccessAsync">
|
||||
<summary>
|
||||
触发
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Furion.Application.Bbs.Services.Impl.AccessLogService.GetWeekAsync">
|
||||
<summary>
|
||||
获取当前周数据
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:Yi.Furion.Application.Bbs.Services.Impl.AgreeService">
|
||||
<summary>
|
||||
点赞功能
|
||||
|
||||
Reference in New Issue
Block a user