feat: 添加访问统计模块

This commit is contained in:
陈淳
2023-09-15 15:05:14 +08:00
parent c2ca0b1f29
commit c988ed7988
10 changed files with 164 additions and 12 deletions

View File

@@ -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
{
}
}

View File

@@ -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();
}
}
}

View File

@@ -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>
点赞功能

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Furion.Core.Bbs.Dtos.AccessLog
{
public class AccessLogDto
{
public long Id { get; set; }
public long Number { get; set; }
public DateTime? LastModificationTime { get; set; }
public DateTime CreationTime { get; set; }
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using Yi.Framework.Infrastructure.Data.Auditing;
using Yi.Framework.Infrastructure.Ddd.Entities;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Furion.Core.Bbs.Entities
{
[SugarTable("AccessLog")]
public class AccessLogEntity : IEntity<long>, IHasModificationTime, IHasCreationTime
{
[SugarColumn(IsPrimaryKey = true)]
public long Id { get; set; } = SnowflakeHelper.NextId;
public long Number { get; set; }
public DateTime? LastModificationTime { get; set; }
public DateTime CreationTime { get; set; }
}
}

View File

@@ -28,10 +28,4 @@
<ItemGroup>
<Folder Include="Bbs\Dtos\" />
</ItemGroup>
</Project>

View File

@@ -21,7 +21,7 @@
"Url": "DataSource=yi-sqlsugar-dev.db",
"DbType": "Sqlite",
"EnabledReadWrite": false,
"EnabledCodeFirst": true,
"EnabledCodeFirst": false,
"ReadUrl": [
"DataSource=[xxxx]", //Sqlite
"server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", //Mysql
@@ -29,7 +29,7 @@
]
},
"EnabledDataSeed": true,
"EnabledDataSeed": false,
"JWTSettings": {
"ValidateIssuerSigningKey": true, // 是否验证密钥bool 类型默认true