mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-04-13 12:46:36 +08:00
73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
|
|
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();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|