2023-12-11 09:55:12 +08:00
|
|
|
|
using Mapster;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.Dtos.AccessLog;
|
|
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.IServices;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities;
|
|
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2023-09-15 15:05:14 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
namespace Yi.Framework.Bbs.Application.Services
|
2023-09-15 15:05:14 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public class AccessLogService : ApplicationService, IAccessLogService
|
2023-09-15 15:05:14 +08:00
|
|
|
|
{
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private readonly ISqlSugarRepository<AccessLogEntity> _repository;
|
|
|
|
|
|
public AccessLogService(ISqlSugarRepository<AccessLogEntity> repository) { _repository = repository; }
|
2023-09-15 15:05:14 +08:00
|
|
|
|
|
2023-09-17 10:50:02 +08:00
|
|
|
|
public DateTime GetWeekFirst()
|
|
|
|
|
|
{
|
|
|
|
|
|
var week = DateTime.Now.DayOfWeek;
|
|
|
|
|
|
switch (week)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DayOfWeek.Sunday:
|
2023-09-18 10:36:22 +08:00
|
|
|
|
return DateTime.Now.AddDays(-6).Date;
|
2023-09-17 10:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
case DayOfWeek.Monday:
|
2023-09-18 10:36:22 +08:00
|
|
|
|
return DateTime.Now.AddDays(-0).Date;
|
2023-09-17 10:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
case DayOfWeek.Tuesday:
|
2023-09-18 10:36:22 +08:00
|
|
|
|
return DateTime.Now.AddDays(-1).Date;
|
2023-09-17 10:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
case DayOfWeek.Wednesday:
|
2023-09-18 10:36:22 +08:00
|
|
|
|
return DateTime.Now.AddDays(-2).Date;
|
2023-09-17 10:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
case DayOfWeek.Thursday:
|
2023-09-18 10:36:22 +08:00
|
|
|
|
return DateTime.Now.AddDays(-3).Date;
|
2023-09-17 10:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
case DayOfWeek.Friday:
|
2023-09-18 10:36:22 +08:00
|
|
|
|
return DateTime.Now.AddDays(-4).Date;
|
2023-09-17 10:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
case DayOfWeek.Saturday:
|
2023-09-18 10:36:22 +08:00
|
|
|
|
return DateTime.Now.AddDays(-5).Date;
|
2023-09-17 10:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new ArgumentException("日期错误");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-15 15:05:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 触发
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-12 17:56:11 +08:00
|
|
|
|
[HttpPost("access-log")]
|
2023-09-15 15:05:14 +08:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2023-09-17 10:50:02 +08:00
|
|
|
|
data = data.Where(x => x.CreationTime >= GetWeekFirst()).OrderByDescending(x => x.CreationTime).DistinctBy(x => x.CreationTime.DayOfWeek).ToArray();
|
|
|
|
|
|
|
2023-09-15 15:05:14 +08:00
|
|
|
|
Dictionary<DayOfWeek, AccessLogDto> processedData = new Dictionary<DayOfWeek, AccessLogDto>();
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化字典,将每天的数据都设为0
|
|
|
|
|
|
foreach (DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek)))
|
|
|
|
|
|
{
|
|
|
|
|
|
processedData.Add(dayOfWeek, new AccessLogDto());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-15 15:21:05 +08:00
|
|
|
|
|
2023-09-15 15:05:14 +08:00
|
|
|
|
// 处理原始数据
|
|
|
|
|
|
foreach (var item in data)
|
|
|
|
|
|
{
|
|
|
|
|
|
DayOfWeek dayOfWeek = item.CreationTime.DayOfWeek;
|
|
|
|
|
|
// 如果当天有数据,则更新字典中的值为对应的Number
|
2023-09-17 10:50:02 +08:00
|
|
|
|
var sss = data.Adapt<AccessLogDto>();
|
2023-09-15 15:05:14 +08:00
|
|
|
|
processedData[dayOfWeek] = item.Adapt<AccessLogDto>();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-09-15 15:21:05 +08:00
|
|
|
|
var result = processedData.Values.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
//此时的时间是周日-周一-周二,需要处理
|
|
|
|
|
|
var first = result[0]; // 获取第一个元素
|
|
|
|
|
|
result.RemoveAt(0); // 移除第一个元素
|
|
|
|
|
|
result.Add(first); // 将第一个元素添加到末尾
|
|
|
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
2023-09-15 15:05:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|